blob: f2a76823a58af7ca279e2b69e500ee1da014fd81 [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
Rian McGuire89fcb7d2018-04-24 11:19:21 -03001622 /* If there is data available for analysis, log the end of the idle time. */
1623 if (buffer_not_empty(req->buf) && s->logs.t_idle == -1)
1624 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
1625
Willy Tarreau83e3af02009-12-28 17:39:57 +01001626 /* There's a protected area at the end of the buffer for rewriting
1627 * purposes. We don't want to start to parse the request if the
1628 * protected area is affected, because we may have to move processed
1629 * data later, which is much more complicated.
1630 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001631 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02001632 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01001633 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001634 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01001635 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01001636 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001637 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001638 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01001639 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01001640 return 0;
1641 }
Willy Tarreau379357a2013-06-08 12:55:46 +02001642 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
1643 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
1644 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001645 }
1646
Willy Tarreau9b28e032012-10-12 23:49:43 +02001647 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01001648 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001649 }
1650
Willy Tarreau59234e92008-11-30 23:51:27 +01001651 /* 1: we might have to print this header in debug mode */
1652 if (unlikely((global.mode & MODE_DEBUG) &&
1653 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02001654 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001655 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001656
Willy Tarreau9b28e032012-10-12 23:49:43 +02001657 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001658 /* this is a bit complex : in case of error on the request line,
1659 * we know that rq.l is still zero, so we display only the part
1660 * up to the end of the line (truncated by debug_hdr).
1661 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001662 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01001663 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001664
Willy Tarreau59234e92008-11-30 23:51:27 +01001665 sol += hdr_idx_first_pos(&txn->hdr_idx);
1666 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001667
Willy Tarreau59234e92008-11-30 23:51:27 +01001668 while (cur_idx) {
1669 eol = sol + txn->hdr_idx.v[cur_idx].len;
1670 debug_hdr("clihdr", s, sol, eol);
1671 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1672 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001673 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001674 }
1675
Willy Tarreau58f10d72006-12-04 02:26:12 +01001676
Willy Tarreau59234e92008-11-30 23:51:27 +01001677 /*
1678 * Now we quickly check if we have found a full valid request.
1679 * If not so, we check the FD and buffer states before leaving.
1680 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01001681 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001682 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02001683 * on a keep-alive stream, if we encounter and error, close, t/o,
1684 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001685 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02001686 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02001687 * Last, we may increase some tracked counters' http request errors on
1688 * the cases that are deliberately the client's fault. For instance,
1689 * a timeout or connection reset is not counted as an error. However
1690 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01001691 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001692
Willy Tarreau655dce92009-11-08 13:10:58 +01001693 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001694 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001695 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001696 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01001697 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001698 stream_inc_http_req_ctr(s);
1699 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001700 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001701 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01001702 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001703
Willy Tarreau59234e92008-11-30 23:51:27 +01001704 /* 1: Since we are in header mode, if there's no space
1705 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02001706 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01001707 * must terminate it now.
1708 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001709 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001710 /* FIXME: check if URI is set and return Status
1711 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001712 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001713 stream_inc_http_req_ctr(s);
1714 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001715 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02001716 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02001717 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01001718 goto return_bad_req;
1719 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001720
Willy Tarreau59234e92008-11-30 23:51:27 +01001721 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001722 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001723 if (!(s->flags & SF_ERR_MASK))
1724 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001725
Willy Tarreaufcffa692010-01-10 14:21:19 +01001726 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001727 goto failed_keep_alive;
1728
Willy Tarreau0f228a02015-05-01 15:37:53 +02001729 if (sess->fe->options & PR_O_IGNORE_PRB)
1730 goto failed_keep_alive;
1731
Willy Tarreau59234e92008-11-30 23:51:27 +01001732 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001733 if (msg->err_pos >= 0) {
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001734 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001735 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001736 }
1737
Willy Tarreaudc979f22012-12-04 10:39:01 +01001738 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001739 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001740 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001741 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01001742 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001743 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001744 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001745 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001746 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001747 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001748
Willy Tarreaue7dff022015-04-03 01:14:29 +02001749 if (!(s->flags & SF_FINST_MASK))
1750 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001751 return 0;
1752 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001753
Willy Tarreau59234e92008-11-30 23:51:27 +01001754 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001755 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001756 if (!(s->flags & SF_ERR_MASK))
1757 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001758
Willy Tarreaufcffa692010-01-10 14:21:19 +01001759 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001760 goto failed_keep_alive;
1761
Willy Tarreau0f228a02015-05-01 15:37:53 +02001762 if (sess->fe->options & PR_O_IGNORE_PRB)
1763 goto failed_keep_alive;
1764
Willy Tarreau59234e92008-11-30 23:51:27 +01001765 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001766 if (msg->err_pos >= 0) {
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001767 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001768 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001769 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001770 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001771 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001772 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001773 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001774 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001775
Willy Tarreau87b09662015-04-03 00:22:06 +02001776 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001777 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001778 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001779 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001780 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001781
Willy Tarreaue7dff022015-04-03 01:14:29 +02001782 if (!(s->flags & SF_FINST_MASK))
1783 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001784 return 0;
1785 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001786
Willy Tarreau59234e92008-11-30 23:51:27 +01001787 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001788 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001789 if (!(s->flags & SF_ERR_MASK))
1790 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001791
Willy Tarreaufcffa692010-01-10 14:21:19 +01001792 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001793 goto failed_keep_alive;
1794
Willy Tarreau0f228a02015-05-01 15:37:53 +02001795 if (sess->fe->options & PR_O_IGNORE_PRB)
1796 goto failed_keep_alive;
1797
Willy Tarreau4076a152009-04-02 15:18:36 +02001798 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001799 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001800 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001801 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001802 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001803 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001804 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001805 stream_inc_http_err_ctr(s);
1806 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001807 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001808 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001809 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001810 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001811
Willy Tarreaue7dff022015-04-03 01:14:29 +02001812 if (!(s->flags & SF_FINST_MASK))
1813 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001814 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001815 }
1816
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001817 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001818 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001819 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01001820#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001821 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
1822 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01001823 /* We need more data, we have to re-enable quick-ack in case we
1824 * previously disabled it, otherwise we might cause the client
1825 * to delay next data.
1826 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001827 setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01001828 }
1829#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001830
Willy Tarreaufcffa692010-01-10 14:21:19 +01001831 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
1832 /* If the client starts to talk, let's fall back to
1833 * request timeout processing.
1834 */
1835 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01001836 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01001837 }
1838
Willy Tarreau59234e92008-11-30 23:51:27 +01001839 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01001840 if (!tick_isset(req->analyse_exp)) {
1841 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
1842 (txn->flags & TX_WAIT_NEXT_RQ) &&
1843 tick_isset(s->be->timeout.httpka))
1844 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
1845 else
1846 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1847 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001848
Willy Tarreau59234e92008-11-30 23:51:27 +01001849 /* we're not ready yet */
1850 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001851
1852 failed_keep_alive:
1853 /* Here we process low-level errors for keep-alive requests. In
1854 * short, if the request is not the first one and it experiences
1855 * a timeout, read error or shutdown, we just silently close so
1856 * that the client can try again.
1857 */
1858 txn->status = 0;
1859 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001860 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001861 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02001862 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001863 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001864 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01001865 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01001866 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001867
Willy Tarreaud787e662009-07-07 10:14:51 +02001868 /* OK now we have a complete HTTP request with indexed headers. Let's
1869 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02001870 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01001871 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01001872 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001873 * byte after the last LF. msg->sov points to the first byte of data.
1874 * msg->eol cannot be trusted because it may have been left uninitialized
1875 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02001876 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001877
Willy Tarreau87b09662015-04-03 00:22:06 +02001878 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001879 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001880
Willy Tarreaub16a5742010-01-10 14:46:16 +01001881 if (txn->flags & TX_WAIT_NEXT_RQ) {
1882 /* kill the pending keep-alive timeout */
1883 txn->flags &= ~TX_WAIT_NEXT_RQ;
1884 req->analyse_exp = TICK_ETERNITY;
1885 }
1886
1887
Willy Tarreaud787e662009-07-07 10:14:51 +02001888 /* Maybe we found in invalid header name while we were configured not
1889 * to block on that, so we have to capture it now.
1890 */
1891 if (unlikely(msg->err_pos >= 0))
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001892 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02001893
Willy Tarreau59234e92008-11-30 23:51:27 +01001894 /*
1895 * 1: identify the method
1896 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001897 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01001898
1899 /* we can make use of server redirect on GET and HEAD */
1900 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001901 s->flags |= SF_REDIRECTABLE;
Willy Tarreau91659792017-11-10 19:38:10 +01001902 else if (txn->meth == HTTP_METH_OTHER &&
1903 msg->sl.rq.m_l == 3 && memcmp(req->buf->p, "PRI", 3) == 0) {
1904 /* PRI is reserved for the HTTP/2 preface */
1905 msg->err_pos = 0;
1906 goto return_bad_req;
1907 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001908
Willy Tarreau59234e92008-11-30 23:51:27 +01001909 /*
1910 * 2: check if the URI matches the monitor_uri.
1911 * We have to do this for every request which gets in, because
1912 * the monitor-uri is defined by the frontend.
1913 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001914 if (unlikely((sess->fe->monitor_uri_len != 0) &&
1915 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02001916 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001917 sess->fe->monitor_uri,
1918 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001919 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001920 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001921 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001922 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001923
Willy Tarreaue7dff022015-04-03 01:14:29 +02001924 s->flags |= SF_MONITOR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001925 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001926
Willy Tarreau59234e92008-11-30 23:51:27 +01001927 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001928 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001929 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02001930
Willy Tarreau59234e92008-11-30 23:51:27 +01001931 ret = acl_pass(ret);
1932 if (cond->pol == ACL_COND_UNLESS)
1933 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001934
Willy Tarreau59234e92008-11-30 23:51:27 +01001935 if (ret) {
1936 /* we fail this request, let's return 503 service unavail */
1937 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001938 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001939 if (!(s->flags & SF_ERR_MASK))
1940 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001941 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001942 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001943 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001944
Willy Tarreau59234e92008-11-30 23:51:27 +01001945 /* nothing to fail, let's reply normaly */
1946 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001947 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001948 if (!(s->flags & SF_ERR_MASK))
1949 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001950 goto return_prx_cond;
1951 }
1952
1953 /*
1954 * 3: Maybe we have to copy the original REQURI for the logs ?
1955 * Note: we cannot log anymore if the request has been
1956 * classified as invalid.
1957 */
1958 if (unlikely(s->logs.logwait & LW_REQ)) {
1959 /* we have a complete HTTP request that we must log */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001960 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001961 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001962
Stéphane Cottin23e9e932017-05-18 08:58:41 +02001963 if (urilen >= global.tune.requri_len )
1964 urilen = global.tune.requri_len - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001965 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01001966 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001967
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001968 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01001969 s->do_log(s);
1970 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001971 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001972 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001973 }
Willy Tarreau06619262006-12-17 08:37:22 +01001974
Willy Tarreau91852eb2015-05-01 13:26:00 +02001975 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
1976 * exactly one digit "." one digit. This check may be disabled using
1977 * option accept-invalid-http-request.
1978 */
1979 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
1980 if (msg->sl.rq.v_l != 8) {
1981 msg->err_pos = msg->sl.rq.v;
1982 goto return_bad_req;
1983 }
1984
1985 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
1986 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
1987 req->buf->p[msg->sl.rq.v + 6] != '.' ||
1988 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
1989 msg->err_pos = msg->sl.rq.v + 4;
1990 goto return_bad_req;
1991 }
1992 }
Willy Tarreau13317662015-05-01 13:47:08 +02001993 else {
1994 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1995 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
1996 goto return_bad_req;
1997 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02001998
Willy Tarreau5b154472009-12-21 20:11:07 +01001999 /* ... and check if the request is HTTP/1.1 or above */
2000 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002001 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2002 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2003 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002004 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002005
2006 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002007 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 +01002008
Willy Tarreau88d349d2010-01-25 12:15:43 +01002009 /* if the frontend has "option http-use-proxy-header", we'll check if
2010 * we have what looks like a proxied connection instead of a connection,
2011 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2012 * Note that this is *not* RFC-compliant, however browsers and proxies
2013 * happen to do that despite being non-standard :-(
2014 * We consider that a request not beginning with either '/' or '*' is
2015 * a proxied connection, which covers both "scheme://location" and
2016 * CONNECT ip:port.
2017 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002018 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002019 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002020 txn->flags |= TX_USE_PX_CONN;
2021
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002022 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002023 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002024
Willy Tarreau59234e92008-11-30 23:51:27 +01002025 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002026 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002027 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002028 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002029
Willy Tarreau557f1992015-05-01 10:05:17 +02002030 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
2031 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002032 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002033 * The length of a message body is determined by one of the following
2034 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02002035 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002036 * 1. Any response to a HEAD request and any response with a 1xx
2037 * (Informational), 204 (No Content), or 304 (Not Modified) status
2038 * code is always terminated by the first empty line after the
2039 * header fields, regardless of the header fields present in the
2040 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002041 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002042 * 2. Any 2xx (Successful) response to a CONNECT request implies that
2043 * the connection will become a tunnel immediately after the empty
2044 * line that concludes the header fields. A client MUST ignore any
2045 * Content-Length or Transfer-Encoding header fields received in
2046 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002047 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002048 * 3. If a Transfer-Encoding header field is present and the chunked
2049 * transfer coding (Section 4.1) is the final encoding, the message
2050 * body length is determined by reading and decoding the chunked
2051 * data until the transfer coding indicates the data is complete.
2052 *
2053 * If a Transfer-Encoding header field is present in a response and
2054 * the chunked transfer coding is not the final encoding, the
2055 * message body length is determined by reading the connection until
2056 * it is closed by the server. If a Transfer-Encoding header field
2057 * is present in a request and the chunked transfer coding is not
2058 * the final encoding, the message body length cannot be determined
2059 * reliably; the server MUST respond with the 400 (Bad Request)
2060 * status code and then close the connection.
2061 *
2062 * If a message is received with both a Transfer-Encoding and a
2063 * Content-Length header field, the Transfer-Encoding overrides the
2064 * Content-Length. Such a message might indicate an attempt to
2065 * perform request smuggling (Section 9.5) or response splitting
2066 * (Section 9.4) and ought to be handled as an error. A sender MUST
2067 * remove the received Content-Length field prior to forwarding such
2068 * a message downstream.
2069 *
2070 * 4. If a message is received without Transfer-Encoding and with
2071 * either multiple Content-Length header fields having differing
2072 * field-values or a single Content-Length header field having an
2073 * invalid value, then the message framing is invalid and the
2074 * recipient MUST treat it as an unrecoverable error. If this is a
2075 * request message, the server MUST respond with a 400 (Bad Request)
2076 * status code and then close the connection. If this is a response
2077 * message received by a proxy, the proxy MUST close the connection
2078 * to the server, discard the received response, and send a 502 (Bad
2079 * Gateway) response to the client. If this is a response message
2080 * received by a user agent, the user agent MUST close the
2081 * connection to the server and discard the received response.
2082 *
2083 * 5. If a valid Content-Length header field is present without
2084 * Transfer-Encoding, its decimal value defines the expected message
2085 * body length in octets. If the sender closes the connection or
2086 * the recipient times out before the indicated number of octets are
2087 * received, the recipient MUST consider the message to be
2088 * incomplete and close the connection.
2089 *
2090 * 6. If this is a request message and none of the above are true, then
2091 * the message body length is zero (no message body is present).
2092 *
2093 * 7. Otherwise, this is a response message without a declared message
2094 * body length, so the message body length is determined by the
2095 * number of octets received prior to the server closing the
2096 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002097 */
2098
Willy Tarreau32b47f42009-10-18 20:55:02 +02002099 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002100 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02002101 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002102 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02002103 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002104 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02002105 /* chunked not last, return badreq */
2106 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002107 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002108 }
2109
Willy Tarreau1c913912015-04-30 10:57:51 +02002110 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02002111 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02002112 if (msg->flags & HTTP_MSGF_TE_CHNK) {
2113 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
2114 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2115 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02002116 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002117 signed long long cl;
2118
Willy Tarreauad14f752011-09-02 20:33:27 +02002119 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002120 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002121 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002122 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002123
Willy Tarreauad14f752011-09-02 20:33:27 +02002124 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002125 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002126 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002127 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002128
Willy Tarreauad14f752011-09-02 20:33:27 +02002129 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002130 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002131 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002132 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002133
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002134 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002135 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002136 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002137 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002138
Christopher Fauletbe821b92017-03-30 11:21:53 +02002139 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002140 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002141 }
2142
Willy Tarreau34dfc602015-05-01 10:09:49 +02002143 /* even bodyless requests have a known length */
2144 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002145
Willy Tarreau179085c2014-04-28 16:48:56 +02002146 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
2147 * only change if both the request and the config reference something else.
2148 * Option httpclose by itself sets tunnel mode where headers are mangled.
2149 * However, if another mode is set, it will affect it (eg: server-close/
2150 * keep-alive + httpclose = close). Note that we avoid to redo the same work
2151 * if FE and BE have the same settings (common). The method consists in
2152 * checking if options changed between the two calls (implying that either
2153 * one is non-null, or one of them is non-null and we are there for the first
2154 * time.
2155 */
2156 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002157 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002158 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02002159
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02002160 /* we may have to wait for the request's body */
2161 if ((s->be->options & PR_O_WREQ_BODY) &&
2162 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
2163 req->analysers |= AN_REQ_HTTP_BODY;
2164
Willy Tarreau83ece462017-12-21 15:13:09 +01002165 /*
2166 * RFC7234#4:
2167 * A cache MUST write through requests with methods
2168 * that are unsafe (Section 4.2.1 of [RFC7231]) to
2169 * the origin server; i.e., a cache is not allowed
2170 * to generate a reply to such a request before
2171 * having forwarded the request and having received
2172 * a corresponding response.
2173 *
2174 * RFC7231#4.2.1:
2175 * Of the request methods defined by this
2176 * specification, the GET, HEAD, OPTIONS, and TRACE
2177 * methods are defined to be safe.
2178 */
2179 if (likely(txn->meth == HTTP_METH_GET ||
2180 txn->meth == HTTP_METH_HEAD ||
2181 txn->meth == HTTP_METH_OPTIONS ||
2182 txn->meth == HTTP_METH_TRACE))
2183 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
2184
Willy Tarreaud787e662009-07-07 10:14:51 +02002185 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002186 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002187 req->analyse_exp = TICK_ETERNITY;
2188 return 1;
2189
2190 return_bad_req:
2191 /* We centralize bad requests processing here */
2192 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2193 /* we detected a parsing error. We want to archive this request
2194 * in the dedicated proxy area for later troubleshooting.
2195 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02002196 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002197 }
2198
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002199 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02002200 txn->req.msg_state = HTTP_MSG_ERROR;
2201 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002202 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002203
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002204 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002205 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002206 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02002207
2208 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02002209 if (!(s->flags & SF_ERR_MASK))
2210 s->flags |= SF_ERR_PRXCOND;
2211 if (!(s->flags & SF_FINST_MASK))
2212 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02002213
Christopher Faulet0184ea72017-01-05 14:06:34 +01002214 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02002215 req->analyse_exp = TICK_ETERNITY;
2216 return 0;
2217}
2218
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002219
Willy Tarreau347a35d2013-11-22 17:51:09 +01002220/* This function prepares an applet to handle the stats. It can deal with the
2221 * "100-continue" expectation, check that admin rules are met for POST requests,
2222 * and program a response message if something was unexpected. It cannot fail
2223 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002224 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002225 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02002226 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002227 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002228int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002229{
2230 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01002231 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02002232 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002233 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002234 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002235 struct uri_auth *uri_auth = s->be->uri_auth;
2236 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002237 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002238
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002239 appctx = si_appctx(si);
2240 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2241 appctx->st1 = appctx->st2 = 0;
2242 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
2243 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02002244 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02002245 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002246
2247 uri = msg->chn->buf->p + msg->sl.rq.u;
2248 lookup = uri + uri_auth->uri_len;
2249
2250 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
2251 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002252 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002253 break;
2254 }
2255 }
2256
2257 if (uri_auth->refresh) {
2258 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
2259 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002260 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002261 break;
2262 }
2263 }
2264 }
2265
2266 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
2267 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002268 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002269 break;
2270 }
2271 }
2272
Willy Tarreau1e62df92016-01-11 18:57:53 +01002273 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
2274 if (memcmp(h, ";typed", 6) == 0) {
2275 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
2276 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
2277 break;
2278 }
2279 }
2280
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002281 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2282 if (memcmp(h, ";st=", 4) == 0) {
2283 int i;
2284 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002285 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002286 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
2287 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002288 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002289 break;
2290 }
2291 }
2292 break;
2293 }
2294 }
2295
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002296 appctx->ctx.stats.scope_str = 0;
2297 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002298 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2299 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
2300 int itx = 0;
2301 const char *h2;
2302 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
2303 const char *err;
2304
2305 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
2306 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002307 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002308 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
2309 itx++;
2310 h++;
2311 }
2312
2313 if (itx > STAT_SCOPE_TXT_MAXLEN)
2314 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002315 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002316
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002317 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002318 memcpy(scope_txt, h2, itx);
2319 scope_txt[itx] = '\0';
2320 err = invalid_char(scope_txt);
2321 if (err) {
2322 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002323 appctx->ctx.stats.scope_str = 0;
2324 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002325 }
2326 break;
2327 }
2328 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002329
2330 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002331 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002332 int ret = 1;
2333
2334 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002335 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 +01002336 ret = acl_pass(ret);
2337 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2338 ret = !ret;
2339 }
2340
2341 if (ret) {
2342 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002343 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002344 break;
2345 }
2346 }
2347
2348 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02002349 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002350 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02002351 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02002352 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
2353 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002354 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002355 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002356 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002357 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
2358 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002359 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002360 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002361 else {
2362 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002363 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002364 }
2365
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002366 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002367 return 1;
2368}
2369
Lukas Tribus67db8df2013-06-23 17:37:13 +02002370/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
2371 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
2372 */
Vincent Bernat6e615892016-05-18 16:17:44 +02002373void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02002374{
2375#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02002376 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02002377 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2378#endif
2379#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02002380 if (from->ss_family == AF_INET6) {
2381 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02002382 /* v4-mapped addresses need IP_TOS */
2383 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2384 else
2385 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
2386 }
2387#endif
2388}
2389
Willy Tarreau87b09662015-04-03 00:22:06 +02002390int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002391 const char* name, unsigned int name_len,
2392 const char *str, struct my_regex *re,
2393 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06002394{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002395 struct hdr_ctx ctx;
2396 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002397 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002398 int (*http_find_hdr_func)(const char *name, int len, char *sol,
2399 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002400 struct chunk *output = get_trash_chunk();
2401
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002402 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06002403
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002404 /* Choose the header browsing function. */
2405 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002406 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002407 http_find_hdr_func = http_find_header2;
2408 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002409 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002410 http_find_hdr_func = http_find_full_header2;
2411 break;
2412 default: /* impossible */
2413 return -1;
2414 }
2415
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002416 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
2417 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06002418 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002419 char *val = ctx.line + ctx.val;
2420 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06002421
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002422 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
2423 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06002424
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002425 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002426 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06002427 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06002428
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002429 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06002430
2431 hdr->len += delta;
2432 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002433
2434 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002435 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06002436 }
2437
2438 return 0;
2439}
2440
Willy Tarreau87b09662015-04-03 00:22:06 +02002441static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002442 const char* name, unsigned int name_len,
2443 struct list *fmt, struct my_regex *re,
2444 int action)
2445{
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002446 struct chunk *replace;
2447 int ret = -1;
2448
2449 replace = alloc_trash_chunk();
2450 if (!replace)
2451 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002452
2453 replace->len = build_logline(s, replace->str, replace->size, fmt);
2454 if (replace->len >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002455 goto leave;
2456
2457 ret = http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002458
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002459 leave:
2460 free_trash_chunk(replace);
2461 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002462}
2463
Willy Tarreau87b09662015-04-03 00:22:06 +02002464/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02002465 * transaction <txn>. Returns the verdict of the first rule that prevents
2466 * further processing of the request (auth, deny, ...), and defaults to
2467 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2468 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02002469 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2470 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2471 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002472 */
Willy Tarreau0b748332014-04-29 00:13:29 +02002473enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02002474http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002475{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002476 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002477 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002478 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002479 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01002480 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02002481 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02002482 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02002483 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002484
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002485 /* If "the current_rule_list" match the executed rule list, we are in
2486 * resume condition. If a resume is needed it is always in the action
2487 * and never in the ACL or converters. In this case, we initialise the
2488 * current rule, and go to the action execution point.
2489 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002490 if (s->current_rule) {
2491 rule = s->current_rule;
2492 s->current_rule = NULL;
2493 if (s->current_rule_list == rules)
2494 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002495 }
2496 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002497
Willy Tarreauff011f22011-01-06 17:51:27 +01002498 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002499
Willy Tarreau96257ec2012-12-27 10:46:37 +01002500 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01002501 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01002502 int ret;
2503
Willy Tarreau192252e2015-04-04 01:47:55 +02002504 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002505 ret = acl_pass(ret);
2506
Willy Tarreauff011f22011-01-06 17:51:27 +01002507 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002508 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002509
2510 if (!ret) /* condition not matched */
2511 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002512 }
2513
Willy Tarreauacc98002015-09-27 23:34:39 +02002514 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002515resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01002516 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002517 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02002518 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002519
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002520 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02002521 if (deny_status)
2522 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02002523 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002524
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002525 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01002526 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002527 if (deny_status)
2528 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02002529 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01002530
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002531 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02002532 /* Auth might be performed on regular http-req rules as well as on stats */
2533 auth_realm = rule->arg.auth.realm;
2534 if (!auth_realm) {
2535 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2536 auth_realm = STATS_DEFAULT_REALM;
2537 else
2538 auth_realm = px->id;
2539 }
2540 /* send 401/407 depending on whether we use a proxy or not. We still
2541 * count one error, because normal browsing won't significantly
2542 * increase the counter but brute force attempts will.
2543 */
2544 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
2545 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002546 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02002547 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02002548 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002549
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002550 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02002551 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
2552 return HTTP_RULE_RES_BADREQ;
2553 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002554
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002555 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002556 s->task->nice = rule->arg.nice;
2557 break;
2558
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002559 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002560 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002561 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002562 break;
2563
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002564 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002565#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002566 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002567 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002568#endif
2569 break;
2570
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002571 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002572 s->logs.level = rule->arg.loglevel;
2573 break;
2574
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002575 case ACT_HTTP_REPLACE_HDR:
2576 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002577 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
2578 rule->arg.hdr_add.name_len,
2579 &rule->arg.hdr_add.fmt,
2580 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06002581 return HTTP_RULE_RES_BADREQ;
2582 break;
2583
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002584 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01002585 ctx.idx = 0;
2586 /* remove all occurrences of the header */
2587 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2588 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
2589 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01002590 }
Willy Tarreau85603282015-01-21 20:39:27 +01002591 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002592
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002593 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002594 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02002595 /* The scope of the trash buffer must be limited to this function. The
2596 * build_logline() function can execute a lot of other function which
2597 * can use the trash buffer. So for limiting the scope of this global
2598 * buffer, we build first the header value using build_logline, and
2599 * after we store the header name.
2600 */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002601 struct chunk *replace;
2602
2603 replace = alloc_trash_chunk();
2604 if (!replace)
2605 return HTTP_RULE_RES_BADREQ;
2606
Thierry Fournier4b788f72016-06-01 13:35:36 +02002607 len = rule->arg.hdr_add.name_len + 2,
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002608 len += build_logline(s, replace->str + len, replace->size - len, &rule->arg.hdr_add.fmt);
2609 memcpy(replace->str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2610 replace->str[rule->arg.hdr_add.name_len] = ':';
2611 replace->str[rule->arg.hdr_add.name_len + 1] = ' ';
2612 replace->len = len;
Willy Tarreau85603282015-01-21 20:39:27 +01002613
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002614 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002615 /* remove all occurrences of the header */
2616 ctx.idx = 0;
2617 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2618 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
2619 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
2620 }
2621 }
2622
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002623 http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->str, replace->len);
2624
2625 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01002626 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002627 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002628
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002629 case ACT_HTTP_DEL_ACL:
2630 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002631 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002632 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002633
2634 /* collect reference */
2635 ref = pat_ref_lookup(rule->arg.map.ref);
2636 if (!ref)
2637 continue;
2638
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002639 /* allocate key */
2640 key = alloc_trash_chunk();
2641 if (!key)
2642 return HTTP_RULE_RES_BADREQ;
2643
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002644 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002645 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2646 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002647
2648 /* perform update */
2649 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002650 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002651 pat_ref_delete(ref, key->str);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002652 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002653
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002654 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002655 break;
2656 }
2657
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002658 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002659 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002660 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002661
2662 /* collect reference */
2663 ref = pat_ref_lookup(rule->arg.map.ref);
2664 if (!ref)
2665 continue;
2666
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002667 /* allocate key */
2668 key = alloc_trash_chunk();
2669 if (!key)
2670 return HTTP_RULE_RES_BADREQ;
2671
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002672 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002673 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2674 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002675
2676 /* perform update */
2677 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002678 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002679 if (pat_ref_find_elt(ref, key->str) == NULL)
2680 pat_ref_add(ref, key->str, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002681 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002682
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002683 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002684 break;
2685 }
2686
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002687 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002688 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002689 struct chunk *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002690
2691 /* collect reference */
2692 ref = pat_ref_lookup(rule->arg.map.ref);
2693 if (!ref)
2694 continue;
2695
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002696 /* allocate key */
2697 key = alloc_trash_chunk();
2698 if (!key)
2699 return HTTP_RULE_RES_BADREQ;
2700
2701 /* allocate value */
2702 value = alloc_trash_chunk();
2703 if (!value) {
2704 free_trash_chunk(key);
2705 return HTTP_RULE_RES_BADREQ;
2706 }
2707
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002708 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002709 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2710 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002711
2712 /* collect value */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002713 value->len = build_logline(s, value->str, value->size, &rule->arg.map.value);
2714 value->str[value->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002715
2716 /* perform update */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002717 if (pat_ref_find_elt(ref, key->str) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002718 /* update entry if it exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002719 pat_ref_set(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002720 else
2721 /* insert a new entry */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002722 pat_ref_add(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002723
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002724 free_trash_chunk(key);
2725 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002726 break;
2727 }
William Lallemand73025dd2014-04-24 14:38:37 +02002728
Thierry FOURNIER42148732015-09-02 17:17:33 +02002729 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002730 if ((s->req.flags & CF_READ_ERROR) ||
2731 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2732 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2733 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002734 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002735
Willy Tarreauacc98002015-09-27 23:34:39 +02002736 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002737 case ACT_RET_ERR:
2738 case ACT_RET_CONT:
2739 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002740 case ACT_RET_STOP:
2741 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002742 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002743 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002744 return HTTP_RULE_RES_YIELD;
2745 }
William Lallemand73025dd2014-04-24 14:38:37 +02002746 break;
2747
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002748 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002749 /* Note: only the first valid tracking parameter of each
2750 * applies.
2751 */
2752
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002753 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002754 struct stktable *t;
2755 struct stksess *ts;
2756 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002757 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02002758
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002759 t = rule->arg.trk_ctr.table.t;
2760 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 +02002761
2762 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002763 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002764
2765 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002766 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2767 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2768 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002769 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02002770
Emeric Brun819fc6f2017-06-13 19:37:32 +02002771 if (ptr1)
2772 stktable_data_cast(ptr1, http_req_cnt)++;
2773
2774 if (ptr2)
2775 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2776 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2777
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002778 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01002779
2780 /* If data was modified, we need to touch to re-schedule sync */
2781 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002782 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002783
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002784 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002785 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002786 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002787 }
2788 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002789 break;
2790
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002791 /* other flags exists, but normaly, they never be matched. */
2792 default:
2793 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002794 }
2795 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002796
2797 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02002798 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002799}
2800
Willy Tarreau71241ab2012-12-27 11:30:54 +01002801
Willy Tarreau51d861a2015-05-22 17:30:48 +02002802/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2803 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2804 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2805 * is returned, the process can continue the evaluation of next rule list. If
2806 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2807 * is returned, it means the operation could not be processed and a server error
2808 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2809 * deny rule. If *YIELD is returned, the caller must call again the function
2810 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002811 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002812static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002813http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002814{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002815 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002816 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002817 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002818 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002819 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02002820 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002821
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002822 /* If "the current_rule_list" match the executed rule list, we are in
2823 * resume condition. If a resume is needed it is always in the action
2824 * and never in the ACL or converters. In this case, we initialise the
2825 * current rule, and go to the action execution point.
2826 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002827 if (s->current_rule) {
2828 rule = s->current_rule;
2829 s->current_rule = NULL;
2830 if (s->current_rule_list == rules)
2831 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002832 }
2833 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002834
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002835 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002836
2837 /* check optional condition */
2838 if (rule->cond) {
2839 int ret;
2840
Willy Tarreau192252e2015-04-04 01:47:55 +02002841 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002842 ret = acl_pass(ret);
2843
2844 if (rule->cond->pol == ACL_COND_UNLESS)
2845 ret = !ret;
2846
2847 if (!ret) /* condition not matched */
2848 continue;
2849 }
2850
Willy Tarreauacc98002015-09-27 23:34:39 +02002851 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002852resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002853 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002854 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002855 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002856
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002857 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002858 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002859 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002860
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002861 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002862 s->task->nice = rule->arg.nice;
2863 break;
2864
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002865 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002866 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002867 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002868 break;
2869
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002870 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002871#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002872 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002873 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002874#endif
2875 break;
2876
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002877 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002878 s->logs.level = rule->arg.loglevel;
2879 break;
2880
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002881 case ACT_HTTP_REPLACE_HDR:
2882 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002883 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2884 rule->arg.hdr_add.name_len,
2885 &rule->arg.hdr_add.fmt,
2886 &rule->arg.hdr_add.re, rule->action))
Christopher Fauletcdade942017-02-08 12:41:31 +01002887 return HTTP_RULE_RES_BADREQ;
Sasha Pachev218f0642014-06-16 12:05:59 -06002888 break;
2889
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002890 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002891 ctx.idx = 0;
2892 /* remove all occurrences of the header */
2893 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2894 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
2895 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2896 }
Willy Tarreau85603282015-01-21 20:39:27 +01002897 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002898
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002899 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002900 case ACT_HTTP_ADD_HDR: {
2901 struct chunk *replace;
2902
2903 replace = alloc_trash_chunk();
2904 if (!replace)
2905 return HTTP_RULE_RES_BADREQ;
2906
2907 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
2908 memcpy(replace->str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2909 replace->len = rule->arg.hdr_add.name_len;
2910 replace->str[replace->len++] = ':';
2911 replace->str[replace->len++] = ' ';
2912 replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len,
2913 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002914
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002915 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002916 /* remove all occurrences of the header */
2917 ctx.idx = 0;
2918 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2919 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
2920 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2921 }
2922 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002923 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->str, replace->len);
2924
2925 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002926 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002927 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002928
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002929 case ACT_HTTP_DEL_ACL:
2930 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002931 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002932 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002933
2934 /* collect reference */
2935 ref = pat_ref_lookup(rule->arg.map.ref);
2936 if (!ref)
2937 continue;
2938
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002939 /* allocate key */
2940 key = alloc_trash_chunk();
2941 if (!key)
2942 return HTTP_RULE_RES_BADREQ;
2943
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002944 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002945 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2946 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002947
2948 /* perform update */
2949 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002950 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002951 pat_ref_delete(ref, key->str);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002952 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002953
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002954 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002955 break;
2956 }
2957
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002958 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002959 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002960 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002961
2962 /* collect reference */
2963 ref = pat_ref_lookup(rule->arg.map.ref);
2964 if (!ref)
2965 continue;
2966
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002967 /* allocate key */
2968 key = alloc_trash_chunk();
2969 if (!key)
2970 return HTTP_RULE_RES_BADREQ;
2971
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002972 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002973 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2974 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002975
2976 /* perform update */
2977 /* check if the entry already exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002978 if (pat_ref_find_elt(ref, key->str) == NULL)
2979 pat_ref_add(ref, key->str, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002980
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002981 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002982 break;
2983 }
2984
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002985 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002986 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002987 struct chunk *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002988
2989 /* collect reference */
2990 ref = pat_ref_lookup(rule->arg.map.ref);
2991 if (!ref)
2992 continue;
2993
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002994 /* allocate key */
2995 key = alloc_trash_chunk();
2996 if (!key)
2997 return HTTP_RULE_RES_BADREQ;
2998
2999 /* allocate value */
3000 value = alloc_trash_chunk();
3001 if (!value) {
3002 free_trash_chunk(key);
3003 return HTTP_RULE_RES_BADREQ;
3004 }
3005
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003006 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003007 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
3008 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003009
3010 /* collect value */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003011 value->len = build_logline(s, value->str, value->size, &rule->arg.map.value);
3012 value->str[value->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003013
3014 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003015 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003016 if (pat_ref_find_elt(ref, key->str) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003017 /* update entry if it exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003018 pat_ref_set(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003019 else
3020 /* insert a new entry */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003021 pat_ref_add(ref, key->str, value->str, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003022 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003023 free_trash_chunk(key);
3024 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003025 break;
3026 }
William Lallemand73025dd2014-04-24 14:38:37 +02003027
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003028 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003029 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3030 return HTTP_RULE_RES_BADREQ;
3031 return HTTP_RULE_RES_DONE;
3032
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003033 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3034 /* Note: only the first valid tracking parameter of each
3035 * applies.
3036 */
3037
Christopher Faulet4fce0d82017-09-18 11:57:31 +02003038 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003039 struct stktable *t;
3040 struct stksess *ts;
3041 struct stktable_key *key;
3042 void *ptr;
3043
3044 t = rule->arg.trk_ctr.table.t;
3045 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
3046
3047 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02003048 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003049
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003050 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003051
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003052 /* let's count a new HTTP request as it's the first time we do it */
3053 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3054 if (ptr)
3055 stktable_data_cast(ptr, http_req_cnt)++;
3056
3057 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3058 if (ptr)
3059 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3060 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3061
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003062 /* When the client triggers a 4xx from the server, it's most often due
3063 * to a missing object or permission. These events should be tracked
3064 * because if they happen often, it may indicate a brute force or a
3065 * vulnerability scan. Normally this is done when receiving the response
3066 * but here we're tracking after this ought to have been done so we have
3067 * to do it on purpose.
3068 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02003069 if ((unsigned)(txn->status - 400) < 100) {
3070 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3071 if (ptr)
3072 stktable_data_cast(ptr, http_err_cnt)++;
3073
3074 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3075 if (ptr)
3076 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3077 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3078 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02003079
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003080 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003081
Emeric Brun0fed0b02017-11-29 16:15:07 +01003082 /* If data was modified, we need to touch to re-schedule sync */
3083 stktable_touch_local(t, ts, 0);
3084
Emeric Brun819fc6f2017-06-13 19:37:32 +02003085 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3086 if (sess->fe != s->be)
3087 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3088
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003089 }
3090 }
3091 break;
3092
Thierry FOURNIER42148732015-09-02 17:17:33 +02003093 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003094 if ((s->req.flags & CF_READ_ERROR) ||
3095 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
3096 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
3097 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02003098 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003099
Willy Tarreauacc98002015-09-27 23:34:39 +02003100 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003101 case ACT_RET_ERR:
3102 case ACT_RET_CONT:
3103 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003104 case ACT_RET_STOP:
3105 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003106 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003107 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003108 return HTTP_RULE_RES_YIELD;
3109 }
William Lallemand73025dd2014-04-24 14:38:37 +02003110 break;
3111
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003112 /* other flags exists, but normaly, they never be matched. */
3113 default:
3114 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003115 }
3116 }
3117
3118 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003119 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003120}
3121
3122
Willy Tarreau71241ab2012-12-27 11:30:54 +01003123/* Perform an HTTP redirect based on the information in <rule>. The function
3124 * returns non-zero on success, or zero in case of a, irrecoverable error such
3125 * as too large a request to build a valid response.
3126 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003127static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003128{
Willy Tarreaub329a312015-05-22 16:27:37 +02003129 struct http_msg *req = &txn->req;
3130 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003131 const char *msg_fmt;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003132 struct chunk *chunk;
3133 int ret = 0;
3134
3135 chunk = alloc_trash_chunk();
3136 if (!chunk)
3137 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003138
3139 /* build redirect message */
3140 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003141 case 308:
3142 msg_fmt = HTTP_308;
3143 break;
3144 case 307:
3145 msg_fmt = HTTP_307;
3146 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003147 case 303:
3148 msg_fmt = HTTP_303;
3149 break;
3150 case 301:
3151 msg_fmt = HTTP_301;
3152 break;
3153 case 302:
3154 default:
3155 msg_fmt = HTTP_302;
3156 break;
3157 }
3158
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003159 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
3160 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003161
3162 switch(rule->type) {
3163 case REDIRECT_TYPE_SCHEME: {
3164 const char *path;
3165 const char *host;
3166 struct hdr_ctx ctx;
3167 int pathlen;
3168 int hostlen;
3169
3170 host = "";
3171 hostlen = 0;
3172 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02003173 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003174 host = ctx.line + ctx.val;
3175 hostlen = ctx.vlen;
3176 }
3177
3178 path = http_get_path(txn);
3179 /* build message using path */
3180 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003181 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003182 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3183 int qs = 0;
3184 while (qs < pathlen) {
3185 if (path[qs] == '?') {
3186 pathlen = qs;
3187 break;
3188 }
3189 qs++;
3190 }
3191 }
3192 } else {
3193 path = "/";
3194 pathlen = 1;
3195 }
3196
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003197 if (rule->rdr_str) { /* this is an old "redirect" rule */
3198 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003199 if (chunk->len + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
3200 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003201
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003202 /* add scheme */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003203 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3204 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003205 }
3206 else {
3207 /* add scheme with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003208 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003209
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003210 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003211 if (chunk->len + 3 + hostlen + pathlen > chunk->size - 4)
3212 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003213 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003214 /* add "://" */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003215 memcpy(chunk->str + chunk->len, "://", 3);
3216 chunk->len += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003217
3218 /* add host */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003219 memcpy(chunk->str + chunk->len, host, hostlen);
3220 chunk->len += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003221
3222 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003223 memcpy(chunk->str + chunk->len, path, pathlen);
3224 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003225
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003226 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003227 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003228 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003229 if (chunk->len > chunk->size - 5)
3230 goto leave;
3231 chunk->str[chunk->len] = '/';
3232 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003233 }
3234
3235 break;
3236 }
3237 case REDIRECT_TYPE_PREFIX: {
3238 const char *path;
3239 int pathlen;
3240
3241 path = http_get_path(txn);
3242 /* build message using path */
3243 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003244 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003245 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3246 int qs = 0;
3247 while (qs < pathlen) {
3248 if (path[qs] == '?') {
3249 pathlen = qs;
3250 break;
3251 }
3252 qs++;
3253 }
3254 }
3255 } else {
3256 path = "/";
3257 pathlen = 1;
3258 }
3259
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003260 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003261 if (chunk->len + rule->rdr_len + pathlen > chunk->size - 4)
3262 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003263
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003264 /* add prefix. Note that if prefix == "/", we don't want to
3265 * add anything, otherwise it makes it hard for the user to
3266 * configure a self-redirection.
3267 */
3268 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003269 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3270 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003271 }
3272 }
3273 else {
3274 /* add prefix with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003275 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003276
3277 /* Check length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003278 if (chunk->len + pathlen > chunk->size - 4)
3279 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003280 }
3281
3282 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003283 memcpy(chunk->str + chunk->len, path, pathlen);
3284 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003285
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003286 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003287 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003288 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003289 if (chunk->len > chunk->size - 5)
3290 goto leave;
3291 chunk->str[chunk->len] = '/';
3292 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003293 }
3294
3295 break;
3296 }
3297 case REDIRECT_TYPE_LOCATION:
3298 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003299 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003300 if (chunk->len + rule->rdr_len > chunk->size - 4)
3301 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003302
3303 /* add location */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003304 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3305 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003306 }
3307 else {
3308 /* add location with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003309 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003310
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003311 /* Check left length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003312 if (chunk->len > chunk->size - 4)
3313 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003314 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003315 break;
3316 }
3317
3318 if (rule->cookie_len) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003319 memcpy(chunk->str + chunk->len, "\r\nSet-Cookie: ", 14);
3320 chunk->len += 14;
3321 memcpy(chunk->str + chunk->len, rule->cookie_str, rule->cookie_len);
3322 chunk->len += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003323 }
3324
Willy Tarreau19b14122017-02-28 09:48:11 +01003325 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003326 txn->status = rule->code;
3327 /* let's log the request time */
3328 s->logs.tv_request = now;
3329
Christopher Fauletbe821b92017-03-30 11:21:53 +02003330 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003331 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3332 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3333 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02003334 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003335 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003336 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: keep-alive", 30);
3337 chunk->len += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003338 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003339 memcpy(chunk->str + chunk->len, "\r\nConnection: keep-alive", 24);
3340 chunk->len += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003341 }
3342 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003343 memcpy(chunk->str + chunk->len, "\r\n\r\n", 4);
3344 chunk->len += 4;
3345 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau06d80a92017-10-19 14:32:15 +02003346 co_inject(res->chn, chunk->str, chunk->len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003347 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02003348 bi_fast_delete(req->chn->buf, req->sov);
3349 req->next -= req->sov;
3350 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003351 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01003352 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02003353 req->msg_state = HTTP_MSG_CLOSED;
3354 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02003355 /* Trim any possible response */
3356 res->chn->buf->i = 0;
3357 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02003358 /* let the server side turn to SI_ST_CLO */
3359 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003360 } else {
3361 /* keep-alive not possible */
3362 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003363 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3364 chunk->len += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003365 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003366 memcpy(chunk->str + chunk->len, "\r\nConnection: close\r\n\r\n", 23);
3367 chunk->len += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003368 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003369 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01003370 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003371 }
3372
Willy Tarreaue7dff022015-04-03 01:14:29 +02003373 if (!(s->flags & SF_ERR_MASK))
3374 s->flags |= SF_ERR_LOCAL;
3375 if (!(s->flags & SF_FINST_MASK))
3376 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003377
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003378 ret = 1;
3379 leave:
3380 free_trash_chunk(chunk);
3381 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003382}
3383
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003384/* This stream analyser runs all HTTP request processing which is common to
3385 * frontends and backends, which means blocking ACLs, filters, connection-close,
3386 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003387 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003388 * either needs more data or wants to immediately abort the request (eg: deny,
3389 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003390 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003391int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003392{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003393 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003394 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02003395 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003396 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003397 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02003398 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003399 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02003400 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02003401
Willy Tarreau655dce92009-11-08 13:10:58 +01003402 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003403 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003404 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02003405 }
3406
Willy Tarreau87b09662015-04-03 00:22:06 +02003407 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 +02003408 now_ms, __FUNCTION__,
3409 s,
3410 req,
3411 req->rex, req->wex,
3412 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003413 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003414 req->analysers);
3415
Willy Tarreau65410832014-04-28 21:25:43 +02003416 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02003417 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02003418
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003419 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02003420 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02003421 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01003422
Willy Tarreau0b748332014-04-29 00:13:29 +02003423 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003424 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
3425 goto return_prx_yield;
3426
Willy Tarreau0b748332014-04-29 00:13:29 +02003427 case HTTP_RULE_RES_CONT:
3428 case HTTP_RULE_RES_STOP: /* nothing to do */
3429 break;
Willy Tarreau52542592014-04-28 18:33:26 +02003430
Willy Tarreau0b748332014-04-29 00:13:29 +02003431 case HTTP_RULE_RES_DENY: /* deny or tarpit */
3432 if (txn->flags & TX_CLTARPIT)
3433 goto tarpit;
3434 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003435
Willy Tarreau0b748332014-04-29 00:13:29 +02003436 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
3437 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02003438
Willy Tarreau0b748332014-04-29 00:13:29 +02003439 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02003440 goto done;
3441
Willy Tarreau0b748332014-04-29 00:13:29 +02003442 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
3443 goto return_bad_req;
3444 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003445 }
3446
Olivier Houchard25ae45a2017-11-29 19:51:19 +01003447 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
3448 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02003449 struct hdr_ctx ctx;
3450
3451 ctx.idx = 0;
3452 if (!http_find_header2("Early-Data", strlen("Early-Data"),
3453 s->req.buf->p, &txn->hdr_idx, &ctx)) {
3454 if (unlikely(http_header_add_tail2(&txn->req,
3455 &txn->hdr_idx, "Early-Data: 1",
3456 strlen("Early-Data: 1"))) < 0) {
3457 goto return_bad_req;
3458 }
3459 }
3460
3461 }
3462
Willy Tarreau52542592014-04-28 18:33:26 +02003463 /* OK at this stage, we know that the request was accepted according to
3464 * the http-request rules, we can check for the stats. Note that the
3465 * URI is detected *before* the req* rules in order not to be affected
3466 * by a possible reqrep, while they are processed *after* so that a
3467 * reqdeny can still block them. This clearly needs to change in 1.6!
3468 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003469 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02003470 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01003471 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02003472 txn->status = 500;
3473 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003474 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003475
Willy Tarreaue7dff022015-04-03 01:14:29 +02003476 if (!(s->flags & SF_ERR_MASK))
3477 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02003478 goto return_prx_cond;
3479 }
3480
3481 /* parse the whole stats request and extract the relevant information */
3482 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02003483 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02003484 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003485
Willy Tarreau0b748332014-04-29 00:13:29 +02003486 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
3487 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003488
Willy Tarreau0b748332014-04-29 00:13:29 +02003489 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
3490 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003491 }
3492
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003493 /* evaluate the req* rules except reqadd */
3494 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003495 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003496 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003497
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003498 if (txn->flags & TX_CLDENY)
3499 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003500
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003501 if (txn->flags & TX_CLTARPIT) {
3502 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003503 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003504 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003505 }
Willy Tarreau06619262006-12-17 08:37:22 +01003506
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003507 /* add request headers from the rule sets in the same order */
3508 list_for_each_entry(wl, &px->req_add, list) {
3509 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003510 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003511 ret = acl_pass(ret);
3512 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3513 ret = !ret;
3514 if (!ret)
3515 continue;
3516 }
3517
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003518 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003519 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003520 }
3521
Willy Tarreau52542592014-04-28 18:33:26 +02003522
3523 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01003524 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
3525 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003526 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003527 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003528 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01003529
Willy Tarreaue7dff022015-04-03 01:14:29 +02003530 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
3531 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
3532 if (!(s->flags & SF_FINST_MASK))
3533 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003534
Willy Tarreau70730dd2014-04-24 18:06:27 +02003535 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01003536 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
3537 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003538 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003539 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003540 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003541
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003542 /* check whether we have some ACLs set to redirect this request */
3543 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003544 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003545 int ret;
3546
Willy Tarreau192252e2015-04-04 01:47:55 +02003547 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003548 ret = acl_pass(ret);
3549 if (rule->cond->pol == ACL_COND_UNLESS)
3550 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003551 if (!ret)
3552 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003553 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003554 if (!http_apply_redirect_rule(rule, s, txn))
3555 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003556 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003557 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003558
Willy Tarreau2be39392010-01-03 17:24:51 +01003559 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3560 * If this happens, then the data will not come immediately, so we must
3561 * send all what we have without waiting. Note that due to the small gain
3562 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003563 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003564 * itself once used.
3565 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003566 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003567
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003568 done: /* done with this analyser, continue with next ones that the calling
3569 * points will have set, if any.
3570 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003571 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003572 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
3573 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003574 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003575
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003576 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02003577 /* Allow cookie logging
3578 */
3579 if (s->be->cookie_name || sess->fe->capture_name)
3580 manage_client_side_cookies(s, req);
3581
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003582 /* When a connection is tarpitted, we use the tarpit timeout,
3583 * which may be the same as the connect timeout if unspecified.
3584 * If unset, then set it to zero because we really want it to
3585 * eventually expire. We build the tarpit as an analyser.
3586 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003587 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003588
3589 /* wipe the request out so that we can drop the connection early
3590 * if the client closes first.
3591 */
3592 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003593
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003594 txn->status = http_err_codes[deny_status];
3595
Christopher Faulet0184ea72017-01-05 14:06:34 +01003596 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003597 req->analysers |= AN_REQ_HTTP_TARPIT;
3598 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3599 if (!req->analyse_exp)
3600 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02003601 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003602 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003603 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003604 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003605 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003606 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003607 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003608
3609 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003610
3611 /* Allow cookie logging
3612 */
3613 if (s->be->cookie_name || sess->fe->capture_name)
3614 manage_client_side_cookies(s, req);
3615
Willy Tarreau0b748332014-04-29 00:13:29 +02003616 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003617 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003618 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003619 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02003620 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003621 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003622 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003623 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003624 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003625 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003626 goto return_prx_cond;
3627
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003628 return_bad_req:
3629 /* We centralize bad requests processing here */
3630 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3631 /* we detected a parsing error. We want to archive this request
3632 * in the dedicated proxy area for later troubleshooting.
3633 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02003634 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003635 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003636
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003637 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003638 txn->req.msg_state = HTTP_MSG_ERROR;
3639 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003640 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003641
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003642 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003643 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003644 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003645
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003646 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003647 if (!(s->flags & SF_ERR_MASK))
3648 s->flags |= SF_ERR_PRXCOND;
3649 if (!(s->flags & SF_FINST_MASK))
3650 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003651
Christopher Faulet0184ea72017-01-05 14:06:34 +01003652 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003653 req->analyse_exp = TICK_ETERNITY;
3654 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003655
3656 return_prx_yield:
3657 channel_dont_connect(req);
3658 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003659}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003660
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003661/* This function performs all the processing enabled for the current request.
3662 * It returns 1 if the processing can continue on next analysers, or zero if it
3663 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003664 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003665 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003666int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003667{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003668 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003669 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003670 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003671 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003672
Willy Tarreau655dce92009-11-08 13:10:58 +01003673 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003674 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003675 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003676 return 0;
3677 }
3678
Willy Tarreau87b09662015-04-03 00:22:06 +02003679 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003680 now_ms, __FUNCTION__,
3681 s,
3682 req,
3683 req->rex, req->wex,
3684 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003685 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003686 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003687
Willy Tarreau59234e92008-11-30 23:51:27 +01003688 /*
3689 * Right now, we know that we have processed the entire headers
3690 * and that unwanted requests have been filtered out. We can do
3691 * whatever we want with the remaining request. Also, now we
3692 * may have separate values for ->fe, ->be.
3693 */
Willy Tarreau06619262006-12-17 08:37:22 +01003694
Willy Tarreau59234e92008-11-30 23:51:27 +01003695 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003696 * If HTTP PROXY is set we simply get remote server address parsing
3697 * incoming request. Note that this requires that a connection is
3698 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003699 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003700 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003701 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003702 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003703
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003704 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003705 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003706 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003707 txn->req.msg_state = HTTP_MSG_ERROR;
3708 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003709 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003710 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003711
Willy Tarreaue7dff022015-04-03 01:14:29 +02003712 if (!(s->flags & SF_ERR_MASK))
3713 s->flags |= SF_ERR_RESOURCE;
3714 if (!(s->flags & SF_FINST_MASK))
3715 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003716
3717 return 0;
3718 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003719
3720 path = http_get_path(txn);
Christopher Faulet11ebb202018-04-13 15:53:12 +02003721 if (url2sa(req->buf->p + msg->sl.rq.u,
3722 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
3723 &conn->addr.to, NULL) == -1)
3724 goto return_bad_req;
3725
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003726 /* if the path was found, we have to remove everything between
3727 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
3728 * found, we need to replace from req->buf->p + msg->sl.rq.u for
3729 * u_l characters by a single "/".
3730 */
3731 if (path) {
3732 char *cur_ptr = req->buf->p;
3733 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3734 int delta;
3735
3736 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
3737 http_msg_move_end(&txn->req, delta);
3738 cur_end += delta;
3739 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3740 goto return_bad_req;
3741 }
3742 else {
3743 char *cur_ptr = req->buf->p;
3744 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3745 int delta;
3746
3747 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
3748 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
3749 http_msg_move_end(&txn->req, delta);
3750 cur_end += delta;
3751 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3752 goto return_bad_req;
3753 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003754 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003755
Willy Tarreau59234e92008-11-30 23:51:27 +01003756 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003757 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003758 * Note that doing so might move headers in the request, but
3759 * the fields will stay coherent and the URI will not move.
3760 * This should only be performed in the backend.
3761 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003762 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003763 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003764
William Lallemanda73203e2012-03-12 12:48:57 +01003765 /* add unique-id if "header-unique-id" is specified */
3766
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003767 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01003768 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003769 goto return_bad_req;
3770 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003771 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003772 }
William Lallemanda73203e2012-03-12 12:48:57 +01003773
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003774 if (sess->fe->header_unique_id && s->unique_id) {
3775 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003776 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003777 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003778 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003779 goto return_bad_req;
3780 }
3781
Cyril Bontéb21570a2009-11-29 20:04:48 +01003782 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003783 * 9: add X-Forwarded-For if either the frontend or the backend
3784 * asks for it.
3785 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003786 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003787 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003788 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3789 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3790 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003791 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003792 /* The header is set to be added only if none is present
3793 * and we found it, so don't do anything.
3794 */
3795 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003796 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003797 /* Add an X-Forwarded-For header unless the source IP is
3798 * in the 'except' network range.
3799 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003800 if ((!sess->fe->except_mask.s_addr ||
3801 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3802 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003803 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003804 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003805 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003806 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003807 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003808 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003809
3810 /* Note: we rely on the backend to get the header name to be used for
3811 * x-forwarded-for, because the header is really meant for the backends.
3812 * However, if the backend did not specify any option, we have to rely
3813 * on the frontend's header name.
3814 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003815 if (s->be->fwdfor_hdr_len) {
3816 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003817 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003818 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003819 len = sess->fe->fwdfor_hdr_len;
3820 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003821 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003822 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 +01003823
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003824 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003825 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003826 }
3827 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003828 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003829 /* FIXME: for the sake of completeness, we should also support
3830 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003831 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003832 int len;
3833 char pn[INET6_ADDRSTRLEN];
3834 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003835 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003836 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003837
Willy Tarreau59234e92008-11-30 23:51:27 +01003838 /* Note: we rely on the backend to get the header name to be used for
3839 * x-forwarded-for, because the header is really meant for the backends.
3840 * However, if the backend did not specify any option, we have to rely
3841 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003842 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003843 if (s->be->fwdfor_hdr_len) {
3844 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003845 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003846 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003847 len = sess->fe->fwdfor_hdr_len;
3848 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003849 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003850 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003851
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003852 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003853 goto return_bad_req;
3854 }
3855 }
3856
3857 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003858 * 10: add X-Original-To if either the frontend or the backend
3859 * asks for it.
3860 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003861 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003862
3863 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003864 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003865 /* Add an X-Original-To header unless the destination IP is
3866 * in the 'except' network range.
3867 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003868 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003869
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003870 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003871 ((!sess->fe->except_mask_to.s_addr ||
3872 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3873 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003874 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003875 (((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 +02003876 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003877 int len;
3878 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003879 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003880
3881 /* Note: we rely on the backend to get the header name to be used for
3882 * x-original-to, because the header is really meant for the backends.
3883 * However, if the backend did not specify any option, we have to rely
3884 * on the frontend's header name.
3885 */
3886 if (s->be->orgto_hdr_len) {
3887 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003888 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003889 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003890 len = sess->fe->orgto_hdr_len;
3891 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003892 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003893 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 +02003894
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003895 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003896 goto return_bad_req;
3897 }
3898 }
3899 }
3900
Willy Tarreau50fc7772012-11-11 22:19:57 +01003901 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3902 * If an "Upgrade" token is found, the header is left untouched in order not to have
3903 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3904 * "Upgrade" is present in the Connection header.
3905 */
3906 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3907 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003908 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003909 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003910 unsigned int want_flags = 0;
3911
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003912 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003913 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003914 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003915 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003916 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003917 want_flags |= TX_CON_CLO_SET;
3918 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003919 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003920 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003921 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003922 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003923 want_flags |= TX_CON_KAL_SET;
3924 }
3925
3926 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003927 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003928 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003929
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003930
Willy Tarreau522d6c02009-12-06 18:49:18 +01003931 /* If we have no server assigned yet and we're balancing on url_param
3932 * with a POST request, we may be interested in checking the body for
3933 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003934 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003935 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003936 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003937 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003938 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003939 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003940 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003941
Christopher Fauletbe821b92017-03-30 11:21:53 +02003942 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3943 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003944#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003945 /* We expect some data from the client. Unless we know for sure
3946 * we already have a full request, we have to re-enable quick-ack
3947 * in case we previously disabled it, otherwise we might cause
3948 * the client to delay further data.
3949 */
3950 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3951 cli_conn && conn_ctrl_ready(cli_conn) &&
3952 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
3953 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003954 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003955#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003956
Willy Tarreau59234e92008-11-30 23:51:27 +01003957 /*************************************************************
3958 * OK, that's finished for the headers. We have done what we *
3959 * could. Let's switch to the DATA state. *
3960 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003961 req->analyse_exp = TICK_ETERNITY;
3962 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003963
Willy Tarreau59234e92008-11-30 23:51:27 +01003964 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003965 /* OK let's go on with the BODY now */
3966 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003967
Willy Tarreau59234e92008-11-30 23:51:27 +01003968 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003969 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003970 /* we detected a parsing error. We want to archive this request
3971 * in the dedicated proxy area for later troubleshooting.
3972 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02003973 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003974 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003975
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003976 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003977 txn->req.msg_state = HTTP_MSG_ERROR;
3978 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003979 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003980 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003981
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003982 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003983 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003984 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003985
Willy Tarreaue7dff022015-04-03 01:14:29 +02003986 if (!(s->flags & SF_ERR_MASK))
3987 s->flags |= SF_ERR_PRXCOND;
3988 if (!(s->flags & SF_FINST_MASK))
3989 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003990 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003991}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003992
Willy Tarreau60b85b02008-11-30 23:28:40 +01003993/* This function is an analyser which processes the HTTP tarpit. It always
3994 * returns zero, at the beginning because it prevents any other processing
3995 * from occurring, and at the end because it terminates the request.
3996 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003997int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003998{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003999 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004000
4001 /* This connection is being tarpitted. The CLIENT side has
4002 * already set the connect expiration date to the right
4003 * timeout. We just have to check that the client is still
4004 * there and that the timeout has not expired.
4005 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004006 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004007 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004008 !tick_is_expired(req->analyse_exp, now_ms))
4009 return 0;
4010
4011 /* We will set the queue timer to the time spent, just for
4012 * logging purposes. We fake a 500 server error, so that the
4013 * attacker will not suspect his connection has been tarpitted.
4014 * It will not cause trouble to the logs because we can exclude
4015 * the tarpitted connections by filtering on the 'PT' status flags.
4016 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004017 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4018
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004019 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004020 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004021
Christopher Faulet0184ea72017-01-05 14:06:34 +01004022 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004023 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004024
Willy Tarreaue7dff022015-04-03 01:14:29 +02004025 if (!(s->flags & SF_ERR_MASK))
4026 s->flags |= SF_ERR_PRXCOND;
4027 if (!(s->flags & SF_FINST_MASK))
4028 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004029 return 0;
4030}
4031
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004032/* This function is an analyser which waits for the HTTP request body. It waits
4033 * for either the buffer to be full, or the full advertised contents to have
4034 * reached the buffer. It must only be called after the standard HTTP request
4035 * processing has occurred, because it expects the request to be parsed and will
4036 * look for the Expect header. It may send a 100-Continue interim response. It
4037 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4038 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4039 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004040 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004041int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004042{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004043 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004044 struct http_txn *txn = s->txn;
4045 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004046
4047 /* We have to parse the HTTP request body to find any required data.
4048 * "balance url_param check_post" should have been the only way to get
4049 * into this. We were brought here after HTTP header analysis, so all
4050 * related structures are ready.
4051 */
4052
Willy Tarreau890988f2014-04-10 11:59:33 +02004053 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4054 /* This is the first call */
4055 if (msg->msg_state < HTTP_MSG_BODY)
4056 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004057
Willy Tarreau890988f2014-04-10 11:59:33 +02004058 if (msg->msg_state < HTTP_MSG_100_SENT) {
4059 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4060 * send an HTTP/1.1 100 Continue intermediate response.
4061 */
4062 if (msg->flags & HTTP_MSGF_VER_11) {
4063 struct hdr_ctx ctx;
4064 ctx.idx = 0;
4065 /* Expect is allowed in 1.1, look for it */
4066 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4067 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02004068 co_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01004069 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02004070 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004071 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004072 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004073 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004074
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004075 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004076 * req->buf->p still points to the beginning of the message. We
4077 * must save the body in msg->next because it survives buffer
4078 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004079 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004080 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004081
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004082 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004083 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4084 else
4085 msg->msg_state = HTTP_MSG_DATA;
4086 }
4087
Willy Tarreau890988f2014-04-10 11:59:33 +02004088 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4089 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004090 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004091 goto missing_data;
4092
4093 /* OK we have everything we need now */
4094 goto http_end;
4095 }
4096
4097 /* OK here we're parsing a chunked-encoded message */
4098
Willy Tarreau522d6c02009-12-06 18:49:18 +01004099 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004100 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004101 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004102 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004103 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004104 unsigned int chunk;
4105 int ret = h1_parse_chunk_size(req->buf, msg->next, req->buf->i, &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01004106
Willy Tarreau115acb92009-12-26 13:56:06 +01004107 if (!ret)
4108 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004109 else if (ret < 0) {
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004110 msg->err_pos = req->buf->i + ret;
4111 if (msg->err_pos < 0)
4112 msg->err_pos += req->buf->size;
Willy Tarreau87b09662015-04-03 00:22:06 +02004113 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004114 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004115 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004116
4117 msg->chunk_len = chunk;
4118 msg->body_len += chunk;
4119
4120 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01004121 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004122 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01004123 }
4124
Willy Tarreaud98cf932009-12-27 22:54:55 +01004125 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004126 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4127 * for at least a whole chunk or the whole content length bytes after
4128 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004129 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004130 if (msg->msg_state == HTTP_MSG_TRAILERS)
4131 goto http_end;
4132
Willy Tarreaue115b492015-05-01 23:05:14 +02004133 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004134 goto http_end;
4135
4136 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004137 /* we get here if we need to wait for more data. If the buffer is full,
4138 * we have the maximum we can expect.
4139 */
4140 if (buffer_full(req->buf, global.tune.maxrewrite))
4141 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004142
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004143 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004144 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004145 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004146
Willy Tarreaue7dff022015-04-03 01:14:29 +02004147 if (!(s->flags & SF_ERR_MASK))
4148 s->flags |= SF_ERR_CLITO;
4149 if (!(s->flags & SF_FINST_MASK))
4150 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004151 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004152 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004153
4154 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004155 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004156 /* Not enough data. We'll re-use the http-request
4157 * timeout here. Ideally, we should set the timeout
4158 * relative to the accept() date. We just set the
4159 * request timeout once at the beginning of the
4160 * request.
4161 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004162 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004163 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004164 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004165 return 0;
4166 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004167
4168 http_end:
4169 /* The situation will not evolve, so let's give up on the analysis. */
4170 s->logs.tv_request = now; /* update the request timer to reflect full request */
4171 req->analysers &= ~an_bit;
4172 req->analyse_exp = TICK_ETERNITY;
4173 return 1;
4174
4175 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004176 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004177 txn->req.msg_state = HTTP_MSG_ERROR;
4178 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004179 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004180
Willy Tarreaue7dff022015-04-03 01:14:29 +02004181 if (!(s->flags & SF_ERR_MASK))
4182 s->flags |= SF_ERR_PRXCOND;
4183 if (!(s->flags & SF_FINST_MASK))
4184 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004185
Willy Tarreau522d6c02009-12-06 18:49:18 +01004186 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01004187 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004188 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004189 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004190 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004191 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004192}
4193
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004194/* send a server's name with an outgoing request over an established connection.
4195 * Note: this function is designed to be called once the request has been scheduled
4196 * for being forwarded. This is the reason why it rewinds the buffer before
4197 * proceeding.
4198 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004199int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004200
4201 struct hdr_ctx ctx;
4202
Mark Lamourinec2247f02012-01-04 13:02:01 -05004203 char *hdr_name = be->server_id_hdr_name;
4204 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004205 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004206 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004207 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004208
William Lallemandd9e90662012-01-30 17:27:17 +01004209 ctx.idx = 0;
4210
Willy Tarreau211cdec2014-04-17 20:18:08 +02004211 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004212 if (old_o) {
4213 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004214 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004215 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004216 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004217 }
4218
Willy Tarreau9b28e032012-10-12 23:49:43 +02004219 old_i = chn->buf->i;
4220 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 -05004221 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004222 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004223 }
4224
4225 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004226 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004227 memcpy(hdr_val, hdr_name, hdr_name_len);
4228 hdr_val += hdr_name_len;
4229 *hdr_val++ = ':';
4230 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004231 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4232 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004233
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004234 if (old_o) {
4235 /* If this was a forwarded request, we must readjust the amount of
4236 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004237 * variations. Note that the current state is >= HTTP_MSG_BODY,
4238 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004239 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004240 old_o += chn->buf->i - old_i;
4241 b_adv(chn->buf, old_o);
4242 txn->req.next -= old_o;
4243 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004244 }
4245
Mark Lamourinec2247f02012-01-04 13:02:01 -05004246 return 0;
4247}
4248
Willy Tarreau610ecce2010-01-04 21:15:02 +01004249/* Terminate current transaction and prepare a new one. This is very tricky
4250 * right now but it works.
4251 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004252void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004253{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004254 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02004255 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01004256 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004257 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02004258 struct connection *srv_conn;
4259 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02004260 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01004261
Willy Tarreau610ecce2010-01-04 21:15:02 +01004262 /* FIXME: We need a more portable way of releasing a backend's and a
4263 * server's connections. We need a safer way to reinitialize buffer
4264 * flags. We also need a more accurate method for computing per-request
4265 * data.
4266 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004267 /*
4268 * XXX cognet: This is probably wrong, this is killing a whole
4269 * connection, in the new world order, we probably want to just kill
4270 * the stream, this is to be revisited the day we handle multiple
4271 * streams in one server connection.
4272 */
4273 cs = objt_cs(s->si[1].end);
4274 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004275
Willy Tarreau4213a112013-12-15 10:25:42 +01004276 /* unless we're doing keep-alive, we want to quickly close the connection
4277 * to the server.
4278 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004279 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004280 !si_conn_ready(&s->si[1])) {
4281 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4282 si_shutr(&s->si[1]);
4283 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004284 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004285
Willy Tarreaue7dff022015-04-03 01:14:29 +02004286 if (s->flags & SF_BE_ASSIGNED) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004287 HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004288 if (unlikely(s->srv_conn))
4289 sess_change_server(s, NULL);
4290 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004291
4292 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02004293 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004294
Willy Tarreaueee5b512015-04-03 23:46:31 +02004295 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004296 int n;
4297
Willy Tarreaueee5b512015-04-03 23:46:31 +02004298 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004299 if (n < 1 || n > 5)
4300 n = 0;
4301
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004302 if (fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004303 HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004304 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02004305 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01004306 (be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004307 HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
4308 HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004309 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004310 }
4311
4312 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004313 s->logs.bytes_in -= s->req.buf->i;
4314 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004315
4316 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004317 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02004318 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004319 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004320 s->do_log(s);
4321 }
4322
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004323 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02004324 stream_stop_content_counters(s);
4325 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004326
Willy Tarreau610ecce2010-01-04 21:15:02 +01004327 s->logs.accept_date = date; /* user-visible date for logging */
4328 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02004329 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
4330 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004331 tv_zero(&s->logs.tv_request);
4332 s->logs.t_queue = -1;
4333 s->logs.t_connect = -1;
4334 s->logs.t_data = -1;
4335 s->logs.t_close = 0;
4336 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4337 s->logs.srv_queue_size = 0; /* we will get this number soon */
4338
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004339 s->logs.bytes_in = s->req.total = s->req.buf->i;
4340 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004341
4342 if (s->pend_pos)
4343 pendconn_free(s->pend_pos);
4344
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004345 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004346 if (s->flags & SF_CURR_SESS) {
4347 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +02004348 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004349 }
Willy Tarreau858b1032015-12-07 17:04:59 +01004350 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004351 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004352 }
4353
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004354 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004355
Willy Tarreau4213a112013-12-15 10:25:42 +01004356 /* only release our endpoint if we don't intend to reuse the
4357 * connection.
4358 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004359 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004360 !si_conn_ready(&s->si[1])) {
4361 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02004362 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004363 }
4364
Willy Tarreau350f4872014-11-28 14:42:25 +01004365 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
4366 s->si[1].err_type = SI_ET_NONE;
4367 s->si[1].conn_retries = 0; /* used for logging too */
4368 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02004369 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 +01004370 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 +01004371 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 +02004372 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
4373 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
4374 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01004375
Willy Tarreaueee5b512015-04-03 23:46:31 +02004376 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004377 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004378 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004379
4380 if (prev_status == 401 || prev_status == 407) {
4381 /* In HTTP keep-alive mode, if we receive a 401, we still have
4382 * a chance of being able to send the visitor again to the same
4383 * server over the same connection. This is required by some
4384 * broken protocols such as NTLM, and anyway whenever there is
4385 * an opportunity for sending the challenge to the proper place,
4386 * it's better to do it (at least it helps with debugging).
4387 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004388 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02004389 if (srv_conn)
4390 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01004391 }
4392
Willy Tarreau53f96852016-02-02 18:50:47 +01004393 /* Never ever allow to reuse a connection from a non-reuse backend */
4394 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
4395 srv_conn->flags |= CO_FL_PRIVATE;
4396
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004397 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01004398 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004399
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004400 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004401 s->req.flags |= CF_NEVER_WAIT;
4402 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004403 }
4404
Willy Tarreau714ea782015-11-25 20:11:11 +01004405 /* we're removing the analysers, we MUST re-enable events detection.
4406 * We don't enable close on the response channel since it's either
4407 * already closed, or in keep-alive with an idle connection handler.
4408 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004409 channel_auto_read(&s->req);
4410 channel_auto_close(&s->req);
4411 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004412
Willy Tarreau1c59bd52015-11-02 20:20:11 +01004413 /* we're in keep-alive with an idle connection, monitor it if not already done */
4414 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02004415 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02004416 if (!srv)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004417 si_idle_cs(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01004418 else if (srv_conn->flags & CO_FL_PRIVATE)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004419 si_idle_cs(&s->si[1], (srv->priv_conns ? &srv->priv_conns[tid] : NULL));
Willy Tarreau449d74a2015-08-05 17:16:33 +02004420 else if (prev_flags & TX_NOT_FIRST)
4421 /* note: we check the request, not the connection, but
4422 * this is valid for strategies SAFE and AGGR, and in
4423 * case of ALWS, we don't care anyway.
4424 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004425 si_idle_cs(&s->si[1], (srv->safe_conns ? &srv->safe_conns[tid] : NULL));
Willy Tarreau8dff9982015-08-04 20:45:52 +02004426 else
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004427 si_idle_cs(&s->si[1], (srv->idle_conns ? &srv->idle_conns[tid] : NULL));
Willy Tarreau4320eaa2015-08-05 11:08:30 +02004428 }
Christopher Faulete6006242017-03-10 11:52:44 +01004429 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
4430 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004431}
4432
4433
4434/* This function updates the request state machine according to the response
4435 * state machine and buffer flags. It returns 1 if it changes anything (flag
4436 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4437 * it is only used to find when a request/response couple is complete. Both
4438 * this function and its equivalent should loop until both return zero. It
4439 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4440 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004441int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004442{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004443 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004444 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004445 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004446 unsigned int old_state = txn->req.msg_state;
4447
Christopher Faulet4be98032017-07-18 10:48:24 +02004448 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004449 return 0;
4450
4451 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004452 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004453 * We can shut the read side unless we want to abort_on_close,
4454 * or we have a POST request. The issue with POST requests is
4455 * that some browsers still send a CRLF after the request, and
4456 * this CRLF must be read so that it does not remain in the kernel
4457 * buffers, otherwise a close could cause an RST on some systems
4458 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004459 * Note that if we're using keep-alive on the client side, we'd
4460 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02004461 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01004462 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004463 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004464 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4465 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004466 (!(s->be->options & PR_O_ABRT_CLOSE) ||
4467 (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01004468 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004469 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004470
Willy Tarreau40f151a2012-12-20 12:10:09 +01004471 /* if the server closes the connection, we want to immediately react
4472 * and close the socket to save packets and syscalls.
4473 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004474 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01004475
Willy Tarreau7f876a12015-11-18 11:59:55 +01004476 /* In any case we've finished parsing the request so we must
4477 * disable Nagle when sending data because 1) we're not going
4478 * to shut this side, and 2) the server is waiting for us to
4479 * send pending data.
4480 */
4481 chn->flags |= CF_NEVER_WAIT;
4482
Willy Tarreau610ecce2010-01-04 21:15:02 +01004483 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4484 goto wait_other_side;
4485
4486 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4487 /* The server has not finished to respond, so we
4488 * don't want to move in order not to upset it.
4489 */
4490 goto wait_other_side;
4491 }
4492
Willy Tarreau610ecce2010-01-04 21:15:02 +01004493 /* When we get here, it means that both the request and the
4494 * response have finished receiving. Depending on the connection
4495 * mode, we'll have to wait for the last bytes to leave in either
4496 * direction, and sometimes for a close to be effective.
4497 */
4498
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004499 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4500 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004501 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4502 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004503 }
4504 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4505 /* Option forceclose is set, or either side wants to close,
4506 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004507 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004508 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004509 *
4510 * However, there is an exception if the response
4511 * length is undefined. In this case, we need to wait
4512 * the close from the server. The response will be
4513 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004514 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004515 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4516 txn->rsp.msg_state != HTTP_MSG_CLOSED)
4517 goto check_channel_flags;
4518
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004519 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4520 channel_shutr_now(chn);
4521 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004522 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004523 }
4524 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004525 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4526 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004527 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004528 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4529 channel_auto_read(chn);
4530 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004531 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004532 }
4533
Christopher Faulet4be98032017-07-18 10:48:24 +02004534 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004535 }
4536
4537 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4538 http_msg_closing:
4539 /* nothing else to forward, just waiting for the output buffer
4540 * to be empty and for the shutw_now to take effect.
4541 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004542 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004543 txn->req.msg_state = HTTP_MSG_CLOSED;
4544 goto http_msg_closed;
4545 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004546 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004547 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004548 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004549 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004550 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004551 }
4552
4553 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4554 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01004555 /* if we don't know whether the server will close, we need to hard close */
4556 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4557 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4558
Willy Tarreau3988d932013-12-27 23:03:08 +01004559 /* see above in MSG_DONE why we only do this in these states */
4560 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4561 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004562 (!(s->be->options & PR_O_ABRT_CLOSE) ||
4563 (s->si[0].flags & SI_FL_CLEAN_ABRT)))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004564 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004565 goto wait_other_side;
4566 }
4567
Christopher Faulet4be98032017-07-18 10:48:24 +02004568 check_channel_flags:
4569 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4570 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4571 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02004572 txn->req.msg_state = HTTP_MSG_CLOSING;
4573 goto http_msg_closing;
4574 }
4575
4576
Willy Tarreau610ecce2010-01-04 21:15:02 +01004577 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004578 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004579}
4580
4581
4582/* This function updates the response state machine according to the request
4583 * state machine and buffer flags. It returns 1 if it changes anything (flag
4584 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4585 * it is only used to find when a request/response couple is complete. Both
4586 * this function and its equivalent should loop until both return zero. It
4587 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4588 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004589int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004590{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004591 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004592 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004593 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004594 unsigned int old_state = txn->rsp.msg_state;
4595
Christopher Faulet4be98032017-07-18 10:48:24 +02004596 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004597 return 0;
4598
4599 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4600 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004601 * still monitor the server connection for a possible close
4602 * while the request is being uploaded, so we don't disable
4603 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004604 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004605 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004606
4607 if (txn->req.msg_state == HTTP_MSG_ERROR)
4608 goto wait_other_side;
4609
4610 if (txn->req.msg_state < HTTP_MSG_DONE) {
4611 /* The client seems to still be sending data, probably
4612 * because we got an error response during an upload.
4613 * We have the choice of either breaking the connection
4614 * or letting it pass through. Let's do the later.
4615 */
4616 goto wait_other_side;
4617 }
4618
Willy Tarreau610ecce2010-01-04 21:15:02 +01004619 /* When we get here, it means that both the request and the
4620 * response have finished receiving. Depending on the connection
4621 * mode, we'll have to wait for the last bytes to leave in either
4622 * direction, and sometimes for a close to be effective.
4623 */
4624
4625 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4626 /* Server-close mode : shut read and wait for the request
4627 * side to close its output buffer. The caller will detect
4628 * when we're in DONE and the other is in CLOSED and will
4629 * catch that for the final cleanup.
4630 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004631 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4632 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004633 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004634 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4635 /* Option forceclose is set, or either side wants to close,
4636 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004637 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004638 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004639 */
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01004640 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004641 channel_shutr_now(chn);
4642 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004643 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004644 }
4645 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004646 /* The last possible modes are keep-alive and tunnel. Tunnel will
4647 * need to forward remaining data. Keep-alive will need to monitor
4648 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004649 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004650 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004651 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004652 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4653 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004654 }
4655
Christopher Faulet4be98032017-07-18 10:48:24 +02004656 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004657 }
4658
4659 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4660 http_msg_closing:
4661 /* nothing else to forward, just waiting for the output buffer
4662 * to be empty and for the shutw_now to take effect.
4663 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004664 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004665 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4666 goto http_msg_closed;
4667 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004668 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004669 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004670 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004671 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004672 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004673 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004674 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004675 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004676 }
4677
4678 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4679 http_msg_closed:
4680 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004681 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004682 channel_auto_close(chn);
4683 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004684 goto wait_other_side;
4685 }
4686
Christopher Faulet4be98032017-07-18 10:48:24 +02004687 check_channel_flags:
4688 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4689 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4690 /* if we've just closed an output, let's switch */
4691 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4692 goto http_msg_closing;
4693 }
4694
Willy Tarreau610ecce2010-01-04 21:15:02 +01004695 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004696 /* We force the response to leave immediately if we're waiting for the
4697 * other side, since there is no pending shutdown to push it out.
4698 */
4699 if (!channel_is_empty(chn))
4700 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004701 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004702}
4703
4704
Christopher Faulet894da4c2017-07-18 11:29:07 +02004705/* Resync the request and response state machines. */
4706void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004707{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004708 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004709#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004710 int old_req_state = txn->req.msg_state;
4711 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004712#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004713
Willy Tarreau610ecce2010-01-04 21:15:02 +01004714 http_sync_req_state(s);
4715 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004716 if (!http_sync_res_state(s))
4717 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004718 if (!http_sync_req_state(s))
4719 break;
4720 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004721
Christopher Faulet894da4c2017-07-18 11:29:07 +02004722 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4723 "req->analysers=0x%08x res->analysers=0x%08x\n",
4724 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004725 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4726 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004727 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004728
4729
Willy Tarreau610ecce2010-01-04 21:15:02 +01004730 /* OK, both state machines agree on a compatible state.
4731 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004732 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4733 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004734 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4735 * means we must abort the request.
4736 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4737 * corresponding channel.
4738 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4739 * on the response with server-close mode means we've completed one
4740 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004741 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004742 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4743 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004744 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004745 channel_auto_close(&s->req);
4746 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004747 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004748 channel_auto_close(&s->res);
4749 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004750 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004751 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4752 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004753 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004754 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004755 channel_auto_close(&s->res);
4756 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004757 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004758 channel_abort(&s->req);
4759 channel_auto_close(&s->req);
4760 channel_auto_read(&s->req);
4761 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004762 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004763 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4764 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4765 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4766 s->req.analysers &= AN_REQ_FLT_END;
4767 if (HAS_REQ_DATA_FILTERS(s))
4768 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4769 }
4770 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4771 s->res.analysers &= AN_RES_FLT_END;
4772 if (HAS_RSP_DATA_FILTERS(s))
4773 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4774 }
4775 channel_auto_close(&s->req);
4776 channel_auto_read(&s->req);
4777 channel_auto_close(&s->res);
4778 channel_auto_read(&s->res);
4779 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004780 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4781 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004782 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004783 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4784 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4785 /* server-close/keep-alive: terminate this transaction,
4786 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004787 * a fresh-new transaction, but only once we're sure there's
4788 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004789 * another request. They must not hold any pending output data
4790 * and the response buffer must realigned
4791 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004792 */
Christopher Faulet894da4c2017-07-18 11:29:07 +02004793 if (s->req.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004794 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004795 else if (s->res.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004796 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004797 else {
4798 s->req.analysers = AN_REQ_FLT_END;
4799 s->res.analysers = AN_RES_FLT_END;
4800 txn->flags |= TX_WAIT_CLEANUP;
4801 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004802 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004803}
4804
Willy Tarreaud98cf932009-12-27 22:54:55 +01004805/* This function is an analyser which forwards request body (including chunk
4806 * sizes if any). It is called as soon as we must forward, even if we forward
4807 * zero byte. The only situation where it must not be called is when we're in
4808 * tunnel mode and we want to forward till the close. It's used both to forward
4809 * remaining data and to resync after end of body. It expects the msg_state to
4810 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004811 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004812 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004813 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004814 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004815int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004816{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004817 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004818 struct http_txn *txn = s->txn;
4819 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004820 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004821
Christopher Faulet814d2702017-03-30 11:33:44 +02004822 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
4823 now_ms, __FUNCTION__,
4824 s,
4825 req,
4826 req->rex, req->wex,
4827 req->flags,
4828 req->buf->i,
4829 req->analysers);
4830
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004831 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4832 return 0;
4833
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004834 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004835 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004836 /* Output closed while we were sending data. We must abort and
4837 * wake the other side up.
4838 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004839 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004840 msg->msg_state = HTTP_MSG_ERROR;
4841 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004842 return 1;
4843 }
4844
Willy Tarreaud98cf932009-12-27 22:54:55 +01004845 /* Note that we don't have to send 100-continue back because we don't
4846 * need the data to complete our job, and it's up to the server to
4847 * decide whether to return 100, 417 or anything else in return of
4848 * an "Expect: 100-continue" header.
4849 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004850 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004851 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4852 ? HTTP_MSG_CHUNK_SIZE
4853 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004854
4855 /* TODO/filters: when http-buffer-request option is set or if a
4856 * rule on url_param exists, the first chunk size could be
4857 * already parsed. In that case, msg->next is after the chunk
4858 * size (including the CRLF after the size). So this case should
4859 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004860 }
4861
Willy Tarreau7ba23542014-04-17 21:50:00 +02004862 /* Some post-connect processing might want us to refrain from starting to
4863 * forward data. Currently, the only reason for this is "balance url_param"
4864 * whichs need to parse/process the request after we've enabled forwarding.
4865 */
4866 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004867 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004868 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004869 req->flags |= CF_WAKE_CONNECT;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004870 goto missing_data_or_waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004871 }
4872 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4873 }
4874
Willy Tarreau80a92c02014-03-12 10:41:13 +01004875 /* in most states, we should abort in case of early close */
4876 channel_auto_close(req);
4877
Willy Tarreauefdf0942014-04-24 20:08:57 +02004878 if (req->to_forward) {
4879 /* We can't process the buffer's contents yet */
4880 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004881 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004882 }
4883
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004884 if (msg->msg_state < HTTP_MSG_DONE) {
4885 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4886 ? http_msg_forward_chunked_body(s, msg)
4887 : http_msg_forward_body(s, msg));
4888 if (!ret)
4889 goto missing_data_or_waiting;
4890 if (ret < 0)
4891 goto return_bad_req;
4892 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004893
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004894 /* other states, DONE...TUNNEL */
4895 /* we don't want to forward closes on DONE except in tunnel mode. */
4896 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4897 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004898
Christopher Faulet894da4c2017-07-18 11:29:07 +02004899 http_resync_states(s);
4900 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004901 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4902 if (req->flags & CF_SHUTW) {
4903 /* request errors are most likely due to the
4904 * server aborting the transfer. */
4905 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004906 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004907 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02004908 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004909 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004910 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004911 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004912 }
4913
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004914 /* If "option abortonclose" is set on the backend, we want to monitor
4915 * the client's connection and forward any shutdown notification to the
4916 * server, which will decide whether to close or to go on processing the
4917 * request. We only do that in tunnel mode, and not in other modes since
4918 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004919 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004920 channel_auto_read(req);
4921 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4922 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4923 s->si[1].flags |= SI_FL_NOLINGER;
4924 channel_auto_close(req);
4925 }
4926 else if (s->txn->meth == HTTP_METH_POST) {
4927 /* POST requests may require to read extra CRLF sent by broken
4928 * browsers and which could cause an RST to be sent upon close
4929 * on some systems (eg: Linux). */
4930 channel_auto_read(req);
4931 }
4932 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004933
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004934 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004935 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004936 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004937 if (!(s->flags & SF_ERR_MASK))
4938 s->flags |= SF_ERR_CLICL;
4939 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004940 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004941 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004942 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004943 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004944 }
4945
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004946 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4947 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004948 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004949 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004950
4951 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004952 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004953
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004954 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004955 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004956 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004957
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004958 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004959 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004960 * And when content-length is used, we never want to let the possible
4961 * shutdown be forwarded to the other side, as the state machine will
4962 * take care of it once the client responds. It's also important to
4963 * prevent TIME_WAITs from accumulating on the backend side, and for
4964 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004965 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004966 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004967 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004968
Willy Tarreau5c620922011-05-11 19:56:11 +02004969 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004970 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004971 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004972 * modes are already handled by the stream sock layer. We must not do
4973 * this in content-length mode because it could present the MSG_MORE
4974 * flag with the last block of forwarded data, which would cause an
4975 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004976 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004977 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004978 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004979
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004980 return 0;
4981
Willy Tarreaud98cf932009-12-27 22:54:55 +01004982 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004983 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004984 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004985 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02004986
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004987 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004988 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004989 txn->req.msg_state = HTTP_MSG_ERROR;
4990 if (txn->status) {
4991 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004992 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004993 } else {
4994 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004995 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004996 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004997 req->analysers &= AN_REQ_FLT_END;
4998 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 +01004999
Willy Tarreaue7dff022015-04-03 01:14:29 +02005000 if (!(s->flags & SF_ERR_MASK))
5001 s->flags |= SF_ERR_PRXCOND;
5002 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005003 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005004 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005005 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005006 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005007 }
5008 return 0;
5009
5010 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005011 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005012 txn->req.msg_state = HTTP_MSG_ERROR;
5013 if (txn->status) {
5014 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005015 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005016 } else {
5017 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005018 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005019 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005020 req->analysers &= AN_REQ_FLT_END;
5021 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 +01005022
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005023 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
5024 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005025 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005026 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005027
Willy Tarreaue7dff022015-04-03 01:14:29 +02005028 if (!(s->flags & SF_ERR_MASK))
5029 s->flags |= SF_ERR_SRVCL;
5030 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005031 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005032 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005033 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005034 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005035 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005036 return 0;
5037}
5038
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005039/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5040 * processing can continue on next analysers, or zero if it either needs more
5041 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005042 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005043 * when it has nothing left to do, and may remove any analyser when it wants to
5044 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005045 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005046int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005047{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005048 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005049 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005050 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005051 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005052 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005053 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005054 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005055
Willy Tarreau87b09662015-04-03 00:22:06 +02005056 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 +02005057 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005058 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005059 rep,
5060 rep->rex, rep->wex,
5061 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005062 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005063 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005064
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005065 /*
5066 * Now parse the partial (or complete) lines.
5067 * We will check the response syntax, and also join multi-line
5068 * headers. An index of all the lines will be elaborated while
5069 * parsing.
5070 *
5071 * For the parsing, we use a 28 states FSM.
5072 *
5073 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005074 * rep->buf->p = beginning of response
5075 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5076 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005077 * msg->eol = end of current header or line (LF or CRLF)
5078 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005079 */
5080
Willy Tarreau628c40c2014-04-24 19:11:26 +02005081 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005082 /* There's a protected area at the end of the buffer for rewriting
5083 * purposes. We don't want to start to parse the request if the
5084 * protected area is affected, because we may have to move processed
5085 * data later, which is much more complicated.
5086 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005087 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005088 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005089 /* some data has still not left the buffer, wake us once that's done */
5090 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5091 goto abort_response;
5092 channel_dont_close(rep);
5093 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005094 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005095 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005096 }
5097
Willy Tarreau379357a2013-06-08 12:55:46 +02005098 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5099 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5100 buffer_slow_realign(rep->buf);
5101
Willy Tarreau9b28e032012-10-12 23:49:43 +02005102 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005103 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005104 }
5105
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005106 /* 1: we might have to print this header in debug mode */
5107 if (unlikely((global.mode & MODE_DEBUG) &&
5108 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005109 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005110 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005111
Willy Tarreau9b28e032012-10-12 23:49:43 +02005112 sol = rep->buf->p;
5113 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005114 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005115
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005116 sol += hdr_idx_first_pos(&txn->hdr_idx);
5117 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005118
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005119 while (cur_idx) {
5120 eol = sol + txn->hdr_idx.v[cur_idx].len;
5121 debug_hdr("srvhdr", s, sol, eol);
5122 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5123 cur_idx = txn->hdr_idx.v[cur_idx].next;
5124 }
5125 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005126
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005127 /*
5128 * Now we quickly check if we have found a full valid response.
5129 * If not so, we check the FD and buffer states before leaving.
5130 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005131 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005132 * responses are checked first.
5133 *
5134 * Depending on whether the client is still there or not, we
5135 * may send an error response back or not. Note that normally
5136 * we should only check for HTTP status there, and check I/O
5137 * errors somewhere else.
5138 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005139
Willy Tarreau655dce92009-11-08 13:10:58 +01005140 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005141 /* Invalid response */
5142 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5143 /* we detected a parsing error. We want to archive this response
5144 * in the dedicated proxy area for later troubleshooting.
5145 */
5146 hdr_response_bad:
5147 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005148 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005149
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005150 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005151 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005152 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005153 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005154 }
Willy Tarreau64648412010-03-05 10:41:54 +01005155 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005156 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005157 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005158 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005159 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005160 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005161 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005162
Willy Tarreaue7dff022015-04-03 01:14:29 +02005163 if (!(s->flags & SF_ERR_MASK))
5164 s->flags |= SF_ERR_PRXCOND;
5165 if (!(s->flags & SF_FINST_MASK))
5166 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005167
5168 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005169 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005170
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005171 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005172 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005173 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005174 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005175 goto hdr_response_bad;
5176 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005177
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005178 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005179 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005180 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005181 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005182 else if (txn->flags & TX_NOT_FIRST)
5183 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005184
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005185 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005186 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005187 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005188 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005189 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005190
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005191 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005192 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005193 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01005194
5195 /* Check to see if the server refused the early data.
5196 * If so, just send a 425
5197 */
5198 if (objt_cs(s->si[1].end)) {
5199 struct connection *conn = objt_cs(s->si[1].end)->conn;
5200
5201 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
5202 txn->status = 425;
5203 }
5204
Willy Tarreau350f4872014-11-28 14:42:25 +01005205 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005206 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005207 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02005208
Willy Tarreaue7dff022015-04-03 01:14:29 +02005209 if (!(s->flags & SF_ERR_MASK))
5210 s->flags |= SF_ERR_SRVCL;
5211 if (!(s->flags & SF_FINST_MASK))
5212 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005213 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005214 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005215
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005216 /* read timeout : return a 504 to the client. */
5217 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005218 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005219 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005220
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005221 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005222 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005223 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005224 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005225 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005226
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005227 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005228 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005229 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005230 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005231 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005232 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02005233
Willy Tarreaue7dff022015-04-03 01:14:29 +02005234 if (!(s->flags & SF_ERR_MASK))
5235 s->flags |= SF_ERR_SRVTO;
5236 if (!(s->flags & SF_FINST_MASK))
5237 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005238 return 0;
5239 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005240
Willy Tarreauf003d372012-11-26 13:35:37 +01005241 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005242 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005243 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5244 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01005245 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005246 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01005247
Christopher Faulet0184ea72017-01-05 14:06:34 +01005248 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01005249 channel_auto_close(rep);
5250
5251 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005252 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005253 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01005254
Willy Tarreaue7dff022015-04-03 01:14:29 +02005255 if (!(s->flags & SF_ERR_MASK))
5256 s->flags |= SF_ERR_CLICL;
5257 if (!(s->flags & SF_FINST_MASK))
5258 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01005259
Willy Tarreau87b09662015-04-03 00:22:06 +02005260 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01005261 return 0;
5262 }
5263
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005264 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005265 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005266 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005267 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005268 else if (txn->flags & TX_NOT_FIRST)
5269 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005270
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005271 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005272 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005273 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005274 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005275 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005276
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005277 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005278 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005279 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005280 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005281 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005282 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005283
Willy Tarreaue7dff022015-04-03 01:14:29 +02005284 if (!(s->flags & SF_ERR_MASK))
5285 s->flags |= SF_ERR_SRVCL;
5286 if (!(s->flags & SF_FINST_MASK))
5287 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005288 return 0;
5289 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005290
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005291 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005292 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005293 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005294 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005295 else if (txn->flags & TX_NOT_FIRST)
5296 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005297
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005298 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005299 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005300 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005301
Willy Tarreaue7dff022015-04-03 01:14:29 +02005302 if (!(s->flags & SF_ERR_MASK))
5303 s->flags |= SF_ERR_CLICL;
5304 if (!(s->flags & SF_FINST_MASK))
5305 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005306
Willy Tarreau87b09662015-04-03 00:22:06 +02005307 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005308 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005309 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005310
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005311 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005312 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005313 return 0;
5314 }
5315
5316 /* More interesting part now : we know that we have a complete
5317 * response which at least looks like HTTP. We have an indicator
5318 * of each header's length, so we can parse them quickly.
5319 */
5320
5321 if (unlikely(msg->err_pos >= 0))
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005322 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005323
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005324 /*
5325 * 1: get the status code
5326 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005327 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005328 if (n < 1 || n > 5)
5329 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005330 /* when the client triggers a 4xx from the server, it's most often due
5331 * to a missing object or permission. These events should be tracked
5332 * because if they happen often, it may indicate a brute force or a
5333 * vulnerability scan.
5334 */
5335 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02005336 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02005337
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005338 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005339 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005340
Willy Tarreau91852eb2015-05-01 13:26:00 +02005341 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
5342 * exactly one digit "." one digit. This check may be disabled using
5343 * option accept-invalid-http-response.
5344 */
5345 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
5346 if (msg->sl.st.v_l != 8) {
5347 msg->err_pos = 0;
5348 goto hdr_response_bad;
5349 }
5350
5351 if (rep->buf->p[4] != '/' ||
5352 !isdigit((unsigned char)rep->buf->p[5]) ||
5353 rep->buf->p[6] != '.' ||
5354 !isdigit((unsigned char)rep->buf->p[7])) {
5355 msg->err_pos = 4;
5356 goto hdr_response_bad;
5357 }
5358 }
5359
Willy Tarreau5b154472009-12-21 20:11:07 +01005360 /* check if the response is HTTP/1.1 or above */
5361 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005362 ((rep->buf->p[5] > '1') ||
5363 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005364 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005365
5366 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005367 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 +01005368
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005369 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005370 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005371
Willy Tarreau9b28e032012-10-12 23:49:43 +02005372 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005373
Willy Tarreau39650402010-03-15 19:44:39 +01005374 /* Adjust server's health based on status code. Note: status codes 501
5375 * and 505 are triggered on demand by client request, so we must not
5376 * count them as server failures.
5377 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005378 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005379 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005380 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005381 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005382 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005383 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005384
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005385 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02005386 * We may be facing a 100-continue response, or any other informational
5387 * 1xx response which is non-final, in which case this is not the right
5388 * response, and we're waiting for the next one. Let's allow this response
5389 * to go to the client and wait for the next one. There's an exception for
5390 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005391 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02005392 if (txn->status < 200 &&
5393 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005394 hdr_idx_init(&txn->hdr_idx);
5395 msg->next -= channel_forward(rep, msg->next);
5396 msg->msg_state = HTTP_MSG_RPBEFORE;
5397 txn->status = 0;
5398 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01005399 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02005400 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02005401 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02005402
Willy Tarreaua14ad722017-07-07 11:36:32 +02005403 /*
5404 * 2: check for cacheability.
5405 */
5406
5407 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005408 case 200:
5409 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005410 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005411 case 206:
5412 case 300:
5413 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005414 case 404:
5415 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005416 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005417 case 414:
5418 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01005419 break;
5420 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005421 /* RFC7231#6.1:
5422 * Responses with status codes that are defined as
5423 * cacheable by default (e.g., 200, 203, 204, 206,
5424 * 300, 301, 404, 405, 410, 414, and 501 in this
5425 * specification) can be reused by a cache with
5426 * heuristic expiration unless otherwise indicated
5427 * by the method definition or explicit cache
5428 * controls [RFC7234]; all other status codes are
5429 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005430 */
Willy Tarreau83ece462017-12-21 15:13:09 +01005431 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005432 break;
5433 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005434
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005435 /*
5436 * 3: we may need to capture headers
5437 */
5438 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005439 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005440 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005441 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005442
Willy Tarreau557f1992015-05-01 10:05:17 +02005443 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
5444 * by RFC7230#3.3.3 :
5445 *
5446 * The length of a message body is determined by one of the following
5447 * (in order of precedence):
5448 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005449 * 1. Any 2xx (Successful) response to a CONNECT request implies that
5450 * the connection will become a tunnel immediately after the empty
5451 * line that concludes the header fields. A client MUST ignore
5452 * any Content-Length or Transfer-Encoding header fields received
5453 * in such a message. Any 101 response (Switching Protocols) is
5454 * managed in the same manner.
5455 *
5456 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02005457 * (Informational), 204 (No Content), or 304 (Not Modified) status
5458 * code is always terminated by the first empty line after the
5459 * header fields, regardless of the header fields present in the
5460 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005461 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005462 * 3. If a Transfer-Encoding header field is present and the chunked
5463 * transfer coding (Section 4.1) is the final encoding, the message
5464 * body length is determined by reading and decoding the chunked
5465 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005466 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005467 * If a Transfer-Encoding header field is present in a response and
5468 * the chunked transfer coding is not the final encoding, the
5469 * message body length is determined by reading the connection until
5470 * it is closed by the server. If a Transfer-Encoding header field
5471 * is present in a request and the chunked transfer coding is not
5472 * the final encoding, the message body length cannot be determined
5473 * reliably; the server MUST respond with the 400 (Bad Request)
5474 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005475 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005476 * If a message is received with both a Transfer-Encoding and a
5477 * Content-Length header field, the Transfer-Encoding overrides the
5478 * Content-Length. Such a message might indicate an attempt to
5479 * perform request smuggling (Section 9.5) or response splitting
5480 * (Section 9.4) and ought to be handled as an error. A sender MUST
5481 * remove the received Content-Length field prior to forwarding such
5482 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005483 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005484 * 4. If a message is received without Transfer-Encoding and with
5485 * either multiple Content-Length header fields having differing
5486 * field-values or a single Content-Length header field having an
5487 * invalid value, then the message framing is invalid and the
5488 * recipient MUST treat it as an unrecoverable error. If this is a
5489 * request message, the server MUST respond with a 400 (Bad Request)
5490 * status code and then close the connection. If this is a response
5491 * message received by a proxy, the proxy MUST close the connection
5492 * to the server, discard the received response, and send a 502 (Bad
5493 * Gateway) response to the client. If this is a response message
5494 * received by a user agent, the user agent MUST close the
5495 * connection to the server and discard the received response.
5496 *
5497 * 5. If a valid Content-Length header field is present without
5498 * Transfer-Encoding, its decimal value defines the expected message
5499 * body length in octets. If the sender closes the connection or
5500 * the recipient times out before the indicated number of octets are
5501 * received, the recipient MUST consider the message to be
5502 * incomplete and close the connection.
5503 *
5504 * 6. If this is a request message and none of the above are true, then
5505 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005506 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005507 * 7. Otherwise, this is a response message without a declared message
5508 * body length, so the message body length is determined by the
5509 * number of octets received prior to the server closing the
5510 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005511 */
5512
5513 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005514 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005515 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005516 * FIXME: should we parse anyway and return an error on chunked encoding ?
5517 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005518 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5519 txn->status == 101)) {
5520 /* Either we've established an explicit tunnel, or we're
5521 * switching the protocol. In both cases, we're very unlikely
5522 * to understand the next protocols. We have to switch to tunnel
5523 * mode, so that we transfer the request and responses then let
5524 * this protocol pass unmodified. When we later implement specific
5525 * parsers for such protocols, we'll want to check the Upgrade
5526 * header which contains information about that protocol for
5527 * responses with status 101 (eg: see RFC2817 about TLS).
5528 */
5529 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5530 msg->flags |= HTTP_MSGF_XFER_LEN;
5531 goto end;
5532 }
5533
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005534 if (txn->meth == HTTP_METH_HEAD ||
5535 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005536 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005537 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005538 goto skip_content_length;
5539 }
5540
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005541 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005542 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02005543 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005544 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005545 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5546 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005547 /* bad transfer-encoding (chunked followed by something else) */
5548 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005549 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005550 break;
5551 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005552 }
5553
Willy Tarreau1c913912015-04-30 10:57:51 +02005554 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005555 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005556 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02005557 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
5558 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5559 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005560 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005561 signed long long cl;
5562
Willy Tarreauad14f752011-09-02 20:33:27 +02005563 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005564 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005565 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005566 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005567
Willy Tarreauad14f752011-09-02 20:33:27 +02005568 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005569 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005570 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005571 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005572
Willy Tarreauad14f752011-09-02 20:33:27 +02005573 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005574 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005575 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005576 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005577
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005578 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005579 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005580 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005581 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005582
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005583 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005584 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005585 }
5586
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005587 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005588 /* Now we have to check if we need to modify the Connection header.
5589 * This is more difficult on the response than it is on the request,
5590 * because we can have two different HTTP versions and we don't know
5591 * how the client will interprete a response. For instance, let's say
5592 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5593 * HTTP/1.1 response without any header. Maybe it will bound itself to
5594 * HTTP/1.0 because it only knows about it, and will consider the lack
5595 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5596 * the lack of header as a keep-alive. Thus we will use two flags
5597 * indicating how a request MAY be understood by the client. In case
5598 * of multiple possibilities, we'll fix the header to be explicit. If
5599 * ambiguous cases such as both close and keepalive are seen, then we
5600 * will fall back to explicit close. Note that we won't take risks with
5601 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005602 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005603 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005604 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5605 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5606 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5607 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005608 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005609
Willy Tarreau70dffda2014-01-30 03:07:23 +01005610 /* this situation happens when combining pretend-keepalive with httpclose. */
5611 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005612 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005613 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01005614 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5615
Willy Tarreau60466522010-01-18 19:08:45 +01005616 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005617 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005618 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5619 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005620
Willy Tarreau60466522010-01-18 19:08:45 +01005621 /* now adjust header transformations depending on current state */
5622 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5623 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5624 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005625 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005626 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005627 }
Willy Tarreau60466522010-01-18 19:08:45 +01005628 else { /* SCL / KAL */
5629 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005630 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005631 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005632 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005633
Willy Tarreau60466522010-01-18 19:08:45 +01005634 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005635 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005636
Willy Tarreau60466522010-01-18 19:08:45 +01005637 /* Some keep-alive responses are converted to Server-close if
5638 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005639 */
Willy Tarreau60466522010-01-18 19:08:45 +01005640 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5641 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005642 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005643 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005644 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005645 }
5646
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005647 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005648 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005649 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005650
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005651 /* end of job, return OK */
5652 rep->analysers &= ~an_bit;
5653 rep->analyse_exp = TICK_ETERNITY;
5654 channel_auto_close(rep);
5655 return 1;
5656
5657 abort_keep_alive:
5658 /* A keep-alive request to the server failed on a network error.
5659 * The client is required to retry. We need to close without returning
5660 * any other information so that the client retries.
5661 */
5662 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005663 rep->analysers &= AN_RES_FLT_END;
5664 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005665 channel_auto_close(rep);
5666 s->logs.logwait = 0;
5667 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005668 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005669 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005670 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005671 return 0;
5672}
5673
5674/* This function performs all the processing enabled for the current response.
5675 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005676 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005677 * other functions. It works like process_request (see indications above).
5678 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005679int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005680{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005681 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005682 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005683 struct http_msg *msg = &txn->rsp;
5684 struct proxy *cur_proxy;
5685 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005686 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005687
Willy Tarreau87b09662015-04-03 00:22:06 +02005688 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 +02005689 now_ms, __FUNCTION__,
5690 s,
5691 rep,
5692 rep->rex, rep->wex,
5693 rep->flags,
5694 rep->buf->i,
5695 rep->analysers);
5696
5697 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5698 return 0;
5699
Willy Tarreau70730dd2014-04-24 18:06:27 +02005700 /* The stats applet needs to adjust the Connection header but we don't
5701 * apply any filter there.
5702 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005703 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5704 rep->analysers &= ~an_bit;
5705 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005706 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005707 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005708
Willy Tarreau58975672014-04-24 21:13:57 +02005709 /*
5710 * We will have to evaluate the filters.
5711 * As opposed to version 1.2, now they will be evaluated in the
5712 * filters order and not in the header order. This means that
5713 * each filter has to be validated among all headers.
5714 *
5715 * Filters are tried with ->be first, then with ->fe if it is
5716 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005717 *
5718 * Maybe we are in resume condiion. In this case I choose the
5719 * "struct proxy" which contains the rule list matching the resume
5720 * pointer. If none of theses "struct proxy" match, I initialise
5721 * the process with the first one.
5722 *
5723 * In fact, I check only correspondance betwwen the current list
5724 * pointer and the ->fe rule list. If it doesn't match, I initialize
5725 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005726 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005727 if (s->current_rule_list == &sess->fe->http_res_rules)
5728 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005729 else
5730 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005731 while (1) {
5732 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005733
Willy Tarreau58975672014-04-24 21:13:57 +02005734 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005735 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005736 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005737
Willy Tarreau51d861a2015-05-22 17:30:48 +02005738 if (ret == HTTP_RULE_RES_BADREQ)
5739 goto return_srv_prx_502;
5740
5741 if (ret == HTTP_RULE_RES_DONE) {
5742 rep->analysers &= ~an_bit;
5743 rep->analyse_exp = TICK_ETERNITY;
5744 return 1;
5745 }
5746 }
5747
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005748 /* we need to be called again. */
5749 if (ret == HTTP_RULE_RES_YIELD) {
5750 channel_dont_close(rep);
5751 return 0;
5752 }
5753
Willy Tarreau58975672014-04-24 21:13:57 +02005754 /* try headers filters */
5755 if (rule_set->rsp_exp != NULL) {
5756 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5757 return_bad_resp:
5758 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005759 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005760 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005761 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005762 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005763 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005764 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005765 txn->status = 502;
5766 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005767 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005768 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005769 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005770 if (!(s->flags & SF_ERR_MASK))
5771 s->flags |= SF_ERR_PRXCOND;
5772 if (!(s->flags & SF_FINST_MASK))
5773 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005774 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005775 }
Willy Tarreau58975672014-04-24 21:13:57 +02005776 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005777
Willy Tarreau58975672014-04-24 21:13:57 +02005778 /* has the response been denied ? */
5779 if (txn->flags & TX_SVDENY) {
5780 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005781 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005782
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005783 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5784 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005785 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005786 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005787
Willy Tarreau58975672014-04-24 21:13:57 +02005788 goto return_srv_prx_502;
5789 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005790
Willy Tarreau58975672014-04-24 21:13:57 +02005791 /* add response headers from the rule sets in the same order */
5792 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005793 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005794 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005795 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005796 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005797 ret = acl_pass(ret);
5798 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5799 ret = !ret;
5800 if (!ret)
5801 continue;
5802 }
5803 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
5804 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005805 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005806
Willy Tarreau58975672014-04-24 21:13:57 +02005807 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005808 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005809 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005810 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005811 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005812
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005813 /* After this point, this anayzer can't return yield, so we can
5814 * remove the bit corresponding to this analyzer from the list.
5815 *
5816 * Note that the intermediate returns and goto found previously
5817 * reset the analyzers.
5818 */
5819 rep->analysers &= ~an_bit;
5820 rep->analyse_exp = TICK_ETERNITY;
5821
Willy Tarreau58975672014-04-24 21:13:57 +02005822 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005823 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005824 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005825
Willy Tarreau58975672014-04-24 21:13:57 +02005826 /*
5827 * Now check for a server cookie.
5828 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005829 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005830 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005831
Willy Tarreau58975672014-04-24 21:13:57 +02005832 /*
5833 * Check for cache-control or pragma headers if required.
5834 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01005835 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02005836 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005837
Willy Tarreau58975672014-04-24 21:13:57 +02005838 /*
5839 * Add server cookie in the response if needed
5840 */
5841 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5842 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005843 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005844 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5845 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5846 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5847 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5848 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005849 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005850 /* the server is known, it's not the one the client requested, or the
5851 * cookie's last seen date needs to be refreshed. We have to
5852 * insert a set-cookie here, except if we want to insert only on POST
5853 * requests and this one isn't. Note that servers which don't have cookies
5854 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005855 */
Willy Tarreau58975672014-04-24 21:13:57 +02005856 if (!objt_server(s->target)->cookie) {
5857 chunk_printf(&trash,
5858 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5859 s->be->cookie_name);
5860 }
5861 else {
5862 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005863
Willy Tarreau58975672014-04-24 21:13:57 +02005864 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5865 /* emit last_date, which is mandatory */
5866 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5867 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5868 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005869
Willy Tarreau58975672014-04-24 21:13:57 +02005870 if (s->be->cookie_maxlife) {
5871 /* emit first_date, which is either the original one or
5872 * the current date.
5873 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005874 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005875 s30tob64(txn->cookie_first_date ?
5876 txn->cookie_first_date >> 2 :
5877 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005878 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005879 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005880 }
Willy Tarreau58975672014-04-24 21:13:57 +02005881 chunk_appendf(&trash, "; path=/");
5882 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005883
Willy Tarreau58975672014-04-24 21:13:57 +02005884 if (s->be->cookie_domain)
5885 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005886
Willy Tarreau58975672014-04-24 21:13:57 +02005887 if (s->be->ck_opts & PR_CK_HTTPONLY)
5888 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005889
Willy Tarreau58975672014-04-24 21:13:57 +02005890 if (s->be->ck_opts & PR_CK_SECURE)
5891 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005892
Willy Tarreau58975672014-04-24 21:13:57 +02005893 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
5894 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005895
Willy Tarreau58975672014-04-24 21:13:57 +02005896 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005897 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005898 /* the server did not change, only the date was updated */
5899 txn->flags |= TX_SCK_UPDATED;
5900 else
5901 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005902
Willy Tarreau58975672014-04-24 21:13:57 +02005903 /* Here, we will tell an eventual cache on the client side that we don't
5904 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5905 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5906 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005907 */
Willy Tarreau58975672014-04-24 21:13:57 +02005908 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005909
Willy Tarreau58975672014-04-24 21:13:57 +02005910 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005911
Willy Tarreau58975672014-04-24 21:13:57 +02005912 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5913 "Cache-control: private", 22) < 0))
5914 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005915 }
Willy Tarreau58975672014-04-24 21:13:57 +02005916 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005917
Willy Tarreau58975672014-04-24 21:13:57 +02005918 /*
5919 * Check if result will be cacheable with a cookie.
5920 * We'll block the response if security checks have caught
5921 * nasty things such as a cacheable cookie.
5922 */
5923 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5924 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5925 (s->be->options & PR_O_CHK_CACHE)) {
5926 /* we're in presence of a cacheable response containing
5927 * a set-cookie header. We'll block it as requested by
5928 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005929 */
Willy Tarreau58975672014-04-24 21:13:57 +02005930 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005931 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005932
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005933 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5934 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005935 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005936 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005937
Christopher Faulet767a84b2017-11-24 16:50:31 +01005938 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5939 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005940 send_log(s->be, LOG_ALERT,
5941 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5942 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5943 goto return_srv_prx_502;
5944 }
Willy Tarreau03945942009-12-22 16:50:27 +01005945
Willy Tarreau70730dd2014-04-24 18:06:27 +02005946 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005947 /*
5948 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5949 * If an "Upgrade" token is found, the header is left untouched in order
5950 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005951 * if anything but "Upgrade" is present in the Connection header. We don't
5952 * want to touch any 101 response either since it's switching to another
5953 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005954 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005955 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02005956 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005957 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02005958 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
5959 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005960
Willy Tarreau58975672014-04-24 21:13:57 +02005961 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5962 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5963 /* we want a keep-alive response here. Keep-alive header
5964 * required if either side is not 1.1.
5965 */
5966 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5967 want_flags |= TX_CON_KAL_SET;
5968 }
5969 else {
5970 /* we want a close response here. Close header required if
5971 * the server is 1.1, regardless of the client.
5972 */
5973 if (msg->flags & HTTP_MSGF_VER_11)
5974 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005975 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005976
Willy Tarreau58975672014-04-24 21:13:57 +02005977 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5978 http_change_connection_header(txn, msg, want_flags);
5979 }
5980
5981 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005982 /* Always enter in the body analyzer */
5983 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5984 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005985
Willy Tarreau58975672014-04-24 21:13:57 +02005986 /* if the user wants to log as soon as possible, without counting
5987 * bytes from the server, then this is the right moment. We have
5988 * to temporarily assign bytes_out to log what we currently have.
5989 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005990 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005991 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5992 s->logs.bytes_out = txn->rsp.eoh;
5993 s->do_log(s);
5994 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005995 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005996 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005997}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005998
Willy Tarreaud98cf932009-12-27 22:54:55 +01005999/* This function is an analyser which forwards response body (including chunk
6000 * sizes if any). It is called as soon as we must forward, even if we forward
6001 * zero byte. The only situation where it must not be called is when we're in
6002 * tunnel mode and we want to forward till the close. It's used both to forward
6003 * remaining data and to resync after end of body. It expects the msg_state to
6004 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006005 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006006 *
6007 * It is capable of compressing response data both in content-length mode and
6008 * in chunked mode. The state machines follows different flows depending on
6009 * whether content-length and chunked modes are used, since there are no
6010 * trailers in content-length :
6011 *
6012 * chk-mode cl-mode
6013 * ,----- BODY -----.
6014 * / \
6015 * V size > 0 V chk-mode
6016 * .--> SIZE -------------> DATA -------------> CRLF
6017 * | | size == 0 | last byte |
6018 * | v final crlf v inspected |
6019 * | TRAILERS -----------> DONE |
6020 * | |
6021 * `----------------------------------------------'
6022 *
6023 * Compression only happens in the DATA state, and must be flushed in final
6024 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6025 * is performed at once on final states for all bytes parsed, or when leaving
6026 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006027 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006028int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006029{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006030 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006031 struct http_txn *txn = s->txn;
6032 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01006033 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006034
Christopher Faulet814d2702017-03-30 11:33:44 +02006035 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6036 now_ms, __FUNCTION__,
6037 s,
6038 res,
6039 res->rex, res->wex,
6040 res->flags,
6041 res->buf->i,
6042 res->analysers);
6043
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006044 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6045 return 0;
6046
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006047 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006048 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006049 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006050 /* Output closed while we were sending data. We must abort and
6051 * wake the other side up.
6052 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006053 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006054 msg->msg_state = HTTP_MSG_ERROR;
6055 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006056 return 1;
6057 }
6058
Willy Tarreau4fe41902010-06-07 22:27:41 +02006059 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006060 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006061
Christopher Fauletd7c91962015-04-30 11:48:27 +02006062 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006063 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
6064 ? HTTP_MSG_CHUNK_SIZE
6065 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006066 }
6067
Willy Tarreauefdf0942014-04-24 20:08:57 +02006068 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006069 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02006070 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006071 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006072 }
6073
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006074 if (msg->msg_state < HTTP_MSG_DONE) {
6075 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6076 ? http_msg_forward_chunked_body(s, msg)
6077 : http_msg_forward_body(s, msg));
6078 if (!ret)
6079 goto missing_data_or_waiting;
6080 if (ret < 0)
6081 goto return_bad_res;
6082 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006083
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006084 /* other states, DONE...TUNNEL */
6085 /* for keep-alive we don't want to forward closes on DONE */
6086 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6087 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6088 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006089
Christopher Faulet894da4c2017-07-18 11:29:07 +02006090 http_resync_states(s);
6091 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006092 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6093 if (res->flags & CF_SHUTW) {
6094 /* response errors are most likely due to the
6095 * client aborting the transfer. */
6096 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006097 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006098 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006099 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 +01006100 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006101 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006102 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006103 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006104 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006105
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006106 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006107 if (res->flags & CF_SHUTW)
6108 goto aborted_xfer;
6109
6110 /* stop waiting for data if the input is closed before the end. If the
6111 * client side was already closed, it means that the client has aborted,
6112 * so we don't want to count this as a server abort. Otherwise it's a
6113 * server abort.
6114 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02006115 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006116 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006117 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006118 /* If we have some pending data, we continue the processing */
6119 if (!buffer_pending(res->buf)) {
6120 if (!(s->flags & SF_ERR_MASK))
6121 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006122 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006123 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006124 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006125 goto return_bad_res_stats_ok;
6126 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006127 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006128
Willy Tarreau40dba092010-03-04 18:14:51 +01006129 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006130 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006131 goto return_bad_res;
6132
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006133 /* When TE: chunked is used, we need to get there again to parse
6134 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02006135 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
6136 * or if there are filters registered on the stream, we don't want to
6137 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006138 */
Christopher Faulet69744d92017-03-30 10:54:35 +02006139 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006140 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006141 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6142 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006143 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006144
Willy Tarreau5c620922011-05-11 19:56:11 +02006145 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006146 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006147 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006148 * modes are already handled by the stream sock layer. We must not do
6149 * this in content-length mode because it could present the MSG_MORE
6150 * flag with the last block of forwarded data, which would cause an
6151 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006152 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006153 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006154 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006155
Willy Tarreau87b09662015-04-03 00:22:06 +02006156 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006157 return 0;
6158
Willy Tarreau40dba092010-03-04 18:14:51 +01006159 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006160 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006161 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006162 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006163
6164 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006165 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006166 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006167 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006168 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006169 res->analysers &= AN_RES_FLT_END;
6170 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 +01006171 if (objt_server(s->target))
6172 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006173
Willy Tarreaue7dff022015-04-03 01:14:29 +02006174 if (!(s->flags & SF_ERR_MASK))
6175 s->flags |= SF_ERR_PRXCOND;
6176 if (!(s->flags & SF_FINST_MASK))
6177 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006178 return 0;
6179
6180 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006181 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006182 txn->rsp.msg_state = HTTP_MSG_ERROR;
6183 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006184 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006185 res->analysers &= AN_RES_FLT_END;
6186 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 +01006187
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006188 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
6189 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006190 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006191 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006192
Willy Tarreaue7dff022015-04-03 01:14:29 +02006193 if (!(s->flags & SF_ERR_MASK))
6194 s->flags |= SF_ERR_CLICL;
6195 if (!(s->flags & SF_FINST_MASK))
6196 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006197 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006198}
6199
6200
6201static inline int
6202http_msg_forward_body(struct stream *s, struct http_msg *msg)
6203{
6204 struct channel *chn = msg->chn;
6205 int ret;
6206
6207 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
6208
6209 if (msg->msg_state == HTTP_MSG_ENDING)
6210 goto ending;
6211
6212 /* Neither content-length, nor transfer-encoding was found, so we must
6213 * read the body until the server connection is closed. In that case, we
6214 * eat data as they come. Of course, this happens for response only. */
6215 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6216 unsigned long long len = (chn->buf->i - msg->next);
6217 msg->chunk_len += len;
6218 msg->body_len += len;
6219 }
Christopher Fauletda02e172015-12-04 09:25:05 +01006220 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6221 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6222 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006223 msg->next += ret;
6224 msg->chunk_len -= ret;
6225 if (msg->chunk_len) {
6226 /* input empty or output full */
6227 if (chn->buf->i > msg->next)
6228 chn->flags |= CF_WAKE_WRITE;
6229 goto missing_data_or_waiting;
6230 }
6231
Christopher Faulet1486b0a2017-07-18 11:42:08 +02006232 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
6233 * always set for a request. */
6234 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6235 /* The server still sending data that should be filtered */
6236 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
6237 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01006238 msg->msg_state = HTTP_MSG_TUNNEL;
6239 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02006240 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006241
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006242 msg->msg_state = HTTP_MSG_ENDING;
6243
6244 ending:
6245 /* we may have some pending data starting at res->buf->p such as a last
6246 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006247 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6248 /* default_ret */ msg->next,
6249 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006250 b_adv(chn->buf, ret);
6251 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006252 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6253 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006254 if (msg->next)
6255 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006256
Christopher Fauletda02e172015-12-04 09:25:05 +01006257 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
6258 /* default_ret */ 1,
6259 /* on_error */ goto error,
6260 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01006261 if (msg->msg_state == HTTP_MSG_ENDING)
6262 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006263 return 1;
6264
6265 missing_data_or_waiting:
6266 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006267 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6268 /* default_ret */ msg->next,
6269 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006270 b_adv(chn->buf, ret);
6271 msg->next -= ret;
6272 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6273 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006274 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006275 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006276 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006277 return 0;
6278 error:
6279 return -1;
6280}
6281
6282static inline int
6283http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
6284{
6285 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006286 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006287 int ret;
6288
6289 /* Here we have the guarantee to be in one of the following state:
6290 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
6291 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
6292
6293 switch_states:
6294 switch (msg->msg_state) {
6295 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01006296 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6297 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6298 /* on_error */ goto error);
6299 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006300 msg->chunk_len -= ret;
6301 if (msg->chunk_len) {
6302 /* input empty or output full */
6303 if (chn->buf->i > msg->next)
6304 chn->flags |= CF_WAKE_WRITE;
6305 goto missing_data_or_waiting;
6306 }
6307
6308 /* nothing left to forward for this chunk*/
6309 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
6310 /* fall through for HTTP_MSG_CHUNK_CRLF */
6311
6312 case HTTP_MSG_CHUNK_CRLF:
6313 /* we want the CRLF after the data */
Willy Tarreaub2892562017-09-21 11:33:54 +02006314 ret = h1_skip_chunk_crlf(chn->buf, msg->next, chn->buf->i);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006315 if (ret == 0)
6316 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02006317 if (ret < 0) {
6318 msg->err_pos = chn->buf->i + ret;
6319 if (msg->err_pos < 0)
6320 msg->err_pos += chn->buf->size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006321 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02006322 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01006323 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006324 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6325 /* fall through for HTTP_MSG_CHUNK_SIZE */
6326
6327 case HTTP_MSG_CHUNK_SIZE:
6328 /* read the chunk size and assign it to ->chunk_len,
6329 * then set ->next to point to the body and switch to
6330 * DATA or TRAILERS state.
6331 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006332 ret = h1_parse_chunk_size(chn->buf, msg->next, chn->buf->i, &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006333 if (ret == 0)
6334 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006335 if (ret < 0) {
6336 msg->err_pos = chn->buf->i + ret;
6337 if (msg->err_pos < 0)
6338 msg->err_pos += chn->buf->size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006339 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006340 }
6341
6342 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01006343 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006344 msg->chunk_len = chunk;
6345 msg->body_len += chunk;
6346
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006347 if (msg->chunk_len) {
6348 msg->msg_state = HTTP_MSG_DATA;
6349 goto switch_states;
6350 }
6351 msg->msg_state = HTTP_MSG_TRAILERS;
6352 /* fall through for HTTP_MSG_TRAILERS */
6353
6354 case HTTP_MSG_TRAILERS:
6355 ret = http_forward_trailers(msg);
6356 if (ret < 0)
6357 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01006358 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
6359 /* default_ret */ 1,
6360 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006361 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006362 if (!ret)
6363 goto missing_data_or_waiting;
6364 break;
6365
6366 case HTTP_MSG_ENDING:
6367 goto ending;
6368
6369 default:
6370 /* This should no happen in this function */
6371 goto error;
6372 }
6373
6374 msg->msg_state = HTTP_MSG_ENDING;
6375 ending:
6376 /* we may have some pending data starting at res->buf->p such as a last
6377 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006378 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006379 /* default_ret */ msg->next,
6380 /* on_error */ goto error);
6381 b_adv(chn->buf, ret);
6382 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006383 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6384 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006385 if (msg->next)
6386 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006387
Christopher Fauletda02e172015-12-04 09:25:05 +01006388 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006389 /* default_ret */ 1,
6390 /* on_error */ goto error,
6391 /* on_wait */ goto waiting);
6392 msg->msg_state = HTTP_MSG_DONE;
6393 return 1;
6394
6395 missing_data_or_waiting:
6396 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006397 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006398 /* default_ret */ msg->next,
6399 /* on_error */ goto error);
6400 b_adv(chn->buf, ret);
6401 msg->next -= ret;
6402 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6403 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006404 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006405 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006406 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006407 return 0;
6408
6409 chunk_parsing_error:
6410 if (msg->err_pos >= 0) {
6411 if (chn->flags & CF_ISRESP)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006412 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006413 msg->msg_state, strm_fe(s));
6414 else
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006415 http_capture_bad_message(strm_fe(s), &strm_fe(s)->invalid_req, s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006416 msg, msg->msg_state, s->be);
6417 }
6418 error:
6419 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006420}
6421
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006422
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006423/* Iterate the same filter through all request headers.
6424 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006425 * Since it can manage the switch to another backend, it updates the per-proxy
6426 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006427 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006428int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006429{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006430 char *cur_ptr, *cur_end, *cur_next;
6431 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006432 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006433 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006434 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006435
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006436 last_hdr = 0;
6437
Willy Tarreau9b28e032012-10-12 23:49:43 +02006438 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006439 old_idx = 0;
6440
6441 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006442 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006443 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006444 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006445 (exp->action == ACT_ALLOW ||
6446 exp->action == ACT_DENY ||
6447 exp->action == ACT_TARPIT))
6448 return 0;
6449
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006450 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006451 if (!cur_idx)
6452 break;
6453
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006454 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006455 cur_ptr = cur_next;
6456 cur_end = cur_ptr + cur_hdr->len;
6457 cur_next = cur_end + cur_hdr->cr + 1;
6458
6459 /* Now we have one header between cur_ptr and cur_end,
6460 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006461 */
6462
Willy Tarreau15a53a42015-01-21 13:39:42 +01006463 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006464 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006465 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006466 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006467 last_hdr = 1;
6468 break;
6469
6470 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006471 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006472 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006473 break;
6474
6475 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006476 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006477 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006478 break;
6479
6480 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006481 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6482 if (trash.len < 0)
6483 return -1;
6484
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006485 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006486 /* FIXME: if the user adds a newline in the replacement, the
6487 * index will not be recalculated for now, and the new line
6488 * will not be counted as a new header.
6489 */
6490
6491 cur_end += delta;
6492 cur_next += delta;
6493 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006494 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006495 break;
6496
6497 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006498 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006499 cur_next += delta;
6500
Willy Tarreaufa355d42009-11-29 18:12:29 +01006501 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006502 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6503 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006504 cur_hdr->len = 0;
6505 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006506 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006507 break;
6508
6509 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006510 }
6511
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006512 /* keep the link from this header to next one in case of later
6513 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006514 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006515 old_idx = cur_idx;
6516 }
6517 return 0;
6518}
6519
6520
6521/* Apply the filter to the request line.
6522 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6523 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006524 * Since it can manage the switch to another backend, it updates the per-proxy
6525 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006526 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006527int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006528{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006529 char *cur_ptr, *cur_end;
6530 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006531 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006532 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006533
Willy Tarreau3d300592007-03-18 18:34:41 +01006534 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006535 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006536 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006537 (exp->action == ACT_ALLOW ||
6538 exp->action == ACT_DENY ||
6539 exp->action == ACT_TARPIT))
6540 return 0;
6541 else if (exp->action == ACT_REMOVE)
6542 return 0;
6543
6544 done = 0;
6545
Willy Tarreau9b28e032012-10-12 23:49:43 +02006546 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006547 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006548
6549 /* Now we have the request line between cur_ptr and cur_end */
6550
Willy Tarreau15a53a42015-01-21 13:39:42 +01006551 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006552 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006553 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006554 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006555 done = 1;
6556 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006557
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006558 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006559 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006560 done = 1;
6561 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006562
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006563 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006564 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006565 done = 1;
6566 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006567
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006568 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006569 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6570 if (trash.len < 0)
6571 return -1;
6572
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006573 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006574 /* FIXME: if the user adds a newline in the replacement, the
6575 * index will not be recalculated for now, and the new line
6576 * will not be counted as a new header.
6577 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006578
Willy Tarreaufa355d42009-11-29 18:12:29 +01006579 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006580 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006581 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006582 HTTP_MSG_RQMETH,
6583 cur_ptr, cur_end + 1,
6584 NULL, NULL);
6585 if (unlikely(!cur_end))
6586 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006587
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006588 /* we have a full request and we know that we have either a CR
6589 * or an LF at <ptr>.
6590 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006591 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6592 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006593 /* there is no point trying this regex on headers */
6594 return 1;
6595 }
6596 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006597 return done;
6598}
Willy Tarreau97de6242006-12-27 17:18:38 +01006599
Willy Tarreau58f10d72006-12-04 02:26:12 +01006600
Willy Tarreau58f10d72006-12-04 02:26:12 +01006601
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006602/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006603 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006604 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006605 * unparsable request. Since it can manage the switch to another backend, it
6606 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006607 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006608int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006609{
Willy Tarreau192252e2015-04-04 01:47:55 +02006610 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006611 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006612 struct hdr_exp *exp;
6613
6614 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006615 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006616
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006617 /*
6618 * The interleaving of transformations and verdicts
6619 * makes it difficult to decide to continue or stop
6620 * the evaluation.
6621 */
6622
Willy Tarreau6c123b12010-01-28 20:22:06 +01006623 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6624 break;
6625
Willy Tarreau3d300592007-03-18 18:34:41 +01006626 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006627 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006628 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006629 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006630
6631 /* if this filter had a condition, evaluate it now and skip to
6632 * next filter if the condition does not match.
6633 */
6634 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006635 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006636 ret = acl_pass(ret);
6637 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6638 ret = !ret;
6639
6640 if (!ret)
6641 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006642 }
6643
6644 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006645 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006646 if (unlikely(ret < 0))
6647 return -1;
6648
6649 if (likely(ret == 0)) {
6650 /* The filter did not match the request, it can be
6651 * iterated through all headers.
6652 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006653 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6654 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006655 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006656 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006657 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006658}
6659
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006660
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006661/* Find the end of a cookie value contained between <s> and <e>. It works the
6662 * same way as with headers above except that the semi-colon also ends a token.
6663 * See RFC2965 for more information. Note that it requires a valid header to
6664 * return a valid result.
6665 */
6666char *find_cookie_value_end(char *s, const char *e)
6667{
6668 int quoted, qdpair;
6669
6670 quoted = qdpair = 0;
6671 for (; s < e; s++) {
6672 if (qdpair) qdpair = 0;
6673 else if (quoted) {
6674 if (*s == '\\') qdpair = 1;
6675 else if (*s == '"') quoted = 0;
6676 }
6677 else if (*s == '"') quoted = 1;
6678 else if (*s == ',' || *s == ';') return s;
6679 }
6680 return s;
6681}
6682
6683/* Delete a value in a header between delimiters <from> and <next> in buffer
6684 * <buf>. The number of characters displaced is returned, and the pointer to
6685 * the first delimiter is updated if required. The function tries as much as
6686 * possible to respect the following principles :
6687 * - replace <from> delimiter by the <next> one unless <from> points to a
6688 * colon, in which case <next> is simply removed
6689 * - set exactly one space character after the new first delimiter, unless
6690 * there are not enough characters in the block being moved to do so.
6691 * - remove unneeded spaces before the previous delimiter and after the new
6692 * one.
6693 *
6694 * It is the caller's responsibility to ensure that :
6695 * - <from> points to a valid delimiter or the colon ;
6696 * - <next> points to a valid delimiter or the final CR/LF ;
6697 * - there are non-space chars before <from> ;
6698 * - there is a CR/LF at or after <next>.
6699 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006700int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006701{
6702 char *prev = *from;
6703
6704 if (*prev == ':') {
6705 /* We're removing the first value, preserve the colon and add a
6706 * space if possible.
6707 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006708 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006709 next++;
6710 prev++;
6711 if (prev < next)
6712 *prev++ = ' ';
6713
Willy Tarreau2235b262016-11-05 15:50:20 +01006714 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006715 next++;
6716 } else {
6717 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006718 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006719 prev--;
6720 *from = prev;
6721
6722 /* copy the delimiter and if possible a space if we're
6723 * not at the end of the line.
6724 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006725 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006726 *prev++ = *next++;
6727 if (prev + 1 < next)
6728 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006729 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006730 next++;
6731 }
6732 }
6733 return buffer_replace2(buf, prev, next, NULL, 0);
6734}
6735
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006736/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006737 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006738 * desirable to call it only when needed. This code is quite complex because
6739 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6740 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006741 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006742void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006743{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006744 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006745 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006746 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006747 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006748 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6749 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006750
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006751 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006752 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006753 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006754
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006755 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006756 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006757 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006758
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006759 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006760 hdr_beg = hdr_next;
6761 hdr_end = hdr_beg + cur_hdr->len;
6762 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006763
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006764 /* We have one full header between hdr_beg and hdr_end, and the
6765 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006766 * "Cookie:" headers.
6767 */
6768
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006769 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006770 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006771 old_idx = cur_idx;
6772 continue;
6773 }
6774
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006775 del_from = NULL; /* nothing to be deleted */
6776 preserve_hdr = 0; /* assume we may kill the whole header */
6777
Willy Tarreau58f10d72006-12-04 02:26:12 +01006778 /* Now look for cookies. Conforming to RFC2109, we have to support
6779 * attributes whose name begin with a '$', and associate them with
6780 * the right cookie, if we want to delete this cookie.
6781 * So there are 3 cases for each cookie read :
6782 * 1) it's a special attribute, beginning with a '$' : ignore it.
6783 * 2) it's a server id cookie that we *MAY* want to delete : save
6784 * some pointers on it (last semi-colon, beginning of cookie...)
6785 * 3) it's an application cookie : we *MAY* have to delete a previous
6786 * "special" cookie.
6787 * At the end of loop, if a "special" cookie remains, we may have to
6788 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006789 * *MUST* delete it.
6790 *
6791 * Note: RFC2965 is unclear about the processing of spaces around
6792 * the equal sign in the ATTR=VALUE form. A careful inspection of
6793 * the RFC explicitly allows spaces before it, and not within the
6794 * tokens (attrs or values). An inspection of RFC2109 allows that
6795 * too but section 10.1.3 lets one think that spaces may be allowed
6796 * after the equal sign too, resulting in some (rare) buggy
6797 * implementations trying to do that. So let's do what servers do.
6798 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6799 * allowed quoted strings in values, with any possible character
6800 * after a backslash, including control chars and delimitors, which
6801 * causes parsing to become ambiguous. Browsers also allow spaces
6802 * within values even without quotes.
6803 *
6804 * We have to keep multiple pointers in order to support cookie
6805 * removal at the beginning, middle or end of header without
6806 * corrupting the header. All of these headers are valid :
6807 *
6808 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6809 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6810 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6811 * | | | | | | | | |
6812 * | | | | | | | | hdr_end <--+
6813 * | | | | | | | +--> next
6814 * | | | | | | +----> val_end
6815 * | | | | | +-----------> val_beg
6816 * | | | | +--------------> equal
6817 * | | | +----------------> att_end
6818 * | | +---------------------> att_beg
6819 * | +--------------------------> prev
6820 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006821 */
6822
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006823 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6824 /* Iterate through all cookies on this line */
6825
6826 /* find att_beg */
6827 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006828 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006829 att_beg++;
6830
6831 /* find att_end : this is the first character after the last non
6832 * space before the equal. It may be equal to hdr_end.
6833 */
6834 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006835
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006836 while (equal < hdr_end) {
6837 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006838 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006839 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006840 continue;
6841 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006842 }
6843
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006844 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6845 * is between <att_beg> and <equal>, both may be identical.
6846 */
6847
6848 /* look for end of cookie if there is an equal sign */
6849 if (equal < hdr_end && *equal == '=') {
6850 /* look for the beginning of the value */
6851 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006852 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006853 val_beg++;
6854
6855 /* find the end of the value, respecting quotes */
6856 next = find_cookie_value_end(val_beg, hdr_end);
6857
6858 /* make val_end point to the first white space or delimitor after the value */
6859 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006860 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006861 val_end--;
6862 } else {
6863 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006864 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006865
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006866 /* We have nothing to do with attributes beginning with '$'. However,
6867 * they will automatically be removed if a header before them is removed,
6868 * since they're supposed to be linked together.
6869 */
6870 if (*att_beg == '$')
6871 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006872
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006873 /* Ignore cookies with no equal sign */
6874 if (equal == next) {
6875 /* This is not our cookie, so we must preserve it. But if we already
6876 * scheduled another cookie for removal, we cannot remove the
6877 * complete header, but we can remove the previous block itself.
6878 */
6879 preserve_hdr = 1;
6880 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006881 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006882 val_end += delta;
6883 next += delta;
6884 hdr_end += delta;
6885 hdr_next += delta;
6886 cur_hdr->len += delta;
6887 http_msg_move_end(&txn->req, delta);
6888 prev = del_from;
6889 del_from = NULL;
6890 }
6891 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006892 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006893
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006894 /* if there are spaces around the equal sign, we need to
6895 * strip them otherwise we'll get trouble for cookie captures,
6896 * or even for rewrites. Since this happens extremely rarely,
6897 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006898 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006899 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6900 int stripped_before = 0;
6901 int stripped_after = 0;
6902
6903 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006904 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006905 equal += stripped_before;
6906 val_beg += stripped_before;
6907 }
6908
6909 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006910 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006911 val_beg += stripped_after;
6912 stripped_before += stripped_after;
6913 }
6914
6915 val_end += stripped_before;
6916 next += stripped_before;
6917 hdr_end += stripped_before;
6918 hdr_next += stripped_before;
6919 cur_hdr->len += stripped_before;
6920 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006921 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006922 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006923
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006924 /* First, let's see if we want to capture this cookie. We check
6925 * that we don't already have a client side cookie, because we
6926 * can only capture one. Also as an optimisation, we ignore
6927 * cookies shorter than the declared name.
6928 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006929 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6930 (val_end - att_beg >= sess->fe->capture_namelen) &&
6931 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006932 int log_len = val_end - att_beg;
6933
Willy Tarreaubafbe012017-11-24 17:34:44 +01006934 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006935 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006936 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006937 if (log_len > sess->fe->capture_len)
6938 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006939 memcpy(txn->cli_cookie, att_beg, log_len);
6940 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006941 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006942 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006943
Willy Tarreaubca99692010-10-06 19:25:55 +02006944 /* Persistence cookies in passive, rewrite or insert mode have the
6945 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006946 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006947 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006948 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006949 * For cookies in prefix mode, the form is :
6950 *
6951 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006952 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006953 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6954 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6955 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006956 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006957
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006958 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6959 * have the server ID between val_beg and delim, and the original cookie between
6960 * delim+1 and val_end. Otherwise, delim==val_end :
6961 *
6962 * Cookie: NAME=SRV; # in all but prefix modes
6963 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6964 * | || || | |+-> next
6965 * | || || | +--> val_end
6966 * | || || +---------> delim
6967 * | || |+------------> val_beg
6968 * | || +-------------> att_end = equal
6969 * | |+-----------------> att_beg
6970 * | +------------------> prev
6971 * +-------------------------> hdr_beg
6972 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006973
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006974 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006975 for (delim = val_beg; delim < val_end; delim++)
6976 if (*delim == COOKIE_DELIM)
6977 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006978 } else {
6979 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006980 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006981 /* Now check if the cookie contains a date field, which would
6982 * appear after a vertical bar ('|') just after the server name
6983 * and before the delimiter.
6984 */
6985 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6986 if (vbar1) {
6987 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006988 * right is the last seen date. It is a base64 encoded
6989 * 30-bit value representing the UNIX date since the
6990 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006991 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006992 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006993 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006994 if (val_end - vbar1 >= 5) {
6995 val = b64tos30(vbar1);
6996 if (val > 0)
6997 txn->cookie_last_date = val << 2;
6998 }
6999 /* look for a second vertical bar */
7000 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7001 if (vbar1 && (val_end - vbar1 > 5)) {
7002 val = b64tos30(vbar1 + 1);
7003 if (val > 0)
7004 txn->cookie_first_date = val << 2;
7005 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007006 }
7007 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007008
Willy Tarreauf64d1412010-10-07 20:06:11 +02007009 /* if the cookie has an expiration date and the proxy wants to check
7010 * it, then we do that now. We first check if the cookie is too old,
7011 * then only if it has expired. We detect strict overflow because the
7012 * time resolution here is not great (4 seconds). Cookies with dates
7013 * in the future are ignored if their offset is beyond one day. This
7014 * allows an admin to fix timezone issues without expiring everyone
7015 * and at the same time avoids keeping unwanted side effects for too
7016 * long.
7017 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007018 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7019 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007020 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007021 txn->flags &= ~TX_CK_MASK;
7022 txn->flags |= TX_CK_OLD;
7023 delim = val_beg; // let's pretend we have not found the cookie
7024 txn->cookie_first_date = 0;
7025 txn->cookie_last_date = 0;
7026 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007027 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7028 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007029 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007030 txn->flags &= ~TX_CK_MASK;
7031 txn->flags |= TX_CK_EXPIRED;
7032 delim = val_beg; // let's pretend we have not found the cookie
7033 txn->cookie_first_date = 0;
7034 txn->cookie_last_date = 0;
7035 }
7036
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007037 /* Here, we'll look for the first running server which supports the cookie.
7038 * This allows to share a same cookie between several servers, for example
7039 * to dedicate backup servers to specific servers only.
7040 * However, to prevent clients from sticking to cookie-less backup server
7041 * when they have incidentely learned an empty cookie, we simply ignore
7042 * empty cookies and mark them as invalid.
7043 * The same behaviour is applied when persistence must be ignored.
7044 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007045 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007046 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007047
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007048 while (srv) {
7049 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7050 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02007051 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007052 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007053 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007054 /* we found the server and we can use it */
7055 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02007056 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007057 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007058 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007059 break;
7060 } else {
7061 /* we found a server, but it's down,
7062 * mark it as such and go on in case
7063 * another one is available.
7064 */
7065 txn->flags &= ~TX_CK_MASK;
7066 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007067 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007068 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007069 srv = srv->next;
7070 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007071
Willy Tarreauf64d1412010-10-07 20:06:11 +02007072 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007073 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007074 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007075 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007076 txn->flags |= TX_CK_UNUSED;
7077 else
7078 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007079 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007080
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007081 /* depending on the cookie mode, we may have to either :
7082 * - delete the complete cookie if we're in insert+indirect mode, so that
7083 * the server never sees it ;
7084 * - remove the server id from the cookie value, and tag the cookie as an
7085 * application cookie so that it does not get accidentely removed later,
7086 * if we're in cookie prefix mode
7087 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007088 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007089 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007090
Willy Tarreau9b28e032012-10-12 23:49:43 +02007091 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007092 val_end += delta;
7093 next += delta;
7094 hdr_end += delta;
7095 hdr_next += delta;
7096 cur_hdr->len += delta;
7097 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007098
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007099 del_from = NULL;
7100 preserve_hdr = 1; /* we want to keep this cookie */
7101 }
7102 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007103 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007104 del_from = prev;
7105 }
7106 } else {
7107 /* This is not our cookie, so we must preserve it. But if we already
7108 * scheduled another cookie for removal, we cannot remove the
7109 * complete header, but we can remove the previous block itself.
7110 */
7111 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007112
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007113 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007114 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007115 if (att_beg >= del_from)
7116 att_beg += delta;
7117 if (att_end >= del_from)
7118 att_end += delta;
7119 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007120 val_end += delta;
7121 next += delta;
7122 hdr_end += delta;
7123 hdr_next += delta;
7124 cur_hdr->len += delta;
7125 http_msg_move_end(&txn->req, delta);
7126 prev = del_from;
7127 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007128 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007129 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007130
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007131 /* continue with next cookie on this header line */
7132 att_beg = next;
7133 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007134
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007135 /* There are no more cookies on this line.
7136 * We may still have one (or several) marked for deletion at the
7137 * end of the line. We must do this now in two ways :
7138 * - if some cookies must be preserved, we only delete from the
7139 * mark to the end of line ;
7140 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007141 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007142 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007143 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007144 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007145 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007146 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007147 cur_hdr->len += delta;
7148 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007149 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007150
7151 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007152 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7153 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007154 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007155 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007156 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007157 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007158 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007159 }
7160
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007161 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007162 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007163 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007164}
7165
7166
Willy Tarreaua15645d2007-03-18 16:22:39 +01007167/* Iterate the same filter through all response headers contained in <rtr>.
7168 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7169 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007170int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007171{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007172 char *cur_ptr, *cur_end, *cur_next;
7173 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007174 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007175 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007176 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007177
7178 last_hdr = 0;
7179
Willy Tarreau9b28e032012-10-12 23:49:43 +02007180 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007181 old_idx = 0;
7182
7183 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007184 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007185 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007186 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007187 (exp->action == ACT_ALLOW ||
7188 exp->action == ACT_DENY))
7189 return 0;
7190
7191 cur_idx = txn->hdr_idx.v[old_idx].next;
7192 if (!cur_idx)
7193 break;
7194
7195 cur_hdr = &txn->hdr_idx.v[cur_idx];
7196 cur_ptr = cur_next;
7197 cur_end = cur_ptr + cur_hdr->len;
7198 cur_next = cur_end + cur_hdr->cr + 1;
7199
7200 /* Now we have one header between cur_ptr and cur_end,
7201 * and the next header starts at cur_next.
7202 */
7203
Willy Tarreau15a53a42015-01-21 13:39:42 +01007204 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007205 switch (exp->action) {
7206 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007207 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007208 last_hdr = 1;
7209 break;
7210
7211 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007212 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007213 last_hdr = 1;
7214 break;
7215
7216 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007217 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7218 if (trash.len < 0)
7219 return -1;
7220
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007221 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007222 /* FIXME: if the user adds a newline in the replacement, the
7223 * index will not be recalculated for now, and the new line
7224 * will not be counted as a new header.
7225 */
7226
7227 cur_end += delta;
7228 cur_next += delta;
7229 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007230 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007231 break;
7232
7233 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007234 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007235 cur_next += delta;
7236
Willy Tarreaufa355d42009-11-29 18:12:29 +01007237 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007238 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7239 txn->hdr_idx.used--;
7240 cur_hdr->len = 0;
7241 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007242 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007243 break;
7244
7245 }
7246 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007247
7248 /* keep the link from this header to next one in case of later
7249 * removal of next header.
7250 */
7251 old_idx = cur_idx;
7252 }
7253 return 0;
7254}
7255
7256
7257/* Apply the filter to the status line in the response buffer <rtr>.
7258 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7259 * or -1 if a replacement resulted in an invalid status line.
7260 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007261int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007262{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007263 char *cur_ptr, *cur_end;
7264 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007265 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007266 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007267
7268
Willy Tarreau3d300592007-03-18 18:34:41 +01007269 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007270 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007271 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007272 (exp->action == ACT_ALLOW ||
7273 exp->action == ACT_DENY))
7274 return 0;
7275 else if (exp->action == ACT_REMOVE)
7276 return 0;
7277
7278 done = 0;
7279
Willy Tarreau9b28e032012-10-12 23:49:43 +02007280 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007281 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007282
7283 /* Now we have the status line between cur_ptr and cur_end */
7284
Willy Tarreau15a53a42015-01-21 13:39:42 +01007285 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007286 switch (exp->action) {
7287 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007288 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007289 done = 1;
7290 break;
7291
7292 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007293 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007294 done = 1;
7295 break;
7296
7297 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007298 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7299 if (trash.len < 0)
7300 return -1;
7301
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007302 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007303 /* FIXME: if the user adds a newline in the replacement, the
7304 * index will not be recalculated for now, and the new line
7305 * will not be counted as a new header.
7306 */
7307
Willy Tarreaufa355d42009-11-29 18:12:29 +01007308 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007309 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007310 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007311 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007312 cur_ptr, cur_end + 1,
7313 NULL, NULL);
7314 if (unlikely(!cur_end))
7315 return -1;
7316
7317 /* we have a full respnse and we know that we have either a CR
7318 * or an LF at <ptr>.
7319 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007320 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007321 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007322 /* there is no point trying this regex on headers */
7323 return 1;
7324 }
7325 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007326 return done;
7327}
7328
7329
7330
7331/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007332 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007333 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7334 * unparsable response.
7335 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007336int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007337{
Willy Tarreau192252e2015-04-04 01:47:55 +02007338 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007339 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007340 struct hdr_exp *exp;
7341
7342 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007343 int ret;
7344
7345 /*
7346 * The interleaving of transformations and verdicts
7347 * makes it difficult to decide to continue or stop
7348 * the evaluation.
7349 */
7350
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007351 if (txn->flags & TX_SVDENY)
7352 break;
7353
Willy Tarreau3d300592007-03-18 18:34:41 +01007354 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007355 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7356 exp->action == ACT_PASS)) {
7357 exp = exp->next;
7358 continue;
7359 }
7360
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007361 /* if this filter had a condition, evaluate it now and skip to
7362 * next filter if the condition does not match.
7363 */
7364 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007365 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007366 ret = acl_pass(ret);
7367 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7368 ret = !ret;
7369 if (!ret)
7370 continue;
7371 }
7372
Willy Tarreaua15645d2007-03-18 16:22:39 +01007373 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007374 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007375 if (unlikely(ret < 0))
7376 return -1;
7377
7378 if (likely(ret == 0)) {
7379 /* The filter did not match the response, it can be
7380 * iterated through all headers.
7381 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007382 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7383 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007384 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007385 }
7386 return 0;
7387}
7388
7389
Willy Tarreaua15645d2007-03-18 16:22:39 +01007390/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007391 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007392 * desirable to call it only when needed. This function is also used when we
7393 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007394 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007395void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007396{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007397 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007398 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01007399 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007400 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007401 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007402 char *hdr_beg, *hdr_end, *hdr_next;
7403 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007404
Willy Tarreaua15645d2007-03-18 16:22:39 +01007405 /* Iterate through the headers.
7406 * we start with the start line.
7407 */
7408 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007409 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007410
7411 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7412 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007413 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007414
7415 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007416 hdr_beg = hdr_next;
7417 hdr_end = hdr_beg + cur_hdr->len;
7418 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007419
Willy Tarreau24581ba2010-08-31 22:39:35 +02007420 /* We have one full header between hdr_beg and hdr_end, and the
7421 * next header starts at hdr_next. We're only interested in
7422 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007423 */
7424
Willy Tarreau24581ba2010-08-31 22:39:35 +02007425 is_cookie2 = 0;
7426 prev = hdr_beg + 10;
7427 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007428 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007429 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7430 if (!val) {
7431 old_idx = cur_idx;
7432 continue;
7433 }
7434 is_cookie2 = 1;
7435 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007436 }
7437
Willy Tarreau24581ba2010-08-31 22:39:35 +02007438 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7439 * <prev> points to the colon.
7440 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007441 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007442
Willy Tarreau24581ba2010-08-31 22:39:35 +02007443 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7444 * check-cache is enabled) and we are not interested in checking
7445 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007446 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02007447 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007448 return;
7449
Willy Tarreau24581ba2010-08-31 22:39:35 +02007450 /* OK so now we know we have to process this response cookie.
7451 * The format of the Set-Cookie header is slightly different
7452 * from the format of the Cookie header in that it does not
7453 * support the comma as a cookie delimiter (thus the header
7454 * cannot be folded) because the Expires attribute described in
7455 * the original Netscape's spec may contain an unquoted date
7456 * with a comma inside. We have to live with this because
7457 * many browsers don't support Max-Age and some browsers don't
7458 * support quoted strings. However the Set-Cookie2 header is
7459 * clean.
7460 *
7461 * We have to keep multiple pointers in order to support cookie
7462 * removal at the beginning, middle or end of header without
7463 * corrupting the header (in case of set-cookie2). A special
7464 * pointer, <scav> points to the beginning of the set-cookie-av
7465 * fields after the first semi-colon. The <next> pointer points
7466 * either to the end of line (set-cookie) or next unquoted comma
7467 * (set-cookie2). All of these headers are valid :
7468 *
7469 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7470 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7471 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7472 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7473 * | | | | | | | | | |
7474 * | | | | | | | | +-> next hdr_end <--+
7475 * | | | | | | | +------------> scav
7476 * | | | | | | +--------------> val_end
7477 * | | | | | +--------------------> val_beg
7478 * | | | | +----------------------> equal
7479 * | | | +------------------------> att_end
7480 * | | +----------------------------> att_beg
7481 * | +------------------------------> prev
7482 * +-----------------------------------------> hdr_beg
7483 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007484
Willy Tarreau24581ba2010-08-31 22:39:35 +02007485 for (; prev < hdr_end; prev = next) {
7486 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007487
Willy Tarreau24581ba2010-08-31 22:39:35 +02007488 /* find att_beg */
7489 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007490 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007491 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007492
Willy Tarreau24581ba2010-08-31 22:39:35 +02007493 /* find att_end : this is the first character after the last non
7494 * space before the equal. It may be equal to hdr_end.
7495 */
7496 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007497
Willy Tarreau24581ba2010-08-31 22:39:35 +02007498 while (equal < hdr_end) {
7499 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7500 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007501 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007502 continue;
7503 att_end = equal;
7504 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007505
Willy Tarreau24581ba2010-08-31 22:39:35 +02007506 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7507 * is between <att_beg> and <equal>, both may be identical.
7508 */
7509
7510 /* look for end of cookie if there is an equal sign */
7511 if (equal < hdr_end && *equal == '=') {
7512 /* look for the beginning of the value */
7513 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007514 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007515 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007516
Willy Tarreau24581ba2010-08-31 22:39:35 +02007517 /* find the end of the value, respecting quotes */
7518 next = find_cookie_value_end(val_beg, hdr_end);
7519
7520 /* make val_end point to the first white space or delimitor after the value */
7521 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007522 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007523 val_end--;
7524 } else {
7525 /* <equal> points to next comma, semi-colon or EOL */
7526 val_beg = val_end = next = equal;
7527 }
7528
7529 if (next < hdr_end) {
7530 /* Set-Cookie2 supports multiple cookies, and <next> points to
7531 * a colon or semi-colon before the end. So skip all attr-value
7532 * pairs and look for the next comma. For Set-Cookie, since
7533 * commas are permitted in values, skip to the end.
7534 */
7535 if (is_cookie2)
7536 next = find_hdr_value_end(next, hdr_end);
7537 else
7538 next = hdr_end;
7539 }
7540
7541 /* Now everything is as on the diagram above */
7542
7543 /* Ignore cookies with no equal sign */
7544 if (equal == val_end)
7545 continue;
7546
7547 /* If there are spaces around the equal sign, we need to
7548 * strip them otherwise we'll get trouble for cookie captures,
7549 * or even for rewrites. Since this happens extremely rarely,
7550 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007551 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007552 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7553 int stripped_before = 0;
7554 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007555
Willy Tarreau24581ba2010-08-31 22:39:35 +02007556 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007557 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007558 equal += stripped_before;
7559 val_beg += stripped_before;
7560 }
7561
7562 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007563 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007564 val_beg += stripped_after;
7565 stripped_before += stripped_after;
7566 }
7567
7568 val_end += stripped_before;
7569 next += stripped_before;
7570 hdr_end += stripped_before;
7571 hdr_next += stripped_before;
7572 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007573 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007574 }
7575
7576 /* First, let's see if we want to capture this cookie. We check
7577 * that we don't already have a server side cookie, because we
7578 * can only capture one. Also as an optimisation, we ignore
7579 * cookies shorter than the declared name.
7580 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007581 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007582 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007583 (val_end - att_beg >= sess->fe->capture_namelen) &&
7584 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007585 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007586 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007587 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01007588 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007589 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007590 if (log_len > sess->fe->capture_len)
7591 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007592 memcpy(txn->srv_cookie, att_beg, log_len);
7593 txn->srv_cookie[log_len] = 0;
7594 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007595 }
7596
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007597 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007598 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007599 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007600 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7601 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007602 /* assume passive cookie by default */
7603 txn->flags &= ~TX_SCK_MASK;
7604 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007605
7606 /* If the cookie is in insert mode on a known server, we'll delete
7607 * this occurrence because we'll insert another one later.
7608 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007609 * a direct access.
7610 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007611 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007612 /* The "preserve" flag was set, we don't want to touch the
7613 * server's cookie.
7614 */
7615 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007616 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007617 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007618 /* this cookie must be deleted */
7619 if (*prev == ':' && next == hdr_end) {
7620 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007621 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007622 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7623 txn->hdr_idx.used--;
7624 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007625 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007626 hdr_next += delta;
7627 http_msg_move_end(&txn->rsp, delta);
7628 /* note: while both invalid now, <next> and <hdr_end>
7629 * are still equal, so the for() will stop as expected.
7630 */
7631 } else {
7632 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007633 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007634 next = prev;
7635 hdr_end += delta;
7636 hdr_next += delta;
7637 cur_hdr->len += delta;
7638 http_msg_move_end(&txn->rsp, delta);
7639 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007640 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007641 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007642 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007643 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007644 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007645 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007646 * with this server since we know it.
7647 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007648 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007649 next += delta;
7650 hdr_end += delta;
7651 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007652 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007653 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007654
Willy Tarreauf1348312010-10-07 15:54:11 +02007655 txn->flags &= ~TX_SCK_MASK;
7656 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007657 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007658 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007659 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007660 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007661 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007662 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007663 next += delta;
7664 hdr_end += delta;
7665 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007666 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007667 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007668
Willy Tarreau827aee92011-03-10 16:55:02 +01007669 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007670 txn->flags &= ~TX_SCK_MASK;
7671 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007672 }
7673 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007674 /* that's done for this cookie, check the next one on the same
7675 * line when next != hdr_end (only if is_cookie2).
7676 */
7677 }
7678 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007679 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007680 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007681}
7682
7683
Willy Tarreaua15645d2007-03-18 16:22:39 +01007684/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007685 * Parses the Cache-Control and Pragma request header fields to determine if
7686 * the request may be served from the cache and/or if it is cacheable. Updates
7687 * s->txn->flags.
7688 */
7689void check_request_for_cacheability(struct stream *s, struct channel *chn)
7690{
7691 struct http_txn *txn = s->txn;
7692 char *p1, *p2;
7693 char *cur_ptr, *cur_end, *cur_next;
7694 int pragma_found;
7695 int cc_found;
7696 int cur_idx;
7697
7698 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
7699 return; /* nothing more to do here */
7700
7701 cur_idx = 0;
7702 pragma_found = cc_found = 0;
7703 cur_next = chn->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
7704
7705 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7706 struct hdr_idx_elem *cur_hdr;
7707 int val;
7708
7709 cur_hdr = &txn->hdr_idx.v[cur_idx];
7710 cur_ptr = cur_next;
7711 cur_end = cur_ptr + cur_hdr->len;
7712 cur_next = cur_end + cur_hdr->cr + 1;
7713
7714 /* We have one full header between cur_ptr and cur_end, and the
7715 * next header starts at cur_next.
7716 */
7717
7718 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7719 if (val) {
7720 if ((cur_end - (cur_ptr + val) >= 8) &&
7721 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7722 pragma_found = 1;
7723 continue;
7724 }
7725 }
7726
7727 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7728 if (!val)
7729 continue;
7730
7731 /* OK, right now we know we have a cache-control header at cur_ptr */
7732 cc_found = 1;
7733 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
7734
7735 if (p1 >= cur_end) /* no more info */
7736 continue;
7737
7738 /* p1 is at the beginning of the value */
7739 p2 = p1;
7740 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
7741 p2++;
7742
7743 /* we have a complete value between p1 and p2. We don't check the
7744 * values after max-age, max-stale nor min-fresh, we simply don't
7745 * use the cache when they're specified.
7746 */
7747 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
7748 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
7749 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
7750 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
7751 txn->flags |= TX_CACHE_IGNORE;
7752 continue;
7753 }
7754
7755 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
7756 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7757 continue;
7758 }
7759 }
7760
7761 /* RFC7234#5.4:
7762 * When the Cache-Control header field is also present and
7763 * understood in a request, Pragma is ignored.
7764 * When the Cache-Control header field is not present in a
7765 * request, caches MUST consider the no-cache request
7766 * pragma-directive as having the same effect as if
7767 * "Cache-Control: no-cache" were present.
7768 */
7769 if (!cc_found && pragma_found)
7770 txn->flags |= TX_CACHE_IGNORE;
7771}
7772
7773/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007774 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007775 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007776void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007777{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007778 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007779 char *p1, *p2;
7780
7781 char *cur_ptr, *cur_end, *cur_next;
7782 int cur_idx;
7783
Willy Tarreau12b32f22017-12-21 16:08:09 +01007784 if (txn->status < 200) {
7785 /* do not try to cache interim responses! */
7786 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007787 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01007788 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007789
7790 /* Iterate through the headers.
7791 * we start with the start line.
7792 */
7793 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007794 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007795
7796 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7797 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007798 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007799
7800 cur_hdr = &txn->hdr_idx.v[cur_idx];
7801 cur_ptr = cur_next;
7802 cur_end = cur_ptr + cur_hdr->len;
7803 cur_next = cur_end + cur_hdr->cr + 1;
7804
7805 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007806 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007807 */
7808
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007809 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7810 if (val) {
7811 if ((cur_end - (cur_ptr + val) >= 8) &&
7812 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7813 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7814 return;
7815 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007816 }
7817
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007818 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7819 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007820 continue;
7821
7822 /* OK, right now we know we have a cache-control header at cur_ptr */
7823
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007824 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007825
7826 if (p1 >= cur_end) /* no more info */
7827 continue;
7828
7829 /* p1 is at the beginning of the value */
7830 p2 = p1;
7831
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007832 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007833 p2++;
7834
7835 /* we have a complete value between p1 and p2 */
7836 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007837 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7838 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7839 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7840 continue;
7841 }
7842
Willy Tarreaua15645d2007-03-18 16:22:39 +01007843 /* we have something of the form no-cache="set-cookie" */
7844 if ((cur_end - p1 >= 21) &&
7845 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7846 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007847 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007848 continue;
7849 }
7850
7851 /* OK, so we know that either p2 points to the end of string or to a comma */
7852 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007853 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007854 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007855 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007856 return;
7857 }
7858
7859 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007860 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007861 continue;
7862 }
7863 }
7864}
7865
Willy Tarreau58f10d72006-12-04 02:26:12 +01007866
Willy Tarreaub2513902006-12-17 14:52:38 +01007867/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007868 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007869 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007870 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007871 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007872 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007873 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007874 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007875 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007876int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007877{
7878 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007879 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007880 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007881
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007882 if (!uri_auth)
7883 return 0;
7884
Cyril Bonté70be45d2010-10-12 00:14:35 +02007885 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007886 return 0;
7887
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007888 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007889 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007890 return 0;
7891
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007892 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007893 return 0;
7894
Willy Tarreaub2513902006-12-17 14:52:38 +01007895 return 1;
7896}
7897
Willy Tarreau4076a152009-04-02 15:18:36 +02007898/*
7899 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007900 * By default it tries to report the error position as msg->err_pos. However if
7901 * this one is not set, it will then report msg->next, which is the last known
7902 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007903 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007904 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02007905void http_capture_bad_message(struct proxy *proxy, struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007906 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007907 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007908{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007909 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007910 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007911 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007912
Christopher Faulet2a944ee2017-11-07 10:42:54 +01007913 HA_SPIN_LOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreauf3764b72016-03-31 13:45:10 +02007914 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02007915 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007916 len1 = MIN(len1, es->len);
7917 len2 = es->len - len1; /* remaining data if buffer wraps */
7918
Willy Tarreauf3764b72016-03-31 13:45:10 +02007919 if (!es->buf)
7920 es->buf = malloc(global.tune.bufsize);
7921
7922 if (es->buf) {
7923 memcpy(es->buf, chn->buf->p, len1);
7924 if (len2)
7925 memcpy(es->buf + len1, chn->buf->data, len2);
7926 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007927
Willy Tarreau4076a152009-04-02 15:18:36 +02007928 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007929 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007930 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007931 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007932
Willy Tarreau4076a152009-04-02 15:18:36 +02007933 es->when = date; // user-visible date
7934 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007935 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007936 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007937 if (objt_conn(sess->origin))
7938 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007939 else
7940 memset(&es->src, 0, sizeof(es->src));
7941
Willy Tarreau078272e2010-12-12 12:46:33 +01007942 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007943 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007944 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007945 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007946 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007947 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007948 es->b_out = chn->buf->o;
7949 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007950 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007951 es->m_clen = msg->chunk_len;
7952 es->m_blen = msg->body_len;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01007953 HA_SPIN_UNLOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreau4076a152009-04-02 15:18:36 +02007954}
Willy Tarreaub2513902006-12-17 14:52:38 +01007955
Willy Tarreau294c4732011-12-16 21:35:50 +01007956/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7957 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7958 * performed over the whole headers. Otherwise it must contain a valid header
7959 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7960 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7961 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7962 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007963 * -1. The value fetch stops at commas, so this function is suited for use with
7964 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007965 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007966 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007967unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007968 struct hdr_idx *idx, int occ,
7969 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007970{
Willy Tarreau294c4732011-12-16 21:35:50 +01007971 struct hdr_ctx local_ctx;
7972 char *ptr_hist[MAX_HDR_HISTORY];
7973 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007974 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007975 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007976
Willy Tarreau294c4732011-12-16 21:35:50 +01007977 if (!ctx) {
7978 local_ctx.idx = 0;
7979 ctx = &local_ctx;
7980 }
7981
Willy Tarreaubce70882009-09-07 11:51:47 +02007982 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007983 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007984 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007985 occ--;
7986 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007987 *vptr = ctx->line + ctx->val;
7988 *vlen = ctx->vlen;
7989 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007990 }
7991 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007992 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007993 }
7994
7995 /* negative occurrence, we scan all the list then walk back */
7996 if (-occ > MAX_HDR_HISTORY)
7997 return 0;
7998
Willy Tarreau294c4732011-12-16 21:35:50 +01007999 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008000 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008001 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8002 len_hist[hist_ptr] = ctx->vlen;
8003 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008004 hist_ptr = 0;
8005 found++;
8006 }
8007 if (-occ > found)
8008 return 0;
8009 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008010 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8011 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8012 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008013 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008014 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008015 if (hist_ptr >= MAX_HDR_HISTORY)
8016 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008017 *vptr = ptr_hist[hist_ptr];
8018 *vlen = len_hist[hist_ptr];
8019 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008020}
8021
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008022/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8023 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8024 * performed over the whole headers. Otherwise it must contain a valid header
8025 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8026 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8027 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8028 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8029 * -1. This function differs from http_get_hdr() in that it only returns full
8030 * line header values and does not stop at commas.
8031 * The return value is 0 if nothing was found, or non-zero otherwise.
8032 */
8033unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8034 struct hdr_idx *idx, int occ,
8035 struct hdr_ctx *ctx, char **vptr, int *vlen)
8036{
8037 struct hdr_ctx local_ctx;
8038 char *ptr_hist[MAX_HDR_HISTORY];
8039 int len_hist[MAX_HDR_HISTORY];
8040 unsigned int hist_ptr;
8041 int found;
8042
8043 if (!ctx) {
8044 local_ctx.idx = 0;
8045 ctx = &local_ctx;
8046 }
8047
8048 if (occ >= 0) {
8049 /* search from the beginning */
8050 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8051 occ--;
8052 if (occ <= 0) {
8053 *vptr = ctx->line + ctx->val;
8054 *vlen = ctx->vlen;
8055 return 1;
8056 }
8057 }
8058 return 0;
8059 }
8060
8061 /* negative occurrence, we scan all the list then walk back */
8062 if (-occ > MAX_HDR_HISTORY)
8063 return 0;
8064
8065 found = hist_ptr = 0;
8066 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8067 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8068 len_hist[hist_ptr] = ctx->vlen;
8069 if (++hist_ptr >= MAX_HDR_HISTORY)
8070 hist_ptr = 0;
8071 found++;
8072 }
8073 if (-occ > found)
8074 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008075
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008076 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008077 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8078 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8079 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008080 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008081 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008082 if (hist_ptr >= MAX_HDR_HISTORY)
8083 hist_ptr -= MAX_HDR_HISTORY;
8084 *vptr = ptr_hist[hist_ptr];
8085 *vlen = len_hist[hist_ptr];
8086 return 1;
8087}
8088
Willy Tarreaubaaee002006-06-26 02:48:02 +02008089/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008090 * Print a debug line with a header. Always stop at the first CR or LF char,
8091 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8092 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008093 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008094void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008095{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008096 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008097 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008098
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008099 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008100 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02008101 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02008102 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 +02008103
8104 for (max = 0; start + max < end; max++)
8105 if (start[max] == '\r' || start[max] == '\n')
8106 break;
8107
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008108 UBOUND(max, trash.size - trash.len - 3);
8109 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8110 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008111 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008112}
8113
Willy Tarreaueee5b512015-04-03 23:46:31 +02008114
8115/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8116 * The hdr_idx is allocated as well. In case of allocation failure, everything
8117 * allocated is freed and NULL is returned. Otherwise the new transaction is
8118 * assigned to the stream and returned.
8119 */
8120struct http_txn *http_alloc_txn(struct stream *s)
8121{
8122 struct http_txn *txn = s->txn;
8123
8124 if (txn)
8125 return txn;
8126
Willy Tarreaubafbe012017-11-24 17:34:44 +01008127 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008128 if (!txn)
8129 return txn;
8130
8131 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01008132 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008133 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01008134 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008135 return NULL;
8136 }
8137
8138 s->txn = txn;
8139 return txn;
8140}
8141
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008142void http_txn_reset_req(struct http_txn *txn)
8143{
8144 txn->req.flags = 0;
8145 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8146 txn->req.next = 0;
8147 txn->req.chunk_len = 0LL;
8148 txn->req.body_len = 0LL;
8149 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8150}
8151
8152void http_txn_reset_res(struct http_txn *txn)
8153{
8154 txn->rsp.flags = 0;
8155 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8156 txn->rsp.next = 0;
8157 txn->rsp.chunk_len = 0LL;
8158 txn->rsp.body_len = 0LL;
8159 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8160}
8161
Willy Tarreau0937bc42009-12-22 15:03:09 +01008162/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008163 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008164 * the required fields are properly allocated and that we only need to (re)init
8165 * them. This should be used before processing any new request.
8166 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008167void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008168{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008169 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008170 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008171
8172 txn->flags = 0;
8173 txn->status = -1;
8174
Willy Tarreauf64d1412010-10-07 20:06:11 +02008175 txn->cookie_first_date = 0;
8176 txn->cookie_last_date = 0;
8177
Willy Tarreaueee5b512015-04-03 23:46:31 +02008178 txn->srv_cookie = NULL;
8179 txn->cli_cookie = NULL;
8180 txn->uri = NULL;
8181
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008182 http_txn_reset_req(txn);
8183 http_txn_reset_res(txn);
8184
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008185 txn->req.chn = &s->req;
8186 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008187
8188 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008189
8190 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8191 if (fe->options2 & PR_O2_REQBUG_OK)
8192 txn->req.err_pos = -1; /* let buggy requests pass */
8193
Willy Tarreau0937bc42009-12-22 15:03:09 +01008194 if (txn->hdr_idx.v)
8195 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008196
8197 vars_init(&s->vars_txn, SCOPE_TXN);
8198 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008199}
8200
8201/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008202void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008203{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008204 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008205 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008206
8207 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01008208 pool_free(pool_head_requri, txn->uri);
8209 pool_free(pool_head_capture, txn->cli_cookie);
8210 pool_free(pool_head_capture, txn->srv_cookie);
8211 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008212
William Lallemanda73203e2012-03-12 12:48:57 +01008213 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008214 txn->uri = NULL;
8215 txn->srv_cookie = NULL;
8216 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008217
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008218 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008219 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008220 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01008221 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008222 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008223 }
8224
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008225 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008226 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008227 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01008228 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008229 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008230 }
8231
Willy Tarreau6204cd92016-03-10 16:33:04 +01008232 vars_prune(&s->vars_txn, s->sess, s);
8233 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008234}
8235
8236/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008237void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008238{
8239 http_end_txn(s);
8240 http_init_txn(s);
8241
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008242 /* reinitialise the current rule list pointer to NULL. We are sure that
8243 * any rulelist match the NULL pointer.
8244 */
8245 s->current_rule_list = NULL;
8246
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008247 s->be = strm_fe(s);
8248 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008249 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008250 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008251 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008252 /* re-init store persistence */
8253 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008254 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008255
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008256 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008257
Willy Tarreau739cfba2010-01-25 23:11:14 +01008258 /* We must trim any excess data from the response buffer, because we
8259 * may have blocked an invalid response from a server that we don't
8260 * want to accidentely forward once we disable the analysers, nor do
8261 * we want those data to come along with next response. A typical
8262 * example of such data would be from a buggy server responding to
8263 * a HEAD with some data, or sending more than the advertised
8264 * content-length.
8265 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008266 if (unlikely(s->res.buf->i))
8267 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008268
Christopher Fauletc0c672a2017-03-28 11:51:33 +02008269 /* Now we can realign the response buffer */
8270 buffer_realign(s->res.buf);
8271
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008272 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008273 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008274
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008275 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008276 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008277
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008278 s->req.rex = TICK_ETERNITY;
8279 s->req.wex = TICK_ETERNITY;
8280 s->req.analyse_exp = TICK_ETERNITY;
8281 s->res.rex = TICK_ETERNITY;
8282 s->res.wex = TICK_ETERNITY;
8283 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01008284 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008285}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008286
Sasha Pachev218f0642014-06-16 12:05:59 -06008287void free_http_res_rules(struct list *r)
8288{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008289 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008290
8291 list_for_each_entry_safe(pr, tr, r, list) {
8292 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008293 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008294 free(pr);
8295 }
8296}
8297
8298void free_http_req_rules(struct list *r)
8299{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008300 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01008301
8302 list_for_each_entry_safe(pr, tr, r, list) {
8303 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008304 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008305 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008306
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008307 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008308 free(pr);
8309 }
8310}
8311
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008312/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008313struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01008314{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008315 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008316 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008317 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008318 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008319
Vincent Bernat02779b62016-04-03 13:48:43 +02008320 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01008321 if (!rule) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008322 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008323 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008324 }
8325
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008326 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008327 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01008328 cur_arg = 1;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008329 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block") || !strcmp(args[0], "tarpit")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04008330 int code;
8331 int hc;
8332
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008333 if (!strcmp(args[0], "tarpit")) {
8334 rule->action = ACT_HTTP_REQ_TARPIT;
8335 rule->deny_status = HTTP_ERR_500;
8336 }
8337 else {
8338 rule->action = ACT_ACTION_DENY;
8339 rule->deny_status = HTTP_ERR_403;
8340 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008341 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04008342 if (strcmp(args[cur_arg], "deny_status") == 0) {
8343 cur_arg++;
8344 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008345 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
8346 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008347 goto out_err;
8348 }
8349
8350 code = atol(args[cur_arg]);
8351 cur_arg++;
8352 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
8353 if (http_err_codes[hc] == code) {
8354 rule->deny_status = hc;
8355 break;
8356 }
8357 }
8358
8359 if (hc >= HTTP_ERR_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008360 ha_warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n",
8361 file, linenum, code, http_err_codes[rule->deny_status]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008362 }
8363 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008364 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008365 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008366 cur_arg = 1;
8367
8368 while(*args[cur_arg]) {
8369 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008370 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008371 cur_arg+=2;
8372 continue;
8373 } else
8374 break;
8375 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008376 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008377 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008378 cur_arg = 1;
8379
8380 if (!*args[cur_arg] ||
8381 (*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 +01008382 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8383 file, linenum, args[0]);
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008384 goto out_err;
8385 }
8386 rule->arg.nice = atoi(args[cur_arg]);
8387 if (rule->arg.nice < -1024)
8388 rule->arg.nice = -1024;
8389 else if (rule->arg.nice > 1024)
8390 rule->arg.nice = 1024;
8391 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008392 } else if (!strcmp(args[0], "set-tos")) {
8393#ifdef IP_TOS
8394 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008395 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008396 cur_arg = 1;
8397
8398 if (!*args[cur_arg] ||
8399 (*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 +01008400 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8401 file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008402 goto out_err;
8403 }
8404
8405 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8406 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008407 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8408 file, linenum, err, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008409 goto out_err;
8410 }
8411 cur_arg++;
8412#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008413 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 +02008414 goto out_err;
8415#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008416 } else if (!strcmp(args[0], "set-mark")) {
8417#ifdef SO_MARK
8418 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008419 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008420 cur_arg = 1;
8421
8422 if (!*args[cur_arg] ||
8423 (*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 +01008424 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8425 file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008426 goto out_err;
8427 }
8428
8429 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8430 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008431 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8432 file, linenum, err, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008433 goto out_err;
8434 }
8435 cur_arg++;
8436 global.last_checks |= LSTCHK_NETADM;
8437#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008438 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 +02008439 goto out_err;
8440#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008441 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008442 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008443 cur_arg = 1;
8444
8445 if (!*args[cur_arg] ||
8446 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8447 bad_log_level:
Christopher Faulet767a84b2017-11-24 16:50:31 +01008448 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8449 file, linenum, args[0]);
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008450 goto out_err;
8451 }
8452 if (strcmp(args[cur_arg], "silent") == 0)
8453 rule->arg.loglevel = -1;
8454 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8455 goto bad_log_level;
8456 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008457 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008458 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008459 cur_arg = 1;
8460
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008461 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8462 (*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 +01008463 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8464 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008465 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008466 }
8467
8468 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8469 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8470 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008471
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008472 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008473 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008474 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 +01008475 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008476 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8477 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008478 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008479 goto out_err;
8480 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008481 free(proxy->conf.lfs_file);
8482 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8483 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008484 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02008485 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008486 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008487 cur_arg = 1;
8488
8489 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02008490 (*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 +01008491 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
8492 file, linenum, args[0]);
Sasha Pachev218f0642014-06-16 12:05:59 -06008493 goto out_err;
8494 }
8495
8496 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8497 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8498 LIST_INIT(&rule->arg.hdr_add.fmt);
8499
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008500 error = NULL;
8501 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008502 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8503 args[cur_arg + 1], error);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008504 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008505 goto out_err;
8506 }
8507
8508 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008509 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008510 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 +01008511 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008512 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8513 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008514 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008515 goto out_err;
8516 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008517
8518 free(proxy->conf.lfs_file);
8519 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8520 proxy->conf.lfs_line = proxy->conf.args.line;
8521 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008522 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008523 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008524 cur_arg = 1;
8525
8526 if (!*args[cur_arg] ||
8527 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008528 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8529 file, linenum, args[0]);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008530 goto out_err;
8531 }
8532
8533 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8534 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8535
8536 proxy->conf.args.ctx = ARGC_HRQ;
8537 free(proxy->conf.lfs_file);
8538 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8539 proxy->conf.lfs_line = proxy->conf.args.line;
8540 cur_arg += 1;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008541 } else if (strncmp(args[0], "track-sc", 8) == 0) {
Willy Tarreau09448f72014-06-25 18:12:15 +02008542 struct sample_expr *expr;
8543 unsigned int where;
8544 char *err = NULL;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008545 unsigned int tsc_num;
8546 const char *tsc_num_str;
Willy Tarreau09448f72014-06-25 18:12:15 +02008547
8548 cur_arg = 1;
8549 proxy->conf.args.ctx = ARGC_TRK;
8550
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008551 tsc_num_str = &args[0][8];
8552 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) {
8553 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8554 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
8555 free(err);
8556 goto out_err;
8557 }
8558
Willy Tarreau09448f72014-06-25 18:12:15 +02008559 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
8560 if (!expr) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008561 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8562 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
Willy Tarreau09448f72014-06-25 18:12:15 +02008563 free(err);
8564 goto out_err;
8565 }
8566
8567 where = 0;
8568 if (proxy->cap & PR_CAP_FE)
8569 where |= SMP_VAL_FE_HRQ_HDR;
8570 if (proxy->cap & PR_CAP_BE)
8571 where |= SMP_VAL_BE_HRQ_HDR;
8572
8573 if (!(expr->fetch->val & where)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008574 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
8575 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
8576 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
8577 args[cur_arg-1], sample_src_names(expr->fetch->use));
Willy Tarreau09448f72014-06-25 18:12:15 +02008578 free(expr);
8579 goto out_err;
8580 }
8581
8582 if (strcmp(args[cur_arg], "table") == 0) {
8583 cur_arg++;
8584 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008585 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
8586 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008587 free(expr);
8588 goto out_err;
8589 }
8590 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008591 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008592 cur_arg++;
8593 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008594 rule->arg.trk_ctr.expr = expr;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008595 rule->action = ACT_ACTION_TRK_SC0 + tsc_num;
Christopher Faulet78880fb2017-09-18 14:43:55 +02008596 rule->check_ptr = check_trk_action;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008597 } else if (strcmp(args[0], "redirect") == 0) {
8598 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008599 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008600
Willy Tarreaube4653b2015-05-28 15:26:58 +02008601 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008602 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8603 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008604 goto out_err;
8605 }
8606
8607 /* this redirect rule might already contain a parsed condition which
8608 * we'll pass to the http-request rule.
8609 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008610 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008611 rule->arg.redir = redir;
8612 rule->cond = redir->cond;
8613 redir->cond = NULL;
8614 cur_arg = 2;
8615 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008616 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8617 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008618 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008619 /*
8620 * '+ 8' for 'add-acl('
8621 * '- 9' for 'add-acl(' + trailing ')'
8622 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008623 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008624
8625 cur_arg = 1;
8626
8627 if (!*args[cur_arg] ||
8628 (*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 +01008629 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8630 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008631 goto out_err;
8632 }
8633
8634 LIST_INIT(&rule->arg.map.key);
8635 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008636 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008637 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008638 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008639 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8640 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008641 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008642 goto out_err;
8643 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008644 free(proxy->conf.lfs_file);
8645 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8646 proxy->conf.lfs_line = proxy->conf.args.line;
8647 cur_arg += 1;
8648 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8649 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008650 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008651 /*
8652 * '+ 8' for 'del-acl('
8653 * '- 9' for 'del-acl(' + trailing ')'
8654 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008655 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008656
8657 cur_arg = 1;
8658
8659 if (!*args[cur_arg] ||
8660 (*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 +01008661 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8662 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008663 goto out_err;
8664 }
8665
8666 LIST_INIT(&rule->arg.map.key);
8667 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008668 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008669 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008670 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008671 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8672 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008673 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008674 goto out_err;
8675 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008676 free(proxy->conf.lfs_file);
8677 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8678 proxy->conf.lfs_line = proxy->conf.args.line;
8679 cur_arg += 1;
8680 } else if (strncmp(args[0], "del-map", 7) == 0) {
8681 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008682 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008683 /*
8684 * '+ 8' for 'del-map('
8685 * '- 9' for 'del-map(' + trailing ')'
8686 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008687 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008688
8689 cur_arg = 1;
8690
8691 if (!*args[cur_arg] ||
8692 (*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 +01008693 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8694 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008695 goto out_err;
8696 }
8697
8698 LIST_INIT(&rule->arg.map.key);
8699 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008700 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008701 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008702 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008703 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8704 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008705 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008706 goto out_err;
8707 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008708 free(proxy->conf.lfs_file);
8709 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8710 proxy->conf.lfs_line = proxy->conf.args.line;
8711 cur_arg += 1;
8712 } else if (strncmp(args[0], "set-map", 7) == 0) {
8713 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008714 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008715 /*
8716 * '+ 8' for 'set-map('
8717 * '- 9' for 'set-map(' + trailing ')'
8718 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008719 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008720
8721 cur_arg = 1;
8722
8723 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8724 (*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 +01008725 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8726 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008727 goto out_err;
8728 }
8729
8730 LIST_INIT(&rule->arg.map.key);
8731 LIST_INIT(&rule->arg.map.value);
8732 proxy->conf.args.ctx = ARGC_HRQ;
8733
8734 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008735 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008736 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008737 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008738 ha_alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
8739 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008740 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008741 goto out_err;
8742 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008743
8744 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008745 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008746 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 +01008747 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008748 ha_alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
8749 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008750 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008751 goto out_err;
8752 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008753 free(proxy->conf.lfs_file);
8754 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8755 proxy->conf.lfs_line = proxy->conf.args.line;
8756
8757 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02008758 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
8759 char *errmsg = NULL;
8760 cur_arg = 1;
8761 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02008762 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02008763 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02008764 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008765 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8766 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
William Lallemand73025dd2014-04-24 14:38:37 +02008767 free(errmsg);
8768 goto out_err;
8769 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008770 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008771 action_build_list(&http_req_keywords.list, &trash);
Christopher Faulet767a84b2017-11-24 16:50:31 +01008772 ha_alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
8773 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
8774 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
8775 "%s%s, but got '%s'%s.\n",
8776 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008777 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008778 }
8779
8780 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8781 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008782 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008783
Christopher Faulet1b421ea2017-09-22 14:38:56 +02008784 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008785 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8786 file, linenum, args[0], errmsg);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008787 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008788 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008789 }
8790 rule->cond = cond;
8791 }
8792 else if (*args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008793 ha_alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8794 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8795 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008796 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008797 }
8798
8799 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008800 out_err:
8801 free(rule);
8802 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008803}
8804
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008805/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008806struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008807{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008808 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008809 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008810 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008811 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008812
8813 rule = calloc(1, sizeof(*rule));
8814 if (!rule) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008815 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008816 goto out_err;
8817 }
8818
8819 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008820 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008821 cur_arg = 1;
8822 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008823 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008824 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008825 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008826 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008827 cur_arg = 1;
8828
8829 if (!*args[cur_arg] ||
8830 (*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 +01008831 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8832 file, linenum, args[0]);
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008833 goto out_err;
8834 }
8835 rule->arg.nice = atoi(args[cur_arg]);
8836 if (rule->arg.nice < -1024)
8837 rule->arg.nice = -1024;
8838 else if (rule->arg.nice > 1024)
8839 rule->arg.nice = 1024;
8840 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008841 } else if (!strcmp(args[0], "set-tos")) {
8842#ifdef IP_TOS
8843 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008844 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008845 cur_arg = 1;
8846
8847 if (!*args[cur_arg] ||
8848 (*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 +01008849 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8850 file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008851 goto out_err;
8852 }
8853
8854 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8855 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008856 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8857 file, linenum, err, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008858 goto out_err;
8859 }
8860 cur_arg++;
8861#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008862 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 +02008863 goto out_err;
8864#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008865 } else if (!strcmp(args[0], "set-mark")) {
8866#ifdef SO_MARK
8867 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008868 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008869 cur_arg = 1;
8870
8871 if (!*args[cur_arg] ||
8872 (*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 +01008873 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8874 file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008875 goto out_err;
8876 }
8877
8878 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8879 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008880 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8881 file, linenum, err, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008882 goto out_err;
8883 }
8884 cur_arg++;
8885 global.last_checks |= LSTCHK_NETADM;
8886#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008887 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 +02008888 goto out_err;
8889#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008890 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008891 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008892 cur_arg = 1;
8893
8894 if (!*args[cur_arg] ||
8895 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8896 bad_log_level:
Christopher Faulet767a84b2017-11-24 16:50:31 +01008897 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8898 file, linenum, args[0]);
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008899 goto out_err;
8900 }
8901 if (strcmp(args[cur_arg], "silent") == 0)
8902 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08008903 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008904 goto bad_log_level;
8905 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008906 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008907 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008908 cur_arg = 1;
8909
8910 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8911 (*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 +01008912 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8913 file, linenum, args[0]);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008914 goto out_err;
8915 }
8916
8917 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8918 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8919 LIST_INIT(&rule->arg.hdr_add.fmt);
8920
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008921 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008922 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008923 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 +01008924 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008925 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8926 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008927 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008928 goto out_err;
8929 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008930 free(proxy->conf.lfs_file);
8931 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8932 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008933 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06008934 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008935 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008936 cur_arg = 1;
8937
8938 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02008939 (*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 +01008940 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
8941 file, linenum, args[0]);
Sasha Pachev218f0642014-06-16 12:05:59 -06008942 goto out_err;
8943 }
8944
8945 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8946 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8947 LIST_INIT(&rule->arg.hdr_add.fmt);
8948
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008949 error = NULL;
8950 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008951 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8952 args[cur_arg + 1], error);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008953 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008954 goto out_err;
8955 }
8956
8957 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008958 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008959 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 +01008960 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008961 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8962 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008963 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008964 goto out_err;
8965 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008966
8967 free(proxy->conf.lfs_file);
8968 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8969 proxy->conf.lfs_line = proxy->conf.args.line;
8970 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008971 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008972 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008973 cur_arg = 1;
8974
8975 if (!*args[cur_arg] ||
8976 (*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 +01008977 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8978 file, linenum, args[0]);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008979 goto out_err;
8980 }
8981
8982 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8983 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8984
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008985 proxy->conf.args.ctx = ARGC_HRS;
8986 free(proxy->conf.lfs_file);
8987 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8988 proxy->conf.lfs_line = proxy->conf.args.line;
8989 cur_arg += 1;
8990 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8991 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008992 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008993 /*
8994 * '+ 8' for 'add-acl('
8995 * '- 9' for 'add-acl(' + trailing ')'
8996 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008997 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008998
8999 cur_arg = 1;
9000
9001 if (!*args[cur_arg] ||
9002 (*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 +01009003 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9004 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009005 goto out_err;
9006 }
9007
9008 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009009 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009010 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009011 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009012 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009013 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9014 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009015 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009016 goto out_err;
9017 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009018 free(proxy->conf.lfs_file);
9019 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9020 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009021
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009022 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009023 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9024 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009025 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009026 /*
9027 * '+ 8' for 'del-acl('
9028 * '- 9' for 'del-acl(' + trailing ')'
9029 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009030 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009031
9032 cur_arg = 1;
9033
9034 if (!*args[cur_arg] ||
9035 (*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 +01009036 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9037 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009038 goto out_err;
9039 }
9040
9041 LIST_INIT(&rule->arg.map.key);
9042 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009043 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009044 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009045 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009046 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9047 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009048 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009049 goto out_err;
9050 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009051 free(proxy->conf.lfs_file);
9052 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9053 proxy->conf.lfs_line = proxy->conf.args.line;
9054 cur_arg += 1;
9055 } else if (strncmp(args[0], "del-map", 7) == 0) {
9056 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009057 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009058 /*
9059 * '+ 8' for 'del-map('
9060 * '- 9' for 'del-map(' + trailing ')'
9061 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009062 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009063
9064 cur_arg = 1;
9065
9066 if (!*args[cur_arg] ||
9067 (*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 +01009068 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9069 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009070 goto out_err;
9071 }
9072
9073 LIST_INIT(&rule->arg.map.key);
9074 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009075 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009076 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009077 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009078 ha_alert("parsing [%s:%d]: 'http-response %s' %s.\n",
9079 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009080 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009081 goto out_err;
9082 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009083 free(proxy->conf.lfs_file);
9084 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9085 proxy->conf.lfs_line = proxy->conf.args.line;
9086 cur_arg += 1;
9087 } else if (strncmp(args[0], "set-map", 7) == 0) {
9088 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009089 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009090 /*
9091 * '+ 8' for 'set-map('
9092 * '- 9' for 'set-map(' + trailing ')'
9093 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009094 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009095
9096 cur_arg = 1;
9097
9098 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9099 (*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 +01009100 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9101 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009102 goto out_err;
9103 }
9104
9105 LIST_INIT(&rule->arg.map.key);
9106 LIST_INIT(&rule->arg.map.value);
9107
9108 proxy->conf.args.ctx = ARGC_HRS;
9109
9110 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009111 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009112 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009113 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009114 ha_alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
9115 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009116 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009117 goto out_err;
9118 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009119
9120 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009121 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009122 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 +01009123 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009124 ha_alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
9125 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009126 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009127 goto out_err;
9128 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009129
9130 free(proxy->conf.lfs_file);
9131 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9132 proxy->conf.lfs_line = proxy->conf.args.line;
9133
9134 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009135 } else if (strcmp(args[0], "redirect") == 0) {
9136 struct redirect_rule *redir;
9137 char *errmsg = NULL;
9138
9139 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009140 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9141 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
Willy Tarreau51d861a2015-05-22 17:30:48 +02009142 goto out_err;
9143 }
9144
9145 /* this redirect rule might already contain a parsed condition which
9146 * we'll pass to the http-request rule.
9147 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009148 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009149 rule->arg.redir = redir;
9150 rule->cond = redir->cond;
9151 redir->cond = NULL;
9152 cur_arg = 2;
9153 return rule;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009154 } else if (strncmp(args[0], "track-sc", 8) == 0) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009155 struct sample_expr *expr;
9156 unsigned int where;
9157 char *err = NULL;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009158 unsigned int tsc_num;
9159 const char *tsc_num_str;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009160
9161 cur_arg = 1;
9162 proxy->conf.args.ctx = ARGC_TRK;
9163
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009164 tsc_num_str = &args[0][8];
9165 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) {
9166 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9167 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9168 free(err);
9169 goto out_err;
9170 }
9171
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009172 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9173 if (!expr) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009174 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9175 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009176 free(err);
9177 goto out_err;
9178 }
9179
9180 where = 0;
9181 if (proxy->cap & PR_CAP_FE)
9182 where |= SMP_VAL_FE_HRS_HDR;
9183 if (proxy->cap & PR_CAP_BE)
9184 where |= SMP_VAL_BE_HRS_HDR;
9185
9186 if (!(expr->fetch->val & where)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009187 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
9188 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9189 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9190 args[cur_arg-1], sample_src_names(expr->fetch->use));
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009191 free(expr);
9192 goto out_err;
9193 }
9194
9195 if (strcmp(args[cur_arg], "table") == 0) {
9196 cur_arg++;
9197 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009198 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9199 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009200 free(expr);
9201 goto out_err;
9202 }
9203 /* we copy the table name for now, it will be resolved later */
9204 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9205 cur_arg++;
9206 }
9207 rule->arg.trk_ctr.expr = expr;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009208 rule->action = ACT_ACTION_TRK_SC0 + tsc_num;
Christopher Faulet78880fb2017-09-18 14:43:55 +02009209 rule->check_ptr = check_trk_action;
William Lallemand73025dd2014-04-24 14:38:37 +02009210 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9211 char *errmsg = NULL;
9212 cur_arg = 1;
9213 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009214 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009215 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009216 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009217 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9218 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
William Lallemand73025dd2014-04-24 14:38:37 +02009219 free(errmsg);
9220 goto out_err;
9221 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009222 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009223 action_build_list(&http_res_keywords.list, &trash);
Christopher Faulet767a84b2017-11-24 16:50:31 +01009224 ha_alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9225 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
9226 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
9227 "%s%s, but got '%s'%s.\n",
9228 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009229 goto out_err;
9230 }
9231
9232 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9233 struct acl_cond *cond;
9234 char *errmsg = NULL;
9235
Christopher Faulet1b421ea2017-09-22 14:38:56 +02009236 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009237 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9238 file, linenum, args[0], errmsg);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009239 free(errmsg);
9240 goto out_err;
9241 }
9242 rule->cond = cond;
9243 }
9244 else if (*args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009245 ha_alert("parsing [%s:%d]: 'http-response %s' expects"
9246 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9247 file, linenum, args[0], args[cur_arg]);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009248 goto out_err;
9249 }
9250
9251 return rule;
9252 out_err:
9253 free(rule);
9254 return NULL;
9255}
9256
Willy Tarreau4baae242012-12-27 12:00:31 +01009257/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009258 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009259 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9260 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009261 */
9262struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009263 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009264{
9265 struct redirect_rule *rule;
9266 int cur_arg;
9267 int type = REDIRECT_TYPE_NONE;
9268 int code = 302;
9269 const char *destination = NULL;
9270 const char *cookie = NULL;
9271 int cookie_set = 0;
9272 unsigned int flags = REDIRECT_FLAG_NONE;
9273 struct acl_cond *cond = NULL;
9274
9275 cur_arg = 0;
9276 while (*(args[cur_arg])) {
9277 if (strcmp(args[cur_arg], "location") == 0) {
9278 if (!*args[cur_arg + 1])
9279 goto missing_arg;
9280
9281 type = REDIRECT_TYPE_LOCATION;
9282 cur_arg++;
9283 destination = args[cur_arg];
9284 }
9285 else if (strcmp(args[cur_arg], "prefix") == 0) {
9286 if (!*args[cur_arg + 1])
9287 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009288 type = REDIRECT_TYPE_PREFIX;
9289 cur_arg++;
9290 destination = args[cur_arg];
9291 }
9292 else if (strcmp(args[cur_arg], "scheme") == 0) {
9293 if (!*args[cur_arg + 1])
9294 goto missing_arg;
9295
9296 type = REDIRECT_TYPE_SCHEME;
9297 cur_arg++;
9298 destination = args[cur_arg];
9299 }
9300 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9301 if (!*args[cur_arg + 1])
9302 goto missing_arg;
9303
9304 cur_arg++;
9305 cookie = args[cur_arg];
9306 cookie_set = 1;
9307 }
9308 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9309 if (!*args[cur_arg + 1])
9310 goto missing_arg;
9311
9312 cur_arg++;
9313 cookie = args[cur_arg];
9314 cookie_set = 0;
9315 }
9316 else if (strcmp(args[cur_arg], "code") == 0) {
9317 if (!*args[cur_arg + 1])
9318 goto missing_arg;
9319
9320 cur_arg++;
9321 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009322 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009323 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009324 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009325 args[cur_arg - 1], args[cur_arg]);
9326 return NULL;
9327 }
9328 }
9329 else if (!strcmp(args[cur_arg],"drop-query")) {
9330 flags |= REDIRECT_FLAG_DROP_QS;
9331 }
9332 else if (!strcmp(args[cur_arg],"append-slash")) {
9333 flags |= REDIRECT_FLAG_APPEND_SLASH;
9334 }
9335 else if (strcmp(args[cur_arg], "if") == 0 ||
9336 strcmp(args[cur_arg], "unless") == 0) {
Olivier Houchardfbc74e82017-11-24 16:54:05 +01009337 cond = build_acl_cond(file, linenum, &curproxy->acl, curproxy, (const char **)args + cur_arg, errmsg);
Willy Tarreau4baae242012-12-27 12:00:31 +01009338 if (!cond) {
9339 memprintf(errmsg, "error in condition: %s", *errmsg);
9340 return NULL;
9341 }
9342 break;
9343 }
9344 else {
9345 memprintf(errmsg,
9346 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9347 args[cur_arg]);
9348 return NULL;
9349 }
9350 cur_arg++;
9351 }
9352
9353 if (type == REDIRECT_TYPE_NONE) {
9354 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9355 return NULL;
9356 }
9357
Willy Tarreaube4653b2015-05-28 15:26:58 +02009358 if (dir && type != REDIRECT_TYPE_LOCATION) {
9359 memprintf(errmsg, "response only supports redirect type 'location'");
9360 return NULL;
9361 }
9362
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02009363 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +01009364 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009365 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009366
9367 if (!use_fmt) {
9368 /* old-style static redirect rule */
9369 rule->rdr_str = strdup(destination);
9370 rule->rdr_len = strlen(destination);
9371 }
9372 else {
9373 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009374
9375 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9376 * if prefix == "/", we don't want to add anything, otherwise it
9377 * makes it hard for the user to configure a self-redirection.
9378 */
Godbachd9722032014-12-18 15:44:58 +08009379 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009380 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009381 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
9382 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 +01009383 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9384 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009385 return NULL;
9386 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009387 free(curproxy->conf.lfs_file);
9388 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9389 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009390 }
9391 }
9392
Willy Tarreau4baae242012-12-27 12:00:31 +01009393 if (cookie) {
9394 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9395 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9396 */
9397 rule->cookie_len = strlen(cookie);
9398 if (cookie_set) {
9399 rule->cookie_str = malloc(rule->cookie_len + 10);
9400 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9401 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9402 rule->cookie_len += 9;
9403 } else {
9404 rule->cookie_str = malloc(rule->cookie_len + 21);
9405 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9406 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9407 rule->cookie_len += 20;
9408 }
9409 }
9410 rule->type = type;
9411 rule->code = code;
9412 rule->flags = flags;
9413 LIST_INIT(&rule->list);
9414 return rule;
9415
9416 missing_arg:
9417 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9418 return NULL;
9419}
9420
Willy Tarreau8797c062007-05-07 00:55:35 +02009421/************************************************************************/
9422/* The code below is dedicated to ACL parsing and matching */
9423/************************************************************************/
9424
9425
Willy Tarreau14174bc2012-04-16 14:34:04 +02009426/* This function ensures that the prerequisites for an L7 fetch are ready,
9427 * which means that a request or response is ready. If some data is missing,
9428 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009429 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9430 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009431 *
9432 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009433 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9434 * decide whether or not an HTTP message is present ;
9435 * 0 if the requested data cannot be fetched or if it is certain that
9436 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009437 * 1 if an HTTP message is ready
9438 */
James Rosewell91a41cb2015-09-18 17:11:16 +01009439int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009440 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009441{
Willy Tarreau192252e2015-04-04 01:47:55 +02009442 struct http_txn *txn;
9443 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009444
Willy Tarreaube508f12016-03-10 11:47:01 +01009445 /* Note: it is possible that <s> is NULL when called before stream
9446 * initialization (eg: tcp-request connection), so this function is the
9447 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009448 */
Willy Tarreau192252e2015-04-04 01:47:55 +02009449 if (!s)
9450 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01009451
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +02009452 if (!s->txn) {
9453 if (unlikely(!http_alloc_txn(s)))
9454 return 0; /* not enough memory */
9455 http_init_txn(s);
9456 }
Willy Tarreau192252e2015-04-04 01:47:55 +02009457 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +02009458 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009459
9460 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009461 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009462
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009463 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009464 /* If the buffer does not leave enough free space at the end,
9465 * we must first realign it.
9466 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009467 if (s->req.buf->p > s->req.buf->data &&
9468 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
9469 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009470
Willy Tarreau14174bc2012-04-16 14:34:04 +02009471 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009472 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009473 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009474
9475 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009476 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009477 http_msg_analyzer(msg, &txn->hdr_idx);
9478
9479 /* Still no valid request ? */
9480 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009481 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009482 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009483 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009484 }
9485 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009486 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009487 return 0;
9488 }
9489
9490 /* OK we just got a valid HTTP request. We have some minor
9491 * preparation to perform so that further checks can rely
9492 * on HTTP tests.
9493 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009494
9495 /* If the request was parsed but was too large, we must absolutely
9496 * return an error so that it is not processed. At the moment this
9497 * cannot happen, but if the parsers are to change in the future,
9498 * we want this check to be maintained.
9499 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009500 if (unlikely(s->req.buf->i + s->req.buf->p >
9501 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01009502 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009503 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009504 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009505 return 1;
9506 }
9507
Willy Tarreau9b28e032012-10-12 23:49:43 +02009508 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009509 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02009510 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009511
Willy Tarreau506d0502013-07-06 13:29:24 +02009512 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9513 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009514 }
9515
Willy Tarreau506d0502013-07-06 13:29:24 +02009516 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009517 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009518 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009519
9520 /* otherwise everything's ready for the request */
9521 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009522 else {
9523 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009524 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9525 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009526 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009527 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009528 }
9529
9530 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009531 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009532 return 1;
9533}
Willy Tarreau8797c062007-05-07 00:55:35 +02009534
Willy Tarreau8797c062007-05-07 00:55:35 +02009535/* 1. Check on METHOD
9536 * We use the pre-parsed method if it is known, and store its number as an
9537 * integer. If it is unknown, we use the pointer and the length.
9538 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009539static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009540{
9541 int len, meth;
9542
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009543 len = strlen(text);
9544 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009545
9546 pattern->val.i = meth;
9547 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +02009548 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009549 pattern->len = len;
9550 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009551 else {
9552 pattern->ptr.str = NULL;
9553 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009554 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009555 return 1;
9556}
9557
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009558/* This function fetches the method of current HTTP request and stores
9559 * it in the global pattern struct as a chunk. There are two possibilities :
9560 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9561 * in <len> and <ptr> is NULL ;
9562 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9563 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009564 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009565 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009566static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009567smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009568{
9569 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +01009570 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009571
Willy Tarreau24e32d82012-04-23 23:55:44 +02009572 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009573
Willy Tarreaube508f12016-03-10 11:47:01 +01009574 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009575 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009576 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009577 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009578 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009579 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9580 /* ensure the indexes are not affected */
9581 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009582 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009583 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
9584 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009585 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009586 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009587 return 1;
9588}
9589
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009590/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009591static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009592{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009593 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009594 struct pattern_list *lst;
9595 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009596
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009597 list_for_each_entry(lst, &expr->patterns, list) {
9598 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009599
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009600 /* well-known method */
9601 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009602 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009603 return pattern;
9604 else
9605 continue;
9606 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009607
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009608 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009609 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009610 continue;
9611
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009612 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009613 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
9614 (!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 +01009615 return pattern;
9616 }
9617 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009618}
9619
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009620static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009621smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009622{
Willy Tarreaube508f12016-03-10 11:47:01 +01009623 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009624 char *ptr;
9625 int len;
9626
Willy Tarreauc0239e02012-04-16 14:42:55 +02009627 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009628
Willy Tarreaube508f12016-03-10 11:47:01 +01009629 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009630 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009631 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009632
9633 while ((len-- > 0) && (*ptr++ != '/'));
9634 if (len <= 0)
9635 return 0;
9636
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009637 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009638 smp->data.u.str.str = ptr;
9639 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009640
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009641 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009642 return 1;
9643}
9644
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009645static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009646smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009647{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009648 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009649 char *ptr;
9650 int len;
9651
Willy Tarreauc0239e02012-04-16 14:42:55 +02009652 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009653
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009654 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009655 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9656 return 0;
9657
Willy Tarreau8797c062007-05-07 00:55:35 +02009658 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009659 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009660
9661 while ((len-- > 0) && (*ptr++ != '/'));
9662 if (len <= 0)
9663 return 0;
9664
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009665 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009666 smp->data.u.str.str = ptr;
9667 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009668
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009669 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009670 return 1;
9671}
9672
9673/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009674static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009675smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009676{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009677 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009678 char *ptr;
9679 int len;
9680
Willy Tarreauc0239e02012-04-16 14:42:55 +02009681 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009682
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009683 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009684 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9685 return 0;
9686
Willy Tarreau8797c062007-05-07 00:55:35 +02009687 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009688 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009689
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009690 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009691 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009692 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009693 return 1;
9694}
9695
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009696static int
9697smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
9698{
9699 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
9700 return 0;
9701
9702 if (!smp->strm->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01009703 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009704 return 0;
9705 smp->strm->unique_id[0] = '\0';
9706 }
9707 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
9708 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
9709
9710 smp->data.type = SMP_T_STR;
9711 smp->data.u.str.str = smp->strm->unique_id;
9712 smp->flags = SMP_F_CONST;
9713 return 1;
9714}
9715
Thierry FOURNIERd7d88812017-04-19 15:15:14 +02009716/* Returns a string block containing all headers including the
9717 * empty line wich separes headers from the body. This is useful
9718 * form some headers analysis.
9719 */
9720static int
9721smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
9722{
9723 struct http_msg *msg;
9724 struct hdr_idx *idx;
9725 struct http_txn *txn;
9726
9727 CHECK_HTTP_MESSAGE_FIRST();
9728
9729 txn = smp->strm->txn;
9730 idx = &txn->hdr_idx;
9731 msg = &txn->req;
9732
9733 smp->data.type = SMP_T_STR;
9734 smp->data.u.str.str = msg->chn->buf->p + hdr_idx_first_pos(idx);
9735 smp->data.u.str.len = msg->eoh - hdr_idx_first_pos(idx) + 1 +
9736 (msg->chn->buf->p[msg->eoh] == '\r');
9737
9738 return 1;
9739}
9740
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009741/* Returns the header request in a length/value encoded format.
9742 * This is useful for exchanges with the SPOE.
9743 *
9744 * A "length value" is a multibyte code encoding numbers. It uses the
9745 * SPOE format. The encoding is the following:
9746 *
9747 * Each couple "header name" / "header value" is composed
9748 * like this:
9749 * "length value" "header name bytes"
9750 * "length value" "header value bytes"
9751 * When the last header is reached, the header name and the header
9752 * value are empty. Their length are 0
9753 */
9754static int
9755smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
9756{
9757 struct http_msg *msg;
9758 struct chunk *temp;
9759 struct hdr_idx *idx;
9760 const char *cur_ptr, *cur_next, *p;
9761 int old_idx, cur_idx;
9762 struct hdr_idx_elem *cur_hdr;
9763 const char *hn, *hv;
9764 int hnl, hvl;
9765 int ret;
9766 struct http_txn *txn;
9767 char *buf;
9768 char *end;
9769
9770 CHECK_HTTP_MESSAGE_FIRST();
9771
9772 temp = get_trash_chunk();
9773 buf = temp->str;
9774 end = temp->str + temp->size;
9775
9776 txn = smp->strm->txn;
9777 idx = &txn->hdr_idx;
9778 msg = &txn->req;
9779
9780 /* Build array of headers. */
9781 old_idx = 0;
9782 cur_next = msg->chn->buf->p + hdr_idx_first_pos(idx);
9783 while (1) {
9784 cur_idx = idx->v[old_idx].next;
9785 if (!cur_idx)
9786 break;
9787 old_idx = cur_idx;
9788
9789 cur_hdr = &idx->v[cur_idx];
9790 cur_ptr = cur_next;
9791 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
9792
9793 /* Now we have one full header at cur_ptr of len cur_hdr->len,
9794 * and the next header starts at cur_next. We'll check
9795 * this header in the list as well as against the default
9796 * rule.
9797 */
9798
9799 /* look for ': *'. */
9800 hn = cur_ptr;
9801 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
9802 if (p >= cur_ptr+cur_hdr->len)
9803 continue;
9804 hnl = p - hn;
9805 p++;
9806 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
9807 p++;
9808 if (p >= cur_ptr + cur_hdr->len)
9809 continue;
9810 hv = p;
9811 hvl = cur_ptr + cur_hdr->len-p;
9812
9813 /* encode the header name. */
9814 ret = encode_varint(hnl, &buf, end);
9815 if (ret == -1)
9816 return 0;
9817 if (buf + hnl > end)
9818 return 0;
9819 memcpy(buf, hn, hnl);
9820 buf += hnl;
9821
9822 /* encode and copy the value. */
9823 ret = encode_varint(hvl, &buf, end);
9824 if (ret == -1)
9825 return 0;
9826 if (buf + hvl > end)
9827 return 0;
9828 memcpy(buf, hv, hvl);
9829 buf += hvl;
9830 }
9831
9832 /* encode the end of the header list with empty
9833 * header name and header value.
9834 */
9835 ret = encode_varint(0, &buf, end);
9836 if (ret == -1)
9837 return 0;
9838 ret = encode_varint(0, &buf, end);
9839 if (ret == -1)
9840 return 0;
9841
9842 /* Initialise sample data which will be filled. */
9843 smp->data.type = SMP_T_BIN;
9844 smp->data.u.str.str = temp->str;
9845 smp->data.u.str.len = buf - temp->str;
9846 smp->data.u.str.size = temp->size;
9847
9848 return 1;
9849}
9850
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009851/* returns the longest available part of the body. This requires that the body
9852 * has been waited for using http-buffer-request.
9853 */
9854static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009855smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009856{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009857 struct http_msg *msg;
9858 unsigned long len;
9859 unsigned long block1;
9860 char *body;
9861 struct chunk *temp;
9862
9863 CHECK_HTTP_MESSAGE_FIRST();
9864
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009865 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009866 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009867 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009868 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009869
9870 len = http_body_bytes(msg);
9871 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
9872
9873 block1 = len;
9874 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
9875 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
9876
9877 if (block1 == len) {
9878 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009879 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009880 smp->data.u.str.str = body;
9881 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009882 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
9883 }
9884 else {
9885 /* buffer is wrapped, we need to defragment it */
9886 temp = get_trash_chunk();
9887 memcpy(temp->str, body, block1);
9888 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009889 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009890 smp->data.u.str.str = temp->str;
9891 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009892 smp->flags = SMP_F_VOL_TEST;
9893 }
9894 return 1;
9895}
9896
9897
9898/* returns the available length of the body. This requires that the body
9899 * has been waited for using http-buffer-request.
9900 */
9901static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009902smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009903{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009904 struct http_msg *msg;
9905
9906 CHECK_HTTP_MESSAGE_FIRST();
9907
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009908 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009909 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009910 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009911 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009912
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009913 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009914 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009915
9916 smp->flags = SMP_F_VOL_TEST;
9917 return 1;
9918}
9919
9920
9921/* returns the advertised length of the body, or the advertised size of the
9922 * chunks available in the buffer. This requires that the body has been waited
9923 * for using http-buffer-request.
9924 */
9925static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009926smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009927{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009928 struct http_msg *msg;
9929
9930 CHECK_HTTP_MESSAGE_FIRST();
9931
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009932 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009933 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009934 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009935 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009936
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009937 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009938 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009939
9940 smp->flags = SMP_F_VOL_TEST;
9941 return 1;
9942}
9943
9944
Willy Tarreau8797c062007-05-07 00:55:35 +02009945/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009946static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009947smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009948{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009949 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009950
Willy Tarreauc0239e02012-04-16 14:42:55 +02009951 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009952
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009953 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009954 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009955 smp->data.u.str.len = txn->req.sl.rq.u_l;
9956 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009957 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009958 return 1;
9959}
9960
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009961static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009962smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009963{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009964 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009965 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009966
Willy Tarreauc0239e02012-04-16 14:42:55 +02009967 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009968
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009969 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009970 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 +02009971 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009972 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009973
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009974 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009975 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009976 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009977 return 1;
9978}
9979
9980static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009981smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009982{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009983 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009984 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009985
Willy Tarreauc0239e02012-04-16 14:42:55 +02009986 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009987
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009988 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009989 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 +02009990 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9991 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009992
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009993 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009994 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009995 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009996 return 1;
9997}
9998
Willy Tarreau185b5c42012-04-26 15:11:51 +02009999/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10000 * Accepts an optional argument of type string containing the header field name,
10001 * and an optional argument of type signed or unsigned integer to request an
10002 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010003 * headers are considered from the first one. It does not stop on commas and
10004 * returns full lines instead (useful for User-Agent or Date for example).
10005 */
10006static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010007smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010008{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010009 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010010 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010011 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010012 int occ = 0;
10013 const char *name_str = NULL;
10014 int name_len = 0;
10015
10016 if (!ctx) {
10017 /* first call */
10018 ctx = &static_hdr_ctx;
10019 ctx->idx = 0;
10020 smp->ctx.a[0] = ctx;
10021 }
10022
10023 if (args) {
10024 if (args[0].type != ARGT_STR)
10025 return 0;
10026 name_str = args[0].data.str.str;
10027 name_len = args[0].data.str.len;
10028
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010029 if (args[1].type == ARGT_SINT)
10030 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010031 }
10032
10033 CHECK_HTTP_MESSAGE_FIRST();
10034
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010035 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010036 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 +020010037
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010038 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10039 /* search for header from the beginning */
10040 ctx->idx = 0;
10041
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010042 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010043 /* no explicit occurrence and single fetch => last header by default */
10044 occ = -1;
10045
10046 if (!occ)
10047 /* prepare to report multiple occurrences for ACL fetches */
10048 smp->flags |= SMP_F_NOT_LAST;
10049
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010050 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010051 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010052 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 +020010053 return 1;
10054
10055 smp->flags &= ~SMP_F_NOT_LAST;
10056 return 0;
10057}
10058
10059/* 6. Check on HTTP header count. The number of occurrences is returned.
10060 * Accepts exactly 1 argument of type string. It does not stop on commas and
10061 * returns full lines instead (useful for User-Agent or Date for example).
10062 */
10063static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010064smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010065{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010066 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010067 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010068 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010069 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010070 const char *name = NULL;
10071 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010072
Willy Tarreau601a4d12015-04-01 19:16:09 +020010073 if (args && args->type == ARGT_STR) {
10074 name = args->data.str.str;
10075 len = args->data.str.len;
10076 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010077
10078 CHECK_HTTP_MESSAGE_FIRST();
10079
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010080 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010081 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 +020010082
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010083 ctx.idx = 0;
10084 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010085 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010086 cnt++;
10087
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010088 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010089 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010090 smp->flags = SMP_F_VOL_HDR;
10091 return 1;
10092}
10093
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010094static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010095smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010096{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010097 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010098 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010099 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010100 struct chunk *temp;
10101 char del = ',';
10102
10103 if (args && args->type == ARGT_STR)
10104 del = *args[0].data.str.str;
10105
10106 CHECK_HTTP_MESSAGE_FIRST();
10107
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010108 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010109 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 +020010110
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010111 temp = get_trash_chunk();
10112
10113 ctx.idx = 0;
10114 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10115 if (temp->len)
10116 temp->str[temp->len++] = del;
10117 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10118 temp->len += ctx.del;
10119 }
10120
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010121 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010122 smp->data.u.str.str = temp->str;
10123 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010124 smp->flags = SMP_F_VOL_HDR;
10125 return 1;
10126}
10127
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010128/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10129 * Accepts an optional argument of type string containing the header field name,
10130 * and an optional argument of type signed or unsigned integer to request an
10131 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010132 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010133 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010134static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010135smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010136{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010137 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010138 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010139 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010140 int occ = 0;
10141 const char *name_str = NULL;
10142 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010143
Willy Tarreaua890d072013-04-02 12:01:06 +020010144 if (!ctx) {
10145 /* first call */
10146 ctx = &static_hdr_ctx;
10147 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010148 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010149 }
10150
Willy Tarreau185b5c42012-04-26 15:11:51 +020010151 if (args) {
10152 if (args[0].type != ARGT_STR)
10153 return 0;
10154 name_str = args[0].data.str.str;
10155 name_len = args[0].data.str.len;
10156
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010157 if (args[1].type == ARGT_SINT)
10158 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010159 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010160
Willy Tarreaue333ec92012-04-16 16:26:40 +020010161 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010162
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010163 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010164 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 +020010165
Willy Tarreau185b5c42012-04-26 15:11:51 +020010166 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010167 /* search for header from the beginning */
10168 ctx->idx = 0;
10169
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010170 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010171 /* no explicit occurrence and single fetch => last header by default */
10172 occ = -1;
10173
10174 if (!occ)
10175 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010176 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010177
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010178 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010179 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010180 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 +020010181 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010182
Willy Tarreau37406352012-04-23 16:16:37 +020010183 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010184 return 0;
10185}
10186
Willy Tarreauc11416f2007-06-17 16:58:38 +020010187/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010188 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010189 */
10190static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010191smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010192{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010193 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010194 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010195 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010196 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010197 const char *name = NULL;
10198 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010199
Willy Tarreau601a4d12015-04-01 19:16:09 +020010200 if (args && args->type == ARGT_STR) {
10201 name = args->data.str.str;
10202 len = args->data.str.len;
10203 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010204
Willy Tarreaue333ec92012-04-16 16:26:40 +020010205 CHECK_HTTP_MESSAGE_FIRST();
10206
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010207 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010208 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 +020010209
Willy Tarreau33a7e692007-06-10 19:45:56 +020010210 ctx.idx = 0;
10211 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010212 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010213 cnt++;
10214
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010215 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010216 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010217 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010218 return 1;
10219}
10220
Willy Tarreau185b5c42012-04-26 15:11:51 +020010221/* Fetch an HTTP header's integer value. The integer value is returned. It
10222 * takes a mandatory argument of type string and an optional one of type int
10223 * to designate a specific occurrence. It returns an unsigned integer, which
10224 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010225 */
10226static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010227smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010228{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010229 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010230
Willy Tarreauf853c462012-04-23 18:53:56 +020010231 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010232 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010233 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010234 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010235
Willy Tarreaud53e2422012-04-16 17:21:11 +020010236 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010237}
10238
Cyril Bonté69fa9922012-10-25 00:01:06 +020010239/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10240 * and an optional one of type int to designate a specific occurrence.
10241 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010242 */
10243static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010244smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010245{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010246 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010247
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010248 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010249 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010250 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010251 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010252 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010253 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010254 if (smp->data.u.str.len < temp->size - 1) {
10255 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10256 temp->str[smp->data.u.str.len] = '\0';
10257 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010258 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010259 break;
10260 }
10261 }
10262 }
10263
Willy Tarreaud53e2422012-04-16 17:21:11 +020010264 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010265 if (!(smp->flags & SMP_F_NOT_LAST))
10266 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010267 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010268 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010269}
10270
Willy Tarreau737b0c12007-06-10 21:28:46 +020010271/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10272 * the first '/' after the possible hostname, and ends before the possible '?'.
10273 */
10274static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010275smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010276{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010277 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010278 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010279
Willy Tarreauc0239e02012-04-16 14:42:55 +020010280 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010281
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010282 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010283 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010284 ptr = http_get_path(txn);
10285 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010286 return 0;
10287
10288 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010289 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010290 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010291
10292 while (ptr < end && *ptr != '?')
10293 ptr++;
10294
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010295 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010296 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010297 return 1;
10298}
10299
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010300/* This produces a concatenation of the first occurrence of the Host header
10301 * followed by the path component if it begins with a slash ('/'). This means
10302 * that '*' will not be added, resulting in exactly the first Host entry.
10303 * If no Host header is found, then the path is returned as-is. The returned
10304 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010305 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010306 */
10307static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010308smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010309{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010310 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010311 char *ptr, *end, *beg;
10312 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010313 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010314
10315 CHECK_HTTP_MESSAGE_FIRST();
10316
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010317 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010318 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010319 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010320 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010321
10322 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010323 temp = get_trash_chunk();
10324 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010325 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010326 smp->data.u.str.str = temp->str;
10327 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010328
10329 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010330 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010331 beg = http_get_path(txn);
10332 if (!beg)
10333 beg = end;
10334
10335 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10336
10337 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010338 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10339 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010340 }
10341
10342 smp->flags = SMP_F_VOL_1ST;
10343 return 1;
10344}
10345
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010346/* This produces a 32-bit hash of the concatenation of the first occurrence of
10347 * the Host header followed by the path component if it begins with a slash ('/').
10348 * This means that '*' will not be added, resulting in exactly the first Host
10349 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010350 * is hashed using the path hash followed by a full avalanche hash and provides a
10351 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010352 * high-traffic sites without having to store whole paths.
10353 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010354int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010355smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010356{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010357 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010358 struct hdr_ctx ctx;
10359 unsigned int hash = 0;
10360 char *ptr, *beg, *end;
10361 int len;
10362
10363 CHECK_HTTP_MESSAGE_FIRST();
10364
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010365 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010366 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010367 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010368 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10369 ptr = ctx.line + ctx.val;
10370 len = ctx.vlen;
10371 while (len--)
10372 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10373 }
10374
10375 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010376 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010377 beg = http_get_path(txn);
10378 if (!beg)
10379 beg = end;
10380
10381 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10382
10383 if (beg < ptr && *beg == '/') {
10384 while (beg < ptr)
10385 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10386 }
10387 hash = full_hash(hash);
10388
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010389 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010390 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010391 smp->flags = SMP_F_VOL_1ST;
10392 return 1;
10393}
10394
Willy Tarreau4a550602012-12-09 14:53:32 +010010395/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010396 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10397 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10398 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010399 * that in environments where IPv6 is insignificant, truncating the output to
10400 * 8 bytes would still work.
10401 */
10402static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010403smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010404{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010405 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010406 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010407
10408 if (!cli_conn)
10409 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010410
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010411 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010412 return 0;
10413
Willy Tarreau47ca5452012-12-23 20:22:19 +010010414 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010415 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010416 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010417
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010418 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010419 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010420 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010421 temp->len += 4;
10422 break;
10423 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010424 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010425 temp->len += 16;
10426 break;
10427 default:
10428 return 0;
10429 }
10430
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010431 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010432 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010433 return 1;
10434}
10435
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010436/* Extracts the query string, which comes after the question mark '?'. If no
10437 * question mark is found, nothing is returned. Otherwise it returns a sample
10438 * of type string carrying the whole query string.
10439 */
10440static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010441smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010442{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010443 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010444 char *ptr, *end;
10445
10446 CHECK_HTTP_MESSAGE_FIRST();
10447
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010448 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010449 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10450 end = ptr + txn->req.sl.rq.u_l;
10451
10452 /* look up the '?' */
10453 do {
10454 if (ptr == end)
10455 return 0;
10456 } while (*ptr++ != '?');
10457
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010458 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010459 smp->data.u.str.str = ptr;
10460 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010461 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10462 return 1;
10463}
10464
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010465static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010466smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010467{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010468 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10469 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10470 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010471
Willy Tarreau24e32d82012-04-23 23:55:44 +020010472 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010473
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010474 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010475 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010476 return 1;
10477}
10478
Willy Tarreau7f18e522010-10-22 20:04:13 +020010479/* return a valid test if the current request is the first one on the connection */
10480static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010481smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010482{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010483 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010484 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010485 return 1;
10486}
10487
Willy Tarreau34db1082012-04-19 17:16:54 +020010488/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010489static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010490smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010491{
10492
Willy Tarreau24e32d82012-04-23 23:55:44 +020010493 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010494 return 0;
10495
Willy Tarreauc0239e02012-04-16 14:42:55 +020010496 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010497
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010498 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010499 return 0;
10500
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010501 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010502 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010503 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010504 return 1;
10505}
Willy Tarreau8797c062007-05-07 00:55:35 +020010506
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010507/* Accepts exactly 1 argument of type userlist */
10508static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010509smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010510{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010511 if (!args || args->type != ARGT_USR)
10512 return 0;
10513
10514 CHECK_HTTP_MESSAGE_FIRST();
10515
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010516 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010517 return 0;
10518
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010519 /* if the user does not belong to the userlist or has a wrong password,
10520 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010521 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010522 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010523 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10524 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010525 return 0;
10526
10527 /* pat_match_auth() will need the user list */
10528 smp->ctx.a[0] = args->data.usr;
10529
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010530 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010531 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010532 smp->data.u.str.str = smp->strm->txn->auth.user;
10533 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010534
10535 return 1;
10536}
10537
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010538/* Try to find the next occurrence of a cookie name in a cookie header value.
10539 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10540 * the cookie value is returned into *value and *value_l, and the function
10541 * returns a pointer to the next pointer to search from if the value was found.
10542 * Otherwise if the cookie was not found, NULL is returned and neither value
10543 * nor value_l are touched. The input <hdr> string should first point to the
10544 * header's value, and the <hdr_end> pointer must point to the first character
10545 * not part of the value. <list> must be non-zero if value may represent a list
10546 * of values (cookie headers). This makes it faster to abort parsing when no
10547 * list is expected.
10548 */
David Carlier4686f792015-09-25 14:10:50 +010010549char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010550extract_cookie_value(char *hdr, const char *hdr_end,
10551 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010552 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010553{
10554 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10555 char *next;
10556
10557 /* we search at least a cookie name followed by an equal, and more
10558 * generally something like this :
10559 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10560 */
10561 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10562 /* Iterate through all cookies on this line */
10563
Willy Tarreau2235b262016-11-05 15:50:20 +010010564 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010565 att_beg++;
10566
10567 /* find att_end : this is the first character after the last non
10568 * space before the equal. It may be equal to hdr_end.
10569 */
10570 equal = att_end = att_beg;
10571
10572 while (equal < hdr_end) {
10573 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10574 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010010575 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010576 continue;
10577 att_end = equal;
10578 }
10579
10580 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10581 * is between <att_beg> and <equal>, both may be identical.
10582 */
10583
10584 /* look for end of cookie if there is an equal sign */
10585 if (equal < hdr_end && *equal == '=') {
10586 /* look for the beginning of the value */
10587 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010010588 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010589 val_beg++;
10590
10591 /* find the end of the value, respecting quotes */
10592 next = find_cookie_value_end(val_beg, hdr_end);
10593
10594 /* make val_end point to the first white space or delimitor after the value */
10595 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010010596 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010597 val_end--;
10598 } else {
10599 val_beg = val_end = next = equal;
10600 }
10601
10602 /* We have nothing to do with attributes beginning with '$'. However,
10603 * they will automatically be removed if a header before them is removed,
10604 * since they're supposed to be linked together.
10605 */
10606 if (*att_beg == '$')
10607 continue;
10608
10609 /* Ignore cookies with no equal sign */
10610 if (equal == next)
10611 continue;
10612
10613 /* Now we have the cookie name between att_beg and att_end, and
10614 * its value between val_beg and val_end.
10615 */
10616
10617 if (att_end - att_beg == cookie_name_l &&
10618 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10619 /* let's return this value and indicate where to go on from */
10620 *value = val_beg;
10621 *value_l = val_end - val_beg;
10622 return next + 1;
10623 }
10624
10625 /* Set-Cookie headers only have the name in the first attr=value part */
10626 if (!list)
10627 break;
10628 }
10629
10630 return NULL;
10631}
10632
William Lallemanda43ba4e2014-01-28 18:14:25 +010010633/* Fetch a captured HTTP request header. The index is the position of
10634 * the "capture" option in the configuration file
10635 */
10636static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010637smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010638{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010639 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010640 int idx;
10641
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010642 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010643 return 0;
10644
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010645 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010646
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010647 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 +010010648 return 0;
10649
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010650 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010651 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010652 smp->data.u.str.str = smp->strm->req_cap[idx];
10653 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010654
10655 return 1;
10656}
10657
10658/* Fetch a captured HTTP response header. The index is the position of
10659 * the "capture" option in the configuration file
10660 */
10661static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010662smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010663{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010664 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010665 int idx;
10666
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010667 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010668 return 0;
10669
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010670 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010671
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010672 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 +010010673 return 0;
10674
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010675 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010676 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010677 smp->data.u.str.str = smp->strm->res_cap[idx];
10678 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010679
10680 return 1;
10681}
10682
William Lallemand65ad6e12014-01-31 15:08:02 +010010683/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10684static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010685smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010686{
10687 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010688 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010010689 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010690
Willy Tarreau15e91e12015-04-04 00:52:09 +020010691 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010692 return 0;
10693
William Lallemand96a77852014-02-05 00:30:02 +010010694 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010695
William Lallemand96a77852014-02-05 00:30:02 +010010696 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10697 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010698
William Lallemand96a77852014-02-05 00:30:02 +010010699 temp = get_trash_chunk();
10700 temp->str = txn->uri;
10701 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010702 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010703 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010704 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010705
10706 return 1;
10707
10708}
10709
10710/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10711static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010712smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010713{
10714 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010715 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010010716 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010717
Willy Tarreau15e91e12015-04-04 00:52:09 +020010718 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010719 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010720
William Lallemand65ad6e12014-01-31 15:08:02 +010010721 ptr = txn->uri;
10722
10723 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10724 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010725
William Lallemand65ad6e12014-01-31 15:08:02 +010010726 if (!*ptr)
10727 return 0;
10728
10729 ptr++; /* skip the space */
10730
10731 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010732 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010733 if (!ptr)
10734 return 0;
10735 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10736 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010737
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010738 smp->data.u.str = *temp;
10739 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010740 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010741 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010742
10743 return 1;
10744}
10745
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010746/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10747 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10748 */
10749static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010750smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010751{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010752 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010753
Willy Tarreau15e91e12015-04-04 00:52:09 +020010754 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010755 return 0;
10756
10757 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010758 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010759 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010760 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010761
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010762 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010763 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010764 smp->flags = SMP_F_CONST;
10765 return 1;
10766
10767}
10768
10769/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10770 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10771 */
10772static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010773smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010774{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010775 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010776
Willy Tarreau15e91e12015-04-04 00:52:09 +020010777 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010778 return 0;
10779
10780 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010781 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010782 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010783 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010784
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010785 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010786 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010787 smp->flags = SMP_F_CONST;
10788 return 1;
10789
10790}
10791
William Lallemand65ad6e12014-01-31 15:08:02 +010010792
Willy Tarreaue333ec92012-04-16 16:26:40 +020010793/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010794 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010795 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010796 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010797 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010798 * Accepts exactly 1 argument of type string. If the input options indicate
10799 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010800 * The returned sample is of type CSTR. Can be used to parse cookies in other
10801 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010802 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010803int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010804{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010805 struct http_txn *txn;
10806 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010807 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010808 const struct http_msg *msg;
10809 const char *hdr_name;
10810 int hdr_name_len;
10811 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010812 int occ = 0;
10813 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010814
Willy Tarreau24e32d82012-04-23 23:55:44 +020010815 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010816 return 0;
10817
Willy Tarreaua890d072013-04-02 12:01:06 +020010818 if (!ctx) {
10819 /* first call */
10820 ctx = &static_hdr_ctx;
10821 ctx->idx = 0;
10822 smp->ctx.a[2] = ctx;
10823 }
10824
Willy Tarreaue333ec92012-04-16 16:26:40 +020010825 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010826
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010827 txn = smp->strm->txn;
10828 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010829
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010830 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010831 msg = &txn->req;
10832 hdr_name = "Cookie";
10833 hdr_name_len = 6;
10834 } else {
10835 msg = &txn->rsp;
10836 hdr_name = "Set-Cookie";
10837 hdr_name_len = 10;
10838 }
10839
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010840 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020010841 /* no explicit occurrence and single fetch => last cookie by default */
10842 occ = -1;
10843
10844 /* OK so basically here, either we want only one value and it's the
10845 * last one, or we want to iterate over all of them and we fetch the
10846 * next one.
10847 */
10848
Willy Tarreau9b28e032012-10-12 23:49:43 +020010849 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010850 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010851 /* search for the header from the beginning, we must first initialize
10852 * the search parameters.
10853 */
Willy Tarreau37406352012-04-23 16:16:37 +020010854 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010855 ctx->idx = 0;
10856 }
10857
Willy Tarreau28376d62012-04-26 21:26:10 +020010858 smp->flags |= SMP_F_VOL_HDR;
10859
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010860 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010861 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10862 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010863 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10864 goto out;
10865
Willy Tarreau24e32d82012-04-23 23:55:44 +020010866 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010867 continue;
10868
Willy Tarreau37406352012-04-23 16:16:37 +020010869 smp->ctx.a[0] = ctx->line + ctx->val;
10870 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010871 }
10872
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010873 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010874 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010875 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010876 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010877 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010878 &smp->data.u.str.str,
10879 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010880 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010881 found = 1;
10882 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010883 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020010884 smp->flags |= SMP_F_NOT_LAST;
10885 return 1;
10886 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010887 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010888 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010889 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010890 /* all cookie headers and values were scanned. If we're looking for the
10891 * last occurrence, we may return it now.
10892 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010893 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010894 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010895 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010896}
10897
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010898/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010899 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010900 * multiple cookies may be parsed on the same line. The returned sample is of
10901 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010902 */
10903static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010904smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010905{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010906 struct http_txn *txn;
10907 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010908 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010909 const struct http_msg *msg;
10910 const char *hdr_name;
10911 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010912 int cnt;
10913 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010914 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010915
Willy Tarreau24e32d82012-04-23 23:55:44 +020010916 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010917 return 0;
10918
Willy Tarreaue333ec92012-04-16 16:26:40 +020010919 CHECK_HTTP_MESSAGE_FIRST();
10920
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010921 txn = smp->strm->txn;
10922 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010923
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010924 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010925 msg = &txn->req;
10926 hdr_name = "Cookie";
10927 hdr_name_len = 6;
10928 } else {
10929 msg = &txn->rsp;
10930 hdr_name = "Set-Cookie";
10931 hdr_name_len = 10;
10932 }
10933
Willy Tarreau9b28e032012-10-12 23:49:43 +020010934 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010935 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010936 ctx.idx = 0;
10937 cnt = 0;
10938
10939 while (1) {
10940 /* Note: val_beg == NULL every time we need to fetch a new header */
10941 if (!val_beg) {
10942 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10943 break;
10944
Willy Tarreau24e32d82012-04-23 23:55:44 +020010945 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010946 continue;
10947
10948 val_beg = ctx.line + ctx.val;
10949 val_end = val_beg + ctx.vlen;
10950 }
10951
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010952 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010953 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010954 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010955 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010956 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010957 &smp->data.u.str.str,
10958 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010959 cnt++;
10960 }
10961 }
10962
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010963 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010964 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010965 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010966 return 1;
10967}
10968
Willy Tarreau51539362012-05-08 12:46:28 +020010969/* Fetch an cookie's integer value. The integer value is returned. It
10970 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10971 */
10972static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010973smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020010974{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010975 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020010976
10977 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010978 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010979 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020010980 }
10981
10982 return ret;
10983}
10984
Willy Tarreau8797c062007-05-07 00:55:35 +020010985/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010986/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010987/************************************************************************/
10988
David Cournapeau16023ee2010-12-23 20:55:41 +090010989/*
10990 * Given a path string and its length, find the position of beginning of the
10991 * query string. Returns NULL if no query string is found in the path.
10992 *
10993 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10994 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010995 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090010996 */
bedis4c75cca2012-10-05 08:38:24 +020010997static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010998{
10999 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011000
bedis4c75cca2012-10-05 08:38:24 +020011001 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011002 return p ? p + 1 : NULL;
11003}
11004
bedis4c75cca2012-10-05 08:38:24 +020011005static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011006{
bedis4c75cca2012-10-05 08:38:24 +020011007 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011008}
11009
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011010/* after increasing a pointer value, it can exceed the first buffer
11011 * size. This function transform the value of <ptr> according with
11012 * the expected position. <chunks> is an array of the one or two
11013 * avalaible chunks. The first value is the start of the first chunk,
11014 * the second value if the end+1 of the first chunks. The third value
11015 * is NULL or the start of the second chunk and the fourth value is
11016 * the end+1 of the second chunk. The function returns 1 if does a
11017 * wrap, else returns 0.
11018 */
11019static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11020{
11021 if (*ptr < chunks[1])
11022 return 0;
11023 if (!chunks[2])
11024 return 0;
11025 *ptr = chunks[2] + ( *ptr - chunks[1] );
11026 return 1;
11027}
11028
David Cournapeau16023ee2010-12-23 20:55:41 +090011029/*
11030 * Given a url parameter, find the starting position of the first occurence,
11031 * or NULL if the parameter is not found.
11032 *
11033 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11034 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011035 *
Willy Tarreauf6625822015-12-27 14:51:01 +010011036 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011037 * or the second chunk. The caller must be check the position before using the
11038 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011039 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011040static const char *
11041find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011042 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011043 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011044{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011045 const char *pos, *last, *equal;
11046 const char **bufs = chunks;
11047 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011048
David Cournapeau16023ee2010-12-23 20:55:41 +090011049
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011050 pos = bufs[0];
11051 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010011052 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011053 /* Check the equal. */
11054 equal = pos + url_param_name_l;
11055 if (fix_pointer_if_wrap(chunks, &equal)) {
11056 if (equal >= chunks[3])
11057 return NULL;
11058 } else {
11059 if (equal >= chunks[1])
11060 return NULL;
11061 }
11062 if (*equal == '=') {
11063 if (pos + url_param_name_l > last) {
11064 /* process wrap case, we detect a wrap. In this case, the
11065 * comparison is performed in two parts.
11066 */
11067
11068 /* This is the end, we dont have any other chunk. */
11069 if (bufs != chunks || !bufs[2])
11070 return NULL;
11071
11072 /* Compute the length of each part of the comparison. */
11073 l1 = last - pos;
11074 l2 = url_param_name_l - l1;
11075
11076 /* The second buffer is too short to contain the compared string. */
11077 if (bufs[2] + l2 > bufs[3])
11078 return NULL;
11079
11080 if (memcmp(pos, url_param_name, l1) == 0 &&
11081 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11082 return pos;
11083
11084 /* Perform wrapping and jump the string who fail the comparison. */
11085 bufs += 2;
11086 pos = bufs[0] + l2;
11087 last = bufs[1];
11088
11089 } else {
11090 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011091 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11092 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011093 pos += url_param_name_l + 1;
11094 if (fix_pointer_if_wrap(chunks, &pos))
11095 last = bufs[2];
11096 }
11097 }
11098
11099 while (1) {
11100 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011101 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011102 pos++;
11103 if (pos < last)
11104 break;
11105 /* process buffer wrapping. */
11106 if (bufs != chunks || !bufs[2])
11107 return NULL;
11108 bufs += 2;
11109 pos = bufs[0];
11110 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011111 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011112 pos++;
11113 }
11114 return NULL;
11115}
11116
11117/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011118 * Given a url parameter name and a query string, find the next value.
11119 * An empty url_param_name matches the first available parameter.
11120 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11121 * respectively provide a pointer to the value and its end.
11122 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011123 */
11124static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011125find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011126 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011127 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011128{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011129 const char *arg_start, *qs_end;
11130 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011131
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011132 arg_start = chunks[0];
11133 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011134 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011135 /* Looks for an argument name. */
11136 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011137 url_param_name, url_param_name_l,
11138 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011139 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011140 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011141 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011142 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011143 if (!arg_start)
11144 return 0;
11145
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011146 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011147 while (1) {
11148 /* looks for the first argument. */
11149 value_start = memchr(arg_start, '=', qs_end - arg_start);
11150 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011151 /* Check for wrapping. */
11152 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011153 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011154 chunks[2]) {
11155 arg_start = chunks[2];
11156 qs_end = chunks[3];
11157 continue;
11158 }
11159 return 0;
11160 }
11161 break;
11162 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011163 value_start++;
11164 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011165 else {
11166 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011167 value_start = arg_start + url_param_name_l + 1;
11168
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011169 /* Check for pointer wrapping. */
11170 if (fix_pointer_if_wrap(chunks, &value_start)) {
11171 /* Update the end pointer. */
11172 qs_end = chunks[3];
11173
11174 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011175 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011176 return 0;
11177 }
11178 }
11179
David Cournapeau16023ee2010-12-23 20:55:41 +090011180 value_end = value_start;
11181
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011182 while (1) {
11183 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11184 value_end++;
11185 if (value_end < qs_end)
11186 break;
11187 /* process buffer wrapping. */
11188 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011189 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011190 chunks[2]) {
11191 value_end = chunks[2];
11192 qs_end = chunks[3];
11193 continue;
11194 }
11195 break;
11196 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011197
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011198 *vstart = value_start;
11199 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011200 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011201}
11202
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011203/* This scans a URL-encoded query string. It takes an optionally wrapping
11204 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11205 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11206 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011207 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011208static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011209smp_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 +090011210{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011211 const char *vstart, *vend;
11212 struct chunk *temp;
11213 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011214
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011215 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011216 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011217 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011218 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011219 return 0;
11220
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011221 /* Create sample. If the value is contiguous, return the pointer as CONST,
11222 * if the value is wrapped, copy-it in a buffer.
11223 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011224 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011225 if (chunks[2] &&
11226 vstart >= chunks[0] && vstart <= chunks[1] &&
11227 vend >= chunks[2] && vend <= chunks[3]) {
11228 /* Wrapped case. */
11229 temp = get_trash_chunk();
11230 memcpy(temp->str, vstart, chunks[1] - vstart);
11231 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011232 smp->data.u.str.str = temp->str;
11233 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011234 } else {
11235 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011236 smp->data.u.str.str = (char *)vstart;
11237 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011238 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11239 }
11240
11241 /* Update context, check wrapping. */
11242 chunks[0] = vend;
11243 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11244 chunks[1] = chunks[3];
11245 chunks[2] = NULL;
11246 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011247
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011248 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011249 smp->flags |= SMP_F_NOT_LAST;
11250
David Cournapeau16023ee2010-12-23 20:55:41 +090011251 return 1;
11252}
11253
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011254/* This function iterates over each parameter of the query string. It uses
11255 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011256 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11257 * An optional parameter name is passed in args[0], otherwise any parameter is
11258 * considered. It supports an optional delimiter argument for the beginning of
11259 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011260 */
11261static int
11262smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11263{
11264 struct http_msg *msg;
11265 char delim = '?';
11266 const char *name;
11267 int name_len;
11268
Dragan Dosen26f77e52015-05-25 10:02:11 +020011269 if (!args ||
11270 (args[0].type && args[0].type != ARGT_STR) ||
11271 (args[1].type && args[1].type != ARGT_STR))
11272 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011273
Dragan Dosen26f77e52015-05-25 10:02:11 +020011274 name = "";
11275 name_len = 0;
11276 if (args->type == ARGT_STR) {
11277 name = args->data.str.str;
11278 name_len = args->data.str.len;
11279 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011280
Dragan Dosen26f77e52015-05-25 10:02:11 +020011281 if (args[1].type)
11282 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011283
Dragan Dosen26f77e52015-05-25 10:02:11 +020011284 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011285 CHECK_HTTP_MESSAGE_FIRST();
11286
11287 msg = &smp->strm->txn->req;
11288
11289 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11290 msg->sl.rq.u_l, delim);
11291 if (!smp->ctx.a[0])
11292 return 0;
11293
11294 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011295
11296 /* Assume that the context is filled with NULL pointer
11297 * before the first call.
11298 * smp->ctx.a[2] = NULL;
11299 * smp->ctx.a[3] = NULL;
11300 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011301 }
11302
11303 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11304}
11305
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011306/* This function iterates over each parameter of the body. This requires
11307 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011308 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11309 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11310 * optional second part if the body wraps at the end of the buffer. An optional
11311 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011312 */
11313static int
11314smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11315{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011316 struct http_msg *msg;
11317 unsigned long len;
11318 unsigned long block1;
11319 char *body;
11320 const char *name;
11321 int name_len;
11322
11323 if (!args || (args[0].type && args[0].type != ARGT_STR))
11324 return 0;
11325
11326 name = "";
11327 name_len = 0;
11328 if (args[0].type == ARGT_STR) {
11329 name = args[0].data.str.str;
11330 name_len = args[0].data.str.len;
11331 }
11332
11333 if (!smp->ctx.a[0]) { // first call, find the query string
11334 CHECK_HTTP_MESSAGE_FIRST();
11335
11336 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011337 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011338 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011339 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011340
11341 len = http_body_bytes(msg);
11342 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11343
11344 block1 = len;
11345 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11346 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11347
11348 if (block1 == len) {
11349 /* buffer is not wrapped (or empty) */
11350 smp->ctx.a[0] = body;
11351 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011352
11353 /* Assume that the context is filled with NULL pointer
11354 * before the first call.
11355 * smp->ctx.a[2] = NULL;
11356 * smp->ctx.a[3] = NULL;
11357 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011358 }
11359 else {
11360 /* buffer is wrapped, we need to defragment it */
11361 smp->ctx.a[0] = body;
11362 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011363 smp->ctx.a[2] = msg->chn->buf->data;
11364 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011365 }
11366 }
11367 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11368}
11369
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011370/* Return the signed integer value for the specified url parameter (see url_param
11371 * above).
11372 */
11373static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011374smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011375{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011376 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011377
11378 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011379 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011380 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011381 }
11382
11383 return ret;
11384}
11385
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011386/* This produces a 32-bit hash of the concatenation of the first occurrence of
11387 * the Host header followed by the path component if it begins with a slash ('/').
11388 * This means that '*' will not be added, resulting in exactly the first Host
11389 * entry. If no Host header is found, then the path is used. The resulting value
11390 * is hashed using the url hash followed by a full avalanche hash and provides a
11391 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11392 * high-traffic sites without having to store whole paths.
11393 * this differs from the base32 functions in that it includes the url parameters
11394 * as well as the path
11395 */
11396static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011397smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011398{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011399 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011400 struct hdr_ctx ctx;
11401 unsigned int hash = 0;
11402 char *ptr, *beg, *end;
11403 int len;
11404
11405 CHECK_HTTP_MESSAGE_FIRST();
11406
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011407 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011408 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011409 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011410 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11411 ptr = ctx.line + ctx.val;
11412 len = ctx.vlen;
11413 while (len--)
11414 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11415 }
11416
11417 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011418 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 +000011419 beg = http_get_path(txn);
11420 if (!beg)
11421 beg = end;
11422
11423 for (ptr = beg; ptr < end ; ptr++);
11424
11425 if (beg < ptr && *beg == '/') {
11426 while (beg < ptr)
11427 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11428 }
11429 hash = full_hash(hash);
11430
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011431 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011432 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011433 smp->flags = SMP_F_VOL_1ST;
11434 return 1;
11435}
11436
11437/* This concatenates the source address with the 32-bit hash of the Host and
11438 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11439 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11440 * on the source address length. The URL hash is stored before the address so
11441 * that in environments where IPv6 is insignificant, truncating the output to
11442 * 8 bytes would still work.
11443 */
11444static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011445smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011446{
11447 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011448 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011449
Dragan Dosendb5af612016-06-16 11:23:01 +020011450 if (!cli_conn)
11451 return 0;
11452
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011453 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011454 return 0;
11455
11456 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020011457 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
11458 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011459
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011460 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011461 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011462 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011463 temp->len += 4;
11464 break;
11465 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011466 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011467 temp->len += 16;
11468 break;
11469 default:
11470 return 0;
11471 }
11472
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011473 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011474 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011475 return 1;
11476}
11477
Willy Tarreau185b5c42012-04-26 15:11:51 +020011478/* This function is used to validate the arguments passed to any "hdr" fetch
11479 * keyword. These keywords support an optional positive or negative occurrence
11480 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11481 * is assumed that the types are already the correct ones. Returns 0 on error,
11482 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11483 * error message in case of error, that the caller is responsible for freeing.
11484 * The initial location must either be freeable or NULL.
11485 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011486int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011487{
11488 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011489 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011490 return 0;
11491 }
11492 return 1;
11493}
11494
Willy Tarreau276fae92013-07-25 14:36:01 +020011495/* takes an UINT value on input supposed to represent the time since EPOCH,
11496 * adds an optional offset found in args[0] and emits a string representing
11497 * the date in RFC-1123/5322 format.
11498 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011499static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011500{
Cyril Bontéf78d8962016-01-22 19:40:28 +010011501 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020011502 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11503 struct chunk *temp;
11504 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011505 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011506 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011507
11508 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011509 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011510 curr_date += args[0].data.sint;
11511
11512 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011513 if (!tm)
11514 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011515
11516 temp = get_trash_chunk();
11517 temp->len = snprintf(temp->str, temp->size - temp->len,
11518 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11519 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11520 tm->tm_hour, tm->tm_min, tm->tm_sec);
11521
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011522 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011523 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011524 return 1;
11525}
11526
Thierry FOURNIERad903512014-04-11 17:51:01 +020011527/* Match language range with language tag. RFC2616 14.4:
11528 *
11529 * A language-range matches a language-tag if it exactly equals
11530 * the tag, or if it exactly equals a prefix of the tag such
11531 * that the first tag character following the prefix is "-".
11532 *
11533 * Return 1 if the strings match, else return 0.
11534 */
11535static inline int language_range_match(const char *range, int range_len,
11536 const char *tag, int tag_len)
11537{
11538 const char *end = range + range_len;
11539 const char *tend = tag + tag_len;
11540 while (range < end) {
11541 if (*range == '-' && tag == tend)
11542 return 1;
11543 if (*range != *tag || tag == tend)
11544 return 0;
11545 range++;
11546 tag++;
11547 }
11548 /* Return true only if the last char of the tag is matched. */
11549 return tag == tend;
11550}
11551
11552/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011553static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011554{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011555 const char *al = smp->data.u.str.str;
11556 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011557 const char *token;
11558 int toklen;
11559 int qvalue;
11560 const char *str;
11561 const char *w;
11562 int best_q = 0;
11563
11564 /* Set the constant to the sample, because the output of the
11565 * function will be peek in the constant configuration string.
11566 */
11567 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011568 smp->data.u.str.size = 0;
11569 smp->data.u.str.str = "";
11570 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011571
11572 /* Parse the accept language */
11573 while (1) {
11574
11575 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011576 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011577 al++;
11578 if (al >= end)
11579 break;
11580
11581 /* Start of the fisrt word. */
11582 token = al;
11583
11584 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011585 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011586 al++;
11587 if (al == token)
11588 goto expect_comma;
11589
11590 /* Length of the token. */
11591 toklen = al - token;
11592 qvalue = 1000;
11593
11594 /* Check if the token exists in the list. If the token not exists,
11595 * jump to the next token.
11596 */
11597 str = args[0].data.str.str;
11598 w = str;
11599 while (1) {
11600 if (*str == ';' || *str == '\0') {
11601 if (language_range_match(token, toklen, w, str-w))
11602 goto look_for_q;
11603 if (*str == '\0')
11604 goto expect_comma;
11605 w = str + 1;
11606 }
11607 str++;
11608 }
11609 goto expect_comma;
11610
11611look_for_q:
11612
11613 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011614 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011615 al++;
11616 if (al >= end)
11617 goto process_value;
11618
11619 /* If ',' is found, process the result */
11620 if (*al == ',')
11621 goto process_value;
11622
11623 /* If the character is different from ';', look
11624 * for the end of the header part in best effort.
11625 */
11626 if (*al != ';')
11627 goto expect_comma;
11628
11629 /* Assumes that the char is ';', now expect "q=". */
11630 al++;
11631
11632 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011633 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011634 al++;
11635 if (al >= end)
11636 goto process_value;
11637
11638 /* Expect 'q'. If no 'q', continue in best effort */
11639 if (*al != 'q')
11640 goto process_value;
11641 al++;
11642
11643 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011644 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011645 al++;
11646 if (al >= end)
11647 goto process_value;
11648
11649 /* Expect '='. If no '=', continue in best effort */
11650 if (*al != '=')
11651 goto process_value;
11652 al++;
11653
11654 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011655 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011656 al++;
11657 if (al >= end)
11658 goto process_value;
11659
11660 /* Parse the q value. */
11661 qvalue = parse_qvalue(al, &al);
11662
11663process_value:
11664
11665 /* If the new q value is the best q value, then store the associated
11666 * language in the response. If qvalue is the biggest value (1000),
11667 * break the process.
11668 */
11669 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011670 smp->data.u.str.str = (char *)w;
11671 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011672 if (qvalue >= 1000)
11673 break;
11674 best_q = qvalue;
11675 }
11676
11677expect_comma:
11678
11679 /* Expect comma or end. If the end is detected, quit the loop. */
11680 while (al < end && *al != ',')
11681 al++;
11682 if (al >= end)
11683 break;
11684
11685 /* Comma is found, jump it and restart the analyzer. */
11686 al++;
11687 }
11688
11689 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011690 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
11691 smp->data.u.str.str = args[1].data.str.str;
11692 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011693 }
11694
11695 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011696 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011697}
11698
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011699/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011700static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011701{
11702 /* If the constant flag is set or if not size is avalaible at
11703 * the end of the buffer, copy the string in other buffer
11704 * before decoding.
11705 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011706 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011707 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011708 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
11709 smp->data.u.str.str = str->str;
11710 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011711 smp->flags &= ~SMP_F_CONST;
11712 }
11713
11714 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011715 smp->data.u.str.str[smp->data.u.str.len] = '\0';
11716 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Christopher Fauleta2584792017-10-05 10:03:12 +020011717 return (smp->data.u.str.len >= 0);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011718}
11719
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011720static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
11721{
11722 struct proxy *fe = strm_fe(smp->strm);
11723 int idx, i;
11724 struct cap_hdr *hdr;
11725 int len;
11726
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011727 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011728 return 0;
11729
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011730 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011731
11732 /* Check the availibity of the capture id. */
11733 if (idx > fe->nb_req_cap - 1)
11734 return 0;
11735
11736 /* Look for the original configuration. */
11737 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
11738 hdr != NULL && i != idx ;
11739 i--, hdr = hdr->next);
11740 if (!hdr)
11741 return 0;
11742
11743 /* check for the memory allocation */
11744 if (smp->strm->req_cap[hdr->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011745 smp->strm->req_cap[hdr->index] = pool_alloc(hdr->pool);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011746 if (smp->strm->req_cap[hdr->index] == NULL)
11747 return 0;
11748
11749 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011750 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011751 if (len > hdr->len)
11752 len = hdr->len;
11753
11754 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011755 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011756 smp->strm->req_cap[idx][len] = '\0';
11757
11758 return 1;
11759}
11760
11761static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
11762{
11763 struct proxy *fe = strm_fe(smp->strm);
11764 int idx, i;
11765 struct cap_hdr *hdr;
11766 int len;
11767
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011768 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011769 return 0;
11770
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011771 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011772
11773 /* Check the availibity of the capture id. */
11774 if (idx > fe->nb_rsp_cap - 1)
11775 return 0;
11776
11777 /* Look for the original configuration. */
11778 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
11779 hdr != NULL && i != idx ;
11780 i--, hdr = hdr->next);
11781 if (!hdr)
11782 return 0;
11783
11784 /* check for the memory allocation */
11785 if (smp->strm->res_cap[hdr->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011786 smp->strm->res_cap[hdr->index] = pool_alloc(hdr->pool);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011787 if (smp->strm->res_cap[hdr->index] == NULL)
11788 return 0;
11789
11790 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011791 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011792 if (len > hdr->len)
11793 len = hdr->len;
11794
11795 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011796 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011797 smp->strm->res_cap[idx][len] = '\0';
11798
11799 return 1;
11800}
11801
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011802/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011803 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011804 * the relevant part of the request line accordingly. Then it updates various
11805 * pointers to the next elements which were moved, and the total buffer length.
11806 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011807 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11808 * error, though this can be revisited when this code is finally exploited.
11809 *
11810 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
11811 * query string and 3 to replace uri.
11812 *
11813 * In query string case, the mark question '?' must be set at the start of the
11814 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011815 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011816int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011817 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011818{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011819 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011820 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011821 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011822 int delta;
11823
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011824 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011825 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011826 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011827 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11828
11829 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011830 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011831 txn->req.sl.rq.m_l += delta;
11832 txn->req.sl.rq.u += delta;
11833 txn->req.sl.rq.v += delta;
11834 break;
11835
11836 case 1: // path
11837 cur_ptr = http_get_path(txn);
11838 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011839 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011840
11841 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011842 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 +010011843 cur_end++;
11844
11845 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011846 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011847 txn->req.sl.rq.u_l += delta;
11848 txn->req.sl.rq.v += delta;
11849 break;
11850
11851 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011852 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011853 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011854 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11855 while (cur_ptr < cur_end && *cur_ptr != '?')
11856 cur_ptr++;
11857
11858 /* skip the question mark or indicate that we must insert it
11859 * (but only if the format string is not empty then).
11860 */
11861 if (cur_ptr < cur_end)
11862 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011863 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011864 offset = 0;
11865
11866 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011867 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011868 txn->req.sl.rq.u_l += delta;
11869 txn->req.sl.rq.v += delta;
11870 break;
11871
11872 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011873 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011874 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11875
11876 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011877 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011878 txn->req.sl.rq.u_l += delta;
11879 txn->req.sl.rq.v += delta;
11880 break;
11881
11882 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011883 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011884 }
11885
11886 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011887 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020011888 txn->req.sl.rq.l += delta;
11889 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011890 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011891 return 0;
11892}
11893
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011894/* This function replace the HTTP status code and the associated message. The
11895 * variable <status> contains the new status code. This function never fails.
11896 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011897void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011898{
11899 struct http_txn *txn = s->txn;
11900 char *cur_ptr, *cur_end;
11901 int delta;
11902 char *res;
11903 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011904 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011905 int msg_len;
11906
11907 chunk_reset(&trash);
11908
11909 res = ultoa_o(status, trash.str, trash.size);
11910 c_l = res - trash.str;
11911
11912 trash.str[c_l] = ' ';
11913 trash.len = c_l + 1;
11914
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011915 /* Do we have a custom reason format string? */
11916 if (msg == NULL)
11917 msg = get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011918 msg_len = strlen(msg);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011919 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
11920 trash.len += msg_len;
11921
11922 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
11923 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
11924
11925 /* commit changes and adjust message */
11926 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
11927
11928 /* adjust res line offsets and lengths */
11929 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
11930 txn->rsp.sl.st.c_l = c_l;
11931 txn->rsp.sl.st.r_l = msg_len;
11932
11933 delta = trash.len - (cur_end - cur_ptr);
11934 txn->rsp.sl.st.l += delta;
11935 txn->hdr_idx.v[0].len += delta;
11936 http_msg_move_end(&txn->rsp, delta);
11937}
11938
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011939/* This function executes one of the set-{method,path,query,uri} actions. It
11940 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011941 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011942 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011943 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
11944 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011945 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011946enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011947 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011948{
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011949 struct chunk *replace;
11950 enum act_return ret = ACT_RET_ERR;
11951
11952 replace = alloc_trash_chunk();
11953 if (!replace)
11954 goto leave;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011955
11956 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011957 if (rule->arg.http.action == 2)
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011958 replace->str[replace->len++] = '?';
11959 replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len,
11960 &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011961
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011962 http_replace_req_line(rule->arg.http.action, replace->str, replace->len, px, s);
11963
11964 ret = ACT_RET_CONT;
11965
11966leave:
11967 free_trash_chunk(replace);
11968 return ret;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011969}
11970
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011971/* This function is just a compliant action wrapper for "set-status". */
11972enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011973 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011974{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011975 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011976 return ACT_RET_CONT;
11977}
11978
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011979/* parse an http-request action among :
11980 * set-method
11981 * set-path
11982 * set-query
11983 * set-uri
11984 *
11985 * All of them accept a single argument of type string representing a log-format.
11986 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
11987 * 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 +020011988 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011989 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011990enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
11991 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011992{
11993 int cur_arg = *orig_arg;
11994
Thierry FOURNIER42148732015-09-02 17:17:33 +020011995 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011996
11997 switch (args[0][4]) {
11998 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011999 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012000 rule->action_ptr = http_action_set_req_line;
12001 break;
12002 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012003 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012004 rule->action_ptr = http_action_set_req_line;
12005 break;
12006 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012007 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012008 rule->action_ptr = http_action_set_req_line;
12009 break;
12010 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012011 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012012 rule->action_ptr = http_action_set_req_line;
12013 break;
12014 default:
12015 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012016 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012017 }
12018
12019 if (!*args[cur_arg] ||
12020 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12021 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012022 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012023 }
12024
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012025 LIST_INIT(&rule->arg.http.logfmt);
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012026 px->conf.args.ctx = ARGC_HRQ;
12027 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.logfmt, LOG_OPT_HTTP,
12028 (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 +010012029 return ACT_RET_PRS_ERR;
12030 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012031
12032 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012033 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012034}
12035
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012036/* parse set-status action:
12037 * This action accepts a single argument of type int representing
12038 * an http status code. It returns ACT_RET_PRS_OK on success,
12039 * ACT_RET_PRS_ERR on error.
12040 */
12041enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12042 struct act_rule *rule, char **err)
12043{
12044 char *error;
12045
Thierry FOURNIER42148732015-09-02 17:17:33 +020012046 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012047 rule->action_ptr = action_http_set_status;
12048
12049 /* Check if an argument is available */
12050 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012051 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012052 return ACT_RET_PRS_ERR;
12053 }
12054
12055 /* convert status code as integer */
12056 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12057 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12058 memprintf(err, "expects an integer status code between 100 and 999");
12059 return ACT_RET_PRS_ERR;
12060 }
12061
12062 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012063
12064 /* set custom reason string */
12065 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
12066 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
12067 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
12068 (*orig_arg)++;
12069 rule->arg.status.reason = strdup(args[*orig_arg]);
12070 (*orig_arg)++;
12071 }
12072
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012073 return ACT_RET_PRS_OK;
12074}
12075
Willy Tarreau53275e82017-11-24 07:52:01 +010012076/* This function executes the "reject" HTTP action. It clears the request and
12077 * response buffer without sending any response. It can be useful as an HTTP
12078 * alternative to the silent-drop action to defend against DoS attacks, and may
12079 * also be used with HTTP/2 to close a connection instead of just a stream.
12080 * The txn status is unchanged, indicating no response was sent. The termination
12081 * flags will indicate "PR". It always returns ACT_RET_STOP.
12082 */
12083enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
12084 struct session *sess, struct stream *s, int flags)
12085{
12086 channel_abort(&s->req);
12087 channel_abort(&s->res);
12088 s->req.analysers = 0;
12089 s->res.analysers = 0;
12090
12091 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
12092 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
12093 if (sess->listener && sess->listener->counters)
12094 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
12095
12096 if (!(s->flags & SF_ERR_MASK))
12097 s->flags |= SF_ERR_PRXCOND;
12098 if (!(s->flags & SF_FINST_MASK))
12099 s->flags |= SF_FINST_R;
12100
12101 return ACT_RET_CONT;
12102}
12103
12104/* parse the "reject" action:
12105 * This action takes no argument and returns ACT_RET_PRS_OK on success,
12106 * ACT_RET_PRS_ERR on error.
12107 */
12108enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
12109 struct act_rule *rule, char **err)
12110{
12111 rule->action = ACT_CUSTOM;
12112 rule->action_ptr = http_action_reject;
12113 return ACT_RET_PRS_OK;
12114}
12115
Willy Tarreaua9083d02015-05-08 15:27:59 +020012116/* This function executes the "capture" action. It executes a fetch expression,
12117 * turns the result into a string and puts it in a capture slot. It always
12118 * returns 1. If an error occurs the action is cancelled, but the rule
12119 * processing continues.
12120 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012121enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012122 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012123{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012124 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012125 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012126 char **cap = s->req_cap;
12127 int len;
12128
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012129 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 +020012130 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012131 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012132
12133 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012134 cap[h->index] = pool_alloc(h->pool);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012135
12136 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012137 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012138
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012139 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012140 if (len > h->len)
12141 len = h->len;
12142
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012143 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012144 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012145 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012146}
12147
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012148/* This function executes the "capture" action and store the result in a
12149 * capture slot if exists. It executes a fetch expression, turns the result
12150 * into a string and puts it in a capture slot. It always returns 1. If an
12151 * error occurs the action is cancelled, but the rule processing continues.
12152 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012153enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012154 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012155{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012156 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012157 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012158 char **cap = s->req_cap;
12159 struct proxy *fe = strm_fe(s);
12160 int len;
12161 int i;
12162
12163 /* Look for the original configuration. */
12164 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012165 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012166 i--, h = h->next);
12167 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012168 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012169
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012170 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 +020012171 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012172 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012173
12174 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012175 cap[h->index] = pool_alloc(h->pool);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012176
12177 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012178 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012179
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012180 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012181 if (len > h->len)
12182 len = h->len;
12183
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012184 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012185 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012186 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012187}
12188
Christopher Faulet29730ba2017-09-18 15:26:32 +020012189/* Check an "http-request capture" action.
12190 *
12191 * The function returns 1 in success case, otherwise, it returns 0 and err is
12192 * filled.
12193 */
12194int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
12195{
Christopher Fauletfd608dd2017-12-04 09:45:15 +010012196 if (rule->action_ptr != http_action_req_capture_by_id)
12197 return 1;
12198
Christopher Faulet29730ba2017-09-18 15:26:32 +020012199 if (rule->arg.capid.idx >= px->nb_req_cap) {
12200 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
12201 rule->arg.capid.idx);
12202 return 0;
12203 }
12204
12205 return 1;
12206}
12207
Willy Tarreaua9083d02015-05-08 15:27:59 +020012208/* parse an "http-request capture" action. It takes a single argument which is
12209 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012210 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012211 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012212 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012213enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12214 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012215{
12216 struct sample_expr *expr;
12217 struct cap_hdr *hdr;
12218 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012219 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012220
12221 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12222 if (strcmp(args[cur_arg], "if") == 0 ||
12223 strcmp(args[cur_arg], "unless") == 0)
12224 break;
12225
12226 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012227 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012228 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012229 }
12230
Willy Tarreaua9083d02015-05-08 15:27:59 +020012231 cur_arg = *orig_arg;
12232 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12233 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012234 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012235
12236 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12237 memprintf(err,
12238 "fetch method '%s' extracts information from '%s', none of which is available here",
12239 args[cur_arg-1], sample_src_names(expr->fetch->use));
12240 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012241 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012242 }
12243
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012244 if (!args[cur_arg] || !*args[cur_arg]) {
12245 memprintf(err, "expects 'len or 'id'");
12246 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012247 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012248 }
12249
Willy Tarreaua9083d02015-05-08 15:27:59 +020012250 if (strcmp(args[cur_arg], "len") == 0) {
12251 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012252
12253 if (!(px->cap & PR_CAP_FE)) {
12254 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012255 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012256 }
12257
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012258 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012259
Willy Tarreaua9083d02015-05-08 15:27:59 +020012260 if (!args[cur_arg]) {
12261 memprintf(err, "missing length value");
12262 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012263 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012264 }
12265 /* we copy the table name for now, it will be resolved later */
12266 len = atoi(args[cur_arg]);
12267 if (len <= 0) {
12268 memprintf(err, "length must be > 0");
12269 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012270 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012271 }
12272 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012273
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012274 if (!len) {
12275 memprintf(err, "a positive 'len' argument is mandatory");
12276 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012277 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012278 }
12279
Vincent Bernat02779b62016-04-03 13:48:43 +020012280 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012281 hdr->next = px->req_cap;
12282 hdr->name = NULL; /* not a header capture */
12283 hdr->namelen = 0;
12284 hdr->len = len;
12285 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12286 hdr->index = px->nb_req_cap++;
12287
12288 px->req_cap = hdr;
12289 px->to_log |= LW_REQHDR;
12290
Thierry FOURNIER42148732015-09-02 17:17:33 +020012291 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012292 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012293 rule->arg.cap.expr = expr;
12294 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012295 }
12296
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012297 else if (strcmp(args[cur_arg], "id") == 0) {
12298 int id;
12299 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012300
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012301 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012302
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012303 if (!args[cur_arg]) {
12304 memprintf(err, "missing id value");
12305 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012306 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012307 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012308
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012309 id = strtol(args[cur_arg], &error, 10);
12310 if (*error != '\0') {
12311 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12312 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012313 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012314 }
12315 cur_arg++;
12316
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012317 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012318
Thierry FOURNIER42148732015-09-02 17:17:33 +020012319 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012320 rule->action_ptr = http_action_req_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012321 rule->check_ptr = check_http_req_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012322 rule->arg.capid.expr = expr;
12323 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012324 }
12325
12326 else {
12327 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12328 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012329 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012330 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012331
12332 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012333 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012334}
12335
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012336/* This function executes the "capture" action and store the result in a
12337 * capture slot if exists. It executes a fetch expression, turns the result
12338 * into a string and puts it in a capture slot. It always returns 1. If an
12339 * error occurs the action is cancelled, but the rule processing continues.
12340 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012341enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012342 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012343{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012344 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012345 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012346 char **cap = s->res_cap;
12347 struct proxy *fe = strm_fe(s);
12348 int len;
12349 int i;
12350
12351 /* Look for the original configuration. */
12352 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012353 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012354 i--, h = h->next);
12355 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012356 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012357
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012358 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 +020012359 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012360 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012361
12362 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012363 cap[h->index] = pool_alloc(h->pool);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012364
12365 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012366 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012367
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012368 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012369 if (len > h->len)
12370 len = h->len;
12371
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012372 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012373 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012374 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012375}
12376
Christopher Faulet29730ba2017-09-18 15:26:32 +020012377/* Check an "http-response capture" action.
12378 *
12379 * The function returns 1 in success case, otherwise, it returns 0 and err is
12380 * filled.
12381 */
12382int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
12383{
Christopher Fauletfd608dd2017-12-04 09:45:15 +010012384 if (rule->action_ptr != http_action_res_capture_by_id)
12385 return 1;
12386
Christopher Faulet29730ba2017-09-18 15:26:32 +020012387 if (rule->arg.capid.idx >= px->nb_rsp_cap) {
12388 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
12389 rule->arg.capid.idx);
12390 return 0;
12391 }
12392
12393 return 1;
12394}
12395
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012396/* parse an "http-response capture" action. It takes a single argument which is
12397 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12398 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012399 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012400 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012401enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12402 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012403{
12404 struct sample_expr *expr;
12405 int cur_arg;
12406 int id;
12407 char *error;
12408
12409 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12410 if (strcmp(args[cur_arg], "if") == 0 ||
12411 strcmp(args[cur_arg], "unless") == 0)
12412 break;
12413
12414 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012415 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012416 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012417 }
12418
12419 cur_arg = *orig_arg;
12420 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12421 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012422 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012423
12424 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12425 memprintf(err,
12426 "fetch method '%s' extracts information from '%s', none of which is available here",
12427 args[cur_arg-1], sample_src_names(expr->fetch->use));
12428 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012429 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012430 }
12431
12432 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012433 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012434 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012435 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012436 }
12437
12438 if (strcmp(args[cur_arg], "id") != 0) {
12439 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12440 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012441 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012442 }
12443
12444 cur_arg++;
12445
12446 if (!args[cur_arg]) {
12447 memprintf(err, "missing id value");
12448 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012449 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012450 }
12451
12452 id = strtol(args[cur_arg], &error, 10);
12453 if (*error != '\0') {
12454 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12455 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012456 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012457 }
12458 cur_arg++;
12459
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012460 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012461
Thierry FOURNIER42148732015-09-02 17:17:33 +020012462 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012463 rule->action_ptr = http_action_res_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012464 rule->check_ptr = check_http_res_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012465 rule->arg.capid.expr = expr;
12466 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012467
12468 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012469 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012470}
12471
William Lallemand73025dd2014-04-24 14:38:37 +020012472/*
12473 * Return the struct http_req_action_kw associated to a keyword.
12474 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012475struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012476{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012477 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012478}
12479
12480/*
12481 * Return the struct http_res_action_kw associated to a keyword.
12482 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012483struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012484{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012485 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012486}
12487
Willy Tarreau12207b32016-11-22 19:48:51 +010012488
12489/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
12490 * now.
12491 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020012492static int cli_parse_show_errors(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau12207b32016-11-22 19:48:51 +010012493{
12494 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
12495 return 1;
12496
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012497 if (*args[2]) {
12498 struct proxy *px;
12499
12500 px = proxy_find_by_name(args[2], 0, 0);
12501 if (px)
12502 appctx->ctx.errors.iid = px->uuid;
12503 else
12504 appctx->ctx.errors.iid = atoi(args[2]);
12505
12506 if (!appctx->ctx.errors.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +020012507 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012508 appctx->ctx.cli.msg = "No such proxy.\n";
12509 appctx->st0 = CLI_ST_PRINT;
12510 return 1;
12511 }
12512 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012513 else
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012514 appctx->ctx.errors.iid = -1; // dump all proxies
12515
Willy Tarreau35069f82016-11-25 09:16:37 +010012516 appctx->ctx.errors.flag = 0;
12517 if (strcmp(args[3], "request") == 0)
12518 appctx->ctx.errors.flag |= 4; // ignore response
12519 else if (strcmp(args[3], "response") == 0)
12520 appctx->ctx.errors.flag |= 2; // ignore request
Willy Tarreau12207b32016-11-22 19:48:51 +010012521 appctx->ctx.errors.px = NULL;
Willy Tarreau12207b32016-11-22 19:48:51 +010012522 return 0;
12523}
12524
12525/* This function dumps all captured errors onto the stream interface's
12526 * read buffer. It returns 0 if the output buffer is full and it needs
12527 * to be called again, otherwise non-zero.
12528 */
12529static int cli_io_handler_show_errors(struct appctx *appctx)
12530{
12531 struct stream_interface *si = appctx->owner;
12532 extern const char *monthname[12];
12533
12534 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
12535 return 1;
12536
12537 chunk_reset(&trash);
12538
12539 if (!appctx->ctx.errors.px) {
12540 /* the function had not been called yet, let's prepare the
12541 * buffer for a response.
12542 */
12543 struct tm tm;
12544
12545 get_localtime(date.tv_sec, &tm);
12546 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
12547 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12548 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
12549 error_snapshot_id);
12550
Willy Tarreau06d80a92017-10-19 14:32:15 +020012551 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012552 /* Socket buffer full. Let's try again later from the same point */
12553 si_applet_cant_put(si);
12554 return 0;
12555 }
12556
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012557 appctx->ctx.errors.px = proxies_list;
Willy Tarreau12207b32016-11-22 19:48:51 +010012558 appctx->ctx.errors.bol = 0;
12559 appctx->ctx.errors.ptr = -1;
12560 }
12561
12562 /* we have two inner loops here, one for the proxy, the other one for
12563 * the buffer.
12564 */
12565 while (appctx->ctx.errors.px) {
12566 struct error_snapshot *es;
12567
Willy Tarreau35069f82016-11-25 09:16:37 +010012568 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012569 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau35069f82016-11-25 09:16:37 +010012570 if (appctx->ctx.errors.flag & 2) // skip req
12571 goto next;
12572 }
12573 else {
Willy Tarreau12207b32016-11-22 19:48:51 +010012574 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau35069f82016-11-25 09:16:37 +010012575 if (appctx->ctx.errors.flag & 4) // skip resp
12576 goto next;
12577 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012578
12579 if (!es->when.tv_sec)
12580 goto next;
12581
12582 if (appctx->ctx.errors.iid >= 0 &&
12583 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
12584 es->oe->uuid != appctx->ctx.errors.iid)
12585 goto next;
12586
12587 if (appctx->ctx.errors.ptr < 0) {
12588 /* just print headers now */
12589
12590 char pn[INET6_ADDRSTRLEN];
12591 struct tm tm;
12592 int port;
12593
12594 get_localtime(es->when.tv_sec, &tm);
12595 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
12596 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12597 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
12598
12599 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
12600 case AF_INET:
12601 case AF_INET6:
12602 port = get_host_port(&es->src);
12603 break;
12604 default:
12605 port = 0;
12606 }
12607
Willy Tarreau35069f82016-11-25 09:16:37 +010012608 switch (appctx->ctx.errors.flag & 1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012609 case 0:
12610 chunk_appendf(&trash,
12611 " frontend %s (#%d): invalid request\n"
12612 " backend %s (#%d)",
12613 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12614 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
12615 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
12616 break;
12617 case 1:
12618 chunk_appendf(&trash,
12619 " backend %s (#%d): invalid response\n"
12620 " frontend %s (#%d)",
12621 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12622 es->oe->id, es->oe->uuid);
12623 break;
12624 }
12625
12626 chunk_appendf(&trash,
12627 ", server %s (#%d), event #%u\n"
12628 " src %s:%d, session #%d, session flags 0x%08x\n"
Willy Tarreau10e61cb2017-01-04 14:51:22 +010012629 " HTTP msg state %s(%d), msg flags 0x%08x, tx flags 0x%08x\n"
Willy Tarreau12207b32016-11-22 19:48:51 +010012630 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
12631 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
12632 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
12633 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
12634 es->ev_id,
12635 pn, port, es->sid, es->s_flags,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020012636 h1_msg_state_str(es->state), es->state, es->m_flags, es->t_flags,
Willy Tarreau12207b32016-11-22 19:48:51 +010012637 es->m_clen, es->m_blen,
12638 es->b_flags, es->b_out, es->b_tot,
12639 es->len, es->b_wrap, es->pos);
12640
Willy Tarreau06d80a92017-10-19 14:32:15 +020012641 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012642 /* Socket buffer full. Let's try again later from the same point */
12643 si_applet_cant_put(si);
12644 return 0;
12645 }
12646 appctx->ctx.errors.ptr = 0;
12647 appctx->ctx.errors.sid = es->sid;
12648 }
12649
12650 if (appctx->ctx.errors.sid != es->sid) {
12651 /* the snapshot changed while we were dumping it */
12652 chunk_appendf(&trash,
12653 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +020012654 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012655 si_applet_cant_put(si);
12656 return 0;
12657 }
12658 goto next;
12659 }
12660
12661 /* OK, ptr >= 0, so we have to dump the current line */
12662 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
12663 int newptr;
12664 int newline;
12665
12666 newline = appctx->ctx.errors.bol;
12667 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
12668 if (newptr == appctx->ctx.errors.ptr)
12669 return 0;
12670
Willy Tarreau06d80a92017-10-19 14:32:15 +020012671 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012672 /* Socket buffer full. Let's try again later from the same point */
12673 si_applet_cant_put(si);
12674 return 0;
12675 }
12676 appctx->ctx.errors.ptr = newptr;
12677 appctx->ctx.errors.bol = newline;
12678 };
12679 next:
12680 appctx->ctx.errors.bol = 0;
12681 appctx->ctx.errors.ptr = -1;
Willy Tarreau35069f82016-11-25 09:16:37 +010012682 appctx->ctx.errors.flag ^= 1;
12683 if (!(appctx->ctx.errors.flag & 1))
Willy Tarreau12207b32016-11-22 19:48:51 +010012684 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau12207b32016-11-22 19:48:51 +010012685 }
12686
12687 /* dump complete */
12688 return 1;
12689}
12690
12691/* register cli keywords */
12692static struct cli_kw_list cli_kws = {{ },{
12693 { { "show", "errors", NULL },
12694 "show errors : report last request and response errors for each proxy",
12695 cli_parse_show_errors, cli_io_handler_show_errors, NULL,
12696 },
12697 {{},}
12698}};
12699
Willy Tarreau4a568972010-05-12 08:08:50 +020012700/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012701/* All supported ACL keywords must be declared here. */
12702/************************************************************************/
12703
12704/* Note: must not be declared <const> as its list will be overwritten.
12705 * Please take care of keeping this list alphabetically sorted.
12706 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012707static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012708 { "base", "base", PAT_MATCH_STR },
12709 { "base_beg", "base", PAT_MATCH_BEG },
12710 { "base_dir", "base", PAT_MATCH_DIR },
12711 { "base_dom", "base", PAT_MATCH_DOM },
12712 { "base_end", "base", PAT_MATCH_END },
12713 { "base_len", "base", PAT_MATCH_LEN },
12714 { "base_reg", "base", PAT_MATCH_REG },
12715 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012716
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012717 { "cook", "req.cook", PAT_MATCH_STR },
12718 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12719 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12720 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12721 { "cook_end", "req.cook", PAT_MATCH_END },
12722 { "cook_len", "req.cook", PAT_MATCH_LEN },
12723 { "cook_reg", "req.cook", PAT_MATCH_REG },
12724 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012725
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012726 { "hdr", "req.hdr", PAT_MATCH_STR },
12727 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12728 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12729 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12730 { "hdr_end", "req.hdr", PAT_MATCH_END },
12731 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12732 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12733 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012734
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012735 /* these two declarations uses strings with list storage (in place
12736 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12737 * and delete functions are relative to the list management. The parse
12738 * and match method are related to the corresponding fetch methods. This
12739 * is very particular ACL declaration mode.
12740 */
12741 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12742 { "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 +020012743
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012744 { "path", "path", PAT_MATCH_STR },
12745 { "path_beg", "path", PAT_MATCH_BEG },
12746 { "path_dir", "path", PAT_MATCH_DIR },
12747 { "path_dom", "path", PAT_MATCH_DOM },
12748 { "path_end", "path", PAT_MATCH_END },
12749 { "path_len", "path", PAT_MATCH_LEN },
12750 { "path_reg", "path", PAT_MATCH_REG },
12751 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012752
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012753 { "req_ver", "req.ver", PAT_MATCH_STR },
12754 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012755
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012756 { "scook", "res.cook", PAT_MATCH_STR },
12757 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12758 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12759 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12760 { "scook_end", "res.cook", PAT_MATCH_END },
12761 { "scook_len", "res.cook", PAT_MATCH_LEN },
12762 { "scook_reg", "res.cook", PAT_MATCH_REG },
12763 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012764
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012765 { "shdr", "res.hdr", PAT_MATCH_STR },
12766 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12767 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12768 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12769 { "shdr_end", "res.hdr", PAT_MATCH_END },
12770 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12771 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12772 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012773
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012774 { "url", "url", PAT_MATCH_STR },
12775 { "url_beg", "url", PAT_MATCH_BEG },
12776 { "url_dir", "url", PAT_MATCH_DIR },
12777 { "url_dom", "url", PAT_MATCH_DOM },
12778 { "url_end", "url", PAT_MATCH_END },
12779 { "url_len", "url", PAT_MATCH_LEN },
12780 { "url_reg", "url", PAT_MATCH_REG },
12781 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012782
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012783 { "urlp", "urlp", PAT_MATCH_STR },
12784 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12785 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12786 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12787 { "urlp_end", "urlp", PAT_MATCH_END },
12788 { "urlp_len", "urlp", PAT_MATCH_LEN },
12789 { "urlp_reg", "urlp", PAT_MATCH_REG },
12790 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012791
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012792 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012793}};
12794
12795/************************************************************************/
12796/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012797/************************************************************************/
12798/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012799static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012800 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012801 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012802 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12803
Willy Tarreau87b09662015-04-03 00:22:06 +020012804 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012805 { "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 +020012806
12807 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012808 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12809 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12810 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012811
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012812 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12813 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012814
Willy Tarreau409bcde2013-01-08 00:31:00 +010012815 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12816 * are only here to match the ACL's name, are request-only and are used
12817 * for ACL compatibility only.
12818 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012819 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12820 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012821 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12822 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012823
12824 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12825 * only here to match the ACL's name, are request-only and are used for
12826 * ACL compatibility only.
12827 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012828 { "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 +020012829 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012830 { "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 +020012831 { "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 +010012832
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012833 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012834 { "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 +010012835 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012836 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012837 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012838 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012839
12840 /* HTTP protocol on the request path */
12841 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012842 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012843
12844 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012845 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12846 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012847
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012848 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012849 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12850 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012851 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012852
Thierry FOURNIERd7d88812017-04-19 15:15:14 +020012853 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER5617dce2017-04-09 05:38:19 +020012854 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12855
Willy Tarreau18ed2562013-01-14 15:56:36 +010012856 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012857 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12858 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012859
Willy Tarreau18ed2562013-01-14 15:56:36 +010012860 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012861 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012862 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12863 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012864
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012865 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012866 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012867 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012868 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012869 { "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 +010012870 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012871 { "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 +010012872
12873 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012874 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012875 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12876 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012877
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012878 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012879 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012880 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012881 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012882 { "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 +010012883 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012884 { "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 +010012885
Willy Tarreau409bcde2013-01-08 00:31:00 +010012886 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012887 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012888 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12889 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012890 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012891
12892 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012893 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012894 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012895 { "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 +020012896 { "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 +010012897
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012898 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020012899 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012900 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012901 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012902 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012903 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012904 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020012905 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12906 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012907 { "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 +010012908 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012909}};
12910
Willy Tarreau8797c062007-05-07 00:55:35 +020012911
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012912/************************************************************************/
12913/* All supported converter keywords must be declared here. */
12914/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012915/* Note: must not be declared <const> as its list will be overwritten */
12916static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012917 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020012918 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012919 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
12920 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012921 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012922 { NULL, NULL, 0, 0, 0 },
12923}};
12924
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012925
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012926/************************************************************************/
12927/* All supported http-request action keywords must be declared here. */
12928/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012929struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012930 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020012931 { "capture", parse_http_req_capture },
Willy Tarreau53275e82017-11-24 07:52:01 +010012932 { "reject", parse_http_action_reject },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012933 { "set-method", parse_set_req_line },
12934 { "set-path", parse_set_req_line },
12935 { "set-query", parse_set_req_line },
12936 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012937 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012938 }
12939};
12940
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012941struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012942 .kw = {
12943 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012944 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012945 { NULL, NULL }
12946 }
12947};
12948
Willy Tarreau8797c062007-05-07 00:55:35 +020012949__attribute__((constructor))
12950static void __http_protocol_init(void)
12951{
12952 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012953 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012954 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012955 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012956 http_res_keywords_register(&http_res_actions);
Willy Tarreau12207b32016-11-22 19:48:51 +010012957 cli_register_kw(&cli_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020012958}
12959
12960
Willy Tarreau58f10d72006-12-04 02:26:12 +010012961/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012962 * Local variables:
12963 * c-indent-level: 8
12964 * c-basic-offset: 8
12965 * End:
12966 */