blob: 699017619499c1b79095e1cb50899e0b82b20c61 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010028#include <common/base64.h>
Frédéric Lécaillea41d5312018-01-29 12:05:07 +010029#include <common/cfgparse.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010043#include <types/cli.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020044#include <types/filters.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020045#include <types/global.h>
William Lallemand71bd11a2017-11-20 19:13:14 +010046#include <types/cache.h>
William Lallemand9ed62032016-11-21 17:49:11 +010047#include <types/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048
Willy Tarreau8797c062007-05-07 00:55:35 +020049#include <proto/acl.h>
Thierry FOURNIER322a1242015-08-19 09:07:47 +020050#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020051#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010052#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020054#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010055#include <proto/checks.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010056#include <proto/cli.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020057#include <proto/compression.h>
William Lallemand9ed62032016-11-21 17:49:11 +010058#include <proto/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020060#include <proto/filters.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020061#include <proto/frontend.h>
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020062#include <proto/h1.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020063#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010064#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010065#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020066#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020067#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010068#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020069#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020070#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010071#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020072#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010073#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020074#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020075#include <proto/pattern.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020076#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020077
Willy Tarreau522d6c02009-12-06 18:49:18 +010078const char HTTP_100[] =
79 "HTTP/1.1 100 Continue\r\n\r\n";
80
81const struct chunk http_100_chunk = {
82 .str = (char *)&HTTP_100,
83 .len = sizeof(HTTP_100)-1
84};
85
Willy Tarreaua9679ac2010-01-03 17:32:57 +010086/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020087const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010088 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010089 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020090 "Location: "; /* not terminated since it will be concatenated with the URL */
91
Willy Tarreau0f772532006-12-23 20:51:41 +010092const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010093 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010094 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010095 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010096 "Location: "; /* not terminated since it will be concatenated with the URL */
97
98/* same as 302 except that the browser MUST retry with the GET method */
99const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +0100100 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100101 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +0100102 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100103 "Location: "; /* not terminated since it will be concatenated with the URL */
104
Yves Lafon3e8d1ae2013-03-11 11:06:05 -0400105
106/* same as 302 except that the browser MUST retry with the same method */
107const char *HTTP_307 =
108 "HTTP/1.1 307 Temporary Redirect\r\n"
109 "Cache-Control: no-cache\r\n"
110 "Content-length: 0\r\n"
111 "Location: "; /* not terminated since it will be concatenated with the URL */
112
113/* same as 301 except that the browser MUST retry with the same method */
114const char *HTTP_308 =
115 "HTTP/1.1 308 Permanent Redirect\r\n"
116 "Content-length: 0\r\n"
117 "Location: "; /* not terminated since it will be concatenated with the URL */
118
Willy Tarreaubaaee002006-06-26 02:48:02 +0200119/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
120const char *HTTP_401_fmt =
121 "HTTP/1.0 401 Unauthorized\r\n"
122 "Cache-Control: no-cache\r\n"
123 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200124 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200125 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
126 "\r\n"
127 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
128
Willy Tarreau844a7e72010-01-31 21:46:18 +0100129const char *HTTP_407_fmt =
130 "HTTP/1.0 407 Unauthorized\r\n"
131 "Cache-Control: no-cache\r\n"
132 "Connection: close\r\n"
133 "Content-Type: text/html\r\n"
134 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
135 "\r\n"
Godbach1f1fae62014-12-17 16:32:05 +0800136 "<html><body><h1>407 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
Willy Tarreau844a7e72010-01-31 21:46:18 +0100137
Willy Tarreau0f772532006-12-23 20:51:41 +0100138
139const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200140 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100141 [HTTP_ERR_400] = 400,
142 [HTTP_ERR_403] = 403,
CJ Ess108b1dd2015-04-07 12:03:37 -0400143 [HTTP_ERR_405] = 405,
Willy Tarreau0f772532006-12-23 20:51:41 +0100144 [HTTP_ERR_408] = 408,
Olivier Houchard51a76d82017-10-02 16:12:07 +0200145 [HTTP_ERR_425] = 425,
CJ Ess108b1dd2015-04-07 12:03:37 -0400146 [HTTP_ERR_429] = 429,
Willy Tarreau0f772532006-12-23 20:51:41 +0100147 [HTTP_ERR_500] = 500,
148 [HTTP_ERR_502] = 502,
149 [HTTP_ERR_503] = 503,
150 [HTTP_ERR_504] = 504,
151};
152
Willy Tarreau80587432006-12-24 17:47:20 +0100153static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200154 [HTTP_ERR_200] =
155 "HTTP/1.0 200 OK\r\n"
156 "Cache-Control: no-cache\r\n"
157 "Connection: close\r\n"
158 "Content-Type: text/html\r\n"
159 "\r\n"
160 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
161
Willy Tarreau0f772532006-12-23 20:51:41 +0100162 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100163 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100164 "Cache-Control: no-cache\r\n"
165 "Connection: close\r\n"
166 "Content-Type: text/html\r\n"
167 "\r\n"
168 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
169
170 [HTTP_ERR_403] =
171 "HTTP/1.0 403 Forbidden\r\n"
172 "Cache-Control: no-cache\r\n"
173 "Connection: close\r\n"
174 "Content-Type: text/html\r\n"
175 "\r\n"
176 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
177
CJ Ess108b1dd2015-04-07 12:03:37 -0400178 [HTTP_ERR_405] =
179 "HTTP/1.0 405 Method Not Allowed\r\n"
180 "Cache-Control: no-cache\r\n"
181 "Connection: close\r\n"
182 "Content-Type: text/html\r\n"
183 "\r\n"
184 "<html><body><h1>405 Method Not Allowed</h1>\nA request was made of a resource using a request method not supported by that resource\n</body></html>\n",
185
Willy Tarreau0f772532006-12-23 20:51:41 +0100186 [HTTP_ERR_408] =
187 "HTTP/1.0 408 Request Time-out\r\n"
188 "Cache-Control: no-cache\r\n"
189 "Connection: close\r\n"
190 "Content-Type: text/html\r\n"
191 "\r\n"
192 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
193
Olivier Houchard51a76d82017-10-02 16:12:07 +0200194 [HTTP_ERR_425] =
195 "HTTP/1.0 425 Too Early\r\n"
196 "Cache-Control: no-cache\r\n"
197 "Connection: close\r\n"
198 "Content-Type: text/html\r\n"
199 "\r\n"
200 "<html><body><h1>425 Too Early</h1>\nYour browser sent early data.\n</body></html>\n",
201
CJ Ess108b1dd2015-04-07 12:03:37 -0400202 [HTTP_ERR_429] =
203 "HTTP/1.0 429 Too Many Requests\r\n"
204 "Cache-Control: no-cache\r\n"
205 "Connection: close\r\n"
206 "Content-Type: text/html\r\n"
207 "\r\n"
208 "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n",
209
Willy Tarreau0f772532006-12-23 20:51:41 +0100210 [HTTP_ERR_500] =
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200211 "HTTP/1.0 500 Internal Server Error\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100212 "Cache-Control: no-cache\r\n"
213 "Connection: close\r\n"
214 "Content-Type: text/html\r\n"
215 "\r\n"
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200216 "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
Willy Tarreau0f772532006-12-23 20:51:41 +0100217
218 [HTTP_ERR_502] =
219 "HTTP/1.0 502 Bad Gateway\r\n"
220 "Cache-Control: no-cache\r\n"
221 "Connection: close\r\n"
222 "Content-Type: text/html\r\n"
223 "\r\n"
224 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
225
226 [HTTP_ERR_503] =
227 "HTTP/1.0 503 Service Unavailable\r\n"
228 "Cache-Control: no-cache\r\n"
229 "Connection: close\r\n"
230 "Content-Type: text/html\r\n"
231 "\r\n"
232 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
233
234 [HTTP_ERR_504] =
235 "HTTP/1.0 504 Gateway Time-out\r\n"
236 "Cache-Control: no-cache\r\n"
237 "Connection: close\r\n"
238 "Content-Type: text/html\r\n"
239 "\r\n"
240 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
241
242};
243
Cyril Bonté19979e12012-04-04 12:57:21 +0200244/* status codes available for the stats admin page (strictly 4 chars length) */
245const char *stat_status_codes[STAT_STATUS_SIZE] = {
246 [STAT_STATUS_DENY] = "DENY",
247 [STAT_STATUS_DONE] = "DONE",
248 [STAT_STATUS_ERRP] = "ERRP",
249 [STAT_STATUS_EXCD] = "EXCD",
250 [STAT_STATUS_NONE] = "NONE",
251 [STAT_STATUS_PART] = "PART",
252 [STAT_STATUS_UNKN] = "UNKN",
253};
254
255
William Lallemand73025dd2014-04-24 14:38:37 +0200256/* List head of all known action keywords for "http-request" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200257struct action_kw_list http_req_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200258 .list = LIST_HEAD_INIT(http_req_keywords.list)
259};
260
261/* List head of all known action keywords for "http-response" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200262struct action_kw_list http_res_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200263 .list = LIST_HEAD_INIT(http_res_keywords.list)
264};
265
Willy Tarreau80587432006-12-24 17:47:20 +0100266/* We must put the messages here since GCC cannot initialize consts depending
267 * on strlen().
268 */
269struct chunk http_err_chunks[HTTP_ERR_SIZE];
270
Willy Tarreaua890d072013-04-02 12:01:06 +0200271/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100272static THREAD_LOCAL struct hdr_ctx static_hdr_ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +0200273
Willy Tarreau42250582007-04-01 01:30:43 +0200274#define FD_SETS_ARE_BITFIELDS
275#ifdef FD_SETS_ARE_BITFIELDS
276/*
277 * This map is used with all the FD_* macros to check whether a particular bit
278 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
279 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
280 * byte should be encoded. Be careful to always pass bytes from 0 to 255
281 * exclusively to the macros.
282 */
283fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
284fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100285fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200286
287#else
288#error "Check if your OS uses bitfields for fd_sets"
289#endif
290
Willy Tarreau87b09662015-04-03 00:22:06 +0200291static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn);
Willy Tarreau0b748332014-04-29 00:13:29 +0200292
David Carlier7365f7d2016-04-04 11:54:42 +0100293static inline int http_msg_forward_body(struct stream *s, struct http_msg *msg);
294static inline int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +0100295
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200296/* This function returns a reason associated with the HTTP status.
297 * This function never fails, a message is always returned.
298 */
299const char *get_reason(unsigned int status)
300{
301 switch (status) {
302 case 100: return "Continue";
303 case 101: return "Switching Protocols";
304 case 102: return "Processing";
305 case 200: return "OK";
306 case 201: return "Created";
307 case 202: return "Accepted";
308 case 203: return "Non-Authoritative Information";
309 case 204: return "No Content";
310 case 205: return "Reset Content";
311 case 206: return "Partial Content";
312 case 207: return "Multi-Status";
313 case 210: return "Content Different";
314 case 226: return "IM Used";
315 case 300: return "Multiple Choices";
316 case 301: return "Moved Permanently";
317 case 302: return "Moved Temporarily";
318 case 303: return "See Other";
319 case 304: return "Not Modified";
320 case 305: return "Use Proxy";
321 case 307: return "Temporary Redirect";
322 case 308: return "Permanent Redirect";
323 case 310: return "Too many Redirects";
324 case 400: return "Bad Request";
325 case 401: return "Unauthorized";
326 case 402: return "Payment Required";
327 case 403: return "Forbidden";
328 case 404: return "Not Found";
329 case 405: return "Method Not Allowed";
330 case 406: return "Not Acceptable";
331 case 407: return "Proxy Authentication Required";
332 case 408: return "Request Time-out";
333 case 409: return "Conflict";
334 case 410: return "Gone";
335 case 411: return "Length Required";
336 case 412: return "Precondition Failed";
337 case 413: return "Request Entity Too Large";
338 case 414: return "Request-URI Too Long";
339 case 415: return "Unsupported Media Type";
340 case 416: return "Requested range unsatisfiable";
341 case 417: return "Expectation failed";
342 case 418: return "I'm a teapot";
343 case 422: return "Unprocessable entity";
344 case 423: return "Locked";
345 case 424: return "Method failure";
Olivier Houchard51a76d82017-10-02 16:12:07 +0200346 case 425: return "Too Early";
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200347 case 426: return "Upgrade Required";
348 case 428: return "Precondition Required";
349 case 429: return "Too Many Requests";
350 case 431: return "Request Header Fields Too Large";
351 case 449: return "Retry With";
352 case 450: return "Blocked by Windows Parental Controls";
353 case 451: return "Unavailable For Legal Reasons";
354 case 456: return "Unrecoverable Error";
355 case 499: return "client has closed connection";
356 case 500: return "Internal Server Error";
357 case 501: return "Not Implemented";
Jarno Huuskonen59af2df2016-12-28 10:49:01 +0200358 case 502: return "Bad Gateway or Proxy Error";
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200359 case 503: return "Service Unavailable";
360 case 504: return "Gateway Time-out";
361 case 505: return "HTTP Version not supported";
362 case 506: return "Variant also negociate";
363 case 507: return "Insufficient storage";
364 case 508: return "Loop detected";
365 case 509: return "Bandwidth Limit Exceeded";
366 case 510: return "Not extended";
367 case 511: return "Network authentication required";
368 case 520: return "Web server is returning an unknown error";
369 default:
370 switch (status) {
371 case 100 ... 199: return "Informational";
372 case 200 ... 299: return "Success";
373 case 300 ... 399: return "Redirection";
374 case 400 ... 499: return "Client Error";
375 case 500 ... 599: return "Server Error";
376 default: return "Other";
377 }
378 }
379}
380
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200381/* This function returns HTTP_ERR_<num> (enum) matching http status code.
382 * Returned value should match codes from http_err_codes.
383 */
384static const int http_get_status_idx(unsigned int status)
385{
386 switch (status) {
387 case 200: return HTTP_ERR_200;
388 case 400: return HTTP_ERR_400;
389 case 403: return HTTP_ERR_403;
390 case 405: return HTTP_ERR_405;
391 case 408: return HTTP_ERR_408;
Olivier Houchard51a76d82017-10-02 16:12:07 +0200392 case 425: return HTTP_ERR_425;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200393 case 429: return HTTP_ERR_429;
394 case 500: return HTTP_ERR_500;
395 case 502: return HTTP_ERR_502;
396 case 503: return HTTP_ERR_503;
397 case 504: return HTTP_ERR_504;
398 default: return HTTP_ERR_500;
399 }
400}
401
Willy Tarreau80587432006-12-24 17:47:20 +0100402void init_proto_http()
403{
Willy Tarreau42250582007-04-01 01:30:43 +0200404 int i;
405 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100406 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200407
Willy Tarreau80587432006-12-24 17:47:20 +0100408 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
409 if (!http_err_msgs[msg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100410 ha_alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
Willy Tarreau80587432006-12-24 17:47:20 +0100411 abort();
412 }
413
414 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
415 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
416 }
Willy Tarreau42250582007-04-01 01:30:43 +0200417
418 /* initialize the log header encoding map : '{|}"#' should be encoded with
419 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
420 * URL encoding only requires '"', '#' to be encoded as well as non-
421 * printable characters above.
422 */
423 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
424 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100425 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200426 for (i = 0; i < 32; i++) {
427 FD_SET(i, hdr_encode_map);
428 FD_SET(i, url_encode_map);
429 }
430 for (i = 127; i < 256; i++) {
431 FD_SET(i, hdr_encode_map);
432 FD_SET(i, url_encode_map);
433 }
434
435 tmp = "\"#{|}";
436 while (*tmp) {
437 FD_SET(*tmp, hdr_encode_map);
438 tmp++;
439 }
440
441 tmp = "\"#";
442 while (*tmp) {
443 FD_SET(*tmp, url_encode_map);
444 tmp++;
445 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200446
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100447 /* initialize the http header encoding map. The draft httpbis define the
448 * header content as:
449 *
450 * HTTP-message = start-line
451 * *( header-field CRLF )
452 * CRLF
453 * [ message-body ]
454 * header-field = field-name ":" OWS field-value OWS
455 * field-value = *( field-content / obs-fold )
456 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
457 * obs-fold = CRLF 1*( SP / HTAB )
458 * field-vchar = VCHAR / obs-text
459 * VCHAR = %x21-7E
460 * obs-text = %x80-FF
461 *
462 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
463 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
464 * "obs-fold" is volontary forgotten because haproxy remove this.
465 */
466 memset(http_encode_map, 0, sizeof(http_encode_map));
467 for (i = 0x00; i <= 0x08; i++)
468 FD_SET(i, http_encode_map);
469 for (i = 0x0a; i <= 0x1f; i++)
470 FD_SET(i, http_encode_map);
471 FD_SET(0x7f, http_encode_map);
472
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200473 /* memory allocations */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100474 pool_head_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
475 pool_head_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100476}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200477
Willy Tarreau53b6c742006-12-17 13:37:46 +0100478/*
479 * We have 26 list of methods (1 per first letter), each of which can have
480 * up to 3 entries (2 valid, 1 null).
481 */
482struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100483 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100484 int len;
485 const char text[8];
486};
487
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100488const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100489 ['C' - 'A'] = {
490 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
491 },
492 ['D' - 'A'] = {
493 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
494 },
495 ['G' - 'A'] = {
496 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
497 },
498 ['H' - 'A'] = {
499 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
500 },
Christopher Fauletd57ad642015-07-31 14:26:57 +0200501 ['O' - 'A'] = {
502 [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" },
503 },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100504 ['P' - 'A'] = {
505 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
506 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
507 },
508 ['T' - 'A'] = {
509 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
510 },
511 /* rest is empty like this :
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200512 * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100513 */
514};
515
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100516const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100517 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
518 [HTTP_METH_GET] = { "GET", 3 },
519 [HTTP_METH_HEAD] = { "HEAD", 4 },
520 [HTTP_METH_POST] = { "POST", 4 },
521 [HTTP_METH_PUT] = { "PUT", 3 },
522 [HTTP_METH_DELETE] = { "DELETE", 6 },
523 [HTTP_METH_TRACE] = { "TRACE", 5 },
524 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
525};
526
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100527/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100528 * Adds a header and its CRLF at the tail of the message's buffer, just before
529 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100530 * The header is also automatically added to the index <hdr_idx>, and the end
531 * of headers is automatically adjusted. The number of bytes added is returned
532 * on success, otherwise <0 is returned indicating an error.
533 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100534int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100535{
536 int bytes, len;
537
538 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200539 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100540 if (!bytes)
541 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100542 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100543 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
544}
545
546/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100547 * Adds a header and its CRLF at the tail of the message's buffer, just before
548 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100549 * the buffer is only opened and the space reserved, but nothing is copied.
550 * The header is also automatically added to the index <hdr_idx>, and the end
551 * of headers is automatically adjusted. The number of bytes added is returned
552 * on success, otherwise <0 is returned indicating an error.
553 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100554int http_header_add_tail2(struct http_msg *msg,
555 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100556{
557 int bytes;
558
Willy Tarreau9b28e032012-10-12 23:49:43 +0200559 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100560 if (!bytes)
561 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100562 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100563 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
564}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200565
566/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100567 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
568 * If so, returns the position of the first non-space character relative to
569 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
570 * to return a pointer to the place after the first space. Returns 0 if the
571 * header name does not match. Checks are case-insensitive.
572 */
573int http_header_match2(const char *hdr, const char *end,
574 const char *name, int len)
575{
576 const char *val;
577
578 if (hdr + len >= end)
579 return 0;
580 if (hdr[len] != ':')
581 return 0;
582 if (strncasecmp(hdr, name, len) != 0)
583 return 0;
584 val = hdr + len + 1;
585 while (val < end && HTTP_IS_SPHT(*val))
586 val++;
587 if ((val >= end) && (len + 2 <= end - hdr))
588 return len + 2; /* we may replace starting from second space */
589 return val - hdr;
590}
591
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200592/* Find the first or next occurrence of header <name> in message buffer <sol>
593 * using headers index <idx>, and return it in the <ctx> structure. This
594 * structure holds everything necessary to use the header and find next
595 * occurrence. If its <idx> member is 0, the header is searched from the
596 * beginning. Otherwise, the next occurrence is returned. The function returns
597 * 1 when it finds a value, and 0 when there is no more. It is very similar to
598 * http_find_header2() except that it is designed to work with full-line headers
599 * whose comma is not a delimiter but is part of the syntax. As a special case,
600 * if ctx->val is NULL when searching for a new values of a header, the current
601 * header is rescanned. This allows rescanning after a header deletion.
602 */
603int http_find_full_header2(const char *name, int len,
604 char *sol, struct hdr_idx *idx,
605 struct hdr_ctx *ctx)
606{
607 char *eol, *sov;
608 int cur_idx, old_idx;
609
610 cur_idx = ctx->idx;
611 if (cur_idx) {
612 /* We have previously returned a header, let's search another one */
613 sol = ctx->line;
614 eol = sol + idx->v[cur_idx].len;
615 goto next_hdr;
616 }
617
618 /* first request for this header */
619 sol += hdr_idx_first_pos(idx);
620 old_idx = 0;
621 cur_idx = hdr_idx_first_idx(idx);
622 while (cur_idx) {
623 eol = sol + idx->v[cur_idx].len;
624
625 if (len == 0) {
626 /* No argument was passed, we want any header.
627 * To achieve this, we simply build a fake request. */
628 while (sol + len < eol && sol[len] != ':')
629 len++;
630 name = sol;
631 }
632
633 if ((len < eol - sol) &&
634 (sol[len] == ':') &&
635 (strncasecmp(sol, name, len) == 0)) {
636 ctx->del = len;
637 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100638 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200639 sov++;
640
641 ctx->line = sol;
642 ctx->prev = old_idx;
643 ctx->idx = cur_idx;
644 ctx->val = sov - sol;
645 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100646 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200647 eol--;
648 ctx->tws++;
649 }
650 ctx->vlen = eol - sov;
651 return 1;
652 }
653 next_hdr:
654 sol = eol + idx->v[cur_idx].cr + 1;
655 old_idx = cur_idx;
656 cur_idx = idx->v[cur_idx].next;
657 }
658 return 0;
659}
660
Willy Tarreauc90dc232015-02-20 13:51:36 +0100661/* Find the first or next header field in message buffer <sol> using headers
662 * index <idx>, and return it in the <ctx> structure. This structure holds
663 * everything necessary to use the header and find next occurrence. If its
664 * <idx> member is 0, the first header is retrieved. Otherwise, the next
665 * occurrence is returned. The function returns 1 when it finds a value, and
666 * 0 when there is no more. It is equivalent to http_find_full_header2() with
667 * no header name.
668 */
669int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
670{
671 char *eol, *sov;
672 int cur_idx, old_idx;
673 int len;
674
675 cur_idx = ctx->idx;
676 if (cur_idx) {
677 /* We have previously returned a header, let's search another one */
678 sol = ctx->line;
679 eol = sol + idx->v[cur_idx].len;
680 goto next_hdr;
681 }
682
683 /* first request for this header */
684 sol += hdr_idx_first_pos(idx);
685 old_idx = 0;
686 cur_idx = hdr_idx_first_idx(idx);
687 while (cur_idx) {
688 eol = sol + idx->v[cur_idx].len;
689
690 len = 0;
691 while (1) {
692 if (len >= eol - sol)
693 goto next_hdr;
694 if (sol[len] == ':')
695 break;
696 len++;
697 }
698
699 ctx->del = len;
700 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100701 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreauc90dc232015-02-20 13:51:36 +0100702 sov++;
703
704 ctx->line = sol;
705 ctx->prev = old_idx;
706 ctx->idx = cur_idx;
707 ctx->val = sov - sol;
708 ctx->tws = 0;
709
Willy Tarreau2235b262016-11-05 15:50:20 +0100710 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreauc90dc232015-02-20 13:51:36 +0100711 eol--;
712 ctx->tws++;
713 }
714 ctx->vlen = eol - sov;
715 return 1;
716
717 next_hdr:
718 sol = eol + idx->v[cur_idx].cr + 1;
719 old_idx = cur_idx;
720 cur_idx = idx->v[cur_idx].next;
721 }
722 return 0;
723}
724
Lukas Tribus23953682017-04-28 13:24:30 +0000725/* Find the end of the header value contained between <s> and <e>. See RFC7230,
726 * par 3.2 for more information. Note that it requires a valid header to return
Willy Tarreau68085d82010-01-18 14:54:04 +0100727 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200728 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100729char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200730{
731 int quoted, qdpair;
732
733 quoted = qdpair = 0;
Willy Tarreaue6d9c212016-11-05 18:23:38 +0100734
735#if defined(__x86_64__) || \
736 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
737 defined(__ARM_ARCH_7A__)
738 /* speedup: skip everything not a comma nor a double quote */
739 for (; s <= e - sizeof(int); s += sizeof(int)) {
740 unsigned int c = *(int *)s; // comma
741 unsigned int q = c; // quote
742
743 c ^= 0x2c2c2c2c; // contains one zero on a comma
744 q ^= 0x22222222; // contains one zero on a quote
745
746 c = (c - 0x01010101) & ~c; // contains 0x80 below a comma
747 q = (q - 0x01010101) & ~q; // contains 0x80 below a quote
748
749 if ((c | q) & 0x80808080)
750 break; // found a comma or a quote
751 }
752#endif
Willy Tarreau33a7e692007-06-10 19:45:56 +0200753 for (; s < e; s++) {
754 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200755 else if (quoted) {
756 if (*s == '\\') qdpair = 1;
757 else if (*s == '"') quoted = 0;
758 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200759 else if (*s == '"') quoted = 1;
760 else if (*s == ',') return s;
761 }
762 return s;
763}
764
765/* Find the first or next occurrence of header <name> in message buffer <sol>
766 * using headers index <idx>, and return it in the <ctx> structure. This
767 * structure holds everything necessary to use the header and find next
768 * occurrence. If its <idx> member is 0, the header is searched from the
769 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100770 * 1 when it finds a value, and 0 when there is no more. It is designed to work
771 * with headers defined as comma-separated lists. As a special case, if ctx->val
772 * is NULL when searching for a new values of a header, the current header is
773 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200774 */
775int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100776 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200777 struct hdr_ctx *ctx)
778{
Willy Tarreau68085d82010-01-18 14:54:04 +0100779 char *eol, *sov;
780 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200781
Willy Tarreau68085d82010-01-18 14:54:04 +0100782 cur_idx = ctx->idx;
783 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200784 /* We have previously returned a value, let's search
785 * another one on the same line.
786 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200787 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200788 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100789 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200790 eol = sol + idx->v[cur_idx].len;
791
792 if (sov >= eol)
793 /* no more values in this header */
794 goto next_hdr;
795
Willy Tarreau68085d82010-01-18 14:54:04 +0100796 /* values remaining for this header, skip the comma but save it
797 * for later use (eg: for header deletion).
798 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200799 sov++;
Willy Tarreau2235b262016-11-05 15:50:20 +0100800 while (sov < eol && HTTP_IS_LWS((*sov)))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200801 sov++;
802
803 goto return_hdr;
804 }
805
806 /* first request for this header */
807 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100808 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200809 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200810 while (cur_idx) {
811 eol = sol + idx->v[cur_idx].len;
812
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200813 if (len == 0) {
814 /* No argument was passed, we want any header.
815 * To achieve this, we simply build a fake request. */
816 while (sol + len < eol && sol[len] != ':')
817 len++;
818 name = sol;
819 }
820
Willy Tarreau33a7e692007-06-10 19:45:56 +0200821 if ((len < eol - sol) &&
822 (sol[len] == ':') &&
823 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100824 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200825 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100826 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200827 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100828
Willy Tarreau33a7e692007-06-10 19:45:56 +0200829 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100830 ctx->prev = old_idx;
831 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200832 ctx->idx = cur_idx;
833 ctx->val = sov - sol;
834
835 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200836 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100837 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200838 eol--;
839 ctx->tws++;
840 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200841 ctx->vlen = eol - sov;
842 return 1;
843 }
844 next_hdr:
845 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100846 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200847 cur_idx = idx->v[cur_idx].next;
848 }
849 return 0;
850}
851
852int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100853 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200854 struct hdr_ctx *ctx)
855{
856 return http_find_header2(name, strlen(name), sol, idx, ctx);
857}
858
Willy Tarreau68085d82010-01-18 14:54:04 +0100859/* Remove one value of a header. This only works on a <ctx> returned by one of
860 * the http_find_header functions. The value is removed, as well as surrounding
861 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100862 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100863 * message <msg>. The new index is returned. If it is zero, it means there is
864 * no more header, so any processing may stop. The ctx is always left in a form
865 * that can be handled by http_find_header2() to find next occurrence.
866 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100867int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100868{
869 int cur_idx = ctx->idx;
870 char *sol = ctx->line;
871 struct hdr_idx_elem *hdr;
872 int delta, skip_comma;
873
874 if (!cur_idx)
875 return 0;
876
877 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200878 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100879 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200880 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100881 http_msg_move_end(msg, delta);
882 idx->used--;
883 hdr->len = 0; /* unused entry */
884 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100885 if (idx->tail == ctx->idx)
886 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100887 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100888 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100889 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200890 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100891 return ctx->idx;
892 }
893
894 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200895 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
896 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100897 */
898
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200899 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200900 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200901 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100902 NULL, 0);
903 hdr->len += delta;
904 http_msg_move_end(msg, delta);
905 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200906 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100907 return ctx->idx;
908}
909
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100910/* This function handles a server error at the stream interface level. The
911 * stream interface is assumed to be already in a closed state. An optional
Willy Tarreau2019f952017-03-14 11:07:31 +0100912 * message is copied into the input buffer.
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100913 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100914 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200915 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200916static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2019f952017-03-14 11:07:31 +0100917 int err, int finst, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200918{
Willy Tarreau2019f952017-03-14 11:07:31 +0100919 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100920 channel_auto_read(si_oc(si));
921 channel_abort(si_oc(si));
922 channel_auto_close(si_oc(si));
923 channel_erase(si_oc(si));
924 channel_auto_close(si_ic(si));
925 channel_auto_read(si_ic(si));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200926 if (msg)
Willy Tarreau06d80a92017-10-19 14:32:15 +0200927 co_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaue7dff022015-04-03 01:14:29 +0200928 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200929 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200930 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200931 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200932}
933
Willy Tarreau87b09662015-04-03 00:22:06 +0200934/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100935 * and message.
936 */
937
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200938struct chunk *http_error_message(struct stream *s)
Willy Tarreau80587432006-12-24 17:47:20 +0100939{
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200940 const int msgnum = http_get_status_idx(s->txn->status);
941
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200942 if (s->be->errmsg[msgnum].str)
943 return &s->be->errmsg[msgnum];
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200944 else if (strm_fe(s)->errmsg[msgnum].str)
945 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100946 else
947 return &http_err_chunks[msgnum];
948}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200949
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100950void
951http_reply_and_close(struct stream *s, short status, struct chunk *msg)
952{
Christopher Fauletd7c91962015-04-30 11:48:27 +0200953 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +0100954 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100955 stream_int_retnclose(&s->si[0], msg);
956}
957
Willy Tarreau53b6c742006-12-17 13:37:46 +0100958/*
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200959 * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown
960 * ones.
Willy Tarreau53b6c742006-12-17 13:37:46 +0100961 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100962enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100963{
964 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100965 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100966
967 m = ((unsigned)*str - 'A');
968
969 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100970 for (h = http_methods[m]; h->len > 0; h++) {
971 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100972 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100973 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100974 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100975 };
Willy Tarreau53b6c742006-12-17 13:37:46 +0100976 }
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200977 return HTTP_METH_OTHER;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100978}
979
Willy Tarreau21d2af32008-02-14 20:25:24 +0100980/* Parse the URI from the given transaction (which is assumed to be in request
981 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
982 * It is returned otherwise.
983 */
Thierry FOURNIER3c331782015-09-17 19:33:35 +0200984char *http_get_path(struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +0100985{
986 char *ptr, *end;
987
Willy Tarreau9b28e032012-10-12 23:49:43 +0200988 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100989 end = ptr + txn->req.sl.rq.u_l;
990
991 if (ptr >= end)
992 return NULL;
993
Lukas Tribus23953682017-04-28 13:24:30 +0000994 /* RFC7230, par. 2.7 :
Willy Tarreau21d2af32008-02-14 20:25:24 +0100995 * Request-URI = "*" | absuri | abspath | authority
996 */
997
998 if (*ptr == '*')
999 return NULL;
1000
1001 if (isalpha((unsigned char)*ptr)) {
1002 /* this is a scheme as described by RFC3986, par. 3.1 */
1003 ptr++;
1004 while (ptr < end &&
1005 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
1006 ptr++;
1007 /* skip '://' */
1008 if (ptr == end || *ptr++ != ':')
1009 return NULL;
1010 if (ptr == end || *ptr++ != '/')
1011 return NULL;
1012 if (ptr == end || *ptr++ != '/')
1013 return NULL;
1014 }
1015 /* skip [user[:passwd]@]host[:[port]] */
1016
1017 while (ptr < end && *ptr != '/')
1018 ptr++;
1019
1020 if (ptr == end)
1021 return NULL;
1022
1023 /* OK, we got the '/' ! */
1024 return ptr;
1025}
1026
William Lallemand65ad6e12014-01-31 15:08:02 +01001027/* Parse the URI from the given string and look for the "/" beginning the PATH.
1028 * If not found, return NULL. It is returned otherwise.
1029 */
1030static char *
1031http_get_path_from_string(char *str)
1032{
1033 char *ptr = str;
1034
1035 /* RFC2616, par. 5.1.2 :
1036 * Request-URI = "*" | absuri | abspath | authority
1037 */
1038
1039 if (*ptr == '*')
1040 return NULL;
1041
1042 if (isalpha((unsigned char)*ptr)) {
1043 /* this is a scheme as described by RFC3986, par. 3.1 */
1044 ptr++;
1045 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
1046 ptr++;
1047 /* skip '://' */
1048 if (*ptr == '\0' || *ptr++ != ':')
1049 return NULL;
1050 if (*ptr == '\0' || *ptr++ != '/')
1051 return NULL;
1052 if (*ptr == '\0' || *ptr++ != '/')
1053 return NULL;
1054 }
1055 /* skip [user[:passwd]@]host[:[port]] */
1056
1057 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
1058 ptr++;
1059
1060 if (*ptr == '\0' || *ptr == ' ')
1061 return NULL;
1062
1063 /* OK, we got the '/' ! */
1064 return ptr;
1065}
1066
Willy Tarreau71241ab2012-12-27 11:30:54 +01001067/* Returns a 302 for a redirectable request that reaches a server working in
1068 * in redirect mode. This may only be called just after the stream interface
1069 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
1070 * follow normal proxy processing. NOTE: this function is designed to support
1071 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001072 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001073void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001074{
1075 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001076 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001077 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001078 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001079
1080 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001081 trash.len = strlen(HTTP_302);
1082 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001083
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001084 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001085
Willy Tarreauefb453c2008-10-26 20:49:47 +01001086 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001087 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001088 return;
1089
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001090 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001091 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001092 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
1093 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001094 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001095
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001096 /* 3: add the request URI. Since it was already forwarded, we need
1097 * to temporarily rewind the buffer.
1098 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001099 txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001100 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001101
Willy Tarreauefb453c2008-10-26 20:49:47 +01001102 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001103 len = buffer_count(s->req.buf, path, b_ptr(s->req.buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001104
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001105 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001106
Willy Tarreauefb453c2008-10-26 20:49:47 +01001107 if (!path)
1108 return;
1109
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001110 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001111 return;
1112
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001113 memcpy(trash.str + trash.len, path, len);
1114 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001115
1116 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001117 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1118 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001119 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001120 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1121 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001122 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001123
1124 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001125 si_shutr(si);
1126 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001127 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001128 si->state = SI_ST_CLO;
1129
1130 /* send the message */
Willy Tarreau2019f952017-03-14 11:07:31 +01001131 txn->status = 302;
1132 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001133
1134 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001135 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001136 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001137}
1138
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001139/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001140 * that the server side is closed. Note that err_type is actually a
1141 * bitmask, where almost only aborts may be cumulated with other
1142 * values. We consider that aborted operations are more important
1143 * than timeouts or errors due to the fact that nobody else in the
1144 * logs might explain incomplete retries. All others should avoid
1145 * being cumulated. It should normally not be possible to have multiple
1146 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001147 * Note that connection errors appearing on the second request of a keep-alive
1148 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001149 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001150void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001151{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001152 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001153
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001154 /* set s->txn->status for http_error_message(s) */
1155 s->txn->status = 503;
1156
Willy Tarreauefb453c2008-10-26 20:49:47 +01001157 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001158 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001159 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001160 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001161 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001162 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001163 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001164 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001165 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001166 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001167 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001168 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001169 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001170 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001171 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001172 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001173 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001174 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001175 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001176 (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001177 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001178 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001179 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001180 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001181 http_error_message(s));
1182 else { /* SI_ET_CONN_OTHER and others */
1183 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001184 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001185 http_error_message(s));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001186 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001187}
1188
Willy Tarreau42250582007-04-01 01:30:43 +02001189extern const char sess_term_cond[8];
1190extern const char sess_fin_state[8];
1191extern const char *monthname[12];
Willy Tarreaubafbe012017-11-24 17:34:44 +01001192struct pool_head *pool_head_http_txn;
1193struct pool_head *pool_head_requri;
1194struct pool_head *pool_head_capture = NULL;
1195struct pool_head *pool_head_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001196
Willy Tarreau117f59e2007-03-04 18:17:17 +01001197/*
1198 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001199 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001200 */
1201void capture_headers(char *som, struct hdr_idx *idx,
1202 char **cap, struct cap_hdr *cap_hdr)
1203{
1204 char *eol, *sol, *col, *sov;
1205 int cur_idx;
1206 struct cap_hdr *h;
1207 int len;
1208
1209 sol = som + hdr_idx_first_pos(idx);
1210 cur_idx = hdr_idx_first_idx(idx);
1211
1212 while (cur_idx) {
1213 eol = sol + idx->v[cur_idx].len;
1214
1215 col = sol;
1216 while (col < eol && *col != ':')
1217 col++;
1218
1219 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01001220 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001221 sov++;
1222
1223 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001224 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001225 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1226 if (cap[h->index] == NULL)
1227 cap[h->index] =
Willy Tarreaubafbe012017-11-24 17:34:44 +01001228 pool_alloc(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001229
1230 if (cap[h->index] == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001231 ha_alert("HTTP capture : out of memory.\n");
Willy Tarreau117f59e2007-03-04 18:17:17 +01001232 continue;
1233 }
1234
1235 len = eol - sov;
1236 if (len > h->len)
1237 len = h->len;
1238
1239 memcpy(cap[h->index], sov, len);
1240 cap[h->index][len]=0;
1241 }
1242 }
1243 sol = eol + idx->v[cur_idx].cr + 1;
1244 cur_idx = idx->v[cur_idx].next;
1245 }
1246}
1247
Willy Tarreaubaaee002006-06-26 02:48:02 +02001248/*
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001249 * Returns the data from Authorization header. Function may be called more
1250 * than once so data is stored in txn->auth_data. When no header is found
1251 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001252 * searching again for something we are unable to find anyway. However, if
1253 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001254 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001255 */
1256
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001257int
Willy Tarreau87b09662015-04-03 00:22:06 +02001258get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001259{
1260
Willy Tarreaueee5b512015-04-03 23:46:31 +02001261 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001262 struct chunk auth_method;
1263 struct hdr_ctx ctx;
1264 char *h, *p;
1265 int len;
1266
1267#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001268 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001269#endif
1270
1271 if (txn->auth.method == HTTP_AUTH_WRONG)
1272 return 0;
1273
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001274 txn->auth.method = HTTP_AUTH_WRONG;
1275
1276 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001277
1278 if (txn->flags & TX_USE_PX_CONN) {
1279 h = "Proxy-Authorization";
1280 len = strlen(h);
1281 } else {
1282 h = "Authorization";
1283 len = strlen(h);
1284 }
1285
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001286 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001287 return 0;
1288
1289 h = ctx.line + ctx.val;
1290
1291 p = memchr(h, ' ', ctx.vlen);
Willy Tarreau5c557d12016-03-13 08:17:02 +01001292 len = p - h;
1293 if (!p || len <= 0)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001294 return 0;
1295
David Carlier7365f7d2016-04-04 11:54:42 +01001296 if (chunk_initlen(&auth_method, h, 0, len) != 1)
1297 return 0;
1298
Willy Tarreau5c557d12016-03-13 08:17:02 +01001299 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001300
1301 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
Christopher Faulet6988f672017-07-27 15:18:52 +02001302 struct chunk *http_auth = get_trash_chunk();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001303
1304 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Christopher Faulet6988f672017-07-27 15:18:52 +02001305 http_auth->str, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001306
1307 if (len < 0)
1308 return 0;
1309
1310
Christopher Faulet6988f672017-07-27 15:18:52 +02001311 http_auth->str[len] = '\0';
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001312
Christopher Faulet6988f672017-07-27 15:18:52 +02001313 p = strchr(http_auth->str, ':');
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001314
1315 if (!p)
1316 return 0;
1317
Christopher Faulet6988f672017-07-27 15:18:52 +02001318 txn->auth.user = http_auth->str;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001319 *p = '\0';
1320 txn->auth.pass = p+1;
1321
1322 txn->auth.method = HTTP_AUTH_BASIC;
1323 return 1;
1324 }
1325
1326 return 0;
1327}
1328
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001329
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001330/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1331 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1332 * nothing is done and 1 is returned.
1333 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001334static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001335{
1336 int delta;
1337 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001338 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001339
1340 if (msg->sl.rq.v_l != 0)
1341 return 1;
1342
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001343 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1344 if (txn->meth != HTTP_METH_GET)
1345 return 0;
1346
Willy Tarreau9b28e032012-10-12 23:49:43 +02001347 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001348
1349 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001350 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1351 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001352 }
1353 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001354 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001355 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001356 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001357 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001358 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001359 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001360 NULL, NULL);
1361 if (unlikely(!cur_end))
1362 return 0;
1363
1364 /* we have a full HTTP/1.0 request now and we know that
1365 * we have either a CR or an LF at <ptr>.
1366 */
1367 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1368 return 1;
1369}
1370
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001371/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001372 * and "keep-alive" values. If we already know that some headers may safely
1373 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001374 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1375 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001376 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001377 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1378 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1379 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001380 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001381 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001382void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001383{
Willy Tarreau5b154472009-12-21 20:11:07 +01001384 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001385 const char *hdr_val = "Connection";
1386 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001387
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001388 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001389 return;
1390
Willy Tarreau88d349d2010-01-25 12:15:43 +01001391 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1392 hdr_val = "Proxy-Connection";
1393 hdr_len = 16;
1394 }
1395
Willy Tarreau5b154472009-12-21 20:11:07 +01001396 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001397 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001398 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001399 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1400 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001401 if (to_del & 2)
1402 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001403 else
1404 txn->flags |= TX_CON_KAL_SET;
1405 }
1406 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1407 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001408 if (to_del & 1)
1409 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001410 else
1411 txn->flags |= TX_CON_CLO_SET;
1412 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001413 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1414 txn->flags |= TX_HDR_CONN_UPG;
1415 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001416 }
1417
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001418 txn->flags |= TX_HDR_CONN_PRS;
1419 return;
1420}
Willy Tarreau5b154472009-12-21 20:11:07 +01001421
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001422/* Apply desired changes on the Connection: header. Values may be removed and/or
1423 * added depending on the <wanted> flags, which are exclusively composed of
1424 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1425 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1426 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001427void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001428{
1429 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001430 const char *hdr_val = "Connection";
1431 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001432
1433 ctx.idx = 0;
1434
Willy Tarreau88d349d2010-01-25 12:15:43 +01001435
1436 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1437 hdr_val = "Proxy-Connection";
1438 hdr_len = 16;
1439 }
1440
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001441 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001442 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001443 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1444 if (wanted & TX_CON_KAL_SET)
1445 txn->flags |= TX_CON_KAL_SET;
1446 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001447 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001448 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001449 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1450 if (wanted & TX_CON_CLO_SET)
1451 txn->flags |= TX_CON_CLO_SET;
1452 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001453 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001454 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001455 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001456
1457 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1458 return;
1459
1460 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1461 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001462 hdr_val = "Connection: close";
1463 hdr_len = 17;
1464 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1465 hdr_val = "Proxy-Connection: close";
1466 hdr_len = 23;
1467 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001468 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001469 }
1470
1471 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1472 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001473 hdr_val = "Connection: keep-alive";
1474 hdr_len = 22;
1475 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1476 hdr_val = "Proxy-Connection: keep-alive";
1477 hdr_len = 28;
1478 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001479 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001480 }
1481 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001482}
1483
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001484/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
1485 * value is larger than 1000, it is bound to 1000. The parser consumes up to
1486 * 1 digit, one dot and 3 digits and stops on the first invalid character.
1487 * Unparsable qvalues return 1000 as "q=1.000".
1488 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02001489int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001490{
1491 int q = 1000;
1492
Willy Tarreau506c69a2014-07-08 00:59:48 +02001493 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001494 goto out;
1495 q = (*qvalue++ - '0') * 1000;
1496
1497 if (*qvalue++ != '.')
1498 goto out;
1499
Willy Tarreau506c69a2014-07-08 00:59:48 +02001500 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001501 goto out;
1502 q += (*qvalue++ - '0') * 100;
1503
Willy Tarreau506c69a2014-07-08 00:59:48 +02001504 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001505 goto out;
1506 q += (*qvalue++ - '0') * 10;
1507
Willy Tarreau506c69a2014-07-08 00:59:48 +02001508 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001509 goto out;
1510 q += (*qvalue++ - '0') * 1;
1511 out:
1512 if (q > 1000)
1513 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02001514 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02001515 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001516 return q;
1517}
William Lallemand82fe75c2012-10-23 10:25:10 +02001518
Willy Tarreau87b09662015-04-03 00:22:06 +02001519void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001520{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001521 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001522 int tmp = TX_CON_WANT_KAL;
1523
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001524 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
1525 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001526 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
1527 tmp = TX_CON_WANT_TUN;
1528
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001529 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001530 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
1531 tmp = TX_CON_WANT_TUN;
1532 }
1533
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001534 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001535 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
1536 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001537 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001538 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
1539 tmp = TX_CON_WANT_CLO;
1540 else
1541 tmp = TX_CON_WANT_SCL;
1542 }
1543
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001544 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001545 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
1546 tmp = TX_CON_WANT_CLO;
1547
1548 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
1549 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
1550
1551 if (!(txn->flags & TX_HDR_CONN_PRS) &&
1552 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
1553 /* parse the Connection header and possibly clean it */
1554 int to_del = 0;
1555 if ((msg->flags & HTTP_MSGF_VER_11) ||
1556 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001557 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001558 to_del |= 2; /* remove "keep-alive" */
1559 if (!(msg->flags & HTTP_MSGF_VER_11))
1560 to_del |= 1; /* remove "close" */
1561 http_parse_connection_header(txn, msg, to_del);
1562 }
1563
1564 /* check if client or config asks for explicit close in KAL/SCL */
1565 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
1566 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
1567 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
1568 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
1569 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001570 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001571 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1572}
William Lallemand82fe75c2012-10-23 10:25:10 +02001573
Willy Tarreaud787e662009-07-07 10:14:51 +02001574/* This stream analyser waits for a complete HTTP request. It returns 1 if the
1575 * processing can continue on next analysers, or zero if it either needs more
1576 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001577 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02001578 * when it has nothing left to do, and may remove any analyser when it wants to
1579 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001580 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001581int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001582{
Willy Tarreau59234e92008-11-30 23:51:27 +01001583 /*
1584 * We will parse the partial (or complete) lines.
1585 * We will check the request syntax, and also join multi-line
1586 * headers. An index of all the lines will be elaborated while
1587 * parsing.
1588 *
1589 * For the parsing, we use a 28 states FSM.
1590 *
1591 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02001592 * req->buf->p = beginning of request
1593 * req->buf->p + msg->eoh = end of processed headers / start of current one
1594 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02001595 * msg->eol = end of current header or line (LF or CRLF)
1596 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02001597 *
1598 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02001599 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02001600 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
1601 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01001602 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001603
Willy Tarreau59234e92008-11-30 23:51:27 +01001604 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001605 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001606 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01001607 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001608 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001609
Willy Tarreau87b09662015-04-03 00:22:06 +02001610 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01001611 now_ms, __FUNCTION__,
1612 s,
1613 req,
1614 req->rex, req->wex,
1615 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001616 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01001617 req->analysers);
1618
Willy Tarreau52a0c602009-08-16 22:45:38 +02001619 /* we're speaking HTTP here, so let's speak HTTP to the client */
1620 s->srv_error = http_return_srv_error;
1621
Willy Tarreau83e3af02009-12-28 17:39:57 +01001622 /* There's a protected area at the end of the buffer for rewriting
1623 * purposes. We don't want to start to parse the request if the
1624 * protected area is affected, because we may have to move processed
1625 * data later, which is much more complicated.
1626 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001627 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001628
1629 /* This point is executed when some data is avalaible for analysis,
1630 * so we log the end of the idle time. */
1631 if (s->logs.t_idle == -1)
1632 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
1633
Willy Tarreau379357a2013-06-08 12:55:46 +02001634 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01001635 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001636 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01001637 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01001638 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001639 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001640 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01001641 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01001642 return 0;
1643 }
Willy Tarreau379357a2013-06-08 12:55:46 +02001644 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
1645 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
1646 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001647 }
1648
Willy Tarreau9b28e032012-10-12 23:49:43 +02001649 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01001650 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001651 }
1652
Willy Tarreau59234e92008-11-30 23:51:27 +01001653 /* 1: we might have to print this header in debug mode */
1654 if (unlikely((global.mode & MODE_DEBUG) &&
1655 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02001656 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001657 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001658
Willy Tarreau9b28e032012-10-12 23:49:43 +02001659 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001660 /* this is a bit complex : in case of error on the request line,
1661 * we know that rq.l is still zero, so we display only the part
1662 * up to the end of the line (truncated by debug_hdr).
1663 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001664 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01001665 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001666
Willy Tarreau59234e92008-11-30 23:51:27 +01001667 sol += hdr_idx_first_pos(&txn->hdr_idx);
1668 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001669
Willy Tarreau59234e92008-11-30 23:51:27 +01001670 while (cur_idx) {
1671 eol = sol + txn->hdr_idx.v[cur_idx].len;
1672 debug_hdr("clihdr", s, sol, eol);
1673 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1674 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001675 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001676 }
1677
Willy Tarreau58f10d72006-12-04 02:26:12 +01001678
Willy Tarreau59234e92008-11-30 23:51:27 +01001679 /*
1680 * Now we quickly check if we have found a full valid request.
1681 * If not so, we check the FD and buffer states before leaving.
1682 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01001683 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001684 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02001685 * on a keep-alive stream, if we encounter and error, close, t/o,
1686 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001687 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02001688 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02001689 * Last, we may increase some tracked counters' http request errors on
1690 * the cases that are deliberately the client's fault. For instance,
1691 * a timeout or connection reset is not counted as an error. However
1692 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01001693 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001694
Willy Tarreau655dce92009-11-08 13:10:58 +01001695 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001696 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001697 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001698 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01001699 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001700 stream_inc_http_req_ctr(s);
1701 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001702 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001703 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01001704 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001705
Willy Tarreau59234e92008-11-30 23:51:27 +01001706 /* 1: Since we are in header mode, if there's no space
1707 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02001708 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01001709 * must terminate it now.
1710 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001711 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001712 /* FIXME: check if URI is set and return Status
1713 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001714 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001715 stream_inc_http_req_ctr(s);
1716 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001717 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02001718 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02001719 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01001720 goto return_bad_req;
1721 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001722
Willy Tarreau59234e92008-11-30 23:51:27 +01001723 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001724 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001725 if (!(s->flags & SF_ERR_MASK))
1726 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001727
Willy Tarreaufcffa692010-01-10 14:21:19 +01001728 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001729 goto failed_keep_alive;
1730
Willy Tarreau0f228a02015-05-01 15:37:53 +02001731 if (sess->fe->options & PR_O_IGNORE_PRB)
1732 goto failed_keep_alive;
1733
Willy Tarreau59234e92008-11-30 23:51:27 +01001734 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001735 if (msg->err_pos >= 0) {
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001736 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001737 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001738 }
1739
Willy Tarreaudc979f22012-12-04 10:39:01 +01001740 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001741 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001742 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001743 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01001744 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001745 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001746 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001747 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001748 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001749 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001750
Willy Tarreaue7dff022015-04-03 01:14:29 +02001751 if (!(s->flags & SF_FINST_MASK))
1752 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001753 return 0;
1754 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001755
Willy Tarreau59234e92008-11-30 23:51:27 +01001756 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001757 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001758 if (!(s->flags & SF_ERR_MASK))
1759 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001760
Willy Tarreaufcffa692010-01-10 14:21:19 +01001761 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001762 goto failed_keep_alive;
1763
Willy Tarreau0f228a02015-05-01 15:37:53 +02001764 if (sess->fe->options & PR_O_IGNORE_PRB)
1765 goto failed_keep_alive;
1766
Willy Tarreau59234e92008-11-30 23:51:27 +01001767 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001768 if (msg->err_pos >= 0) {
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001769 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001770 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001771 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001772 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001773 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001774 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001775 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001776 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001777
Willy Tarreau87b09662015-04-03 00:22:06 +02001778 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001779 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001780 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001781 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001782 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001783
Willy Tarreaue7dff022015-04-03 01:14:29 +02001784 if (!(s->flags & SF_FINST_MASK))
1785 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001786 return 0;
1787 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001788
Willy Tarreau59234e92008-11-30 23:51:27 +01001789 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001790 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001791 if (!(s->flags & SF_ERR_MASK))
1792 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001793
Willy Tarreaufcffa692010-01-10 14:21:19 +01001794 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001795 goto failed_keep_alive;
1796
Willy Tarreau0f228a02015-05-01 15:37:53 +02001797 if (sess->fe->options & PR_O_IGNORE_PRB)
1798 goto failed_keep_alive;
1799
Willy Tarreau4076a152009-04-02 15:18:36 +02001800 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001801 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001802 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001803 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001804 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001805 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001806 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001807 stream_inc_http_err_ctr(s);
1808 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001809 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001810 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001811 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001812 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001813
Willy Tarreaue7dff022015-04-03 01:14:29 +02001814 if (!(s->flags & SF_FINST_MASK))
1815 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001816 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001817 }
1818
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001819 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001820 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001821 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01001822#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001823 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
1824 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01001825 /* We need more data, we have to re-enable quick-ack in case we
1826 * previously disabled it, otherwise we might cause the client
1827 * to delay next data.
1828 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001829 setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01001830 }
1831#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001832
Willy Tarreaufcffa692010-01-10 14:21:19 +01001833 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
1834 /* If the client starts to talk, let's fall back to
1835 * request timeout processing.
1836 */
1837 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01001838 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01001839 }
1840
Willy Tarreau59234e92008-11-30 23:51:27 +01001841 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01001842 if (!tick_isset(req->analyse_exp)) {
1843 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
1844 (txn->flags & TX_WAIT_NEXT_RQ) &&
1845 tick_isset(s->be->timeout.httpka))
1846 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
1847 else
1848 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1849 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001850
Willy Tarreau59234e92008-11-30 23:51:27 +01001851 /* we're not ready yet */
1852 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001853
1854 failed_keep_alive:
1855 /* Here we process low-level errors for keep-alive requests. In
1856 * short, if the request is not the first one and it experiences
1857 * a timeout, read error or shutdown, we just silently close so
1858 * that the client can try again.
1859 */
1860 txn->status = 0;
1861 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001862 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001863 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02001864 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001865 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001866 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01001867 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01001868 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001869
Willy Tarreaud787e662009-07-07 10:14:51 +02001870 /* OK now we have a complete HTTP request with indexed headers. Let's
1871 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02001872 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01001873 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01001874 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001875 * byte after the last LF. msg->sov points to the first byte of data.
1876 * msg->eol cannot be trusted because it may have been left uninitialized
1877 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02001878 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001879
Willy Tarreau87b09662015-04-03 00:22:06 +02001880 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001881 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001882
Willy Tarreaub16a5742010-01-10 14:46:16 +01001883 if (txn->flags & TX_WAIT_NEXT_RQ) {
1884 /* kill the pending keep-alive timeout */
1885 txn->flags &= ~TX_WAIT_NEXT_RQ;
1886 req->analyse_exp = TICK_ETERNITY;
1887 }
1888
1889
Willy Tarreaud787e662009-07-07 10:14:51 +02001890 /* Maybe we found in invalid header name while we were configured not
1891 * to block on that, so we have to capture it now.
1892 */
1893 if (unlikely(msg->err_pos >= 0))
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001894 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02001895
Willy Tarreau59234e92008-11-30 23:51:27 +01001896 /*
1897 * 1: identify the method
1898 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001899 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01001900
1901 /* we can make use of server redirect on GET and HEAD */
1902 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001903 s->flags |= SF_REDIRECTABLE;
Willy Tarreau91659792017-11-10 19:38:10 +01001904 else if (txn->meth == HTTP_METH_OTHER &&
1905 msg->sl.rq.m_l == 3 && memcmp(req->buf->p, "PRI", 3) == 0) {
1906 /* PRI is reserved for the HTTP/2 preface */
1907 msg->err_pos = 0;
1908 goto return_bad_req;
1909 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001910
Willy Tarreau59234e92008-11-30 23:51:27 +01001911 /*
1912 * 2: check if the URI matches the monitor_uri.
1913 * We have to do this for every request which gets in, because
1914 * the monitor-uri is defined by the frontend.
1915 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001916 if (unlikely((sess->fe->monitor_uri_len != 0) &&
1917 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02001918 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001919 sess->fe->monitor_uri,
1920 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001921 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001922 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001923 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001924 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001925
Willy Tarreaue7dff022015-04-03 01:14:29 +02001926 s->flags |= SF_MONITOR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001927 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001928
Willy Tarreau59234e92008-11-30 23:51:27 +01001929 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001930 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001931 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02001932
Willy Tarreau59234e92008-11-30 23:51:27 +01001933 ret = acl_pass(ret);
1934 if (cond->pol == ACL_COND_UNLESS)
1935 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001936
Willy Tarreau59234e92008-11-30 23:51:27 +01001937 if (ret) {
1938 /* we fail this request, let's return 503 service unavail */
1939 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001940 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001941 if (!(s->flags & SF_ERR_MASK))
1942 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001943 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001944 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001945 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001946
Willy Tarreau59234e92008-11-30 23:51:27 +01001947 /* nothing to fail, let's reply normaly */
1948 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001949 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001950 if (!(s->flags & SF_ERR_MASK))
1951 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001952 goto return_prx_cond;
1953 }
1954
1955 /*
1956 * 3: Maybe we have to copy the original REQURI for the logs ?
1957 * Note: we cannot log anymore if the request has been
1958 * classified as invalid.
1959 */
1960 if (unlikely(s->logs.logwait & LW_REQ)) {
1961 /* we have a complete HTTP request that we must log */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001962 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001963 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001964
Stéphane Cottin23e9e932017-05-18 08:58:41 +02001965 if (urilen >= global.tune.requri_len )
1966 urilen = global.tune.requri_len - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001967 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01001968 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001969
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001970 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01001971 s->do_log(s);
1972 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001973 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001974 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001975 }
Willy Tarreau06619262006-12-17 08:37:22 +01001976
Willy Tarreau91852eb2015-05-01 13:26:00 +02001977 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
1978 * exactly one digit "." one digit. This check may be disabled using
1979 * option accept-invalid-http-request.
1980 */
1981 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
1982 if (msg->sl.rq.v_l != 8) {
1983 msg->err_pos = msg->sl.rq.v;
1984 goto return_bad_req;
1985 }
1986
1987 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
1988 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
1989 req->buf->p[msg->sl.rq.v + 6] != '.' ||
1990 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
1991 msg->err_pos = msg->sl.rq.v + 4;
1992 goto return_bad_req;
1993 }
1994 }
Willy Tarreau13317662015-05-01 13:47:08 +02001995 else {
1996 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1997 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
1998 goto return_bad_req;
1999 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02002000
Willy Tarreau5b154472009-12-21 20:11:07 +01002001 /* ... and check if the request is HTTP/1.1 or above */
2002 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002003 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2004 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2005 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002006 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002007
2008 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002009 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG);
Willy Tarreau5b154472009-12-21 20:11:07 +01002010
Willy Tarreau88d349d2010-01-25 12:15:43 +01002011 /* if the frontend has "option http-use-proxy-header", we'll check if
2012 * we have what looks like a proxied connection instead of a connection,
2013 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2014 * Note that this is *not* RFC-compliant, however browsers and proxies
2015 * happen to do that despite being non-standard :-(
2016 * We consider that a request not beginning with either '/' or '*' is
2017 * a proxied connection, which covers both "scheme://location" and
2018 * CONNECT ip:port.
2019 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002020 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002021 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002022 txn->flags |= TX_USE_PX_CONN;
2023
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002024 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002025 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002026
Willy Tarreau59234e92008-11-30 23:51:27 +01002027 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002028 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002029 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002030 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002031
Willy Tarreau557f1992015-05-01 10:05:17 +02002032 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
2033 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002034 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002035 * The length of a message body is determined by one of the following
2036 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02002037 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002038 * 1. Any response to a HEAD request and any response with a 1xx
2039 * (Informational), 204 (No Content), or 304 (Not Modified) status
2040 * code is always terminated by the first empty line after the
2041 * header fields, regardless of the header fields present in the
2042 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002043 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002044 * 2. Any 2xx (Successful) response to a CONNECT request implies that
2045 * the connection will become a tunnel immediately after the empty
2046 * line that concludes the header fields. A client MUST ignore any
2047 * Content-Length or Transfer-Encoding header fields received in
2048 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002049 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002050 * 3. If a Transfer-Encoding header field is present and the chunked
2051 * transfer coding (Section 4.1) is the final encoding, the message
2052 * body length is determined by reading and decoding the chunked
2053 * data until the transfer coding indicates the data is complete.
2054 *
2055 * If a Transfer-Encoding header field is present in a response and
2056 * the chunked transfer coding is not the final encoding, the
2057 * message body length is determined by reading the connection until
2058 * it is closed by the server. If a Transfer-Encoding header field
2059 * is present in a request and the chunked transfer coding is not
2060 * the final encoding, the message body length cannot be determined
2061 * reliably; the server MUST respond with the 400 (Bad Request)
2062 * status code and then close the connection.
2063 *
2064 * If a message is received with both a Transfer-Encoding and a
2065 * Content-Length header field, the Transfer-Encoding overrides the
2066 * Content-Length. Such a message might indicate an attempt to
2067 * perform request smuggling (Section 9.5) or response splitting
2068 * (Section 9.4) and ought to be handled as an error. A sender MUST
2069 * remove the received Content-Length field prior to forwarding such
2070 * a message downstream.
2071 *
2072 * 4. If a message is received without Transfer-Encoding and with
2073 * either multiple Content-Length header fields having differing
2074 * field-values or a single Content-Length header field having an
2075 * invalid value, then the message framing is invalid and the
2076 * recipient MUST treat it as an unrecoverable error. If this is a
2077 * request message, the server MUST respond with a 400 (Bad Request)
2078 * status code and then close the connection. If this is a response
2079 * message received by a proxy, the proxy MUST close the connection
2080 * to the server, discard the received response, and send a 502 (Bad
2081 * Gateway) response to the client. If this is a response message
2082 * received by a user agent, the user agent MUST close the
2083 * connection to the server and discard the received response.
2084 *
2085 * 5. If a valid Content-Length header field is present without
2086 * Transfer-Encoding, its decimal value defines the expected message
2087 * body length in octets. If the sender closes the connection or
2088 * the recipient times out before the indicated number of octets are
2089 * received, the recipient MUST consider the message to be
2090 * incomplete and close the connection.
2091 *
2092 * 6. If this is a request message and none of the above are true, then
2093 * the message body length is zero (no message body is present).
2094 *
2095 * 7. Otherwise, this is a response message without a declared message
2096 * body length, so the message body length is determined by the
2097 * number of octets received prior to the server closing the
2098 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002099 */
2100
Willy Tarreau32b47f42009-10-18 20:55:02 +02002101 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002102 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02002103 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002104 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02002105 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002106 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02002107 /* chunked not last, return badreq */
2108 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002109 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002110 }
2111
Willy Tarreau1c913912015-04-30 10:57:51 +02002112 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02002113 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02002114 if (msg->flags & HTTP_MSGF_TE_CHNK) {
2115 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
2116 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2117 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02002118 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002119 signed long long cl;
2120
Willy Tarreauad14f752011-09-02 20:33:27 +02002121 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002122 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002123 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002124 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002125
Willy Tarreauad14f752011-09-02 20:33:27 +02002126 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002127 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002128 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002129 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002130
Willy Tarreauad14f752011-09-02 20:33:27 +02002131 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002132 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002133 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002134 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002135
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002136 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002137 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002138 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002139 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002140
Christopher Fauletbe821b92017-03-30 11:21:53 +02002141 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002142 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002143 }
2144
Willy Tarreau34dfc602015-05-01 10:09:49 +02002145 /* even bodyless requests have a known length */
2146 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002147
Willy Tarreau179085c2014-04-28 16:48:56 +02002148 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
2149 * only change if both the request and the config reference something else.
2150 * Option httpclose by itself sets tunnel mode where headers are mangled.
2151 * However, if another mode is set, it will affect it (eg: server-close/
2152 * keep-alive + httpclose = close). Note that we avoid to redo the same work
2153 * if FE and BE have the same settings (common). The method consists in
2154 * checking if options changed between the two calls (implying that either
2155 * one is non-null, or one of them is non-null and we are there for the first
2156 * time.
2157 */
2158 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002159 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002160 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02002161
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02002162 /* we may have to wait for the request's body */
2163 if ((s->be->options & PR_O_WREQ_BODY) &&
2164 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
2165 req->analysers |= AN_REQ_HTTP_BODY;
2166
Willy Tarreau83ece462017-12-21 15:13:09 +01002167 /*
2168 * RFC7234#4:
2169 * A cache MUST write through requests with methods
2170 * that are unsafe (Section 4.2.1 of [RFC7231]) to
2171 * the origin server; i.e., a cache is not allowed
2172 * to generate a reply to such a request before
2173 * having forwarded the request and having received
2174 * a corresponding response.
2175 *
2176 * RFC7231#4.2.1:
2177 * Of the request methods defined by this
2178 * specification, the GET, HEAD, OPTIONS, and TRACE
2179 * methods are defined to be safe.
2180 */
2181 if (likely(txn->meth == HTTP_METH_GET ||
2182 txn->meth == HTTP_METH_HEAD ||
2183 txn->meth == HTTP_METH_OPTIONS ||
2184 txn->meth == HTTP_METH_TRACE))
2185 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
2186
Willy Tarreaud787e662009-07-07 10:14:51 +02002187 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002188 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002189 req->analyse_exp = TICK_ETERNITY;
2190 return 1;
2191
2192 return_bad_req:
2193 /* We centralize bad requests processing here */
2194 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2195 /* we detected a parsing error. We want to archive this request
2196 * in the dedicated proxy area for later troubleshooting.
2197 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02002198 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002199 }
2200
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002201 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02002202 txn->req.msg_state = HTTP_MSG_ERROR;
2203 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002204 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002205
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002206 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002207 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002208 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02002209
2210 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02002211 if (!(s->flags & SF_ERR_MASK))
2212 s->flags |= SF_ERR_PRXCOND;
2213 if (!(s->flags & SF_FINST_MASK))
2214 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02002215
Christopher Faulet0184ea72017-01-05 14:06:34 +01002216 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02002217 req->analyse_exp = TICK_ETERNITY;
2218 return 0;
2219}
2220
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002221
Willy Tarreau347a35d2013-11-22 17:51:09 +01002222/* This function prepares an applet to handle the stats. It can deal with the
2223 * "100-continue" expectation, check that admin rules are met for POST requests,
2224 * and program a response message if something was unexpected. It cannot fail
2225 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002226 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002227 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02002228 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002229 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002230int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002231{
2232 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01002233 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02002234 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002235 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002236 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002237 struct uri_auth *uri_auth = s->be->uri_auth;
2238 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002239 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002240
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002241 appctx = si_appctx(si);
2242 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2243 appctx->st1 = appctx->st2 = 0;
2244 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
2245 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02002246 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02002247 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002248
2249 uri = msg->chn->buf->p + msg->sl.rq.u;
2250 lookup = uri + uri_auth->uri_len;
2251
2252 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
2253 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002254 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002255 break;
2256 }
2257 }
2258
2259 if (uri_auth->refresh) {
2260 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
2261 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002262 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002263 break;
2264 }
2265 }
2266 }
2267
2268 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
2269 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002270 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002271 break;
2272 }
2273 }
2274
Willy Tarreau1e62df92016-01-11 18:57:53 +01002275 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
2276 if (memcmp(h, ";typed", 6) == 0) {
2277 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
2278 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
2279 break;
2280 }
2281 }
2282
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002283 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2284 if (memcmp(h, ";st=", 4) == 0) {
2285 int i;
2286 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002287 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002288 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
2289 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002290 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002291 break;
2292 }
2293 }
2294 break;
2295 }
2296 }
2297
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002298 appctx->ctx.stats.scope_str = 0;
2299 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002300 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2301 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
2302 int itx = 0;
2303 const char *h2;
2304 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
2305 const char *err;
2306
2307 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
2308 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002309 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002310 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
2311 itx++;
2312 h++;
2313 }
2314
2315 if (itx > STAT_SCOPE_TXT_MAXLEN)
2316 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002317 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002318
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002319 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002320 memcpy(scope_txt, h2, itx);
2321 scope_txt[itx] = '\0';
2322 err = invalid_char(scope_txt);
2323 if (err) {
2324 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002325 appctx->ctx.stats.scope_str = 0;
2326 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002327 }
2328 break;
2329 }
2330 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002331
2332 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002333 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002334 int ret = 1;
2335
2336 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002337 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002338 ret = acl_pass(ret);
2339 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2340 ret = !ret;
2341 }
2342
2343 if (ret) {
2344 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002345 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002346 break;
2347 }
2348 }
2349
2350 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02002351 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002352 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02002353 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02002354 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
2355 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002356 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002357 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002358 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002359 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
2360 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002361 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002362 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002363 else {
2364 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002365 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002366 }
2367
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002368 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002369 return 1;
2370}
2371
Lukas Tribus67db8df2013-06-23 17:37:13 +02002372/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
2373 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
2374 */
Vincent Bernat6e615892016-05-18 16:17:44 +02002375void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02002376{
2377#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02002378 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02002379 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2380#endif
2381#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02002382 if (from->ss_family == AF_INET6) {
2383 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02002384 /* v4-mapped addresses need IP_TOS */
2385 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2386 else
2387 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
2388 }
2389#endif
2390}
2391
Willy Tarreau87b09662015-04-03 00:22:06 +02002392int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002393 const char* name, unsigned int name_len,
2394 const char *str, struct my_regex *re,
2395 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06002396{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002397 struct hdr_ctx ctx;
2398 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002399 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002400 int (*http_find_hdr_func)(const char *name, int len, char *sol,
2401 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002402 struct chunk *output = get_trash_chunk();
2403
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002404 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06002405
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002406 /* Choose the header browsing function. */
2407 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002408 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002409 http_find_hdr_func = http_find_header2;
2410 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002411 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002412 http_find_hdr_func = http_find_full_header2;
2413 break;
2414 default: /* impossible */
2415 return -1;
2416 }
2417
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002418 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
2419 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06002420 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002421 char *val = ctx.line + ctx.val;
2422 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06002423
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002424 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
2425 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06002426
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002427 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002428 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06002429 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06002430
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002431 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06002432
2433 hdr->len += delta;
2434 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002435
2436 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002437 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06002438 }
2439
2440 return 0;
2441}
2442
Willy Tarreau87b09662015-04-03 00:22:06 +02002443static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002444 const char* name, unsigned int name_len,
2445 struct list *fmt, struct my_regex *re,
2446 int action)
2447{
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002448 struct chunk *replace;
2449 int ret = -1;
2450
2451 replace = alloc_trash_chunk();
2452 if (!replace)
2453 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002454
2455 replace->len = build_logline(s, replace->str, replace->size, fmt);
2456 if (replace->len >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002457 goto leave;
2458
2459 ret = http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002460
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002461 leave:
2462 free_trash_chunk(replace);
2463 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002464}
2465
Willy Tarreau87b09662015-04-03 00:22:06 +02002466/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02002467 * transaction <txn>. Returns the verdict of the first rule that prevents
2468 * further processing of the request (auth, deny, ...), and defaults to
2469 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2470 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02002471 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2472 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2473 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002474 */
Willy Tarreau0b748332014-04-29 00:13:29 +02002475enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02002476http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002477{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002478 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002479 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002480 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002481 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01002482 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02002483 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02002484 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02002485 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002486
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002487 /* If "the current_rule_list" match the executed rule list, we are in
2488 * resume condition. If a resume is needed it is always in the action
2489 * and never in the ACL or converters. In this case, we initialise the
2490 * current rule, and go to the action execution point.
2491 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002492 if (s->current_rule) {
2493 rule = s->current_rule;
2494 s->current_rule = NULL;
2495 if (s->current_rule_list == rules)
2496 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002497 }
2498 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002499
Willy Tarreauff011f22011-01-06 17:51:27 +01002500 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002501
Willy Tarreau96257ec2012-12-27 10:46:37 +01002502 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01002503 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01002504 int ret;
2505
Willy Tarreau192252e2015-04-04 01:47:55 +02002506 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002507 ret = acl_pass(ret);
2508
Willy Tarreauff011f22011-01-06 17:51:27 +01002509 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002510 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002511
2512 if (!ret) /* condition not matched */
2513 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002514 }
2515
Willy Tarreauacc98002015-09-27 23:34:39 +02002516 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002517resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01002518 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002519 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02002520 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002521
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002522 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02002523 if (deny_status)
2524 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02002525 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002526
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002527 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01002528 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002529 if (deny_status)
2530 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02002531 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01002532
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002533 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02002534 /* Auth might be performed on regular http-req rules as well as on stats */
2535 auth_realm = rule->arg.auth.realm;
2536 if (!auth_realm) {
2537 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2538 auth_realm = STATS_DEFAULT_REALM;
2539 else
2540 auth_realm = px->id;
2541 }
2542 /* send 401/407 depending on whether we use a proxy or not. We still
2543 * count one error, because normal browsing won't significantly
2544 * increase the counter but brute force attempts will.
2545 */
2546 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
2547 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002548 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02002549 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02002550 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002551
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002552 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02002553 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
2554 return HTTP_RULE_RES_BADREQ;
2555 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002556
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002557 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002558 s->task->nice = rule->arg.nice;
2559 break;
2560
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002561 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002562 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002563 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002564 break;
2565
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002566 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002567#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002568 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002569 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002570#endif
2571 break;
2572
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002573 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002574 s->logs.level = rule->arg.loglevel;
2575 break;
2576
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002577 case ACT_HTTP_REPLACE_HDR:
2578 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002579 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
2580 rule->arg.hdr_add.name_len,
2581 &rule->arg.hdr_add.fmt,
2582 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06002583 return HTTP_RULE_RES_BADREQ;
2584 break;
2585
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002586 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01002587 ctx.idx = 0;
2588 /* remove all occurrences of the header */
2589 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2590 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
2591 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01002592 }
Willy Tarreau85603282015-01-21 20:39:27 +01002593 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002594
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002595 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002596 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02002597 /* The scope of the trash buffer must be limited to this function. The
2598 * build_logline() function can execute a lot of other function which
2599 * can use the trash buffer. So for limiting the scope of this global
2600 * buffer, we build first the header value using build_logline, and
2601 * after we store the header name.
2602 */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002603 struct chunk *replace;
2604
2605 replace = alloc_trash_chunk();
2606 if (!replace)
2607 return HTTP_RULE_RES_BADREQ;
2608
Thierry Fournier4b788f72016-06-01 13:35:36 +02002609 len = rule->arg.hdr_add.name_len + 2,
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002610 len += build_logline(s, replace->str + len, replace->size - len, &rule->arg.hdr_add.fmt);
2611 memcpy(replace->str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2612 replace->str[rule->arg.hdr_add.name_len] = ':';
2613 replace->str[rule->arg.hdr_add.name_len + 1] = ' ';
2614 replace->len = len;
Willy Tarreau85603282015-01-21 20:39:27 +01002615
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002616 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002617 /* remove all occurrences of the header */
2618 ctx.idx = 0;
2619 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2620 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
2621 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
2622 }
2623 }
2624
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002625 http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->str, replace->len);
2626
2627 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01002628 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002629 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002630
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002631 case ACT_HTTP_DEL_ACL:
2632 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002633 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002634 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002635
2636 /* collect reference */
2637 ref = pat_ref_lookup(rule->arg.map.ref);
2638 if (!ref)
2639 continue;
2640
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002641 /* allocate key */
2642 key = alloc_trash_chunk();
2643 if (!key)
2644 return HTTP_RULE_RES_BADREQ;
2645
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002646 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002647 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2648 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002649
2650 /* perform update */
2651 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002652 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002653 pat_ref_delete(ref, key->str);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002654 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002655
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002656 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002657 break;
2658 }
2659
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002660 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002661 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002662 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002663
2664 /* collect reference */
2665 ref = pat_ref_lookup(rule->arg.map.ref);
2666 if (!ref)
2667 continue;
2668
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002669 /* allocate key */
2670 key = alloc_trash_chunk();
2671 if (!key)
2672 return HTTP_RULE_RES_BADREQ;
2673
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002674 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002675 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2676 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002677
2678 /* perform update */
2679 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002680 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002681 if (pat_ref_find_elt(ref, key->str) == NULL)
2682 pat_ref_add(ref, key->str, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002683 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002684
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002685 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002686 break;
2687 }
2688
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002689 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002690 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002691 struct chunk *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002692
2693 /* collect reference */
2694 ref = pat_ref_lookup(rule->arg.map.ref);
2695 if (!ref)
2696 continue;
2697
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002698 /* allocate key */
2699 key = alloc_trash_chunk();
2700 if (!key)
2701 return HTTP_RULE_RES_BADREQ;
2702
2703 /* allocate value */
2704 value = alloc_trash_chunk();
2705 if (!value) {
2706 free_trash_chunk(key);
2707 return HTTP_RULE_RES_BADREQ;
2708 }
2709
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002710 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002711 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2712 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002713
2714 /* collect value */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002715 value->len = build_logline(s, value->str, value->size, &rule->arg.map.value);
2716 value->str[value->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002717
2718 /* perform update */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002719 if (pat_ref_find_elt(ref, key->str) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002720 /* update entry if it exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002721 pat_ref_set(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002722 else
2723 /* insert a new entry */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002724 pat_ref_add(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002725
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002726 free_trash_chunk(key);
2727 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002728 break;
2729 }
William Lallemand73025dd2014-04-24 14:38:37 +02002730
Thierry FOURNIER42148732015-09-02 17:17:33 +02002731 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002732 if ((s->req.flags & CF_READ_ERROR) ||
2733 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2734 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2735 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002736 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002737
Willy Tarreauacc98002015-09-27 23:34:39 +02002738 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002739 case ACT_RET_ERR:
2740 case ACT_RET_CONT:
2741 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002742 case ACT_RET_STOP:
2743 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002744 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002745 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002746 return HTTP_RULE_RES_YIELD;
2747 }
William Lallemand73025dd2014-04-24 14:38:37 +02002748 break;
2749
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002750 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002751 /* Note: only the first valid tracking parameter of each
2752 * applies.
2753 */
2754
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002755 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002756 struct stktable *t;
2757 struct stksess *ts;
2758 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002759 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02002760
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002761 t = rule->arg.trk_ctr.table.t;
2762 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
Willy Tarreau09448f72014-06-25 18:12:15 +02002763
2764 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002765 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002766
2767 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002768 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2769 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2770 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002771 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02002772
Emeric Brun819fc6f2017-06-13 19:37:32 +02002773 if (ptr1)
2774 stktable_data_cast(ptr1, http_req_cnt)++;
2775
2776 if (ptr2)
2777 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2778 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2779
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002780 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01002781
2782 /* If data was modified, we need to touch to re-schedule sync */
2783 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002784 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002785
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002786 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002787 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002788 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002789 }
2790 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002791 break;
2792
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002793 /* other flags exists, but normaly, they never be matched. */
2794 default:
2795 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002796 }
2797 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002798
2799 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02002800 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002801}
2802
Willy Tarreau71241ab2012-12-27 11:30:54 +01002803
Willy Tarreau51d861a2015-05-22 17:30:48 +02002804/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2805 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2806 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2807 * is returned, the process can continue the evaluation of next rule list. If
2808 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2809 * is returned, it means the operation could not be processed and a server error
2810 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2811 * deny rule. If *YIELD is returned, the caller must call again the function
2812 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002813 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002814static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002815http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002816{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002817 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002818 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002819 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002820 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002821 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02002822 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002823
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002824 /* If "the current_rule_list" match the executed rule list, we are in
2825 * resume condition. If a resume is needed it is always in the action
2826 * and never in the ACL or converters. In this case, we initialise the
2827 * current rule, and go to the action execution point.
2828 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002829 if (s->current_rule) {
2830 rule = s->current_rule;
2831 s->current_rule = NULL;
2832 if (s->current_rule_list == rules)
2833 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002834 }
2835 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002836
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002837 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002838
2839 /* check optional condition */
2840 if (rule->cond) {
2841 int ret;
2842
Willy Tarreau192252e2015-04-04 01:47:55 +02002843 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002844 ret = acl_pass(ret);
2845
2846 if (rule->cond->pol == ACL_COND_UNLESS)
2847 ret = !ret;
2848
2849 if (!ret) /* condition not matched */
2850 continue;
2851 }
2852
Willy Tarreauacc98002015-09-27 23:34:39 +02002853 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002854resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002855 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002856 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002857 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002858
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002859 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002860 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002861 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002862
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002863 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002864 s->task->nice = rule->arg.nice;
2865 break;
2866
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002867 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002868 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002869 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002870 break;
2871
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002872 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002873#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002874 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002875 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002876#endif
2877 break;
2878
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002879 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002880 s->logs.level = rule->arg.loglevel;
2881 break;
2882
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002883 case ACT_HTTP_REPLACE_HDR:
2884 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002885 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2886 rule->arg.hdr_add.name_len,
2887 &rule->arg.hdr_add.fmt,
2888 &rule->arg.hdr_add.re, rule->action))
Christopher Fauletcdade942017-02-08 12:41:31 +01002889 return HTTP_RULE_RES_BADREQ;
Sasha Pachev218f0642014-06-16 12:05:59 -06002890 break;
2891
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002892 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002893 ctx.idx = 0;
2894 /* remove all occurrences of the header */
2895 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2896 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
2897 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2898 }
Willy Tarreau85603282015-01-21 20:39:27 +01002899 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002900
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002901 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002902 case ACT_HTTP_ADD_HDR: {
2903 struct chunk *replace;
2904
2905 replace = alloc_trash_chunk();
2906 if (!replace)
2907 return HTTP_RULE_RES_BADREQ;
2908
2909 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
2910 memcpy(replace->str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2911 replace->len = rule->arg.hdr_add.name_len;
2912 replace->str[replace->len++] = ':';
2913 replace->str[replace->len++] = ' ';
2914 replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len,
2915 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002916
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002917 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002918 /* remove all occurrences of the header */
2919 ctx.idx = 0;
2920 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2921 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
2922 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2923 }
2924 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002925 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->str, replace->len);
2926
2927 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002928 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002929 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002930
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002931 case ACT_HTTP_DEL_ACL:
2932 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002933 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002934 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002935
2936 /* collect reference */
2937 ref = pat_ref_lookup(rule->arg.map.ref);
2938 if (!ref)
2939 continue;
2940
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002941 /* allocate key */
2942 key = alloc_trash_chunk();
2943 if (!key)
2944 return HTTP_RULE_RES_BADREQ;
2945
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002946 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002947 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2948 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002949
2950 /* perform update */
2951 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002952 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002953 pat_ref_delete(ref, key->str);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002954 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002955
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002956 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002957 break;
2958 }
2959
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002960 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002961 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002962 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002963
2964 /* collect reference */
2965 ref = pat_ref_lookup(rule->arg.map.ref);
2966 if (!ref)
2967 continue;
2968
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002969 /* allocate key */
2970 key = alloc_trash_chunk();
2971 if (!key)
2972 return HTTP_RULE_RES_BADREQ;
2973
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002974 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002975 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2976 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002977
2978 /* perform update */
2979 /* check if the entry already exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002980 if (pat_ref_find_elt(ref, key->str) == NULL)
2981 pat_ref_add(ref, key->str, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002982
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002983 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002984 break;
2985 }
2986
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002987 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002988 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002989 struct chunk *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002990
2991 /* collect reference */
2992 ref = pat_ref_lookup(rule->arg.map.ref);
2993 if (!ref)
2994 continue;
2995
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002996 /* allocate key */
2997 key = alloc_trash_chunk();
2998 if (!key)
2999 return HTTP_RULE_RES_BADREQ;
3000
3001 /* allocate value */
3002 value = alloc_trash_chunk();
3003 if (!value) {
3004 free_trash_chunk(key);
3005 return HTTP_RULE_RES_BADREQ;
3006 }
3007
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003008 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003009 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
3010 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003011
3012 /* collect value */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003013 value->len = build_logline(s, value->str, value->size, &rule->arg.map.value);
3014 value->str[value->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003015
3016 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003017 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003018 if (pat_ref_find_elt(ref, key->str) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003019 /* update entry if it exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003020 pat_ref_set(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003021 else
3022 /* insert a new entry */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003023 pat_ref_add(ref, key->str, value->str, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003024 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003025 free_trash_chunk(key);
3026 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003027 break;
3028 }
William Lallemand73025dd2014-04-24 14:38:37 +02003029
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003030 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003031 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3032 return HTTP_RULE_RES_BADREQ;
3033 return HTTP_RULE_RES_DONE;
3034
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003035 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3036 /* Note: only the first valid tracking parameter of each
3037 * applies.
3038 */
3039
Christopher Faulet4fce0d82017-09-18 11:57:31 +02003040 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003041 struct stktable *t;
3042 struct stksess *ts;
3043 struct stktable_key *key;
3044 void *ptr;
3045
3046 t = rule->arg.trk_ctr.table.t;
3047 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
3048
3049 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02003050 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003051
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003052 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003053
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003054 /* let's count a new HTTP request as it's the first time we do it */
3055 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3056 if (ptr)
3057 stktable_data_cast(ptr, http_req_cnt)++;
3058
3059 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3060 if (ptr)
3061 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3062 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3063
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003064 /* When the client triggers a 4xx from the server, it's most often due
3065 * to a missing object or permission. These events should be tracked
3066 * because if they happen often, it may indicate a brute force or a
3067 * vulnerability scan. Normally this is done when receiving the response
3068 * but here we're tracking after this ought to have been done so we have
3069 * to do it on purpose.
3070 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02003071 if ((unsigned)(txn->status - 400) < 100) {
3072 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3073 if (ptr)
3074 stktable_data_cast(ptr, http_err_cnt)++;
3075
3076 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3077 if (ptr)
3078 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3079 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3080 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02003081
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003082 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003083
Emeric Brun0fed0b02017-11-29 16:15:07 +01003084 /* If data was modified, we need to touch to re-schedule sync */
3085 stktable_touch_local(t, ts, 0);
3086
Emeric Brun819fc6f2017-06-13 19:37:32 +02003087 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3088 if (sess->fe != s->be)
3089 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3090
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003091 }
3092 }
3093 break;
3094
Thierry FOURNIER42148732015-09-02 17:17:33 +02003095 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003096 if ((s->req.flags & CF_READ_ERROR) ||
3097 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
3098 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
3099 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02003100 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003101
Willy Tarreauacc98002015-09-27 23:34:39 +02003102 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003103 case ACT_RET_ERR:
3104 case ACT_RET_CONT:
3105 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003106 case ACT_RET_STOP:
3107 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003108 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003109 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003110 return HTTP_RULE_RES_YIELD;
3111 }
William Lallemand73025dd2014-04-24 14:38:37 +02003112 break;
3113
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003114 /* other flags exists, but normaly, they never be matched. */
3115 default:
3116 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003117 }
3118 }
3119
3120 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003121 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003122}
3123
3124
Willy Tarreau71241ab2012-12-27 11:30:54 +01003125/* Perform an HTTP redirect based on the information in <rule>. The function
3126 * returns non-zero on success, or zero in case of a, irrecoverable error such
3127 * as too large a request to build a valid response.
3128 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003129static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003130{
Willy Tarreaub329a312015-05-22 16:27:37 +02003131 struct http_msg *req = &txn->req;
3132 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003133 const char *msg_fmt;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003134 struct chunk *chunk;
3135 int ret = 0;
3136
3137 chunk = alloc_trash_chunk();
3138 if (!chunk)
3139 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003140
3141 /* build redirect message */
3142 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003143 case 308:
3144 msg_fmt = HTTP_308;
3145 break;
3146 case 307:
3147 msg_fmt = HTTP_307;
3148 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003149 case 303:
3150 msg_fmt = HTTP_303;
3151 break;
3152 case 301:
3153 msg_fmt = HTTP_301;
3154 break;
3155 case 302:
3156 default:
3157 msg_fmt = HTTP_302;
3158 break;
3159 }
3160
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003161 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
3162 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003163
3164 switch(rule->type) {
3165 case REDIRECT_TYPE_SCHEME: {
3166 const char *path;
3167 const char *host;
3168 struct hdr_ctx ctx;
3169 int pathlen;
3170 int hostlen;
3171
3172 host = "";
3173 hostlen = 0;
3174 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02003175 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003176 host = ctx.line + ctx.val;
3177 hostlen = ctx.vlen;
3178 }
3179
3180 path = http_get_path(txn);
3181 /* build message using path */
3182 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003183 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003184 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3185 int qs = 0;
3186 while (qs < pathlen) {
3187 if (path[qs] == '?') {
3188 pathlen = qs;
3189 break;
3190 }
3191 qs++;
3192 }
3193 }
3194 } else {
3195 path = "/";
3196 pathlen = 1;
3197 }
3198
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003199 if (rule->rdr_str) { /* this is an old "redirect" rule */
3200 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003201 if (chunk->len + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
3202 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003203
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003204 /* add scheme */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003205 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3206 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003207 }
3208 else {
3209 /* add scheme with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003210 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003211
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003212 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003213 if (chunk->len + 3 + hostlen + pathlen > chunk->size - 4)
3214 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003215 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003216 /* add "://" */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003217 memcpy(chunk->str + chunk->len, "://", 3);
3218 chunk->len += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003219
3220 /* add host */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003221 memcpy(chunk->str + chunk->len, host, hostlen);
3222 chunk->len += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003223
3224 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003225 memcpy(chunk->str + chunk->len, path, pathlen);
3226 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003227
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003228 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003229 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003230 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003231 if (chunk->len > chunk->size - 5)
3232 goto leave;
3233 chunk->str[chunk->len] = '/';
3234 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003235 }
3236
3237 break;
3238 }
3239 case REDIRECT_TYPE_PREFIX: {
3240 const char *path;
3241 int pathlen;
3242
3243 path = http_get_path(txn);
3244 /* build message using path */
3245 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003246 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003247 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3248 int qs = 0;
3249 while (qs < pathlen) {
3250 if (path[qs] == '?') {
3251 pathlen = qs;
3252 break;
3253 }
3254 qs++;
3255 }
3256 }
3257 } else {
3258 path = "/";
3259 pathlen = 1;
3260 }
3261
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003262 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003263 if (chunk->len + rule->rdr_len + pathlen > chunk->size - 4)
3264 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003265
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003266 /* add prefix. Note that if prefix == "/", we don't want to
3267 * add anything, otherwise it makes it hard for the user to
3268 * configure a self-redirection.
3269 */
3270 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003271 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3272 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003273 }
3274 }
3275 else {
3276 /* add prefix with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003277 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003278
3279 /* Check length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003280 if (chunk->len + pathlen > chunk->size - 4)
3281 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003282 }
3283
3284 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003285 memcpy(chunk->str + chunk->len, path, pathlen);
3286 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003287
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003288 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003289 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003290 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003291 if (chunk->len > chunk->size - 5)
3292 goto leave;
3293 chunk->str[chunk->len] = '/';
3294 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003295 }
3296
3297 break;
3298 }
3299 case REDIRECT_TYPE_LOCATION:
3300 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003301 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003302 if (chunk->len + rule->rdr_len > chunk->size - 4)
3303 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003304
3305 /* add location */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003306 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3307 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003308 }
3309 else {
3310 /* add location with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003311 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003312
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003313 /* Check left length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003314 if (chunk->len > chunk->size - 4)
3315 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003316 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003317 break;
3318 }
3319
3320 if (rule->cookie_len) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003321 memcpy(chunk->str + chunk->len, "\r\nSet-Cookie: ", 14);
3322 chunk->len += 14;
3323 memcpy(chunk->str + chunk->len, rule->cookie_str, rule->cookie_len);
3324 chunk->len += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003325 }
3326
Willy Tarreau19b14122017-02-28 09:48:11 +01003327 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003328 txn->status = rule->code;
3329 /* let's log the request time */
3330 s->logs.tv_request = now;
3331
Christopher Fauletbe821b92017-03-30 11:21:53 +02003332 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003333 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3334 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3335 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02003336 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003337 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003338 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: keep-alive", 30);
3339 chunk->len += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003340 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003341 memcpy(chunk->str + chunk->len, "\r\nConnection: keep-alive", 24);
3342 chunk->len += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003343 }
3344 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003345 memcpy(chunk->str + chunk->len, "\r\n\r\n", 4);
3346 chunk->len += 4;
3347 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau06d80a92017-10-19 14:32:15 +02003348 co_inject(res->chn, chunk->str, chunk->len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003349 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02003350 bi_fast_delete(req->chn->buf, req->sov);
3351 req->next -= req->sov;
3352 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003353 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01003354 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02003355 req->msg_state = HTTP_MSG_CLOSED;
3356 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02003357 /* Trim any possible response */
3358 res->chn->buf->i = 0;
3359 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02003360 /* let the server side turn to SI_ST_CLO */
3361 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003362 } else {
3363 /* keep-alive not possible */
3364 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003365 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3366 chunk->len += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003367 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003368 memcpy(chunk->str + chunk->len, "\r\nConnection: close\r\n\r\n", 23);
3369 chunk->len += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003370 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003371 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01003372 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003373 }
3374
Willy Tarreaue7dff022015-04-03 01:14:29 +02003375 if (!(s->flags & SF_ERR_MASK))
3376 s->flags |= SF_ERR_LOCAL;
3377 if (!(s->flags & SF_FINST_MASK))
3378 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003379
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003380 ret = 1;
3381 leave:
3382 free_trash_chunk(chunk);
3383 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003384}
3385
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003386/* This stream analyser runs all HTTP request processing which is common to
3387 * frontends and backends, which means blocking ACLs, filters, connection-close,
3388 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003389 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003390 * either needs more data or wants to immediately abort the request (eg: deny,
3391 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003392 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003393int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003394{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003395 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003396 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02003397 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003398 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003399 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02003400 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003401 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02003402 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02003403
Willy Tarreau655dce92009-11-08 13:10:58 +01003404 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003405 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003406 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02003407 }
3408
Willy Tarreau87b09662015-04-03 00:22:06 +02003409 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaud787e662009-07-07 10:14:51 +02003410 now_ms, __FUNCTION__,
3411 s,
3412 req,
3413 req->rex, req->wex,
3414 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003415 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003416 req->analysers);
3417
Willy Tarreau65410832014-04-28 21:25:43 +02003418 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02003419 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02003420
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003421 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02003422 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02003423 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01003424
Willy Tarreau0b748332014-04-29 00:13:29 +02003425 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003426 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
3427 goto return_prx_yield;
3428
Willy Tarreau0b748332014-04-29 00:13:29 +02003429 case HTTP_RULE_RES_CONT:
3430 case HTTP_RULE_RES_STOP: /* nothing to do */
3431 break;
Willy Tarreau52542592014-04-28 18:33:26 +02003432
Willy Tarreau0b748332014-04-29 00:13:29 +02003433 case HTTP_RULE_RES_DENY: /* deny or tarpit */
3434 if (txn->flags & TX_CLTARPIT)
3435 goto tarpit;
3436 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003437
Willy Tarreau0b748332014-04-29 00:13:29 +02003438 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
3439 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02003440
Willy Tarreau0b748332014-04-29 00:13:29 +02003441 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02003442 goto done;
3443
Willy Tarreau0b748332014-04-29 00:13:29 +02003444 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
3445 goto return_bad_req;
3446 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003447 }
3448
Olivier Houchardc2aae742017-09-22 18:26:28 +02003449 if (conn && conn->flags & CO_FL_EARLY_DATA) {
3450 struct hdr_ctx ctx;
3451
3452 ctx.idx = 0;
3453 if (!http_find_header2("Early-Data", strlen("Early-Data"),
3454 s->req.buf->p, &txn->hdr_idx, &ctx)) {
3455 if (unlikely(http_header_add_tail2(&txn->req,
3456 &txn->hdr_idx, "Early-Data: 1",
3457 strlen("Early-Data: 1"))) < 0) {
3458 goto return_bad_req;
3459 }
3460 }
3461
3462 }
3463
Willy Tarreau52542592014-04-28 18:33:26 +02003464 /* OK at this stage, we know that the request was accepted according to
3465 * the http-request rules, we can check for the stats. Note that the
3466 * URI is detected *before* the req* rules in order not to be affected
3467 * by a possible reqrep, while they are processed *after* so that a
3468 * reqdeny can still block them. This clearly needs to change in 1.6!
3469 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003470 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02003471 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01003472 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02003473 txn->status = 500;
3474 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003475 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003476
Willy Tarreaue7dff022015-04-03 01:14:29 +02003477 if (!(s->flags & SF_ERR_MASK))
3478 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02003479 goto return_prx_cond;
3480 }
3481
3482 /* parse the whole stats request and extract the relevant information */
3483 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02003484 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02003485 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003486
Willy Tarreau0b748332014-04-29 00:13:29 +02003487 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
3488 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003489
Willy Tarreau0b748332014-04-29 00:13:29 +02003490 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
3491 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003492 }
3493
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003494 /* evaluate the req* rules except reqadd */
3495 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003496 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003497 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003498
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003499 if (txn->flags & TX_CLDENY)
3500 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003501
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003502 if (txn->flags & TX_CLTARPIT) {
3503 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003504 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003505 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003506 }
Willy Tarreau06619262006-12-17 08:37:22 +01003507
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003508 /* add request headers from the rule sets in the same order */
3509 list_for_each_entry(wl, &px->req_add, list) {
3510 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003511 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003512 ret = acl_pass(ret);
3513 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3514 ret = !ret;
3515 if (!ret)
3516 continue;
3517 }
3518
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003519 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003520 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003521 }
3522
Willy Tarreau52542592014-04-28 18:33:26 +02003523
3524 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01003525 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
3526 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003527 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003528 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003529 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01003530
Willy Tarreaue7dff022015-04-03 01:14:29 +02003531 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
3532 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
3533 if (!(s->flags & SF_FINST_MASK))
3534 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003535
Willy Tarreau70730dd2014-04-24 18:06:27 +02003536 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01003537 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
3538 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003539 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003540 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003541 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003542
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003543 /* check whether we have some ACLs set to redirect this request */
3544 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003545 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003546 int ret;
3547
Willy Tarreau192252e2015-04-04 01:47:55 +02003548 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003549 ret = acl_pass(ret);
3550 if (rule->cond->pol == ACL_COND_UNLESS)
3551 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003552 if (!ret)
3553 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003554 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003555 if (!http_apply_redirect_rule(rule, s, txn))
3556 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003557 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003558 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003559
Willy Tarreau2be39392010-01-03 17:24:51 +01003560 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3561 * If this happens, then the data will not come immediately, so we must
3562 * send all what we have without waiting. Note that due to the small gain
3563 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003564 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003565 * itself once used.
3566 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003567 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003568
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003569 done: /* done with this analyser, continue with next ones that the calling
3570 * points will have set, if any.
3571 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003572 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003573 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
3574 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003575 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003576
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003577 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02003578 /* Allow cookie logging
3579 */
3580 if (s->be->cookie_name || sess->fe->capture_name)
3581 manage_client_side_cookies(s, req);
3582
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003583 /* When a connection is tarpitted, we use the tarpit timeout,
3584 * which may be the same as the connect timeout if unspecified.
3585 * If unset, then set it to zero because we really want it to
3586 * eventually expire. We build the tarpit as an analyser.
3587 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003588 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003589
3590 /* wipe the request out so that we can drop the connection early
3591 * if the client closes first.
3592 */
3593 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003594
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003595 txn->status = http_err_codes[deny_status];
3596
Christopher Faulet0184ea72017-01-05 14:06:34 +01003597 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003598 req->analysers |= AN_REQ_HTTP_TARPIT;
3599 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3600 if (!req->analyse_exp)
3601 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02003602 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003603 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003604 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003605 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003606 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003607 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003608 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003609
3610 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003611
3612 /* Allow cookie logging
3613 */
3614 if (s->be->cookie_name || sess->fe->capture_name)
3615 manage_client_side_cookies(s, req);
3616
Willy Tarreau0b748332014-04-29 00:13:29 +02003617 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003618 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003619 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003620 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02003621 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003622 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003623 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003624 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003625 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003626 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003627 goto return_prx_cond;
3628
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003629 return_bad_req:
3630 /* We centralize bad requests processing here */
3631 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3632 /* we detected a parsing error. We want to archive this request
3633 * in the dedicated proxy area for later troubleshooting.
3634 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02003635 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003636 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003637
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003638 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003639 txn->req.msg_state = HTTP_MSG_ERROR;
3640 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003641 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003642
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003643 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003644 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003645 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003646
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003647 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003648 if (!(s->flags & SF_ERR_MASK))
3649 s->flags |= SF_ERR_PRXCOND;
3650 if (!(s->flags & SF_FINST_MASK))
3651 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003652
Christopher Faulet0184ea72017-01-05 14:06:34 +01003653 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003654 req->analyse_exp = TICK_ETERNITY;
3655 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003656
3657 return_prx_yield:
3658 channel_dont_connect(req);
3659 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003660}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003661
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003662/* This function performs all the processing enabled for the current request.
3663 * It returns 1 if the processing can continue on next analysers, or zero if it
3664 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003665 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003666 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003667int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003668{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003669 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003670 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003671 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003672 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003673
Willy Tarreau655dce92009-11-08 13:10:58 +01003674 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003675 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003676 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003677 return 0;
3678 }
3679
Willy Tarreau87b09662015-04-03 00:22:06 +02003680 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 +02003681 now_ms, __FUNCTION__,
3682 s,
3683 req,
3684 req->rex, req->wex,
3685 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003686 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003687 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003688
Willy Tarreau59234e92008-11-30 23:51:27 +01003689 /*
3690 * Right now, we know that we have processed the entire headers
3691 * and that unwanted requests have been filtered out. We can do
3692 * whatever we want with the remaining request. Also, now we
3693 * may have separate values for ->fe, ->be.
3694 */
Willy Tarreau06619262006-12-17 08:37:22 +01003695
Willy Tarreau59234e92008-11-30 23:51:27 +01003696 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003697 * If HTTP PROXY is set we simply get remote server address parsing
3698 * incoming request. Note that this requires that a connection is
3699 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003700 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003701 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003702 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003703 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003704
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003705 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003706 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003707 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003708 txn->req.msg_state = HTTP_MSG_ERROR;
3709 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003710 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003711 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003712
Willy Tarreaue7dff022015-04-03 01:14:29 +02003713 if (!(s->flags & SF_ERR_MASK))
3714 s->flags |= SF_ERR_RESOURCE;
3715 if (!(s->flags & SF_FINST_MASK))
3716 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003717
3718 return 0;
3719 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003720
3721 path = http_get_path(txn);
3722 url2sa(req->buf->p + msg->sl.rq.u,
3723 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01003724 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003725 /* if the path was found, we have to remove everything between
3726 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
3727 * found, we need to replace from req->buf->p + msg->sl.rq.u for
3728 * u_l characters by a single "/".
3729 */
3730 if (path) {
3731 char *cur_ptr = req->buf->p;
3732 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3733 int delta;
3734
3735 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
3736 http_msg_move_end(&txn->req, delta);
3737 cur_end += delta;
3738 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3739 goto return_bad_req;
3740 }
3741 else {
3742 char *cur_ptr = req->buf->p;
3743 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3744 int delta;
3745
3746 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
3747 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
3748 http_msg_move_end(&txn->req, delta);
3749 cur_end += delta;
3750 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3751 goto return_bad_req;
3752 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003753 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003754
Willy Tarreau59234e92008-11-30 23:51:27 +01003755 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003756 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003757 * Note that doing so might move headers in the request, but
3758 * the fields will stay coherent and the URI will not move.
3759 * This should only be performed in the backend.
3760 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003761 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003762 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003763
William Lallemanda73203e2012-03-12 12:48:57 +01003764 /* add unique-id if "header-unique-id" is specified */
3765
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003766 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01003767 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003768 goto return_bad_req;
3769 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003770 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003771 }
William Lallemanda73203e2012-03-12 12:48:57 +01003772
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003773 if (sess->fe->header_unique_id && s->unique_id) {
3774 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003775 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003776 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003777 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003778 goto return_bad_req;
3779 }
3780
Cyril Bontéb21570a2009-11-29 20:04:48 +01003781 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003782 * 9: add X-Forwarded-For if either the frontend or the backend
3783 * asks for it.
3784 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003785 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003786 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003787 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3788 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3789 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003790 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003791 /* The header is set to be added only if none is present
3792 * and we found it, so don't do anything.
3793 */
3794 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003795 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003796 /* Add an X-Forwarded-For header unless the source IP is
3797 * in the 'except' network range.
3798 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003799 if ((!sess->fe->except_mask.s_addr ||
3800 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3801 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003802 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003803 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003804 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003805 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003806 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003807 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003808
3809 /* Note: we rely on the backend to get the header name to be used for
3810 * x-forwarded-for, because the header is really meant for the backends.
3811 * However, if the backend did not specify any option, we have to rely
3812 * on the frontend's header name.
3813 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003814 if (s->be->fwdfor_hdr_len) {
3815 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003816 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003817 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003818 len = sess->fe->fwdfor_hdr_len;
3819 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003820 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003821 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 +01003822
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003823 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003824 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003825 }
3826 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003827 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003828 /* FIXME: for the sake of completeness, we should also support
3829 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003830 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003831 int len;
3832 char pn[INET6_ADDRSTRLEN];
3833 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003834 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003835 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003836
Willy Tarreau59234e92008-11-30 23:51:27 +01003837 /* Note: we rely on the backend to get the header name to be used for
3838 * x-forwarded-for, because the header is really meant for the backends.
3839 * However, if the backend did not specify any option, we have to rely
3840 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003841 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003842 if (s->be->fwdfor_hdr_len) {
3843 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003844 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003845 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003846 len = sess->fe->fwdfor_hdr_len;
3847 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003848 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003849 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003850
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003851 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003852 goto return_bad_req;
3853 }
3854 }
3855
3856 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003857 * 10: add X-Original-To if either the frontend or the backend
3858 * asks for it.
3859 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003860 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003861
3862 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003863 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003864 /* Add an X-Original-To header unless the destination IP is
3865 * in the 'except' network range.
3866 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003867 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003868
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003869 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003870 ((!sess->fe->except_mask_to.s_addr ||
3871 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3872 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003873 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003874 (((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 +02003875 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003876 int len;
3877 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003878 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003879
3880 /* Note: we rely on the backend to get the header name to be used for
3881 * x-original-to, because the header is really meant for the backends.
3882 * However, if the backend did not specify any option, we have to rely
3883 * on the frontend's header name.
3884 */
3885 if (s->be->orgto_hdr_len) {
3886 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003887 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003888 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003889 len = sess->fe->orgto_hdr_len;
3890 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003891 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003892 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 +02003893
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003894 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003895 goto return_bad_req;
3896 }
3897 }
3898 }
3899
Willy Tarreau50fc7772012-11-11 22:19:57 +01003900 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3901 * If an "Upgrade" token is found, the header is left untouched in order not to have
3902 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3903 * "Upgrade" is present in the Connection header.
3904 */
3905 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3906 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003907 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003908 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003909 unsigned int want_flags = 0;
3910
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003911 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003912 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003913 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003914 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003915 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003916 want_flags |= TX_CON_CLO_SET;
3917 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003918 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003919 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003920 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003921 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003922 want_flags |= TX_CON_KAL_SET;
3923 }
3924
3925 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003926 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003927 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003928
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003929
Willy Tarreau522d6c02009-12-06 18:49:18 +01003930 /* If we have no server assigned yet and we're balancing on url_param
3931 * with a POST request, we may be interested in checking the body for
3932 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003933 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003934 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003935 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003936 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003937 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003938 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003939 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003940
Christopher Fauletbe821b92017-03-30 11:21:53 +02003941 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3942 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003943#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003944 /* We expect some data from the client. Unless we know for sure
3945 * we already have a full request, we have to re-enable quick-ack
3946 * in case we previously disabled it, otherwise we might cause
3947 * the client to delay further data.
3948 */
3949 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3950 cli_conn && conn_ctrl_ready(cli_conn) &&
3951 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
3952 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003953 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003954#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003955
Willy Tarreau59234e92008-11-30 23:51:27 +01003956 /*************************************************************
3957 * OK, that's finished for the headers. We have done what we *
3958 * could. Let's switch to the DATA state. *
3959 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003960 req->analyse_exp = TICK_ETERNITY;
3961 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003962
Willy Tarreau59234e92008-11-30 23:51:27 +01003963 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003964 /* OK let's go on with the BODY now */
3965 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003966
Willy Tarreau59234e92008-11-30 23:51:27 +01003967 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003968 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003969 /* we detected a parsing error. We want to archive this request
3970 * in the dedicated proxy area for later troubleshooting.
3971 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02003972 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003973 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003974
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003975 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003976 txn->req.msg_state = HTTP_MSG_ERROR;
3977 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003978 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003979 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003980
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003981 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003982 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003983 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003984
Willy Tarreaue7dff022015-04-03 01:14:29 +02003985 if (!(s->flags & SF_ERR_MASK))
3986 s->flags |= SF_ERR_PRXCOND;
3987 if (!(s->flags & SF_FINST_MASK))
3988 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003989 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003990}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003991
Willy Tarreau60b85b02008-11-30 23:28:40 +01003992/* This function is an analyser which processes the HTTP tarpit. It always
3993 * returns zero, at the beginning because it prevents any other processing
3994 * from occurring, and at the end because it terminates the request.
3995 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003996int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003997{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003998 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003999
4000 /* This connection is being tarpitted. The CLIENT side has
4001 * already set the connect expiration date to the right
4002 * timeout. We just have to check that the client is still
4003 * there and that the timeout has not expired.
4004 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004005 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004006 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004007 !tick_is_expired(req->analyse_exp, now_ms))
4008 return 0;
4009
4010 /* We will set the queue timer to the time spent, just for
4011 * logging purposes. We fake a 500 server error, so that the
4012 * attacker will not suspect his connection has been tarpitted.
4013 * It will not cause trouble to the logs because we can exclude
4014 * the tarpitted connections by filtering on the 'PT' status flags.
4015 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004016 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4017
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004018 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004019 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004020
Christopher Faulet0184ea72017-01-05 14:06:34 +01004021 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004022 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004023
Willy Tarreaue7dff022015-04-03 01:14:29 +02004024 if (!(s->flags & SF_ERR_MASK))
4025 s->flags |= SF_ERR_PRXCOND;
4026 if (!(s->flags & SF_FINST_MASK))
4027 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004028 return 0;
4029}
4030
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004031/* This function is an analyser which waits for the HTTP request body. It waits
4032 * for either the buffer to be full, or the full advertised contents to have
4033 * reached the buffer. It must only be called after the standard HTTP request
4034 * processing has occurred, because it expects the request to be parsed and will
4035 * look for the Expect header. It may send a 100-Continue interim response. It
4036 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4037 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4038 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004039 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004040int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004041{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004042 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004043 struct http_txn *txn = s->txn;
4044 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004045
4046 /* We have to parse the HTTP request body to find any required data.
4047 * "balance url_param check_post" should have been the only way to get
4048 * into this. We were brought here after HTTP header analysis, so all
4049 * related structures are ready.
4050 */
4051
Willy Tarreau890988f2014-04-10 11:59:33 +02004052 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4053 /* This is the first call */
4054 if (msg->msg_state < HTTP_MSG_BODY)
4055 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004056
Willy Tarreau890988f2014-04-10 11:59:33 +02004057 if (msg->msg_state < HTTP_MSG_100_SENT) {
4058 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4059 * send an HTTP/1.1 100 Continue intermediate response.
4060 */
4061 if (msg->flags & HTTP_MSGF_VER_11) {
4062 struct hdr_ctx ctx;
4063 ctx.idx = 0;
4064 /* Expect is allowed in 1.1, look for it */
4065 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4066 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02004067 co_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01004068 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02004069 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004070 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004071 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004072 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004073
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004074 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004075 * req->buf->p still points to the beginning of the message. We
4076 * must save the body in msg->next because it survives buffer
4077 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004078 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004079 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004080
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004081 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004082 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4083 else
4084 msg->msg_state = HTTP_MSG_DATA;
4085 }
4086
Willy Tarreau890988f2014-04-10 11:59:33 +02004087 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4088 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004089 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004090 goto missing_data;
4091
4092 /* OK we have everything we need now */
4093 goto http_end;
4094 }
4095
4096 /* OK here we're parsing a chunked-encoded message */
4097
Willy Tarreau522d6c02009-12-06 18:49:18 +01004098 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004099 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004100 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004101 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004102 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004103 unsigned int chunk;
4104 int ret = h1_parse_chunk_size(req->buf, msg->next, req->buf->i, &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01004105
Willy Tarreau115acb92009-12-26 13:56:06 +01004106 if (!ret)
4107 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004108 else if (ret < 0) {
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004109 msg->err_pos = req->buf->i + ret;
4110 if (msg->err_pos < 0)
4111 msg->err_pos += req->buf->size;
Willy Tarreau87b09662015-04-03 00:22:06 +02004112 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004113 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004114 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004115
4116 msg->chunk_len = chunk;
4117 msg->body_len += chunk;
4118
4119 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01004120 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004121 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01004122 }
4123
Willy Tarreaud98cf932009-12-27 22:54:55 +01004124 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004125 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4126 * for at least a whole chunk or the whole content length bytes after
4127 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004128 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004129 if (msg->msg_state == HTTP_MSG_TRAILERS)
4130 goto http_end;
4131
Willy Tarreaue115b492015-05-01 23:05:14 +02004132 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004133 goto http_end;
4134
4135 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004136 /* we get here if we need to wait for more data. If the buffer is full,
4137 * we have the maximum we can expect.
4138 */
4139 if (buffer_full(req->buf, global.tune.maxrewrite))
4140 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004141
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004142 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004143 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004144 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004145
Willy Tarreaue7dff022015-04-03 01:14:29 +02004146 if (!(s->flags & SF_ERR_MASK))
4147 s->flags |= SF_ERR_CLITO;
4148 if (!(s->flags & SF_FINST_MASK))
4149 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004150 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004151 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004152
4153 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004154 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004155 /* Not enough data. We'll re-use the http-request
4156 * timeout here. Ideally, we should set the timeout
4157 * relative to the accept() date. We just set the
4158 * request timeout once at the beginning of the
4159 * request.
4160 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004161 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004162 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004163 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004164 return 0;
4165 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004166
4167 http_end:
4168 /* The situation will not evolve, so let's give up on the analysis. */
4169 s->logs.tv_request = now; /* update the request timer to reflect full request */
4170 req->analysers &= ~an_bit;
4171 req->analyse_exp = TICK_ETERNITY;
4172 return 1;
4173
4174 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004175 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004176 txn->req.msg_state = HTTP_MSG_ERROR;
4177 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004178 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004179
Willy Tarreaue7dff022015-04-03 01:14:29 +02004180 if (!(s->flags & SF_ERR_MASK))
4181 s->flags |= SF_ERR_PRXCOND;
4182 if (!(s->flags & SF_FINST_MASK))
4183 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004184
Willy Tarreau522d6c02009-12-06 18:49:18 +01004185 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01004186 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004187 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004188 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004189 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004190 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004191}
4192
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004193/* send a server's name with an outgoing request over an established connection.
4194 * Note: this function is designed to be called once the request has been scheduled
4195 * for being forwarded. This is the reason why it rewinds the buffer before
4196 * proceeding.
4197 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004198int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004199
4200 struct hdr_ctx ctx;
4201
Mark Lamourinec2247f02012-01-04 13:02:01 -05004202 char *hdr_name = be->server_id_hdr_name;
4203 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004204 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004205 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004206 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004207
William Lallemandd9e90662012-01-30 17:27:17 +01004208 ctx.idx = 0;
4209
Willy Tarreau211cdec2014-04-17 20:18:08 +02004210 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004211 if (old_o) {
4212 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004213 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004214 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004215 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004216 }
4217
Willy Tarreau9b28e032012-10-12 23:49:43 +02004218 old_i = chn->buf->i;
4219 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 -05004220 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004221 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004222 }
4223
4224 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004225 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004226 memcpy(hdr_val, hdr_name, hdr_name_len);
4227 hdr_val += hdr_name_len;
4228 *hdr_val++ = ':';
4229 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004230 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4231 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004232
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004233 if (old_o) {
4234 /* If this was a forwarded request, we must readjust the amount of
4235 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004236 * variations. Note that the current state is >= HTTP_MSG_BODY,
4237 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004238 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004239 old_o += chn->buf->i - old_i;
4240 b_adv(chn->buf, old_o);
4241 txn->req.next -= old_o;
4242 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004243 }
4244
Mark Lamourinec2247f02012-01-04 13:02:01 -05004245 return 0;
4246}
4247
Willy Tarreau610ecce2010-01-04 21:15:02 +01004248/* Terminate current transaction and prepare a new one. This is very tricky
4249 * right now but it works.
4250 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004251void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004252{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004253 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02004254 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01004255 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004256 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02004257 struct connection *srv_conn;
4258 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02004259 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01004260
Willy Tarreau610ecce2010-01-04 21:15:02 +01004261 /* FIXME: We need a more portable way of releasing a backend's and a
4262 * server's connections. We need a safer way to reinitialize buffer
4263 * flags. We also need a more accurate method for computing per-request
4264 * data.
4265 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004266 /*
4267 * XXX cognet: This is probably wrong, this is killing a whole
4268 * connection, in the new world order, we probably want to just kill
4269 * the stream, this is to be revisited the day we handle multiple
4270 * streams in one server connection.
4271 */
4272 cs = objt_cs(s->si[1].end);
4273 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004274
Willy Tarreau4213a112013-12-15 10:25:42 +01004275 /* unless we're doing keep-alive, we want to quickly close the connection
4276 * to the server.
4277 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004278 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004279 !si_conn_ready(&s->si[1])) {
4280 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4281 si_shutr(&s->si[1]);
4282 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004283 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004284
Willy Tarreaue7dff022015-04-03 01:14:29 +02004285 if (s->flags & SF_BE_ASSIGNED) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004286 HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004287 if (unlikely(s->srv_conn))
4288 sess_change_server(s, NULL);
4289 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004290
4291 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02004292 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004293
Willy Tarreaueee5b512015-04-03 23:46:31 +02004294 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004295 int n;
4296
Willy Tarreaueee5b512015-04-03 23:46:31 +02004297 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004298 if (n < 1 || n > 5)
4299 n = 0;
4300
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004301 if (fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004302 HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004303 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02004304 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01004305 (be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004306 HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
4307 HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004308 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004309 }
4310
4311 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004312 s->logs.bytes_in -= s->req.buf->i;
4313 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004314
4315 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004316 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02004317 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004318 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004319 s->do_log(s);
4320 }
4321
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004322 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02004323 stream_stop_content_counters(s);
4324 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004325
Willy Tarreau610ecce2010-01-04 21:15:02 +01004326 s->logs.accept_date = date; /* user-visible date for logging */
4327 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02004328 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
4329 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004330 tv_zero(&s->logs.tv_request);
4331 s->logs.t_queue = -1;
4332 s->logs.t_connect = -1;
4333 s->logs.t_data = -1;
4334 s->logs.t_close = 0;
4335 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4336 s->logs.srv_queue_size = 0; /* we will get this number soon */
4337
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004338 s->logs.bytes_in = s->req.total = s->req.buf->i;
4339 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004340
4341 if (s->pend_pos)
4342 pendconn_free(s->pend_pos);
4343
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004344 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004345 if (s->flags & SF_CURR_SESS) {
4346 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +02004347 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004348 }
Willy Tarreau858b1032015-12-07 17:04:59 +01004349 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004350 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004351 }
4352
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004353 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004354
Willy Tarreau4213a112013-12-15 10:25:42 +01004355 /* only release our endpoint if we don't intend to reuse the
4356 * connection.
4357 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004358 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004359 !si_conn_ready(&s->si[1])) {
4360 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02004361 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004362 }
4363
Willy Tarreau350f4872014-11-28 14:42:25 +01004364 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
4365 s->si[1].err_type = SI_ET_NONE;
4366 s->si[1].conn_retries = 0; /* used for logging too */
4367 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02004368 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 +01004369 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 +01004370 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 +02004371 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
4372 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
4373 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01004374
Willy Tarreaueee5b512015-04-03 23:46:31 +02004375 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004376 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004377 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004378
4379 if (prev_status == 401 || prev_status == 407) {
4380 /* In HTTP keep-alive mode, if we receive a 401, we still have
4381 * a chance of being able to send the visitor again to the same
4382 * server over the same connection. This is required by some
4383 * broken protocols such as NTLM, and anyway whenever there is
4384 * an opportunity for sending the challenge to the proper place,
4385 * it's better to do it (at least it helps with debugging).
4386 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004387 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02004388 if (srv_conn)
4389 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01004390 }
4391
Willy Tarreau53f96852016-02-02 18:50:47 +01004392 /* Never ever allow to reuse a connection from a non-reuse backend */
4393 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
4394 srv_conn->flags |= CO_FL_PRIVATE;
4395
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004396 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01004397 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004398
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004399 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004400 s->req.flags |= CF_NEVER_WAIT;
4401 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004402 }
4403
Willy Tarreau714ea782015-11-25 20:11:11 +01004404 /* we're removing the analysers, we MUST re-enable events detection.
4405 * We don't enable close on the response channel since it's either
4406 * already closed, or in keep-alive with an idle connection handler.
4407 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004408 channel_auto_read(&s->req);
4409 channel_auto_close(&s->req);
4410 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004411
Willy Tarreau1c59bd52015-11-02 20:20:11 +01004412 /* we're in keep-alive with an idle connection, monitor it if not already done */
4413 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02004414 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02004415 if (!srv)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004416 si_idle_cs(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01004417 else if (srv_conn->flags & CO_FL_PRIVATE)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004418 si_idle_cs(&s->si[1], (srv->priv_conns ? &srv->priv_conns[tid] : NULL));
Willy Tarreau449d74a2015-08-05 17:16:33 +02004419 else if (prev_flags & TX_NOT_FIRST)
4420 /* note: we check the request, not the connection, but
4421 * this is valid for strategies SAFE and AGGR, and in
4422 * case of ALWS, we don't care anyway.
4423 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004424 si_idle_cs(&s->si[1], (srv->safe_conns ? &srv->safe_conns[tid] : NULL));
Willy Tarreau8dff9982015-08-04 20:45:52 +02004425 else
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004426 si_idle_cs(&s->si[1], (srv->idle_conns ? &srv->idle_conns[tid] : NULL));
Willy Tarreau4320eaa2015-08-05 11:08:30 +02004427 }
Christopher Faulete6006242017-03-10 11:52:44 +01004428 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
4429 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004430}
4431
4432
4433/* This function updates the request state machine according to the response
4434 * state machine and buffer flags. It returns 1 if it changes anything (flag
4435 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4436 * it is only used to find when a request/response couple is complete. Both
4437 * this function and its equivalent should loop until both return zero. It
4438 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4439 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004440int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004441{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004442 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004443 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004444 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004445 unsigned int old_state = txn->req.msg_state;
4446
Christopher Faulet4be98032017-07-18 10:48:24 +02004447 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004448 return 0;
4449
4450 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004451 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004452 * We can shut the read side unless we want to abort_on_close,
4453 * or we have a POST request. The issue with POST requests is
4454 * that some browsers still send a CRLF after the request, and
4455 * this CRLF must be read so that it does not remain in the kernel
4456 * buffers, otherwise a close could cause an RST on some systems
4457 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004458 * Note that if we're using keep-alive on the client side, we'd
4459 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02004460 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01004461 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004462 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004463 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4464 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004465 (!(s->be->options & PR_O_ABRT_CLOSE) ||
4466 (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01004467 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004468 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004469
Willy Tarreau40f151a2012-12-20 12:10:09 +01004470 /* if the server closes the connection, we want to immediately react
4471 * and close the socket to save packets and syscalls.
4472 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004473 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01004474
Willy Tarreau7f876a12015-11-18 11:59:55 +01004475 /* In any case we've finished parsing the request so we must
4476 * disable Nagle when sending data because 1) we're not going
4477 * to shut this side, and 2) the server is waiting for us to
4478 * send pending data.
4479 */
4480 chn->flags |= CF_NEVER_WAIT;
4481
Willy Tarreau610ecce2010-01-04 21:15:02 +01004482 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4483 goto wait_other_side;
4484
4485 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4486 /* The server has not finished to respond, so we
4487 * don't want to move in order not to upset it.
4488 */
4489 goto wait_other_side;
4490 }
4491
Willy Tarreau610ecce2010-01-04 21:15:02 +01004492 /* When we get here, it means that both the request and the
4493 * response have finished receiving. Depending on the connection
4494 * mode, we'll have to wait for the last bytes to leave in either
4495 * direction, and sometimes for a close to be effective.
4496 */
4497
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004498 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4499 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004500 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4501 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004502 }
4503 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4504 /* Option forceclose is set, or either side wants to close,
4505 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004506 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004507 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004508 *
4509 * However, there is an exception if the response
4510 * length is undefined. In this case, we need to wait
4511 * the close from the server. The response will be
4512 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004513 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004514 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4515 txn->rsp.msg_state != HTTP_MSG_CLOSED)
4516 goto check_channel_flags;
4517
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004518 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4519 channel_shutr_now(chn);
4520 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004521 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004522 }
4523 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004524 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4525 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004526 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004527 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4528 channel_auto_read(chn);
4529 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004530 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004531 }
4532
Christopher Faulet4be98032017-07-18 10:48:24 +02004533 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004534 }
4535
4536 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4537 http_msg_closing:
4538 /* nothing else to forward, just waiting for the output buffer
4539 * to be empty and for the shutw_now to take effect.
4540 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004541 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004542 txn->req.msg_state = HTTP_MSG_CLOSED;
4543 goto http_msg_closed;
4544 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004545 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004546 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004547 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004548 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004549 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004550 }
4551
4552 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4553 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01004554 /* if we don't know whether the server will close, we need to hard close */
4555 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4556 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4557
Willy Tarreau3988d932013-12-27 23:03:08 +01004558 /* see above in MSG_DONE why we only do this in these states */
4559 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4560 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004561 (!(s->be->options & PR_O_ABRT_CLOSE) ||
4562 (s->si[0].flags & SI_FL_CLEAN_ABRT)))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004563 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004564 goto wait_other_side;
4565 }
4566
Christopher Faulet4be98032017-07-18 10:48:24 +02004567 check_channel_flags:
4568 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4569 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4570 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02004571 txn->req.msg_state = HTTP_MSG_CLOSING;
4572 goto http_msg_closing;
4573 }
4574
4575
Willy Tarreau610ecce2010-01-04 21:15:02 +01004576 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004577 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004578}
4579
4580
4581/* This function updates the response state machine according to the request
4582 * state machine and buffer flags. It returns 1 if it changes anything (flag
4583 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4584 * it is only used to find when a request/response couple is complete. Both
4585 * this function and its equivalent should loop until both return zero. It
4586 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4587 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004588int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004589{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004590 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004591 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004592 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004593 unsigned int old_state = txn->rsp.msg_state;
4594
Christopher Faulet4be98032017-07-18 10:48:24 +02004595 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004596 return 0;
4597
4598 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4599 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004600 * still monitor the server connection for a possible close
4601 * while the request is being uploaded, so we don't disable
4602 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004603 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004604 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004605
4606 if (txn->req.msg_state == HTTP_MSG_ERROR)
4607 goto wait_other_side;
4608
4609 if (txn->req.msg_state < HTTP_MSG_DONE) {
4610 /* The client seems to still be sending data, probably
4611 * because we got an error response during an upload.
4612 * We have the choice of either breaking the connection
4613 * or letting it pass through. Let's do the later.
4614 */
4615 goto wait_other_side;
4616 }
4617
Willy Tarreau610ecce2010-01-04 21:15:02 +01004618 /* When we get here, it means that both the request and the
4619 * response have finished receiving. Depending on the connection
4620 * mode, we'll have to wait for the last bytes to leave in either
4621 * direction, and sometimes for a close to be effective.
4622 */
4623
4624 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4625 /* Server-close mode : shut read and wait for the request
4626 * side to close its output buffer. The caller will detect
4627 * when we're in DONE and the other is in CLOSED and will
4628 * catch that for the final cleanup.
4629 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004630 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4631 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004632 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004633 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4634 /* Option forceclose is set, or either side wants to close,
4635 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004636 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004637 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004638 *
4639 * However, there is an exception if the response length
4640 * is undefined. In this case, we switch in TUNNEL mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004641 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004642 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN)) {
4643 channel_auto_read(chn);
4644 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4645 chn->flags |= CF_NEVER_WAIT;
4646 }
4647 else if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004648 channel_shutr_now(chn);
4649 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004650 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004651 }
4652 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004653 /* The last possible modes are keep-alive and tunnel. Tunnel will
4654 * need to forward remaining data. Keep-alive will need to monitor
4655 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004656 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004657 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004658 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004659 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4660 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004661 }
4662
Christopher Faulet4be98032017-07-18 10:48:24 +02004663 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004664 }
4665
4666 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4667 http_msg_closing:
4668 /* nothing else to forward, just waiting for the output buffer
4669 * to be empty and for the shutw_now to take effect.
4670 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004671 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004672 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4673 goto http_msg_closed;
4674 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004675 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004676 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004677 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004678 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004679 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004680 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004681 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004682 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004683 }
4684
4685 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4686 http_msg_closed:
4687 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004688 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004689 channel_auto_close(chn);
4690 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004691 goto wait_other_side;
4692 }
4693
Christopher Faulet4be98032017-07-18 10:48:24 +02004694 check_channel_flags:
4695 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4696 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4697 /* if we've just closed an output, let's switch */
4698 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4699 goto http_msg_closing;
4700 }
4701
Willy Tarreau610ecce2010-01-04 21:15:02 +01004702 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004703 /* We force the response to leave immediately if we're waiting for the
4704 * other side, since there is no pending shutdown to push it out.
4705 */
4706 if (!channel_is_empty(chn))
4707 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004708 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004709}
4710
4711
Christopher Faulet894da4c2017-07-18 11:29:07 +02004712/* Resync the request and response state machines. */
4713void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004714{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004715 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004716#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004717 int old_req_state = txn->req.msg_state;
4718 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004719#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004720
Willy Tarreau610ecce2010-01-04 21:15:02 +01004721 http_sync_req_state(s);
4722 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004723 if (!http_sync_res_state(s))
4724 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004725 if (!http_sync_req_state(s))
4726 break;
4727 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004728
Christopher Faulet894da4c2017-07-18 11:29:07 +02004729 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4730 "req->analysers=0x%08x res->analysers=0x%08x\n",
4731 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004732 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4733 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004734 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004735
4736
Willy Tarreau610ecce2010-01-04 21:15:02 +01004737 /* OK, both state machines agree on a compatible state.
4738 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004739 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4740 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004741 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4742 * means we must abort the request.
4743 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4744 * corresponding channel.
4745 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4746 * on the response with server-close mode means we've completed one
4747 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004748 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004749 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4750 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004751 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004752 channel_auto_close(&s->req);
4753 channel_auto_read(&s->req);
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);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004757 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004758 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4759 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004760 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004761 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004762 channel_auto_close(&s->res);
4763 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004764 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004765 channel_abort(&s->req);
4766 channel_auto_close(&s->req);
4767 channel_auto_read(&s->req);
4768 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004769 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004770 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4771 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4772 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4773 s->req.analysers &= AN_REQ_FLT_END;
4774 if (HAS_REQ_DATA_FILTERS(s))
4775 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4776 }
4777 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4778 s->res.analysers &= AN_RES_FLT_END;
4779 if (HAS_RSP_DATA_FILTERS(s))
4780 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4781 }
4782 channel_auto_close(&s->req);
4783 channel_auto_read(&s->req);
4784 channel_auto_close(&s->res);
4785 channel_auto_read(&s->res);
4786 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004787 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4788 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004789 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004790 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4791 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4792 /* server-close/keep-alive: terminate this transaction,
4793 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004794 * a fresh-new transaction, but only once we're sure there's
4795 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004796 * another request. They must not hold any pending output data
4797 * and the response buffer must realigned
4798 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004799 */
Christopher Faulet894da4c2017-07-18 11:29:07 +02004800 if (s->req.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004801 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004802 else if (s->res.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004803 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004804 else {
4805 s->req.analysers = AN_REQ_FLT_END;
4806 s->res.analysers = AN_RES_FLT_END;
4807 txn->flags |= TX_WAIT_CLEANUP;
4808 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004809 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004810}
4811
Willy Tarreaud98cf932009-12-27 22:54:55 +01004812/* This function is an analyser which forwards request body (including chunk
4813 * sizes if any). It is called as soon as we must forward, even if we forward
4814 * zero byte. The only situation where it must not be called is when we're in
4815 * tunnel mode and we want to forward till the close. It's used both to forward
4816 * remaining data and to resync after end of body. It expects the msg_state to
4817 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004818 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004819 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004820 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004821 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004822int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004823{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004824 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004825 struct http_txn *txn = s->txn;
4826 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004827 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004828
Christopher Faulet814d2702017-03-30 11:33:44 +02004829 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
4830 now_ms, __FUNCTION__,
4831 s,
4832 req,
4833 req->rex, req->wex,
4834 req->flags,
4835 req->buf->i,
4836 req->analysers);
4837
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004838 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4839 return 0;
4840
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004841 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004842 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004843 /* Output closed while we were sending data. We must abort and
4844 * wake the other side up.
4845 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004846 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004847 msg->msg_state = HTTP_MSG_ERROR;
4848 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004849 return 1;
4850 }
4851
Willy Tarreaud98cf932009-12-27 22:54:55 +01004852 /* Note that we don't have to send 100-continue back because we don't
4853 * need the data to complete our job, and it's up to the server to
4854 * decide whether to return 100, 417 or anything else in return of
4855 * an "Expect: 100-continue" header.
4856 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004857 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004858 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4859 ? HTTP_MSG_CHUNK_SIZE
4860 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004861
4862 /* TODO/filters: when http-buffer-request option is set or if a
4863 * rule on url_param exists, the first chunk size could be
4864 * already parsed. In that case, msg->next is after the chunk
4865 * size (including the CRLF after the size). So this case should
4866 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004867 }
4868
Willy Tarreau7ba23542014-04-17 21:50:00 +02004869 /* Some post-connect processing might want us to refrain from starting to
4870 * forward data. Currently, the only reason for this is "balance url_param"
4871 * whichs need to parse/process the request after we've enabled forwarding.
4872 */
4873 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004874 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004875 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004876 req->flags |= CF_WAKE_CONNECT;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004877 goto missing_data_or_waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004878 }
4879 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4880 }
4881
Willy Tarreau80a92c02014-03-12 10:41:13 +01004882 /* in most states, we should abort in case of early close */
4883 channel_auto_close(req);
4884
Willy Tarreauefdf0942014-04-24 20:08:57 +02004885 if (req->to_forward) {
4886 /* We can't process the buffer's contents yet */
4887 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004888 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004889 }
4890
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004891 if (msg->msg_state < HTTP_MSG_DONE) {
4892 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4893 ? http_msg_forward_chunked_body(s, msg)
4894 : http_msg_forward_body(s, msg));
4895 if (!ret)
4896 goto missing_data_or_waiting;
4897 if (ret < 0)
4898 goto return_bad_req;
4899 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004900
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004901 /* other states, DONE...TUNNEL */
4902 /* we don't want to forward closes on DONE except in tunnel mode. */
4903 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4904 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004905
Christopher Faulet894da4c2017-07-18 11:29:07 +02004906 http_resync_states(s);
4907 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004908 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4909 if (req->flags & CF_SHUTW) {
4910 /* request errors are most likely due to the
4911 * server aborting the transfer. */
4912 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004913 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004914 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02004915 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004916 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004917 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004918 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004919 }
4920
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004921 /* If "option abortonclose" is set on the backend, we want to monitor
4922 * the client's connection and forward any shutdown notification to the
4923 * server, which will decide whether to close or to go on processing the
4924 * request. We only do that in tunnel mode, and not in other modes since
4925 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004926 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004927 channel_auto_read(req);
4928 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4929 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4930 s->si[1].flags |= SI_FL_NOLINGER;
4931 channel_auto_close(req);
4932 }
4933 else if (s->txn->meth == HTTP_METH_POST) {
4934 /* POST requests may require to read extra CRLF sent by broken
4935 * browsers and which could cause an RST to be sent upon close
4936 * on some systems (eg: Linux). */
4937 channel_auto_read(req);
4938 }
4939 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004940
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004941 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004942 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004943 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004944 if (!(s->flags & SF_ERR_MASK))
4945 s->flags |= SF_ERR_CLICL;
4946 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004947 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004948 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004949 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004950 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004951 }
4952
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004953 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4954 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004955 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004956 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004957
4958 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004959 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004960
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004961 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004962 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004963 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004964
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004965 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004966 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004967 * And when content-length is used, we never want to let the possible
4968 * shutdown be forwarded to the other side, as the state machine will
4969 * take care of it once the client responds. It's also important to
4970 * prevent TIME_WAITs from accumulating on the backend side, and for
4971 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004972 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004973 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004974 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004975
Willy Tarreau5c620922011-05-11 19:56:11 +02004976 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004977 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004978 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004979 * modes are already handled by the stream sock layer. We must not do
4980 * this in content-length mode because it could present the MSG_MORE
4981 * flag with the last block of forwarded data, which would cause an
4982 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004983 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004984 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004985 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004986
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004987 return 0;
4988
Willy Tarreaud98cf932009-12-27 22:54:55 +01004989 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004990 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004991 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004992 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02004993
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004994 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004995 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004996 txn->req.msg_state = HTTP_MSG_ERROR;
4997 if (txn->status) {
4998 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004999 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005000 } else {
5001 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005002 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005003 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005004 req->analysers &= AN_REQ_FLT_END;
5005 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 +01005006
Willy Tarreaue7dff022015-04-03 01:14:29 +02005007 if (!(s->flags & SF_ERR_MASK))
5008 s->flags |= SF_ERR_PRXCOND;
5009 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005010 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005011 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005012 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005013 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005014 }
5015 return 0;
5016
5017 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005018 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005019 txn->req.msg_state = HTTP_MSG_ERROR;
5020 if (txn->status) {
5021 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005022 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005023 } else {
5024 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005025 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005026 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005027 req->analysers &= AN_REQ_FLT_END;
5028 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 +01005029
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005030 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
5031 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005032 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005033 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005034
Willy Tarreaue7dff022015-04-03 01:14:29 +02005035 if (!(s->flags & SF_ERR_MASK))
5036 s->flags |= SF_ERR_SRVCL;
5037 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005038 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005039 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005040 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005041 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005042 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005043 return 0;
5044}
5045
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005046/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5047 * processing can continue on next analysers, or zero if it either needs more
5048 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005049 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005050 * when it has nothing left to do, and may remove any analyser when it wants to
5051 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005052 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005053int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005054{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005055 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005056 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005057 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005058 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005059 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005060 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005061 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005062
Willy Tarreau87b09662015-04-03 00:22:06 +02005063 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 +02005064 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005065 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005066 rep,
5067 rep->rex, rep->wex,
5068 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005069 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005070 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005071
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005072 /*
5073 * Now parse the partial (or complete) lines.
5074 * We will check the response syntax, and also join multi-line
5075 * headers. An index of all the lines will be elaborated while
5076 * parsing.
5077 *
5078 * For the parsing, we use a 28 states FSM.
5079 *
5080 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005081 * rep->buf->p = beginning of response
5082 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5083 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005084 * msg->eol = end of current header or line (LF or CRLF)
5085 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005086 */
5087
Willy Tarreau628c40c2014-04-24 19:11:26 +02005088 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005089 /* There's a protected area at the end of the buffer for rewriting
5090 * purposes. We don't want to start to parse the request if the
5091 * protected area is affected, because we may have to move processed
5092 * data later, which is much more complicated.
5093 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005094 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005095 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005096 /* some data has still not left the buffer, wake us once that's done */
5097 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5098 goto abort_response;
5099 channel_dont_close(rep);
5100 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005101 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005102 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005103 }
5104
Willy Tarreau379357a2013-06-08 12:55:46 +02005105 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5106 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5107 buffer_slow_realign(rep->buf);
5108
Willy Tarreau9b28e032012-10-12 23:49:43 +02005109 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005110 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005111 }
5112
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005113 /* 1: we might have to print this header in debug mode */
5114 if (unlikely((global.mode & MODE_DEBUG) &&
5115 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005116 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005117 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005118
Willy Tarreau9b28e032012-10-12 23:49:43 +02005119 sol = rep->buf->p;
5120 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005121 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005122
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005123 sol += hdr_idx_first_pos(&txn->hdr_idx);
5124 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005125
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005126 while (cur_idx) {
5127 eol = sol + txn->hdr_idx.v[cur_idx].len;
5128 debug_hdr("srvhdr", s, sol, eol);
5129 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5130 cur_idx = txn->hdr_idx.v[cur_idx].next;
5131 }
5132 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005133
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005134 /*
5135 * Now we quickly check if we have found a full valid response.
5136 * If not so, we check the FD and buffer states before leaving.
5137 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005138 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005139 * responses are checked first.
5140 *
5141 * Depending on whether the client is still there or not, we
5142 * may send an error response back or not. Note that normally
5143 * we should only check for HTTP status there, and check I/O
5144 * errors somewhere else.
5145 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005146
Willy Tarreau655dce92009-11-08 13:10:58 +01005147 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005148 /* Invalid response */
5149 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5150 /* we detected a parsing error. We want to archive this response
5151 * in the dedicated proxy area for later troubleshooting.
5152 */
5153 hdr_response_bad:
5154 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005155 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005156
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005157 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005158 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005159 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005160 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005161 }
Willy Tarreau64648412010-03-05 10:41:54 +01005162 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005163 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005164 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005165 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005166 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005167 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005168 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005169
Willy Tarreaue7dff022015-04-03 01:14:29 +02005170 if (!(s->flags & SF_ERR_MASK))
5171 s->flags |= SF_ERR_PRXCOND;
5172 if (!(s->flags & SF_FINST_MASK))
5173 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005174
5175 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005176 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005177
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005178 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005179 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005180 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005181 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005182 goto hdr_response_bad;
5183 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005184
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005185 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005186 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005187 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005188 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005189 else if (txn->flags & TX_NOT_FIRST)
5190 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005191
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005192 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005193 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005194 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005195 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005196 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005197
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005198 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005199 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005200 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01005201
5202 /* Check to see if the server refused the early data.
5203 * If so, just send a 425
5204 */
5205 if (objt_cs(s->si[1].end)) {
5206 struct connection *conn = objt_cs(s->si[1].end)->conn;
5207
5208 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
5209 txn->status = 425;
5210 }
5211
Willy Tarreau350f4872014-11-28 14:42:25 +01005212 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005213 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005214 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02005215
Willy Tarreaue7dff022015-04-03 01:14:29 +02005216 if (!(s->flags & SF_ERR_MASK))
5217 s->flags |= SF_ERR_SRVCL;
5218 if (!(s->flags & SF_FINST_MASK))
5219 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005220 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005221 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005222
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005223 /* read timeout : return a 504 to the client. */
5224 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005225 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005226 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005227
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005228 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005229 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005230 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005231 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005232 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005233
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005234 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005235 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005236 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005237 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005238 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005239 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02005240
Willy Tarreaue7dff022015-04-03 01:14:29 +02005241 if (!(s->flags & SF_ERR_MASK))
5242 s->flags |= SF_ERR_SRVTO;
5243 if (!(s->flags & SF_FINST_MASK))
5244 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005245 return 0;
5246 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005247
Willy Tarreauf003d372012-11-26 13:35:37 +01005248 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005249 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005250 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5251 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01005252 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005253 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01005254
Christopher Faulet0184ea72017-01-05 14:06:34 +01005255 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01005256 channel_auto_close(rep);
5257
5258 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005259 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005260 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01005261
Willy Tarreaue7dff022015-04-03 01:14:29 +02005262 if (!(s->flags & SF_ERR_MASK))
5263 s->flags |= SF_ERR_CLICL;
5264 if (!(s->flags & SF_FINST_MASK))
5265 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01005266
Willy Tarreau87b09662015-04-03 00:22:06 +02005267 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01005268 return 0;
5269 }
5270
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005271 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005272 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005273 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005274 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005275 else if (txn->flags & TX_NOT_FIRST)
5276 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005277
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005278 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005279 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005280 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005281 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005282 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005283
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005284 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005285 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005286 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005287 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005288 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005289 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005290
Willy Tarreaue7dff022015-04-03 01:14:29 +02005291 if (!(s->flags & SF_ERR_MASK))
5292 s->flags |= SF_ERR_SRVCL;
5293 if (!(s->flags & SF_FINST_MASK))
5294 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005295 return 0;
5296 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005297
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005298 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005299 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005300 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005301 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005302 else if (txn->flags & TX_NOT_FIRST)
5303 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005304
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005305 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005306 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005307 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005308
Willy Tarreaue7dff022015-04-03 01:14:29 +02005309 if (!(s->flags & SF_ERR_MASK))
5310 s->flags |= SF_ERR_CLICL;
5311 if (!(s->flags & SF_FINST_MASK))
5312 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005313
Willy Tarreau87b09662015-04-03 00:22:06 +02005314 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005315 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005316 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005317
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005318 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005319 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005320 return 0;
5321 }
5322
5323 /* More interesting part now : we know that we have a complete
5324 * response which at least looks like HTTP. We have an indicator
5325 * of each header's length, so we can parse them quickly.
5326 */
5327
5328 if (unlikely(msg->err_pos >= 0))
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005329 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005330
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005331 /*
5332 * 1: get the status code
5333 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005334 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005335 if (n < 1 || n > 5)
5336 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005337 /* when the client triggers a 4xx from the server, it's most often due
5338 * to a missing object or permission. These events should be tracked
5339 * because if they happen often, it may indicate a brute force or a
5340 * vulnerability scan.
5341 */
5342 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02005343 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02005344
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005345 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005346 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005347
Willy Tarreau91852eb2015-05-01 13:26:00 +02005348 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
5349 * exactly one digit "." one digit. This check may be disabled using
5350 * option accept-invalid-http-response.
5351 */
5352 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
5353 if (msg->sl.st.v_l != 8) {
5354 msg->err_pos = 0;
5355 goto hdr_response_bad;
5356 }
5357
5358 if (rep->buf->p[4] != '/' ||
5359 !isdigit((unsigned char)rep->buf->p[5]) ||
5360 rep->buf->p[6] != '.' ||
5361 !isdigit((unsigned char)rep->buf->p[7])) {
5362 msg->err_pos = 4;
5363 goto hdr_response_bad;
5364 }
5365 }
5366
Willy Tarreau5b154472009-12-21 20:11:07 +01005367 /* check if the response is HTTP/1.1 or above */
5368 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005369 ((rep->buf->p[5] > '1') ||
5370 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005371 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005372
5373 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005374 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 +01005375
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005376 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005377 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005378
Willy Tarreau9b28e032012-10-12 23:49:43 +02005379 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005380
Willy Tarreau39650402010-03-15 19:44:39 +01005381 /* Adjust server's health based on status code. Note: status codes 501
5382 * and 505 are triggered on demand by client request, so we must not
5383 * count them as server failures.
5384 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005385 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005386 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005387 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005388 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005389 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005390 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005391
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005392 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02005393 * We may be facing a 100-continue response, or any other informational
5394 * 1xx response which is non-final, in which case this is not the right
5395 * response, and we're waiting for the next one. Let's allow this response
5396 * to go to the client and wait for the next one. There's an exception for
5397 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005398 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02005399 if (txn->status < 200 &&
5400 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005401 hdr_idx_init(&txn->hdr_idx);
5402 msg->next -= channel_forward(rep, msg->next);
5403 msg->msg_state = HTTP_MSG_RPBEFORE;
5404 txn->status = 0;
5405 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01005406 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02005407 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02005408 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02005409
Willy Tarreaua14ad722017-07-07 11:36:32 +02005410 /*
5411 * 2: check for cacheability.
5412 */
5413
5414 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005415 case 200:
5416 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005417 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005418 case 206:
5419 case 300:
5420 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005421 case 404:
5422 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005423 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005424 case 414:
5425 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01005426 break;
5427 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005428 /* RFC7231#6.1:
5429 * Responses with status codes that are defined as
5430 * cacheable by default (e.g., 200, 203, 204, 206,
5431 * 300, 301, 404, 405, 410, 414, and 501 in this
5432 * specification) can be reused by a cache with
5433 * heuristic expiration unless otherwise indicated
5434 * by the method definition or explicit cache
5435 * controls [RFC7234]; all other status codes are
5436 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005437 */
Willy Tarreau83ece462017-12-21 15:13:09 +01005438 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005439 break;
5440 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005441
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005442 /*
5443 * 3: we may need to capture headers
5444 */
5445 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005446 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005447 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005448 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005449
Willy Tarreau557f1992015-05-01 10:05:17 +02005450 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
5451 * by RFC7230#3.3.3 :
5452 *
5453 * The length of a message body is determined by one of the following
5454 * (in order of precedence):
5455 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005456 * 1. Any 2xx (Successful) response to a CONNECT request implies that
5457 * the connection will become a tunnel immediately after the empty
5458 * line that concludes the header fields. A client MUST ignore
5459 * any Content-Length or Transfer-Encoding header fields received
5460 * in such a message. Any 101 response (Switching Protocols) is
5461 * managed in the same manner.
5462 *
5463 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02005464 * (Informational), 204 (No Content), or 304 (Not Modified) status
5465 * code is always terminated by the first empty line after the
5466 * header fields, regardless of the header fields present in the
5467 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005468 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005469 * 3. If a Transfer-Encoding header field is present and the chunked
5470 * transfer coding (Section 4.1) is the final encoding, the message
5471 * body length is determined by reading and decoding the chunked
5472 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005473 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005474 * If a Transfer-Encoding header field is present in a response and
5475 * the chunked transfer coding is not the final encoding, the
5476 * message body length is determined by reading the connection until
5477 * it is closed by the server. If a Transfer-Encoding header field
5478 * is present in a request and the chunked transfer coding is not
5479 * the final encoding, the message body length cannot be determined
5480 * reliably; the server MUST respond with the 400 (Bad Request)
5481 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005482 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005483 * If a message is received with both a Transfer-Encoding and a
5484 * Content-Length header field, the Transfer-Encoding overrides the
5485 * Content-Length. Such a message might indicate an attempt to
5486 * perform request smuggling (Section 9.5) or response splitting
5487 * (Section 9.4) and ought to be handled as an error. A sender MUST
5488 * remove the received Content-Length field prior to forwarding such
5489 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005490 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005491 * 4. If a message is received without Transfer-Encoding and with
5492 * either multiple Content-Length header fields having differing
5493 * field-values or a single Content-Length header field having an
5494 * invalid value, then the message framing is invalid and the
5495 * recipient MUST treat it as an unrecoverable error. If this is a
5496 * request message, the server MUST respond with a 400 (Bad Request)
5497 * status code and then close the connection. If this is a response
5498 * message received by a proxy, the proxy MUST close the connection
5499 * to the server, discard the received response, and send a 502 (Bad
5500 * Gateway) response to the client. If this is a response message
5501 * received by a user agent, the user agent MUST close the
5502 * connection to the server and discard the received response.
5503 *
5504 * 5. If a valid Content-Length header field is present without
5505 * Transfer-Encoding, its decimal value defines the expected message
5506 * body length in octets. If the sender closes the connection or
5507 * the recipient times out before the indicated number of octets are
5508 * received, the recipient MUST consider the message to be
5509 * incomplete and close the connection.
5510 *
5511 * 6. If this is a request message and none of the above are true, then
5512 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005513 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005514 * 7. Otherwise, this is a response message without a declared message
5515 * body length, so the message body length is determined by the
5516 * number of octets received prior to the server closing the
5517 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005518 */
5519
5520 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005521 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005522 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005523 * FIXME: should we parse anyway and return an error on chunked encoding ?
5524 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005525 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5526 txn->status == 101)) {
5527 /* Either we've established an explicit tunnel, or we're
5528 * switching the protocol. In both cases, we're very unlikely
5529 * to understand the next protocols. We have to switch to tunnel
5530 * mode, so that we transfer the request and responses then let
5531 * this protocol pass unmodified. When we later implement specific
5532 * parsers for such protocols, we'll want to check the Upgrade
5533 * header which contains information about that protocol for
5534 * responses with status 101 (eg: see RFC2817 about TLS).
5535 */
5536 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5537 msg->flags |= HTTP_MSGF_XFER_LEN;
5538 goto end;
5539 }
5540
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005541 if (txn->meth == HTTP_METH_HEAD ||
5542 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005543 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005544 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005545 goto skip_content_length;
5546 }
5547
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005548 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005549 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02005550 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005551 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005552 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5553 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005554 /* bad transfer-encoding (chunked followed by something else) */
5555 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005556 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005557 break;
5558 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005559 }
5560
Willy Tarreau1c913912015-04-30 10:57:51 +02005561 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005562 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005563 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02005564 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
5565 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5566 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005567 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005568 signed long long cl;
5569
Willy Tarreauad14f752011-09-02 20:33:27 +02005570 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005571 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005572 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005573 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005574
Willy Tarreauad14f752011-09-02 20:33:27 +02005575 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005576 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005577 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005578 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005579
Willy Tarreauad14f752011-09-02 20:33:27 +02005580 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005581 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005582 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005583 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005584
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005585 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005586 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005587 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005588 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005589
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005590 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005591 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005592 }
5593
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005594 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005595 /* Now we have to check if we need to modify the Connection header.
5596 * This is more difficult on the response than it is on the request,
5597 * because we can have two different HTTP versions and we don't know
5598 * how the client will interprete a response. For instance, let's say
5599 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5600 * HTTP/1.1 response without any header. Maybe it will bound itself to
5601 * HTTP/1.0 because it only knows about it, and will consider the lack
5602 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5603 * the lack of header as a keep-alive. Thus we will use two flags
5604 * indicating how a request MAY be understood by the client. In case
5605 * of multiple possibilities, we'll fix the header to be explicit. If
5606 * ambiguous cases such as both close and keepalive are seen, then we
5607 * will fall back to explicit close. Note that we won't take risks with
5608 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005609 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005610 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005611 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5612 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5613 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5614 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005615 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005616
Willy Tarreau70dffda2014-01-30 03:07:23 +01005617 /* this situation happens when combining pretend-keepalive with httpclose. */
5618 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005619 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005620 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01005621 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5622
Willy Tarreau60466522010-01-18 19:08:45 +01005623 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005624 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005625 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5626 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005627
Willy Tarreau60466522010-01-18 19:08:45 +01005628 /* now adjust header transformations depending on current state */
5629 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5630 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5631 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005632 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005633 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005634 }
Willy Tarreau60466522010-01-18 19:08:45 +01005635 else { /* SCL / KAL */
5636 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005637 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005638 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005639 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005640
Willy Tarreau60466522010-01-18 19:08:45 +01005641 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005642 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005643
Willy Tarreau60466522010-01-18 19:08:45 +01005644 /* Some keep-alive responses are converted to Server-close if
5645 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005646 */
Willy Tarreau60466522010-01-18 19:08:45 +01005647 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5648 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005649 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005650 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005651 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005652 }
5653
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005654 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005655 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005656 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005657
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005658 /* end of job, return OK */
5659 rep->analysers &= ~an_bit;
5660 rep->analyse_exp = TICK_ETERNITY;
5661 channel_auto_close(rep);
5662 return 1;
5663
5664 abort_keep_alive:
5665 /* A keep-alive request to the server failed on a network error.
5666 * The client is required to retry. We need to close without returning
5667 * any other information so that the client retries.
5668 */
5669 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005670 rep->analysers &= AN_RES_FLT_END;
5671 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005672 channel_auto_close(rep);
5673 s->logs.logwait = 0;
5674 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005675 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005676 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005677 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005678 return 0;
5679}
5680
5681/* This function performs all the processing enabled for the current response.
5682 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005683 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005684 * other functions. It works like process_request (see indications above).
5685 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005686int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005687{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005688 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005689 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005690 struct http_msg *msg = &txn->rsp;
5691 struct proxy *cur_proxy;
5692 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005693 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005694
Willy Tarreau87b09662015-04-03 00:22:06 +02005695 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 +02005696 now_ms, __FUNCTION__,
5697 s,
5698 rep,
5699 rep->rex, rep->wex,
5700 rep->flags,
5701 rep->buf->i,
5702 rep->analysers);
5703
5704 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5705 return 0;
5706
Willy Tarreau70730dd2014-04-24 18:06:27 +02005707 /* The stats applet needs to adjust the Connection header but we don't
5708 * apply any filter there.
5709 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005710 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5711 rep->analysers &= ~an_bit;
5712 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005713 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005714 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005715
Willy Tarreau58975672014-04-24 21:13:57 +02005716 /*
5717 * We will have to evaluate the filters.
5718 * As opposed to version 1.2, now they will be evaluated in the
5719 * filters order and not in the header order. This means that
5720 * each filter has to be validated among all headers.
5721 *
5722 * Filters are tried with ->be first, then with ->fe if it is
5723 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005724 *
5725 * Maybe we are in resume condiion. In this case I choose the
5726 * "struct proxy" which contains the rule list matching the resume
5727 * pointer. If none of theses "struct proxy" match, I initialise
5728 * the process with the first one.
5729 *
5730 * In fact, I check only correspondance betwwen the current list
5731 * pointer and the ->fe rule list. If it doesn't match, I initialize
5732 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005733 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005734 if (s->current_rule_list == &sess->fe->http_res_rules)
5735 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005736 else
5737 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005738 while (1) {
5739 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005740
Willy Tarreau58975672014-04-24 21:13:57 +02005741 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005742 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005743 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005744
Willy Tarreau51d861a2015-05-22 17:30:48 +02005745 if (ret == HTTP_RULE_RES_BADREQ)
5746 goto return_srv_prx_502;
5747
5748 if (ret == HTTP_RULE_RES_DONE) {
5749 rep->analysers &= ~an_bit;
5750 rep->analyse_exp = TICK_ETERNITY;
5751 return 1;
5752 }
5753 }
5754
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005755 /* we need to be called again. */
5756 if (ret == HTTP_RULE_RES_YIELD) {
5757 channel_dont_close(rep);
5758 return 0;
5759 }
5760
Willy Tarreau58975672014-04-24 21:13:57 +02005761 /* try headers filters */
5762 if (rule_set->rsp_exp != NULL) {
5763 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5764 return_bad_resp:
5765 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005766 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005767 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005768 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005769 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005770 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005771 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005772 txn->status = 502;
5773 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005774 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005775 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005776 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005777 if (!(s->flags & SF_ERR_MASK))
5778 s->flags |= SF_ERR_PRXCOND;
5779 if (!(s->flags & SF_FINST_MASK))
5780 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005781 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005782 }
Willy Tarreau58975672014-04-24 21:13:57 +02005783 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005784
Willy Tarreau58975672014-04-24 21:13:57 +02005785 /* has the response been denied ? */
5786 if (txn->flags & TX_SVDENY) {
5787 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005788 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005789
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005790 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5791 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005792 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005793 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005794
Willy Tarreau58975672014-04-24 21:13:57 +02005795 goto return_srv_prx_502;
5796 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005797
Willy Tarreau58975672014-04-24 21:13:57 +02005798 /* add response headers from the rule sets in the same order */
5799 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005800 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005801 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005802 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005803 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005804 ret = acl_pass(ret);
5805 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5806 ret = !ret;
5807 if (!ret)
5808 continue;
5809 }
5810 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
5811 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005812 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005813
Willy Tarreau58975672014-04-24 21:13:57 +02005814 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005815 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005816 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005817 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005818 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005819
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005820 /* After this point, this anayzer can't return yield, so we can
5821 * remove the bit corresponding to this analyzer from the list.
5822 *
5823 * Note that the intermediate returns and goto found previously
5824 * reset the analyzers.
5825 */
5826 rep->analysers &= ~an_bit;
5827 rep->analyse_exp = TICK_ETERNITY;
5828
Willy Tarreau58975672014-04-24 21:13:57 +02005829 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005830 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005831 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005832
Willy Tarreau58975672014-04-24 21:13:57 +02005833 /*
5834 * Now check for a server cookie.
5835 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005836 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005837 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005838
Willy Tarreau58975672014-04-24 21:13:57 +02005839 /*
5840 * Check for cache-control or pragma headers if required.
5841 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01005842 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02005843 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005844
Willy Tarreau58975672014-04-24 21:13:57 +02005845 /*
5846 * Add server cookie in the response if needed
5847 */
5848 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5849 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005850 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005851 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5852 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5853 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5854 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5855 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005856 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005857 /* the server is known, it's not the one the client requested, or the
5858 * cookie's last seen date needs to be refreshed. We have to
5859 * insert a set-cookie here, except if we want to insert only on POST
5860 * requests and this one isn't. Note that servers which don't have cookies
5861 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005862 */
Willy Tarreau58975672014-04-24 21:13:57 +02005863 if (!objt_server(s->target)->cookie) {
5864 chunk_printf(&trash,
5865 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5866 s->be->cookie_name);
5867 }
5868 else {
5869 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005870
Willy Tarreau58975672014-04-24 21:13:57 +02005871 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5872 /* emit last_date, which is mandatory */
5873 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5874 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5875 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005876
Willy Tarreau58975672014-04-24 21:13:57 +02005877 if (s->be->cookie_maxlife) {
5878 /* emit first_date, which is either the original one or
5879 * the current date.
5880 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005881 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005882 s30tob64(txn->cookie_first_date ?
5883 txn->cookie_first_date >> 2 :
5884 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005885 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005886 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005887 }
Willy Tarreau58975672014-04-24 21:13:57 +02005888 chunk_appendf(&trash, "; path=/");
5889 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005890
Willy Tarreau58975672014-04-24 21:13:57 +02005891 if (s->be->cookie_domain)
5892 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005893
Willy Tarreau58975672014-04-24 21:13:57 +02005894 if (s->be->ck_opts & PR_CK_HTTPONLY)
5895 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005896
Willy Tarreau58975672014-04-24 21:13:57 +02005897 if (s->be->ck_opts & PR_CK_SECURE)
5898 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005899
Willy Tarreau58975672014-04-24 21:13:57 +02005900 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
5901 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005902
Willy Tarreau58975672014-04-24 21:13:57 +02005903 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005904 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005905 /* the server did not change, only the date was updated */
5906 txn->flags |= TX_SCK_UPDATED;
5907 else
5908 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005909
Willy Tarreau58975672014-04-24 21:13:57 +02005910 /* Here, we will tell an eventual cache on the client side that we don't
5911 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5912 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5913 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005914 */
Willy Tarreau58975672014-04-24 21:13:57 +02005915 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005916
Willy Tarreau58975672014-04-24 21:13:57 +02005917 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005918
Willy Tarreau58975672014-04-24 21:13:57 +02005919 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5920 "Cache-control: private", 22) < 0))
5921 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005922 }
Willy Tarreau58975672014-04-24 21:13:57 +02005923 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005924
Willy Tarreau58975672014-04-24 21:13:57 +02005925 /*
5926 * Check if result will be cacheable with a cookie.
5927 * We'll block the response if security checks have caught
5928 * nasty things such as a cacheable cookie.
5929 */
5930 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5931 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5932 (s->be->options & PR_O_CHK_CACHE)) {
5933 /* we're in presence of a cacheable response containing
5934 * a set-cookie header. We'll block it as requested by
5935 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005936 */
Willy Tarreau58975672014-04-24 21:13:57 +02005937 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005938 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005939
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005940 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5941 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005942 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005943 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005944
Christopher Faulet767a84b2017-11-24 16:50:31 +01005945 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5946 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005947 send_log(s->be, LOG_ALERT,
5948 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5949 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5950 goto return_srv_prx_502;
5951 }
Willy Tarreau03945942009-12-22 16:50:27 +01005952
Willy Tarreau70730dd2014-04-24 18:06:27 +02005953 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005954 /*
5955 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5956 * If an "Upgrade" token is found, the header is left untouched in order
5957 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005958 * if anything but "Upgrade" is present in the Connection header. We don't
5959 * want to touch any 101 response either since it's switching to another
5960 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005961 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005962 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02005963 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005964 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02005965 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
5966 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005967
Willy Tarreau58975672014-04-24 21:13:57 +02005968 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5969 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5970 /* we want a keep-alive response here. Keep-alive header
5971 * required if either side is not 1.1.
5972 */
5973 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5974 want_flags |= TX_CON_KAL_SET;
5975 }
5976 else {
5977 /* we want a close response here. Close header required if
5978 * the server is 1.1, regardless of the client.
5979 */
5980 if (msg->flags & HTTP_MSGF_VER_11)
5981 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005982 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005983
Willy Tarreau58975672014-04-24 21:13:57 +02005984 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5985 http_change_connection_header(txn, msg, want_flags);
5986 }
5987
5988 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005989 /* Always enter in the body analyzer */
5990 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5991 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005992
Willy Tarreau58975672014-04-24 21:13:57 +02005993 /* if the user wants to log as soon as possible, without counting
5994 * bytes from the server, then this is the right moment. We have
5995 * to temporarily assign bytes_out to log what we currently have.
5996 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005997 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005998 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5999 s->logs.bytes_out = txn->rsp.eoh;
6000 s->do_log(s);
6001 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006002 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006003 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006004}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006005
Willy Tarreaud98cf932009-12-27 22:54:55 +01006006/* This function is an analyser which forwards response body (including chunk
6007 * sizes if any). It is called as soon as we must forward, even if we forward
6008 * zero byte. The only situation where it must not be called is when we're in
6009 * tunnel mode and we want to forward till the close. It's used both to forward
6010 * remaining data and to resync after end of body. It expects the msg_state to
6011 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006012 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006013 *
6014 * It is capable of compressing response data both in content-length mode and
6015 * in chunked mode. The state machines follows different flows depending on
6016 * whether content-length and chunked modes are used, since there are no
6017 * trailers in content-length :
6018 *
6019 * chk-mode cl-mode
6020 * ,----- BODY -----.
6021 * / \
6022 * V size > 0 V chk-mode
6023 * .--> SIZE -------------> DATA -------------> CRLF
6024 * | | size == 0 | last byte |
6025 * | v final crlf v inspected |
6026 * | TRAILERS -----------> DONE |
6027 * | |
6028 * `----------------------------------------------'
6029 *
6030 * Compression only happens in the DATA state, and must be flushed in final
6031 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6032 * is performed at once on final states for all bytes parsed, or when leaving
6033 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006034 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006035int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006036{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006037 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006038 struct http_txn *txn = s->txn;
6039 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01006040 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006041
Christopher Faulet814d2702017-03-30 11:33:44 +02006042 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6043 now_ms, __FUNCTION__,
6044 s,
6045 res,
6046 res->rex, res->wex,
6047 res->flags,
6048 res->buf->i,
6049 res->analysers);
6050
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006051 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6052 return 0;
6053
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006054 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006055 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006056 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006057 /* Output closed while we were sending data. We must abort and
6058 * wake the other side up.
6059 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006060 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006061 msg->msg_state = HTTP_MSG_ERROR;
6062 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006063 return 1;
6064 }
6065
Willy Tarreau4fe41902010-06-07 22:27:41 +02006066 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006067 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006068
Christopher Fauletd7c91962015-04-30 11:48:27 +02006069 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006070 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
6071 ? HTTP_MSG_CHUNK_SIZE
6072 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006073 }
6074
Willy Tarreauefdf0942014-04-24 20:08:57 +02006075 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006076 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02006077 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006078 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006079 }
6080
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006081 if (msg->msg_state < HTTP_MSG_DONE) {
6082 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6083 ? http_msg_forward_chunked_body(s, msg)
6084 : http_msg_forward_body(s, msg));
6085 if (!ret)
6086 goto missing_data_or_waiting;
6087 if (ret < 0)
6088 goto return_bad_res;
6089 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006090
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006091 /* other states, DONE...TUNNEL */
6092 /* for keep-alive we don't want to forward closes on DONE */
6093 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6094 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6095 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006096
Christopher Faulet894da4c2017-07-18 11:29:07 +02006097 http_resync_states(s);
6098 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006099 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6100 if (res->flags & CF_SHUTW) {
6101 /* response errors are most likely due to the
6102 * client aborting the transfer. */
6103 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006104 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006105 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006106 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 +01006107 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006108 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006109 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006110 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006111 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006112
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006113 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006114 if (res->flags & CF_SHUTW)
6115 goto aborted_xfer;
6116
6117 /* stop waiting for data if the input is closed before the end. If the
6118 * client side was already closed, it means that the client has aborted,
6119 * so we don't want to count this as a server abort. Otherwise it's a
6120 * server abort.
6121 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02006122 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006123 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006124 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006125 /* If we have some pending data, we continue the processing */
6126 if (!buffer_pending(res->buf)) {
6127 if (!(s->flags & SF_ERR_MASK))
6128 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006129 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006130 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006131 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006132 goto return_bad_res_stats_ok;
6133 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006134 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006135
Willy Tarreau40dba092010-03-04 18:14:51 +01006136 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006137 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006138 goto return_bad_res;
6139
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006140 /* When TE: chunked is used, we need to get there again to parse
6141 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02006142 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
6143 * or if there are filters registered on the stream, we don't want to
6144 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006145 */
Christopher Faulet69744d92017-03-30 10:54:35 +02006146 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006147 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006148 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6149 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006150 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006151
Willy Tarreau5c620922011-05-11 19:56:11 +02006152 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006153 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006154 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006155 * modes are already handled by the stream sock layer. We must not do
6156 * this in content-length mode because it could present the MSG_MORE
6157 * flag with the last block of forwarded data, which would cause an
6158 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006159 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006160 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006161 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006162
Willy Tarreau87b09662015-04-03 00:22:06 +02006163 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006164 return 0;
6165
Willy Tarreau40dba092010-03-04 18:14:51 +01006166 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006167 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006168 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006169 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006170
6171 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006172 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006173 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006174 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006175 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006176 res->analysers &= AN_RES_FLT_END;
6177 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 +01006178 if (objt_server(s->target))
6179 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006180
Willy Tarreaue7dff022015-04-03 01:14:29 +02006181 if (!(s->flags & SF_ERR_MASK))
6182 s->flags |= SF_ERR_PRXCOND;
6183 if (!(s->flags & SF_FINST_MASK))
6184 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006185 return 0;
6186
6187 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006188 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006189 txn->rsp.msg_state = HTTP_MSG_ERROR;
6190 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006191 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006192 res->analysers &= AN_RES_FLT_END;
6193 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 +01006194
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006195 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
6196 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006197 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006198 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006199
Willy Tarreaue7dff022015-04-03 01:14:29 +02006200 if (!(s->flags & SF_ERR_MASK))
6201 s->flags |= SF_ERR_CLICL;
6202 if (!(s->flags & SF_FINST_MASK))
6203 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006204 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006205}
6206
6207
6208static inline int
6209http_msg_forward_body(struct stream *s, struct http_msg *msg)
6210{
6211 struct channel *chn = msg->chn;
6212 int ret;
6213
6214 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
6215
6216 if (msg->msg_state == HTTP_MSG_ENDING)
6217 goto ending;
6218
6219 /* Neither content-length, nor transfer-encoding was found, so we must
6220 * read the body until the server connection is closed. In that case, we
6221 * eat data as they come. Of course, this happens for response only. */
6222 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6223 unsigned long long len = (chn->buf->i - msg->next);
6224 msg->chunk_len += len;
6225 msg->body_len += len;
6226 }
Christopher Fauletda02e172015-12-04 09:25:05 +01006227 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6228 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6229 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006230 msg->next += ret;
6231 msg->chunk_len -= ret;
6232 if (msg->chunk_len) {
6233 /* input empty or output full */
6234 if (chn->buf->i > msg->next)
6235 chn->flags |= CF_WAKE_WRITE;
6236 goto missing_data_or_waiting;
6237 }
6238
Christopher Faulet1486b0a2017-07-18 11:42:08 +02006239 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
6240 * always set for a request. */
6241 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6242 /* The server still sending data that should be filtered */
6243 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
6244 goto missing_data_or_waiting;
6245 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006246
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006247 msg->msg_state = HTTP_MSG_ENDING;
6248
6249 ending:
6250 /* we may have some pending data starting at res->buf->p such as a last
6251 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006252 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6253 /* default_ret */ msg->next,
6254 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006255 b_adv(chn->buf, ret);
6256 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006257 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6258 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006259 if (msg->next)
6260 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006261
Christopher Fauletda02e172015-12-04 09:25:05 +01006262 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
6263 /* default_ret */ 1,
6264 /* on_error */ goto error,
6265 /* on_wait */ goto waiting);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006266 msg->msg_state = HTTP_MSG_DONE;
6267 return 1;
6268
6269 missing_data_or_waiting:
6270 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006271 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6272 /* default_ret */ msg->next,
6273 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006274 b_adv(chn->buf, ret);
6275 msg->next -= ret;
6276 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6277 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006278 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006279 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006280 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006281 return 0;
6282 error:
6283 return -1;
6284}
6285
6286static inline int
6287http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
6288{
6289 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006290 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006291 int ret;
6292
6293 /* Here we have the guarantee to be in one of the following state:
6294 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
6295 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
6296
6297 switch_states:
6298 switch (msg->msg_state) {
6299 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01006300 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6301 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6302 /* on_error */ goto error);
6303 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006304 msg->chunk_len -= ret;
6305 if (msg->chunk_len) {
6306 /* input empty or output full */
6307 if (chn->buf->i > msg->next)
6308 chn->flags |= CF_WAKE_WRITE;
6309 goto missing_data_or_waiting;
6310 }
6311
6312 /* nothing left to forward for this chunk*/
6313 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
6314 /* fall through for HTTP_MSG_CHUNK_CRLF */
6315
6316 case HTTP_MSG_CHUNK_CRLF:
6317 /* we want the CRLF after the data */
Willy Tarreaub2892562017-09-21 11:33:54 +02006318 ret = h1_skip_chunk_crlf(chn->buf, msg->next, chn->buf->i);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006319 if (ret == 0)
6320 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02006321 if (ret < 0) {
6322 msg->err_pos = chn->buf->i + ret;
6323 if (msg->err_pos < 0)
6324 msg->err_pos += chn->buf->size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006325 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02006326 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01006327 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006328 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6329 /* fall through for HTTP_MSG_CHUNK_SIZE */
6330
6331 case HTTP_MSG_CHUNK_SIZE:
6332 /* read the chunk size and assign it to ->chunk_len,
6333 * then set ->next to point to the body and switch to
6334 * DATA or TRAILERS state.
6335 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006336 ret = h1_parse_chunk_size(chn->buf, msg->next, chn->buf->i, &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006337 if (ret == 0)
6338 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006339 if (ret < 0) {
6340 msg->err_pos = chn->buf->i + ret;
6341 if (msg->err_pos < 0)
6342 msg->err_pos += chn->buf->size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006343 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006344 }
6345
6346 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01006347 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006348 msg->chunk_len = chunk;
6349 msg->body_len += chunk;
6350
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006351 if (msg->chunk_len) {
6352 msg->msg_state = HTTP_MSG_DATA;
6353 goto switch_states;
6354 }
6355 msg->msg_state = HTTP_MSG_TRAILERS;
6356 /* fall through for HTTP_MSG_TRAILERS */
6357
6358 case HTTP_MSG_TRAILERS:
6359 ret = http_forward_trailers(msg);
6360 if (ret < 0)
6361 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01006362 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
6363 /* default_ret */ 1,
6364 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006365 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006366 if (!ret)
6367 goto missing_data_or_waiting;
6368 break;
6369
6370 case HTTP_MSG_ENDING:
6371 goto ending;
6372
6373 default:
6374 /* This should no happen in this function */
6375 goto error;
6376 }
6377
6378 msg->msg_state = HTTP_MSG_ENDING;
6379 ending:
6380 /* we may have some pending data starting at res->buf->p such as a last
6381 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006382 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006383 /* default_ret */ msg->next,
6384 /* on_error */ goto error);
6385 b_adv(chn->buf, ret);
6386 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006387 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6388 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006389 if (msg->next)
6390 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006391
Christopher Fauletda02e172015-12-04 09:25:05 +01006392 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006393 /* default_ret */ 1,
6394 /* on_error */ goto error,
6395 /* on_wait */ goto waiting);
6396 msg->msg_state = HTTP_MSG_DONE;
6397 return 1;
6398
6399 missing_data_or_waiting:
6400 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006401 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006402 /* default_ret */ msg->next,
6403 /* on_error */ goto error);
6404 b_adv(chn->buf, ret);
6405 msg->next -= ret;
6406 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6407 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006408 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006409 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006410 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006411 return 0;
6412
6413 chunk_parsing_error:
6414 if (msg->err_pos >= 0) {
6415 if (chn->flags & CF_ISRESP)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006416 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006417 msg->msg_state, strm_fe(s));
6418 else
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006419 http_capture_bad_message(strm_fe(s), &strm_fe(s)->invalid_req, s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006420 msg, msg->msg_state, s->be);
6421 }
6422 error:
6423 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006424}
6425
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006426
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006427/* Iterate the same filter through all request headers.
6428 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006429 * Since it can manage the switch to another backend, it updates the per-proxy
6430 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006431 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006432int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006433{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006434 char *cur_ptr, *cur_end, *cur_next;
6435 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006436 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006437 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006438 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006439
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006440 last_hdr = 0;
6441
Willy Tarreau9b28e032012-10-12 23:49:43 +02006442 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006443 old_idx = 0;
6444
6445 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006446 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006447 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006448 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006449 (exp->action == ACT_ALLOW ||
6450 exp->action == ACT_DENY ||
6451 exp->action == ACT_TARPIT))
6452 return 0;
6453
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006454 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006455 if (!cur_idx)
6456 break;
6457
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006458 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006459 cur_ptr = cur_next;
6460 cur_end = cur_ptr + cur_hdr->len;
6461 cur_next = cur_end + cur_hdr->cr + 1;
6462
6463 /* Now we have one header between cur_ptr and cur_end,
6464 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006465 */
6466
Willy Tarreau15a53a42015-01-21 13:39:42 +01006467 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006468 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006469 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006470 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006471 last_hdr = 1;
6472 break;
6473
6474 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006475 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006476 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006477 break;
6478
6479 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006480 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006481 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006482 break;
6483
6484 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006485 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6486 if (trash.len < 0)
6487 return -1;
6488
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006489 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006490 /* FIXME: if the user adds a newline in the replacement, the
6491 * index will not be recalculated for now, and the new line
6492 * will not be counted as a new header.
6493 */
6494
6495 cur_end += delta;
6496 cur_next += delta;
6497 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006498 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006499 break;
6500
6501 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006502 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006503 cur_next += delta;
6504
Willy Tarreaufa355d42009-11-29 18:12:29 +01006505 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006506 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6507 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006508 cur_hdr->len = 0;
6509 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006510 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006511 break;
6512
6513 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006514 }
6515
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006516 /* keep the link from this header to next one in case of later
6517 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006518 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006519 old_idx = cur_idx;
6520 }
6521 return 0;
6522}
6523
6524
6525/* Apply the filter to the request line.
6526 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6527 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006528 * Since it can manage the switch to another backend, it updates the per-proxy
6529 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006530 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006531int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006532{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006533 char *cur_ptr, *cur_end;
6534 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006535 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006536 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006537
Willy Tarreau3d300592007-03-18 18:34:41 +01006538 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006539 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006540 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006541 (exp->action == ACT_ALLOW ||
6542 exp->action == ACT_DENY ||
6543 exp->action == ACT_TARPIT))
6544 return 0;
6545 else if (exp->action == ACT_REMOVE)
6546 return 0;
6547
6548 done = 0;
6549
Willy Tarreau9b28e032012-10-12 23:49:43 +02006550 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006551 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006552
6553 /* Now we have the request line between cur_ptr and cur_end */
6554
Willy Tarreau15a53a42015-01-21 13:39:42 +01006555 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006556 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006557 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006558 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006559 done = 1;
6560 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006561
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006562 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006563 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006564 done = 1;
6565 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006566
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006567 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006568 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006569 done = 1;
6570 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006571
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006572 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006573 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6574 if (trash.len < 0)
6575 return -1;
6576
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006577 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006578 /* FIXME: if the user adds a newline in the replacement, the
6579 * index will not be recalculated for now, and the new line
6580 * will not be counted as a new header.
6581 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006582
Willy Tarreaufa355d42009-11-29 18:12:29 +01006583 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006584 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006585 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006586 HTTP_MSG_RQMETH,
6587 cur_ptr, cur_end + 1,
6588 NULL, NULL);
6589 if (unlikely(!cur_end))
6590 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006591
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006592 /* we have a full request and we know that we have either a CR
6593 * or an LF at <ptr>.
6594 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006595 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6596 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006597 /* there is no point trying this regex on headers */
6598 return 1;
6599 }
6600 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006601 return done;
6602}
Willy Tarreau97de6242006-12-27 17:18:38 +01006603
Willy Tarreau58f10d72006-12-04 02:26:12 +01006604
Willy Tarreau58f10d72006-12-04 02:26:12 +01006605
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006606/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006607 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006608 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006609 * unparsable request. Since it can manage the switch to another backend, it
6610 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006611 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006612int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006613{
Willy Tarreau192252e2015-04-04 01:47:55 +02006614 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006615 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006616 struct hdr_exp *exp;
6617
6618 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006619 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006620
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006621 /*
6622 * The interleaving of transformations and verdicts
6623 * makes it difficult to decide to continue or stop
6624 * the evaluation.
6625 */
6626
Willy Tarreau6c123b12010-01-28 20:22:06 +01006627 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6628 break;
6629
Willy Tarreau3d300592007-03-18 18:34:41 +01006630 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006631 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006632 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006633 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006634
6635 /* if this filter had a condition, evaluate it now and skip to
6636 * next filter if the condition does not match.
6637 */
6638 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006639 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006640 ret = acl_pass(ret);
6641 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6642 ret = !ret;
6643
6644 if (!ret)
6645 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006646 }
6647
6648 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006649 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006650 if (unlikely(ret < 0))
6651 return -1;
6652
6653 if (likely(ret == 0)) {
6654 /* The filter did not match the request, it can be
6655 * iterated through all headers.
6656 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006657 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6658 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006659 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006660 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006661 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006662}
6663
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006664
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006665/* Find the end of a cookie value contained between <s> and <e>. It works the
6666 * same way as with headers above except that the semi-colon also ends a token.
6667 * See RFC2965 for more information. Note that it requires a valid header to
6668 * return a valid result.
6669 */
6670char *find_cookie_value_end(char *s, const char *e)
6671{
6672 int quoted, qdpair;
6673
6674 quoted = qdpair = 0;
6675 for (; s < e; s++) {
6676 if (qdpair) qdpair = 0;
6677 else if (quoted) {
6678 if (*s == '\\') qdpair = 1;
6679 else if (*s == '"') quoted = 0;
6680 }
6681 else if (*s == '"') quoted = 1;
6682 else if (*s == ',' || *s == ';') return s;
6683 }
6684 return s;
6685}
6686
6687/* Delete a value in a header between delimiters <from> and <next> in buffer
6688 * <buf>. The number of characters displaced is returned, and the pointer to
6689 * the first delimiter is updated if required. The function tries as much as
6690 * possible to respect the following principles :
6691 * - replace <from> delimiter by the <next> one unless <from> points to a
6692 * colon, in which case <next> is simply removed
6693 * - set exactly one space character after the new first delimiter, unless
6694 * there are not enough characters in the block being moved to do so.
6695 * - remove unneeded spaces before the previous delimiter and after the new
6696 * one.
6697 *
6698 * It is the caller's responsibility to ensure that :
6699 * - <from> points to a valid delimiter or the colon ;
6700 * - <next> points to a valid delimiter or the final CR/LF ;
6701 * - there are non-space chars before <from> ;
6702 * - there is a CR/LF at or after <next>.
6703 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006704int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006705{
6706 char *prev = *from;
6707
6708 if (*prev == ':') {
6709 /* We're removing the first value, preserve the colon and add a
6710 * space if possible.
6711 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006712 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006713 next++;
6714 prev++;
6715 if (prev < next)
6716 *prev++ = ' ';
6717
Willy Tarreau2235b262016-11-05 15:50:20 +01006718 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006719 next++;
6720 } else {
6721 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006722 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006723 prev--;
6724 *from = prev;
6725
6726 /* copy the delimiter and if possible a space if we're
6727 * not at the end of the line.
6728 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006729 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006730 *prev++ = *next++;
6731 if (prev + 1 < next)
6732 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006733 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006734 next++;
6735 }
6736 }
6737 return buffer_replace2(buf, prev, next, NULL, 0);
6738}
6739
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006740/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006741 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006742 * desirable to call it only when needed. This code is quite complex because
6743 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6744 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006745 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006746void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006747{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006748 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006749 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006750 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006751 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006752 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6753 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006754
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006755 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006756 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006757 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006758
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006759 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006760 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006761 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006762
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006763 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006764 hdr_beg = hdr_next;
6765 hdr_end = hdr_beg + cur_hdr->len;
6766 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006767
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006768 /* We have one full header between hdr_beg and hdr_end, and the
6769 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006770 * "Cookie:" headers.
6771 */
6772
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006773 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006774 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006775 old_idx = cur_idx;
6776 continue;
6777 }
6778
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006779 del_from = NULL; /* nothing to be deleted */
6780 preserve_hdr = 0; /* assume we may kill the whole header */
6781
Willy Tarreau58f10d72006-12-04 02:26:12 +01006782 /* Now look for cookies. Conforming to RFC2109, we have to support
6783 * attributes whose name begin with a '$', and associate them with
6784 * the right cookie, if we want to delete this cookie.
6785 * So there are 3 cases for each cookie read :
6786 * 1) it's a special attribute, beginning with a '$' : ignore it.
6787 * 2) it's a server id cookie that we *MAY* want to delete : save
6788 * some pointers on it (last semi-colon, beginning of cookie...)
6789 * 3) it's an application cookie : we *MAY* have to delete a previous
6790 * "special" cookie.
6791 * At the end of loop, if a "special" cookie remains, we may have to
6792 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006793 * *MUST* delete it.
6794 *
6795 * Note: RFC2965 is unclear about the processing of spaces around
6796 * the equal sign in the ATTR=VALUE form. A careful inspection of
6797 * the RFC explicitly allows spaces before it, and not within the
6798 * tokens (attrs or values). An inspection of RFC2109 allows that
6799 * too but section 10.1.3 lets one think that spaces may be allowed
6800 * after the equal sign too, resulting in some (rare) buggy
6801 * implementations trying to do that. So let's do what servers do.
6802 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6803 * allowed quoted strings in values, with any possible character
6804 * after a backslash, including control chars and delimitors, which
6805 * causes parsing to become ambiguous. Browsers also allow spaces
6806 * within values even without quotes.
6807 *
6808 * We have to keep multiple pointers in order to support cookie
6809 * removal at the beginning, middle or end of header without
6810 * corrupting the header. All of these headers are valid :
6811 *
6812 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6813 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6814 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6815 * | | | | | | | | |
6816 * | | | | | | | | hdr_end <--+
6817 * | | | | | | | +--> next
6818 * | | | | | | +----> val_end
6819 * | | | | | +-----------> val_beg
6820 * | | | | +--------------> equal
6821 * | | | +----------------> att_end
6822 * | | +---------------------> att_beg
6823 * | +--------------------------> prev
6824 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006825 */
6826
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006827 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6828 /* Iterate through all cookies on this line */
6829
6830 /* find att_beg */
6831 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006832 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006833 att_beg++;
6834
6835 /* find att_end : this is the first character after the last non
6836 * space before the equal. It may be equal to hdr_end.
6837 */
6838 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006839
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006840 while (equal < hdr_end) {
6841 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006842 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006843 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006844 continue;
6845 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006846 }
6847
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006848 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6849 * is between <att_beg> and <equal>, both may be identical.
6850 */
6851
6852 /* look for end of cookie if there is an equal sign */
6853 if (equal < hdr_end && *equal == '=') {
6854 /* look for the beginning of the value */
6855 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006856 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006857 val_beg++;
6858
6859 /* find the end of the value, respecting quotes */
6860 next = find_cookie_value_end(val_beg, hdr_end);
6861
6862 /* make val_end point to the first white space or delimitor after the value */
6863 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006864 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006865 val_end--;
6866 } else {
6867 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006868 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006869
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006870 /* We have nothing to do with attributes beginning with '$'. However,
6871 * they will automatically be removed if a header before them is removed,
6872 * since they're supposed to be linked together.
6873 */
6874 if (*att_beg == '$')
6875 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006876
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006877 /* Ignore cookies with no equal sign */
6878 if (equal == next) {
6879 /* This is not our cookie, so we must preserve it. But if we already
6880 * scheduled another cookie for removal, we cannot remove the
6881 * complete header, but we can remove the previous block itself.
6882 */
6883 preserve_hdr = 1;
6884 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006885 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006886 val_end += delta;
6887 next += delta;
6888 hdr_end += delta;
6889 hdr_next += delta;
6890 cur_hdr->len += delta;
6891 http_msg_move_end(&txn->req, delta);
6892 prev = del_from;
6893 del_from = NULL;
6894 }
6895 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006896 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006897
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006898 /* if there are spaces around the equal sign, we need to
6899 * strip them otherwise we'll get trouble for cookie captures,
6900 * or even for rewrites. Since this happens extremely rarely,
6901 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006902 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006903 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6904 int stripped_before = 0;
6905 int stripped_after = 0;
6906
6907 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006908 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006909 equal += stripped_before;
6910 val_beg += stripped_before;
6911 }
6912
6913 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006914 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006915 val_beg += stripped_after;
6916 stripped_before += stripped_after;
6917 }
6918
6919 val_end += stripped_before;
6920 next += stripped_before;
6921 hdr_end += stripped_before;
6922 hdr_next += stripped_before;
6923 cur_hdr->len += stripped_before;
6924 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006925 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006926 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006927
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006928 /* First, let's see if we want to capture this cookie. We check
6929 * that we don't already have a client side cookie, because we
6930 * can only capture one. Also as an optimisation, we ignore
6931 * cookies shorter than the declared name.
6932 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006933 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6934 (val_end - att_beg >= sess->fe->capture_namelen) &&
6935 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006936 int log_len = val_end - att_beg;
6937
Willy Tarreaubafbe012017-11-24 17:34:44 +01006938 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006939 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006940 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006941 if (log_len > sess->fe->capture_len)
6942 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006943 memcpy(txn->cli_cookie, att_beg, log_len);
6944 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006945 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006946 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006947
Willy Tarreaubca99692010-10-06 19:25:55 +02006948 /* Persistence cookies in passive, rewrite or insert mode have the
6949 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006950 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006951 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006952 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006953 * For cookies in prefix mode, the form is :
6954 *
6955 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006956 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006957 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6958 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6959 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006960 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006961
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006962 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6963 * have the server ID between val_beg and delim, and the original cookie between
6964 * delim+1 and val_end. Otherwise, delim==val_end :
6965 *
6966 * Cookie: NAME=SRV; # in all but prefix modes
6967 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6968 * | || || | |+-> next
6969 * | || || | +--> val_end
6970 * | || || +---------> delim
6971 * | || |+------------> val_beg
6972 * | || +-------------> att_end = equal
6973 * | |+-----------------> att_beg
6974 * | +------------------> prev
6975 * +-------------------------> hdr_beg
6976 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006977
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006978 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006979 for (delim = val_beg; delim < val_end; delim++)
6980 if (*delim == COOKIE_DELIM)
6981 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006982 } else {
6983 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006984 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006985 /* Now check if the cookie contains a date field, which would
6986 * appear after a vertical bar ('|') just after the server name
6987 * and before the delimiter.
6988 */
6989 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6990 if (vbar1) {
6991 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006992 * right is the last seen date. It is a base64 encoded
6993 * 30-bit value representing the UNIX date since the
6994 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006995 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006996 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006997 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006998 if (val_end - vbar1 >= 5) {
6999 val = b64tos30(vbar1);
7000 if (val > 0)
7001 txn->cookie_last_date = val << 2;
7002 }
7003 /* look for a second vertical bar */
7004 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7005 if (vbar1 && (val_end - vbar1 > 5)) {
7006 val = b64tos30(vbar1 + 1);
7007 if (val > 0)
7008 txn->cookie_first_date = val << 2;
7009 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007010 }
7011 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007012
Willy Tarreauf64d1412010-10-07 20:06:11 +02007013 /* if the cookie has an expiration date and the proxy wants to check
7014 * it, then we do that now. We first check if the cookie is too old,
7015 * then only if it has expired. We detect strict overflow because the
7016 * time resolution here is not great (4 seconds). Cookies with dates
7017 * in the future are ignored if their offset is beyond one day. This
7018 * allows an admin to fix timezone issues without expiring everyone
7019 * and at the same time avoids keeping unwanted side effects for too
7020 * long.
7021 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007022 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7023 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007024 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007025 txn->flags &= ~TX_CK_MASK;
7026 txn->flags |= TX_CK_OLD;
7027 delim = val_beg; // let's pretend we have not found the cookie
7028 txn->cookie_first_date = 0;
7029 txn->cookie_last_date = 0;
7030 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007031 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7032 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007033 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007034 txn->flags &= ~TX_CK_MASK;
7035 txn->flags |= TX_CK_EXPIRED;
7036 delim = val_beg; // let's pretend we have not found the cookie
7037 txn->cookie_first_date = 0;
7038 txn->cookie_last_date = 0;
7039 }
7040
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007041 /* Here, we'll look for the first running server which supports the cookie.
7042 * This allows to share a same cookie between several servers, for example
7043 * to dedicate backup servers to specific servers only.
7044 * However, to prevent clients from sticking to cookie-less backup server
7045 * when they have incidentely learned an empty cookie, we simply ignore
7046 * empty cookies and mark them as invalid.
7047 * The same behaviour is applied when persistence must be ignored.
7048 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007049 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007050 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007051
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007052 while (srv) {
7053 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7054 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02007055 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007056 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007057 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007058 /* we found the server and we can use it */
7059 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02007060 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007061 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007062 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007063 break;
7064 } else {
7065 /* we found a server, but it's down,
7066 * mark it as such and go on in case
7067 * another one is available.
7068 */
7069 txn->flags &= ~TX_CK_MASK;
7070 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007071 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007072 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007073 srv = srv->next;
7074 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007075
Willy Tarreauf64d1412010-10-07 20:06:11 +02007076 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007077 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007078 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007079 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007080 txn->flags |= TX_CK_UNUSED;
7081 else
7082 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007083 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007084
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007085 /* depending on the cookie mode, we may have to either :
7086 * - delete the complete cookie if we're in insert+indirect mode, so that
7087 * the server never sees it ;
7088 * - remove the server id from the cookie value, and tag the cookie as an
7089 * application cookie so that it does not get accidentely removed later,
7090 * if we're in cookie prefix mode
7091 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007092 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007093 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007094
Willy Tarreau9b28e032012-10-12 23:49:43 +02007095 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007096 val_end += delta;
7097 next += delta;
7098 hdr_end += delta;
7099 hdr_next += delta;
7100 cur_hdr->len += delta;
7101 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007102
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007103 del_from = NULL;
7104 preserve_hdr = 1; /* we want to keep this cookie */
7105 }
7106 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007107 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007108 del_from = prev;
7109 }
7110 } else {
7111 /* This is not our cookie, so we must preserve it. But if we already
7112 * scheduled another cookie for removal, we cannot remove the
7113 * complete header, but we can remove the previous block itself.
7114 */
7115 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007116
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007117 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007118 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007119 if (att_beg >= del_from)
7120 att_beg += delta;
7121 if (att_end >= del_from)
7122 att_end += delta;
7123 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007124 val_end += delta;
7125 next += delta;
7126 hdr_end += delta;
7127 hdr_next += delta;
7128 cur_hdr->len += delta;
7129 http_msg_move_end(&txn->req, delta);
7130 prev = del_from;
7131 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007132 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007133 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007134
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007135 /* continue with next cookie on this header line */
7136 att_beg = next;
7137 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007138
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007139 /* There are no more cookies on this line.
7140 * We may still have one (or several) marked for deletion at the
7141 * end of the line. We must do this now in two ways :
7142 * - if some cookies must be preserved, we only delete from the
7143 * mark to the end of line ;
7144 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007145 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007146 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007147 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007148 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007149 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007150 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007151 cur_hdr->len += delta;
7152 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007153 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007154
7155 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007156 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7157 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007158 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007159 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007160 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007161 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007162 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007163 }
7164
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007165 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007166 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007167 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007168}
7169
7170
Willy Tarreaua15645d2007-03-18 16:22:39 +01007171/* Iterate the same filter through all response headers contained in <rtr>.
7172 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7173 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007174int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007175{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007176 char *cur_ptr, *cur_end, *cur_next;
7177 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007178 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007179 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007180 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007181
7182 last_hdr = 0;
7183
Willy Tarreau9b28e032012-10-12 23:49:43 +02007184 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007185 old_idx = 0;
7186
7187 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007188 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007189 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007190 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007191 (exp->action == ACT_ALLOW ||
7192 exp->action == ACT_DENY))
7193 return 0;
7194
7195 cur_idx = txn->hdr_idx.v[old_idx].next;
7196 if (!cur_idx)
7197 break;
7198
7199 cur_hdr = &txn->hdr_idx.v[cur_idx];
7200 cur_ptr = cur_next;
7201 cur_end = cur_ptr + cur_hdr->len;
7202 cur_next = cur_end + cur_hdr->cr + 1;
7203
7204 /* Now we have one header between cur_ptr and cur_end,
7205 * and the next header starts at cur_next.
7206 */
7207
Willy Tarreau15a53a42015-01-21 13:39:42 +01007208 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007209 switch (exp->action) {
7210 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007211 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007212 last_hdr = 1;
7213 break;
7214
7215 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007216 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007217 last_hdr = 1;
7218 break;
7219
7220 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007221 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7222 if (trash.len < 0)
7223 return -1;
7224
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007225 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007226 /* FIXME: if the user adds a newline in the replacement, the
7227 * index will not be recalculated for now, and the new line
7228 * will not be counted as a new header.
7229 */
7230
7231 cur_end += delta;
7232 cur_next += delta;
7233 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007234 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007235 break;
7236
7237 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007238 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007239 cur_next += delta;
7240
Willy Tarreaufa355d42009-11-29 18:12:29 +01007241 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007242 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7243 txn->hdr_idx.used--;
7244 cur_hdr->len = 0;
7245 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007246 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007247 break;
7248
7249 }
7250 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007251
7252 /* keep the link from this header to next one in case of later
7253 * removal of next header.
7254 */
7255 old_idx = cur_idx;
7256 }
7257 return 0;
7258}
7259
7260
7261/* Apply the filter to the status line in the response buffer <rtr>.
7262 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7263 * or -1 if a replacement resulted in an invalid status line.
7264 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007265int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007266{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007267 char *cur_ptr, *cur_end;
7268 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007269 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007270 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007271
7272
Willy Tarreau3d300592007-03-18 18:34:41 +01007273 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007274 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007275 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007276 (exp->action == ACT_ALLOW ||
7277 exp->action == ACT_DENY))
7278 return 0;
7279 else if (exp->action == ACT_REMOVE)
7280 return 0;
7281
7282 done = 0;
7283
Willy Tarreau9b28e032012-10-12 23:49:43 +02007284 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007285 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007286
7287 /* Now we have the status line between cur_ptr and cur_end */
7288
Willy Tarreau15a53a42015-01-21 13:39:42 +01007289 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007290 switch (exp->action) {
7291 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007292 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007293 done = 1;
7294 break;
7295
7296 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007297 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007298 done = 1;
7299 break;
7300
7301 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007302 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7303 if (trash.len < 0)
7304 return -1;
7305
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007306 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007307 /* FIXME: if the user adds a newline in the replacement, the
7308 * index will not be recalculated for now, and the new line
7309 * will not be counted as a new header.
7310 */
7311
Willy Tarreaufa355d42009-11-29 18:12:29 +01007312 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007313 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007314 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007315 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007316 cur_ptr, cur_end + 1,
7317 NULL, NULL);
7318 if (unlikely(!cur_end))
7319 return -1;
7320
7321 /* we have a full respnse and we know that we have either a CR
7322 * or an LF at <ptr>.
7323 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007324 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007325 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007326 /* there is no point trying this regex on headers */
7327 return 1;
7328 }
7329 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007330 return done;
7331}
7332
7333
7334
7335/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007336 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007337 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7338 * unparsable response.
7339 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007340int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007341{
Willy Tarreau192252e2015-04-04 01:47:55 +02007342 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007343 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007344 struct hdr_exp *exp;
7345
7346 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007347 int ret;
7348
7349 /*
7350 * The interleaving of transformations and verdicts
7351 * makes it difficult to decide to continue or stop
7352 * the evaluation.
7353 */
7354
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007355 if (txn->flags & TX_SVDENY)
7356 break;
7357
Willy Tarreau3d300592007-03-18 18:34:41 +01007358 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007359 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7360 exp->action == ACT_PASS)) {
7361 exp = exp->next;
7362 continue;
7363 }
7364
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007365 /* if this filter had a condition, evaluate it now and skip to
7366 * next filter if the condition does not match.
7367 */
7368 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007369 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007370 ret = acl_pass(ret);
7371 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7372 ret = !ret;
7373 if (!ret)
7374 continue;
7375 }
7376
Willy Tarreaua15645d2007-03-18 16:22:39 +01007377 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007378 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007379 if (unlikely(ret < 0))
7380 return -1;
7381
7382 if (likely(ret == 0)) {
7383 /* The filter did not match the response, it can be
7384 * iterated through all headers.
7385 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007386 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7387 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007388 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007389 }
7390 return 0;
7391}
7392
7393
Willy Tarreaua15645d2007-03-18 16:22:39 +01007394/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007395 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007396 * desirable to call it only when needed. This function is also used when we
7397 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007398 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007399void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007400{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007401 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007402 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01007403 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007404 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007405 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007406 char *hdr_beg, *hdr_end, *hdr_next;
7407 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007408
Willy Tarreaua15645d2007-03-18 16:22:39 +01007409 /* Iterate through the headers.
7410 * we start with the start line.
7411 */
7412 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007413 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007414
7415 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7416 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007417 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007418
7419 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007420 hdr_beg = hdr_next;
7421 hdr_end = hdr_beg + cur_hdr->len;
7422 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007423
Willy Tarreau24581ba2010-08-31 22:39:35 +02007424 /* We have one full header between hdr_beg and hdr_end, and the
7425 * next header starts at hdr_next. We're only interested in
7426 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007427 */
7428
Willy Tarreau24581ba2010-08-31 22:39:35 +02007429 is_cookie2 = 0;
7430 prev = hdr_beg + 10;
7431 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007432 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007433 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7434 if (!val) {
7435 old_idx = cur_idx;
7436 continue;
7437 }
7438 is_cookie2 = 1;
7439 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007440 }
7441
Willy Tarreau24581ba2010-08-31 22:39:35 +02007442 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7443 * <prev> points to the colon.
7444 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007445 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007446
Willy Tarreau24581ba2010-08-31 22:39:35 +02007447 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7448 * check-cache is enabled) and we are not interested in checking
7449 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007450 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02007451 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007452 return;
7453
Willy Tarreau24581ba2010-08-31 22:39:35 +02007454 /* OK so now we know we have to process this response cookie.
7455 * The format of the Set-Cookie header is slightly different
7456 * from the format of the Cookie header in that it does not
7457 * support the comma as a cookie delimiter (thus the header
7458 * cannot be folded) because the Expires attribute described in
7459 * the original Netscape's spec may contain an unquoted date
7460 * with a comma inside. We have to live with this because
7461 * many browsers don't support Max-Age and some browsers don't
7462 * support quoted strings. However the Set-Cookie2 header is
7463 * clean.
7464 *
7465 * We have to keep multiple pointers in order to support cookie
7466 * removal at the beginning, middle or end of header without
7467 * corrupting the header (in case of set-cookie2). A special
7468 * pointer, <scav> points to the beginning of the set-cookie-av
7469 * fields after the first semi-colon. The <next> pointer points
7470 * either to the end of line (set-cookie) or next unquoted comma
7471 * (set-cookie2). All of these headers are valid :
7472 *
7473 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7474 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7475 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7476 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7477 * | | | | | | | | | |
7478 * | | | | | | | | +-> next hdr_end <--+
7479 * | | | | | | | +------------> scav
7480 * | | | | | | +--------------> val_end
7481 * | | | | | +--------------------> val_beg
7482 * | | | | +----------------------> equal
7483 * | | | +------------------------> att_end
7484 * | | +----------------------------> att_beg
7485 * | +------------------------------> prev
7486 * +-----------------------------------------> hdr_beg
7487 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007488
Willy Tarreau24581ba2010-08-31 22:39:35 +02007489 for (; prev < hdr_end; prev = next) {
7490 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007491
Willy Tarreau24581ba2010-08-31 22:39:35 +02007492 /* find att_beg */
7493 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007494 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007495 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007496
Willy Tarreau24581ba2010-08-31 22:39:35 +02007497 /* find att_end : this is the first character after the last non
7498 * space before the equal. It may be equal to hdr_end.
7499 */
7500 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007501
Willy Tarreau24581ba2010-08-31 22:39:35 +02007502 while (equal < hdr_end) {
7503 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7504 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007505 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007506 continue;
7507 att_end = equal;
7508 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007509
Willy Tarreau24581ba2010-08-31 22:39:35 +02007510 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7511 * is between <att_beg> and <equal>, both may be identical.
7512 */
7513
7514 /* look for end of cookie if there is an equal sign */
7515 if (equal < hdr_end && *equal == '=') {
7516 /* look for the beginning of the value */
7517 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007518 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007519 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007520
Willy Tarreau24581ba2010-08-31 22:39:35 +02007521 /* find the end of the value, respecting quotes */
7522 next = find_cookie_value_end(val_beg, hdr_end);
7523
7524 /* make val_end point to the first white space or delimitor after the value */
7525 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007526 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007527 val_end--;
7528 } else {
7529 /* <equal> points to next comma, semi-colon or EOL */
7530 val_beg = val_end = next = equal;
7531 }
7532
7533 if (next < hdr_end) {
7534 /* Set-Cookie2 supports multiple cookies, and <next> points to
7535 * a colon or semi-colon before the end. So skip all attr-value
7536 * pairs and look for the next comma. For Set-Cookie, since
7537 * commas are permitted in values, skip to the end.
7538 */
7539 if (is_cookie2)
7540 next = find_hdr_value_end(next, hdr_end);
7541 else
7542 next = hdr_end;
7543 }
7544
7545 /* Now everything is as on the diagram above */
7546
7547 /* Ignore cookies with no equal sign */
7548 if (equal == val_end)
7549 continue;
7550
7551 /* If there are spaces around the equal sign, we need to
7552 * strip them otherwise we'll get trouble for cookie captures,
7553 * or even for rewrites. Since this happens extremely rarely,
7554 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007555 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007556 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7557 int stripped_before = 0;
7558 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007559
Willy Tarreau24581ba2010-08-31 22:39:35 +02007560 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007561 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007562 equal += stripped_before;
7563 val_beg += stripped_before;
7564 }
7565
7566 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007567 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007568 val_beg += stripped_after;
7569 stripped_before += stripped_after;
7570 }
7571
7572 val_end += stripped_before;
7573 next += stripped_before;
7574 hdr_end += stripped_before;
7575 hdr_next += stripped_before;
7576 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007577 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007578 }
7579
7580 /* First, let's see if we want to capture this cookie. We check
7581 * that we don't already have a server side cookie, because we
7582 * can only capture one. Also as an optimisation, we ignore
7583 * cookies shorter than the declared name.
7584 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007585 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007586 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007587 (val_end - att_beg >= sess->fe->capture_namelen) &&
7588 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007589 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007590 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007591 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01007592 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007593 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007594 if (log_len > sess->fe->capture_len)
7595 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007596 memcpy(txn->srv_cookie, att_beg, log_len);
7597 txn->srv_cookie[log_len] = 0;
7598 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007599 }
7600
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007601 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007602 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007603 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007604 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7605 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007606 /* assume passive cookie by default */
7607 txn->flags &= ~TX_SCK_MASK;
7608 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007609
7610 /* If the cookie is in insert mode on a known server, we'll delete
7611 * this occurrence because we'll insert another one later.
7612 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007613 * a direct access.
7614 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007615 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007616 /* The "preserve" flag was set, we don't want to touch the
7617 * server's cookie.
7618 */
7619 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007620 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007621 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007622 /* this cookie must be deleted */
7623 if (*prev == ':' && next == hdr_end) {
7624 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007625 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007626 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7627 txn->hdr_idx.used--;
7628 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007629 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007630 hdr_next += delta;
7631 http_msg_move_end(&txn->rsp, delta);
7632 /* note: while both invalid now, <next> and <hdr_end>
7633 * are still equal, so the for() will stop as expected.
7634 */
7635 } else {
7636 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007637 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007638 next = prev;
7639 hdr_end += delta;
7640 hdr_next += delta;
7641 cur_hdr->len += delta;
7642 http_msg_move_end(&txn->rsp, delta);
7643 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007644 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007645 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007646 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007647 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007648 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007649 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007650 * with this server since we know it.
7651 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007652 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007653 next += delta;
7654 hdr_end += delta;
7655 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007656 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007657 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007658
Willy Tarreauf1348312010-10-07 15:54:11 +02007659 txn->flags &= ~TX_SCK_MASK;
7660 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007661 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007662 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007663 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007664 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007665 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007666 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007667 next += delta;
7668 hdr_end += delta;
7669 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007670 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007671 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007672
Willy Tarreau827aee92011-03-10 16:55:02 +01007673 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007674 txn->flags &= ~TX_SCK_MASK;
7675 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007676 }
7677 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007678 /* that's done for this cookie, check the next one on the same
7679 * line when next != hdr_end (only if is_cookie2).
7680 */
7681 }
7682 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007683 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007684 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007685}
7686
7687
Willy Tarreaua15645d2007-03-18 16:22:39 +01007688/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007689 * Parses the Cache-Control and Pragma request header fields to determine if
7690 * the request may be served from the cache and/or if it is cacheable. Updates
7691 * s->txn->flags.
7692 */
7693void check_request_for_cacheability(struct stream *s, struct channel *chn)
7694{
7695 struct http_txn *txn = s->txn;
7696 char *p1, *p2;
7697 char *cur_ptr, *cur_end, *cur_next;
7698 int pragma_found;
7699 int cc_found;
7700 int cur_idx;
7701
7702 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
7703 return; /* nothing more to do here */
7704
7705 cur_idx = 0;
7706 pragma_found = cc_found = 0;
7707 cur_next = chn->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
7708
7709 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7710 struct hdr_idx_elem *cur_hdr;
7711 int val;
7712
7713 cur_hdr = &txn->hdr_idx.v[cur_idx];
7714 cur_ptr = cur_next;
7715 cur_end = cur_ptr + cur_hdr->len;
7716 cur_next = cur_end + cur_hdr->cr + 1;
7717
7718 /* We have one full header between cur_ptr and cur_end, and the
7719 * next header starts at cur_next.
7720 */
7721
7722 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7723 if (val) {
7724 if ((cur_end - (cur_ptr + val) >= 8) &&
7725 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7726 pragma_found = 1;
7727 continue;
7728 }
7729 }
7730
7731 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7732 if (!val)
7733 continue;
7734
7735 /* OK, right now we know we have a cache-control header at cur_ptr */
7736 cc_found = 1;
7737 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
7738
7739 if (p1 >= cur_end) /* no more info */
7740 continue;
7741
7742 /* p1 is at the beginning of the value */
7743 p2 = p1;
7744 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
7745 p2++;
7746
7747 /* we have a complete value between p1 and p2. We don't check the
7748 * values after max-age, max-stale nor min-fresh, we simply don't
7749 * use the cache when they're specified.
7750 */
7751 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
7752 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
7753 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
7754 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
7755 txn->flags |= TX_CACHE_IGNORE;
7756 continue;
7757 }
7758
7759 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
7760 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7761 continue;
7762 }
7763 }
7764
7765 /* RFC7234#5.4:
7766 * When the Cache-Control header field is also present and
7767 * understood in a request, Pragma is ignored.
7768 * When the Cache-Control header field is not present in a
7769 * request, caches MUST consider the no-cache request
7770 * pragma-directive as having the same effect as if
7771 * "Cache-Control: no-cache" were present.
7772 */
7773 if (!cc_found && pragma_found)
7774 txn->flags |= TX_CACHE_IGNORE;
7775}
7776
7777/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007778 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007779 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007780void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007781{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007782 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007783 char *p1, *p2;
7784
7785 char *cur_ptr, *cur_end, *cur_next;
7786 int cur_idx;
7787
Willy Tarreau12b32f22017-12-21 16:08:09 +01007788 if (txn->status < 200) {
7789 /* do not try to cache interim responses! */
7790 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007791 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01007792 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007793
7794 /* Iterate through the headers.
7795 * we start with the start line.
7796 */
7797 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007798 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007799
7800 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7801 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007802 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007803
7804 cur_hdr = &txn->hdr_idx.v[cur_idx];
7805 cur_ptr = cur_next;
7806 cur_end = cur_ptr + cur_hdr->len;
7807 cur_next = cur_end + cur_hdr->cr + 1;
7808
7809 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007810 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007811 */
7812
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007813 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7814 if (val) {
7815 if ((cur_end - (cur_ptr + val) >= 8) &&
7816 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7817 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7818 return;
7819 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007820 }
7821
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007822 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7823 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007824 continue;
7825
7826 /* OK, right now we know we have a cache-control header at cur_ptr */
7827
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007828 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007829
7830 if (p1 >= cur_end) /* no more info */
7831 continue;
7832
7833 /* p1 is at the beginning of the value */
7834 p2 = p1;
7835
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007836 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007837 p2++;
7838
7839 /* we have a complete value between p1 and p2 */
7840 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007841 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7842 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7843 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7844 continue;
7845 }
7846
Willy Tarreaua15645d2007-03-18 16:22:39 +01007847 /* we have something of the form no-cache="set-cookie" */
7848 if ((cur_end - p1 >= 21) &&
7849 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7850 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007851 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007852 continue;
7853 }
7854
7855 /* OK, so we know that either p2 points to the end of string or to a comma */
7856 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007857 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007858 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007859 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007860 return;
7861 }
7862
7863 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007864 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007865 continue;
7866 }
7867 }
7868}
7869
Willy Tarreau58f10d72006-12-04 02:26:12 +01007870
Willy Tarreaub2513902006-12-17 14:52:38 +01007871/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007872 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007873 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007874 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007875 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007876 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007877 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007878 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007879 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007880int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007881{
7882 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007883 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007884 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007885
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007886 if (!uri_auth)
7887 return 0;
7888
Cyril Bonté70be45d2010-10-12 00:14:35 +02007889 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007890 return 0;
7891
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007892 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007893 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007894 return 0;
7895
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007896 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007897 return 0;
7898
Willy Tarreaub2513902006-12-17 14:52:38 +01007899 return 1;
7900}
7901
Willy Tarreau4076a152009-04-02 15:18:36 +02007902/*
7903 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007904 * By default it tries to report the error position as msg->err_pos. However if
7905 * this one is not set, it will then report msg->next, which is the last known
7906 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007907 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007908 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02007909void http_capture_bad_message(struct proxy *proxy, struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007910 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007911 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007912{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007913 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007914 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007915 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007916
Christopher Faulet2a944ee2017-11-07 10:42:54 +01007917 HA_SPIN_LOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreauf3764b72016-03-31 13:45:10 +02007918 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02007919 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007920 len1 = MIN(len1, es->len);
7921 len2 = es->len - len1; /* remaining data if buffer wraps */
7922
Willy Tarreauf3764b72016-03-31 13:45:10 +02007923 if (!es->buf)
7924 es->buf = malloc(global.tune.bufsize);
7925
7926 if (es->buf) {
7927 memcpy(es->buf, chn->buf->p, len1);
7928 if (len2)
7929 memcpy(es->buf + len1, chn->buf->data, len2);
7930 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007931
Willy Tarreau4076a152009-04-02 15:18:36 +02007932 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007933 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007934 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007935 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007936
Willy Tarreau4076a152009-04-02 15:18:36 +02007937 es->when = date; // user-visible date
7938 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007939 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007940 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007941 if (objt_conn(sess->origin))
7942 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007943 else
7944 memset(&es->src, 0, sizeof(es->src));
7945
Willy Tarreau078272e2010-12-12 12:46:33 +01007946 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007947 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007948 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007949 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007950 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007951 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007952 es->b_out = chn->buf->o;
7953 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007954 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007955 es->m_clen = msg->chunk_len;
7956 es->m_blen = msg->body_len;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01007957 HA_SPIN_UNLOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreau4076a152009-04-02 15:18:36 +02007958}
Willy Tarreaub2513902006-12-17 14:52:38 +01007959
Willy Tarreau294c4732011-12-16 21:35:50 +01007960/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7961 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7962 * performed over the whole headers. Otherwise it must contain a valid header
7963 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7964 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7965 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7966 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007967 * -1. The value fetch stops at commas, so this function is suited for use with
7968 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007969 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007970 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007971unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007972 struct hdr_idx *idx, int occ,
7973 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007974{
Willy Tarreau294c4732011-12-16 21:35:50 +01007975 struct hdr_ctx local_ctx;
7976 char *ptr_hist[MAX_HDR_HISTORY];
7977 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007978 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007979 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007980
Willy Tarreau294c4732011-12-16 21:35:50 +01007981 if (!ctx) {
7982 local_ctx.idx = 0;
7983 ctx = &local_ctx;
7984 }
7985
Willy Tarreaubce70882009-09-07 11:51:47 +02007986 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007987 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007988 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007989 occ--;
7990 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007991 *vptr = ctx->line + ctx->val;
7992 *vlen = ctx->vlen;
7993 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007994 }
7995 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007996 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007997 }
7998
7999 /* negative occurrence, we scan all the list then walk back */
8000 if (-occ > MAX_HDR_HISTORY)
8001 return 0;
8002
Willy Tarreau294c4732011-12-16 21:35:50 +01008003 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008004 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008005 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8006 len_hist[hist_ptr] = ctx->vlen;
8007 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008008 hist_ptr = 0;
8009 found++;
8010 }
8011 if (-occ > found)
8012 return 0;
8013 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008014 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8015 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8016 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008017 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008018 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008019 if (hist_ptr >= MAX_HDR_HISTORY)
8020 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008021 *vptr = ptr_hist[hist_ptr];
8022 *vlen = len_hist[hist_ptr];
8023 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008024}
8025
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008026/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8027 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8028 * performed over the whole headers. Otherwise it must contain a valid header
8029 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8030 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8031 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8032 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8033 * -1. This function differs from http_get_hdr() in that it only returns full
8034 * line header values and does not stop at commas.
8035 * The return value is 0 if nothing was found, or non-zero otherwise.
8036 */
8037unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8038 struct hdr_idx *idx, int occ,
8039 struct hdr_ctx *ctx, char **vptr, int *vlen)
8040{
8041 struct hdr_ctx local_ctx;
8042 char *ptr_hist[MAX_HDR_HISTORY];
8043 int len_hist[MAX_HDR_HISTORY];
8044 unsigned int hist_ptr;
8045 int found;
8046
8047 if (!ctx) {
8048 local_ctx.idx = 0;
8049 ctx = &local_ctx;
8050 }
8051
8052 if (occ >= 0) {
8053 /* search from the beginning */
8054 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8055 occ--;
8056 if (occ <= 0) {
8057 *vptr = ctx->line + ctx->val;
8058 *vlen = ctx->vlen;
8059 return 1;
8060 }
8061 }
8062 return 0;
8063 }
8064
8065 /* negative occurrence, we scan all the list then walk back */
8066 if (-occ > MAX_HDR_HISTORY)
8067 return 0;
8068
8069 found = hist_ptr = 0;
8070 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8071 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8072 len_hist[hist_ptr] = ctx->vlen;
8073 if (++hist_ptr >= MAX_HDR_HISTORY)
8074 hist_ptr = 0;
8075 found++;
8076 }
8077 if (-occ > found)
8078 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008079
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008080 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008081 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8082 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8083 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008084 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008085 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008086 if (hist_ptr >= MAX_HDR_HISTORY)
8087 hist_ptr -= MAX_HDR_HISTORY;
8088 *vptr = ptr_hist[hist_ptr];
8089 *vlen = len_hist[hist_ptr];
8090 return 1;
8091}
8092
Willy Tarreaubaaee002006-06-26 02:48:02 +02008093/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008094 * Print a debug line with a header. Always stop at the first CR or LF char,
8095 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8096 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008097 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008098void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008099{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008100 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008101 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008102
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008103 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008104 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02008105 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02008106 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 +02008107
8108 for (max = 0; start + max < end; max++)
8109 if (start[max] == '\r' || start[max] == '\n')
8110 break;
8111
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008112 UBOUND(max, trash.size - trash.len - 3);
8113 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8114 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008115 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008116}
8117
Willy Tarreaueee5b512015-04-03 23:46:31 +02008118
8119/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8120 * The hdr_idx is allocated as well. In case of allocation failure, everything
8121 * allocated is freed and NULL is returned. Otherwise the new transaction is
8122 * assigned to the stream and returned.
8123 */
8124struct http_txn *http_alloc_txn(struct stream *s)
8125{
8126 struct http_txn *txn = s->txn;
8127
8128 if (txn)
8129 return txn;
8130
Willy Tarreaubafbe012017-11-24 17:34:44 +01008131 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008132 if (!txn)
8133 return txn;
8134
8135 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01008136 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008137 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01008138 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008139 return NULL;
8140 }
8141
8142 s->txn = txn;
8143 return txn;
8144}
8145
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008146void http_txn_reset_req(struct http_txn *txn)
8147{
8148 txn->req.flags = 0;
8149 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8150 txn->req.next = 0;
8151 txn->req.chunk_len = 0LL;
8152 txn->req.body_len = 0LL;
8153 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8154}
8155
8156void http_txn_reset_res(struct http_txn *txn)
8157{
8158 txn->rsp.flags = 0;
8159 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8160 txn->rsp.next = 0;
8161 txn->rsp.chunk_len = 0LL;
8162 txn->rsp.body_len = 0LL;
8163 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8164}
8165
Willy Tarreau0937bc42009-12-22 15:03:09 +01008166/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008167 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008168 * the required fields are properly allocated and that we only need to (re)init
8169 * them. This should be used before processing any new request.
8170 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008171void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008172{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008173 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008174 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008175
8176 txn->flags = 0;
8177 txn->status = -1;
8178
Willy Tarreauf64d1412010-10-07 20:06:11 +02008179 txn->cookie_first_date = 0;
8180 txn->cookie_last_date = 0;
8181
Willy Tarreaueee5b512015-04-03 23:46:31 +02008182 txn->srv_cookie = NULL;
8183 txn->cli_cookie = NULL;
8184 txn->uri = NULL;
8185
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008186 http_txn_reset_req(txn);
8187 http_txn_reset_res(txn);
8188
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008189 txn->req.chn = &s->req;
8190 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008191
8192 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008193
8194 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8195 if (fe->options2 & PR_O2_REQBUG_OK)
8196 txn->req.err_pos = -1; /* let buggy requests pass */
8197
Willy Tarreau0937bc42009-12-22 15:03:09 +01008198 if (txn->hdr_idx.v)
8199 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008200
8201 vars_init(&s->vars_txn, SCOPE_TXN);
8202 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008203}
8204
8205/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008206void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008207{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008208 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008209 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008210
8211 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01008212 pool_free(pool_head_requri, txn->uri);
8213 pool_free(pool_head_capture, txn->cli_cookie);
8214 pool_free(pool_head_capture, txn->srv_cookie);
8215 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008216
William Lallemanda73203e2012-03-12 12:48:57 +01008217 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008218 txn->uri = NULL;
8219 txn->srv_cookie = NULL;
8220 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008221
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008222 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008223 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008224 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01008225 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008226 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008227 }
8228
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008229 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008230 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008231 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01008232 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008233 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008234 }
8235
Willy Tarreau6204cd92016-03-10 16:33:04 +01008236 vars_prune(&s->vars_txn, s->sess, s);
8237 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008238}
8239
8240/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008241void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008242{
8243 http_end_txn(s);
8244 http_init_txn(s);
8245
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008246 /* reinitialise the current rule list pointer to NULL. We are sure that
8247 * any rulelist match the NULL pointer.
8248 */
8249 s->current_rule_list = NULL;
8250
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008251 s->be = strm_fe(s);
8252 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008253 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008254 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008255 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008256 /* re-init store persistence */
8257 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008258 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008259
Willy Tarreau0937bc42009-12-22 15:03:09 +01008260 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008261
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008262 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008263
Willy Tarreau739cfba2010-01-25 23:11:14 +01008264 /* We must trim any excess data from the response buffer, because we
8265 * may have blocked an invalid response from a server that we don't
8266 * want to accidentely forward once we disable the analysers, nor do
8267 * we want those data to come along with next response. A typical
8268 * example of such data would be from a buggy server responding to
8269 * a HEAD with some data, or sending more than the advertised
8270 * content-length.
8271 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008272 if (unlikely(s->res.buf->i))
8273 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008274
Christopher Fauletc0c672a2017-03-28 11:51:33 +02008275 /* Now we can realign the response buffer */
8276 buffer_realign(s->res.buf);
8277
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008278 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008279 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008280
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008281 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008282 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008283
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008284 s->req.rex = TICK_ETERNITY;
8285 s->req.wex = TICK_ETERNITY;
8286 s->req.analyse_exp = TICK_ETERNITY;
8287 s->res.rex = TICK_ETERNITY;
8288 s->res.wex = TICK_ETERNITY;
8289 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01008290 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008291}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008292
Sasha Pachev218f0642014-06-16 12:05:59 -06008293void free_http_res_rules(struct list *r)
8294{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008295 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008296
8297 list_for_each_entry_safe(pr, tr, r, list) {
8298 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008299 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008300 free(pr);
8301 }
8302}
8303
8304void free_http_req_rules(struct list *r)
8305{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008306 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01008307
8308 list_for_each_entry_safe(pr, tr, r, list) {
8309 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008310 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008311 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008312
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008313 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008314 free(pr);
8315 }
8316}
8317
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008318/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008319struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01008320{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008321 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008322 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008323 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008324 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008325
Vincent Bernat02779b62016-04-03 13:48:43 +02008326 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01008327 if (!rule) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008328 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008329 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008330 }
8331
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008332 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008333 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01008334 cur_arg = 1;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008335 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block") || !strcmp(args[0], "tarpit")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04008336 int code;
8337 int hc;
8338
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008339 if (!strcmp(args[0], "tarpit")) {
8340 rule->action = ACT_HTTP_REQ_TARPIT;
8341 rule->deny_status = HTTP_ERR_500;
8342 }
8343 else {
8344 rule->action = ACT_ACTION_DENY;
8345 rule->deny_status = HTTP_ERR_403;
8346 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008347 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04008348 if (strcmp(args[cur_arg], "deny_status") == 0) {
8349 cur_arg++;
8350 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008351 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
8352 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008353 goto out_err;
8354 }
8355
8356 code = atol(args[cur_arg]);
8357 cur_arg++;
8358 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
8359 if (http_err_codes[hc] == code) {
8360 rule->deny_status = hc;
8361 break;
8362 }
8363 }
8364
8365 if (hc >= HTTP_ERR_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008366 ha_warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n",
8367 file, linenum, code, http_err_codes[rule->deny_status]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008368 }
8369 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008370 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008371 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008372 cur_arg = 1;
8373
8374 while(*args[cur_arg]) {
8375 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008376 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008377 cur_arg+=2;
8378 continue;
8379 } else
8380 break;
8381 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008382 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008383 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008384 cur_arg = 1;
8385
8386 if (!*args[cur_arg] ||
8387 (*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 +01008388 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8389 file, linenum, args[0]);
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008390 goto out_err;
8391 }
8392 rule->arg.nice = atoi(args[cur_arg]);
8393 if (rule->arg.nice < -1024)
8394 rule->arg.nice = -1024;
8395 else if (rule->arg.nice > 1024)
8396 rule->arg.nice = 1024;
8397 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008398 } else if (!strcmp(args[0], "set-tos")) {
8399#ifdef IP_TOS
8400 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008401 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008402 cur_arg = 1;
8403
8404 if (!*args[cur_arg] ||
8405 (*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 +01008406 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8407 file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008408 goto out_err;
8409 }
8410
8411 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8412 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008413 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8414 file, linenum, err, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008415 goto out_err;
8416 }
8417 cur_arg++;
8418#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008419 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 +02008420 goto out_err;
8421#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008422 } else if (!strcmp(args[0], "set-mark")) {
8423#ifdef SO_MARK
8424 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008425 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008426 cur_arg = 1;
8427
8428 if (!*args[cur_arg] ||
8429 (*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 +01008430 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8431 file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008432 goto out_err;
8433 }
8434
8435 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8436 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008437 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8438 file, linenum, err, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008439 goto out_err;
8440 }
8441 cur_arg++;
8442 global.last_checks |= LSTCHK_NETADM;
8443#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008444 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 +02008445 goto out_err;
8446#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008447 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008448 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008449 cur_arg = 1;
8450
8451 if (!*args[cur_arg] ||
8452 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8453 bad_log_level:
Christopher Faulet767a84b2017-11-24 16:50:31 +01008454 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8455 file, linenum, args[0]);
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008456 goto out_err;
8457 }
8458 if (strcmp(args[cur_arg], "silent") == 0)
8459 rule->arg.loglevel = -1;
8460 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8461 goto bad_log_level;
8462 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008463 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008464 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008465 cur_arg = 1;
8466
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008467 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8468 (*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 +01008469 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8470 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008471 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008472 }
8473
8474 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8475 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8476 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008477
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008478 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008479 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008480 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 +01008481 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008482 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8483 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008484 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008485 goto out_err;
8486 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008487 free(proxy->conf.lfs_file);
8488 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8489 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008490 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02008491 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008492 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008493 cur_arg = 1;
8494
8495 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02008496 (*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 +01008497 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
8498 file, linenum, args[0]);
Sasha Pachev218f0642014-06-16 12:05:59 -06008499 goto out_err;
8500 }
8501
8502 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8503 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8504 LIST_INIT(&rule->arg.hdr_add.fmt);
8505
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008506 error = NULL;
8507 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008508 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8509 args[cur_arg + 1], error);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008510 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008511 goto out_err;
8512 }
8513
8514 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008515 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008516 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 +01008517 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008518 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8519 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008520 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008521 goto out_err;
8522 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008523
8524 free(proxy->conf.lfs_file);
8525 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8526 proxy->conf.lfs_line = proxy->conf.args.line;
8527 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008528 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008529 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008530 cur_arg = 1;
8531
8532 if (!*args[cur_arg] ||
8533 (*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 +01008534 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8535 file, linenum, args[0]);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008536 goto out_err;
8537 }
8538
8539 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8540 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8541
8542 proxy->conf.args.ctx = ARGC_HRQ;
8543 free(proxy->conf.lfs_file);
8544 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8545 proxy->conf.lfs_line = proxy->conf.args.line;
8546 cur_arg += 1;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008547 } else if (strncmp(args[0], "track-sc", 8) == 0) {
Willy Tarreau09448f72014-06-25 18:12:15 +02008548 struct sample_expr *expr;
8549 unsigned int where;
8550 char *err = NULL;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008551 unsigned int tsc_num;
8552 const char *tsc_num_str;
Willy Tarreau09448f72014-06-25 18:12:15 +02008553
8554 cur_arg = 1;
8555 proxy->conf.args.ctx = ARGC_TRK;
8556
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008557 tsc_num_str = &args[0][8];
8558 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) {
8559 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8560 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
8561 free(err);
8562 goto out_err;
8563 }
8564
Willy Tarreau09448f72014-06-25 18:12:15 +02008565 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
8566 if (!expr) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008567 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8568 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
Willy Tarreau09448f72014-06-25 18:12:15 +02008569 free(err);
8570 goto out_err;
8571 }
8572
8573 where = 0;
8574 if (proxy->cap & PR_CAP_FE)
8575 where |= SMP_VAL_FE_HRQ_HDR;
8576 if (proxy->cap & PR_CAP_BE)
8577 where |= SMP_VAL_BE_HRQ_HDR;
8578
8579 if (!(expr->fetch->val & where)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008580 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
8581 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
8582 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
8583 args[cur_arg-1], sample_src_names(expr->fetch->use));
Willy Tarreau09448f72014-06-25 18:12:15 +02008584 free(expr);
8585 goto out_err;
8586 }
8587
8588 if (strcmp(args[cur_arg], "table") == 0) {
8589 cur_arg++;
8590 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008591 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
8592 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008593 free(expr);
8594 goto out_err;
8595 }
8596 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008597 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008598 cur_arg++;
8599 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008600 rule->arg.trk_ctr.expr = expr;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008601 rule->action = ACT_ACTION_TRK_SC0 + tsc_num;
Christopher Faulet78880fb2017-09-18 14:43:55 +02008602 rule->check_ptr = check_trk_action;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008603 } else if (strcmp(args[0], "redirect") == 0) {
8604 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008605 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008606
Willy Tarreaube4653b2015-05-28 15:26:58 +02008607 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008608 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8609 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008610 goto out_err;
8611 }
8612
8613 /* this redirect rule might already contain a parsed condition which
8614 * we'll pass to the http-request rule.
8615 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008616 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008617 rule->arg.redir = redir;
8618 rule->cond = redir->cond;
8619 redir->cond = NULL;
8620 cur_arg = 2;
8621 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008622 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8623 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008624 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008625 /*
8626 * '+ 8' for 'add-acl('
8627 * '- 9' for 'add-acl(' + trailing ')'
8628 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008629 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008630
8631 cur_arg = 1;
8632
8633 if (!*args[cur_arg] ||
8634 (*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 +01008635 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8636 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008637 goto out_err;
8638 }
8639
8640 LIST_INIT(&rule->arg.map.key);
8641 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008642 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008643 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008644 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008645 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8646 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008647 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008648 goto out_err;
8649 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008650 free(proxy->conf.lfs_file);
8651 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8652 proxy->conf.lfs_line = proxy->conf.args.line;
8653 cur_arg += 1;
8654 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8655 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008656 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008657 /*
8658 * '+ 8' for 'del-acl('
8659 * '- 9' for 'del-acl(' + trailing ')'
8660 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008661 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008662
8663 cur_arg = 1;
8664
8665 if (!*args[cur_arg] ||
8666 (*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 +01008667 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8668 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008669 goto out_err;
8670 }
8671
8672 LIST_INIT(&rule->arg.map.key);
8673 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008674 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008675 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008676 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008677 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8678 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008679 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008680 goto out_err;
8681 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008682 free(proxy->conf.lfs_file);
8683 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8684 proxy->conf.lfs_line = proxy->conf.args.line;
8685 cur_arg += 1;
8686 } else if (strncmp(args[0], "del-map", 7) == 0) {
8687 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008688 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008689 /*
8690 * '+ 8' for 'del-map('
8691 * '- 9' for 'del-map(' + trailing ')'
8692 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008693 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008694
8695 cur_arg = 1;
8696
8697 if (!*args[cur_arg] ||
8698 (*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 +01008699 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8700 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008701 goto out_err;
8702 }
8703
8704 LIST_INIT(&rule->arg.map.key);
8705 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008706 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008707 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008708 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008709 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8710 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008711 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008712 goto out_err;
8713 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008714 free(proxy->conf.lfs_file);
8715 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8716 proxy->conf.lfs_line = proxy->conf.args.line;
8717 cur_arg += 1;
8718 } else if (strncmp(args[0], "set-map", 7) == 0) {
8719 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008720 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008721 /*
8722 * '+ 8' for 'set-map('
8723 * '- 9' for 'set-map(' + trailing ')'
8724 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008725 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008726
8727 cur_arg = 1;
8728
8729 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8730 (*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 +01008731 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8732 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008733 goto out_err;
8734 }
8735
8736 LIST_INIT(&rule->arg.map.key);
8737 LIST_INIT(&rule->arg.map.value);
8738 proxy->conf.args.ctx = ARGC_HRQ;
8739
8740 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008741 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008742 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008743 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008744 ha_alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
8745 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008746 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008747 goto out_err;
8748 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008749
8750 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008751 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008752 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 +01008753 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008754 ha_alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
8755 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008756 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008757 goto out_err;
8758 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008759 free(proxy->conf.lfs_file);
8760 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8761 proxy->conf.lfs_line = proxy->conf.args.line;
8762
8763 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02008764 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
8765 char *errmsg = NULL;
8766 cur_arg = 1;
8767 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02008768 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02008769 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02008770 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008771 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8772 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
William Lallemand73025dd2014-04-24 14:38:37 +02008773 free(errmsg);
8774 goto out_err;
8775 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008776 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008777 action_build_list(&http_req_keywords.list, &trash);
Christopher Faulet767a84b2017-11-24 16:50:31 +01008778 ha_alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
8779 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
8780 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
8781 "%s%s, but got '%s'%s.\n",
8782 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008783 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008784 }
8785
8786 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8787 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008788 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008789
Christopher Faulet1b421ea2017-09-22 14:38:56 +02008790 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008791 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8792 file, linenum, args[0], errmsg);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008793 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008794 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008795 }
8796 rule->cond = cond;
8797 }
8798 else if (*args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008799 ha_alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8800 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8801 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008802 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008803 }
8804
8805 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008806 out_err:
8807 free(rule);
8808 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008809}
8810
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008811/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008812struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008813{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008814 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008815 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008816 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008817 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008818
8819 rule = calloc(1, sizeof(*rule));
8820 if (!rule) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008821 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008822 goto out_err;
8823 }
8824
8825 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008826 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008827 cur_arg = 1;
8828 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008829 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008830 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008831 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008832 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008833 cur_arg = 1;
8834
8835 if (!*args[cur_arg] ||
8836 (*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 +01008837 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8838 file, linenum, args[0]);
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008839 goto out_err;
8840 }
8841 rule->arg.nice = atoi(args[cur_arg]);
8842 if (rule->arg.nice < -1024)
8843 rule->arg.nice = -1024;
8844 else if (rule->arg.nice > 1024)
8845 rule->arg.nice = 1024;
8846 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008847 } else if (!strcmp(args[0], "set-tos")) {
8848#ifdef IP_TOS
8849 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008850 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008851 cur_arg = 1;
8852
8853 if (!*args[cur_arg] ||
8854 (*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 +01008855 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8856 file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008857 goto out_err;
8858 }
8859
8860 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8861 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008862 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8863 file, linenum, err, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008864 goto out_err;
8865 }
8866 cur_arg++;
8867#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008868 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 +02008869 goto out_err;
8870#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008871 } else if (!strcmp(args[0], "set-mark")) {
8872#ifdef SO_MARK
8873 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008874 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008875 cur_arg = 1;
8876
8877 if (!*args[cur_arg] ||
8878 (*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 +01008879 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8880 file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008881 goto out_err;
8882 }
8883
8884 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8885 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008886 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8887 file, linenum, err, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008888 goto out_err;
8889 }
8890 cur_arg++;
8891 global.last_checks |= LSTCHK_NETADM;
8892#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008893 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 +02008894 goto out_err;
8895#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008896 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008897 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008898 cur_arg = 1;
8899
8900 if (!*args[cur_arg] ||
8901 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8902 bad_log_level:
Christopher Faulet767a84b2017-11-24 16:50:31 +01008903 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8904 file, linenum, args[0]);
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008905 goto out_err;
8906 }
8907 if (strcmp(args[cur_arg], "silent") == 0)
8908 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08008909 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008910 goto bad_log_level;
8911 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008912 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008913 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008914 cur_arg = 1;
8915
8916 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8917 (*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 +01008918 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8919 file, linenum, args[0]);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008920 goto out_err;
8921 }
8922
8923 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8924 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8925 LIST_INIT(&rule->arg.hdr_add.fmt);
8926
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008927 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008928 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008929 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 +01008930 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008931 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8932 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008933 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008934 goto out_err;
8935 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008936 free(proxy->conf.lfs_file);
8937 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8938 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008939 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06008940 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008941 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008942 cur_arg = 1;
8943
8944 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02008945 (*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 +01008946 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
8947 file, linenum, args[0]);
Sasha Pachev218f0642014-06-16 12:05:59 -06008948 goto out_err;
8949 }
8950
8951 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8952 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8953 LIST_INIT(&rule->arg.hdr_add.fmt);
8954
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008955 error = NULL;
8956 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008957 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8958 args[cur_arg + 1], error);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008959 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008960 goto out_err;
8961 }
8962
8963 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008964 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008965 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 +01008966 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008967 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8968 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008969 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008970 goto out_err;
8971 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008972
8973 free(proxy->conf.lfs_file);
8974 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8975 proxy->conf.lfs_line = proxy->conf.args.line;
8976 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008977 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008978 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008979 cur_arg = 1;
8980
8981 if (!*args[cur_arg] ||
8982 (*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 +01008983 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8984 file, linenum, args[0]);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008985 goto out_err;
8986 }
8987
8988 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8989 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8990
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008991 proxy->conf.args.ctx = ARGC_HRS;
8992 free(proxy->conf.lfs_file);
8993 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8994 proxy->conf.lfs_line = proxy->conf.args.line;
8995 cur_arg += 1;
8996 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8997 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008998 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008999 /*
9000 * '+ 8' for 'add-acl('
9001 * '- 9' for 'add-acl(' + trailing ')'
9002 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009003 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009004
9005 cur_arg = 1;
9006
9007 if (!*args[cur_arg] ||
9008 (*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 +01009009 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9010 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009011 goto out_err;
9012 }
9013
9014 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009015 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009016 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009017 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009018 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009019 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9020 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009021 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009022 goto out_err;
9023 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009024 free(proxy->conf.lfs_file);
9025 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9026 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009027
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009028 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009029 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9030 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009031 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009032 /*
9033 * '+ 8' for 'del-acl('
9034 * '- 9' for 'del-acl(' + trailing ')'
9035 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009036 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009037
9038 cur_arg = 1;
9039
9040 if (!*args[cur_arg] ||
9041 (*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 +01009042 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9043 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009044 goto out_err;
9045 }
9046
9047 LIST_INIT(&rule->arg.map.key);
9048 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009049 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009050 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009051 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009052 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9053 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009054 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009055 goto out_err;
9056 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009057 free(proxy->conf.lfs_file);
9058 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9059 proxy->conf.lfs_line = proxy->conf.args.line;
9060 cur_arg += 1;
9061 } else if (strncmp(args[0], "del-map", 7) == 0) {
9062 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009063 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009064 /*
9065 * '+ 8' for 'del-map('
9066 * '- 9' for 'del-map(' + trailing ')'
9067 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009068 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009069
9070 cur_arg = 1;
9071
9072 if (!*args[cur_arg] ||
9073 (*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 +01009074 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9075 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009076 goto out_err;
9077 }
9078
9079 LIST_INIT(&rule->arg.map.key);
9080 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009081 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009082 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009083 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009084 ha_alert("parsing [%s:%d]: 'http-response %s' %s.\n",
9085 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009086 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009087 goto out_err;
9088 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009089 free(proxy->conf.lfs_file);
9090 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9091 proxy->conf.lfs_line = proxy->conf.args.line;
9092 cur_arg += 1;
9093 } else if (strncmp(args[0], "set-map", 7) == 0) {
9094 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009095 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009096 /*
9097 * '+ 8' for 'set-map('
9098 * '- 9' for 'set-map(' + trailing ')'
9099 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009100 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009101
9102 cur_arg = 1;
9103
9104 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9105 (*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 +01009106 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9107 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009108 goto out_err;
9109 }
9110
9111 LIST_INIT(&rule->arg.map.key);
9112 LIST_INIT(&rule->arg.map.value);
9113
9114 proxy->conf.args.ctx = ARGC_HRS;
9115
9116 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009117 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009118 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009119 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009120 ha_alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
9121 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009122 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009123 goto out_err;
9124 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009125
9126 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009127 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009128 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 +01009129 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009130 ha_alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
9131 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009132 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009133 goto out_err;
9134 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009135
9136 free(proxy->conf.lfs_file);
9137 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9138 proxy->conf.lfs_line = proxy->conf.args.line;
9139
9140 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009141 } else if (strcmp(args[0], "redirect") == 0) {
9142 struct redirect_rule *redir;
9143 char *errmsg = NULL;
9144
9145 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009146 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9147 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
Willy Tarreau51d861a2015-05-22 17:30:48 +02009148 goto out_err;
9149 }
9150
9151 /* this redirect rule might already contain a parsed condition which
9152 * we'll pass to the http-request rule.
9153 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009154 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009155 rule->arg.redir = redir;
9156 rule->cond = redir->cond;
9157 redir->cond = NULL;
9158 cur_arg = 2;
9159 return rule;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009160 } else if (strncmp(args[0], "track-sc", 8) == 0) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009161 struct sample_expr *expr;
9162 unsigned int where;
9163 char *err = NULL;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009164 unsigned int tsc_num;
9165 const char *tsc_num_str;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009166
9167 cur_arg = 1;
9168 proxy->conf.args.ctx = ARGC_TRK;
9169
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009170 tsc_num_str = &args[0][8];
9171 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) {
9172 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9173 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9174 free(err);
9175 goto out_err;
9176 }
9177
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009178 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9179 if (!expr) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009180 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9181 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009182 free(err);
9183 goto out_err;
9184 }
9185
9186 where = 0;
9187 if (proxy->cap & PR_CAP_FE)
9188 where |= SMP_VAL_FE_HRS_HDR;
9189 if (proxy->cap & PR_CAP_BE)
9190 where |= SMP_VAL_BE_HRS_HDR;
9191
9192 if (!(expr->fetch->val & where)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009193 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
9194 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9195 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9196 args[cur_arg-1], sample_src_names(expr->fetch->use));
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009197 free(expr);
9198 goto out_err;
9199 }
9200
9201 if (strcmp(args[cur_arg], "table") == 0) {
9202 cur_arg++;
9203 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009204 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9205 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009206 free(expr);
9207 goto out_err;
9208 }
9209 /* we copy the table name for now, it will be resolved later */
9210 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9211 cur_arg++;
9212 }
9213 rule->arg.trk_ctr.expr = expr;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009214 rule->action = ACT_ACTION_TRK_SC0 + tsc_num;
Christopher Faulet78880fb2017-09-18 14:43:55 +02009215 rule->check_ptr = check_trk_action;
William Lallemand73025dd2014-04-24 14:38:37 +02009216 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9217 char *errmsg = NULL;
9218 cur_arg = 1;
9219 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009220 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009221 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009222 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009223 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9224 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
William Lallemand73025dd2014-04-24 14:38:37 +02009225 free(errmsg);
9226 goto out_err;
9227 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009228 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009229 action_build_list(&http_res_keywords.list, &trash);
Christopher Faulet767a84b2017-11-24 16:50:31 +01009230 ha_alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9231 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
9232 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
9233 "%s%s, but got '%s'%s.\n",
9234 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009235 goto out_err;
9236 }
9237
9238 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9239 struct acl_cond *cond;
9240 char *errmsg = NULL;
9241
Christopher Faulet1b421ea2017-09-22 14:38:56 +02009242 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009243 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9244 file, linenum, args[0], errmsg);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009245 free(errmsg);
9246 goto out_err;
9247 }
9248 rule->cond = cond;
9249 }
9250 else if (*args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009251 ha_alert("parsing [%s:%d]: 'http-response %s' expects"
9252 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9253 file, linenum, args[0], args[cur_arg]);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009254 goto out_err;
9255 }
9256
9257 return rule;
9258 out_err:
9259 free(rule);
9260 return NULL;
9261}
9262
Willy Tarreau4baae242012-12-27 12:00:31 +01009263/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009264 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009265 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9266 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009267 */
9268struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009269 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009270{
9271 struct redirect_rule *rule;
9272 int cur_arg;
9273 int type = REDIRECT_TYPE_NONE;
9274 int code = 302;
9275 const char *destination = NULL;
9276 const char *cookie = NULL;
9277 int cookie_set = 0;
9278 unsigned int flags = REDIRECT_FLAG_NONE;
9279 struct acl_cond *cond = NULL;
9280
9281 cur_arg = 0;
9282 while (*(args[cur_arg])) {
9283 if (strcmp(args[cur_arg], "location") == 0) {
9284 if (!*args[cur_arg + 1])
9285 goto missing_arg;
9286
9287 type = REDIRECT_TYPE_LOCATION;
9288 cur_arg++;
9289 destination = args[cur_arg];
9290 }
9291 else if (strcmp(args[cur_arg], "prefix") == 0) {
9292 if (!*args[cur_arg + 1])
9293 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009294 type = REDIRECT_TYPE_PREFIX;
9295 cur_arg++;
9296 destination = args[cur_arg];
9297 }
9298 else if (strcmp(args[cur_arg], "scheme") == 0) {
9299 if (!*args[cur_arg + 1])
9300 goto missing_arg;
9301
9302 type = REDIRECT_TYPE_SCHEME;
9303 cur_arg++;
9304 destination = args[cur_arg];
9305 }
9306 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9307 if (!*args[cur_arg + 1])
9308 goto missing_arg;
9309
9310 cur_arg++;
9311 cookie = args[cur_arg];
9312 cookie_set = 1;
9313 }
9314 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9315 if (!*args[cur_arg + 1])
9316 goto missing_arg;
9317
9318 cur_arg++;
9319 cookie = args[cur_arg];
9320 cookie_set = 0;
9321 }
9322 else if (strcmp(args[cur_arg], "code") == 0) {
9323 if (!*args[cur_arg + 1])
9324 goto missing_arg;
9325
9326 cur_arg++;
9327 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009328 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009329 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009330 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009331 args[cur_arg - 1], args[cur_arg]);
9332 return NULL;
9333 }
9334 }
9335 else if (!strcmp(args[cur_arg],"drop-query")) {
9336 flags |= REDIRECT_FLAG_DROP_QS;
9337 }
9338 else if (!strcmp(args[cur_arg],"append-slash")) {
9339 flags |= REDIRECT_FLAG_APPEND_SLASH;
9340 }
9341 else if (strcmp(args[cur_arg], "if") == 0 ||
9342 strcmp(args[cur_arg], "unless") == 0) {
Olivier Houchardfbc74e82017-11-24 16:54:05 +01009343 cond = build_acl_cond(file, linenum, &curproxy->acl, curproxy, (const char **)args + cur_arg, errmsg);
Willy Tarreau4baae242012-12-27 12:00:31 +01009344 if (!cond) {
9345 memprintf(errmsg, "error in condition: %s", *errmsg);
9346 return NULL;
9347 }
9348 break;
9349 }
9350 else {
9351 memprintf(errmsg,
9352 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9353 args[cur_arg]);
9354 return NULL;
9355 }
9356 cur_arg++;
9357 }
9358
9359 if (type == REDIRECT_TYPE_NONE) {
9360 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9361 return NULL;
9362 }
9363
Willy Tarreaube4653b2015-05-28 15:26:58 +02009364 if (dir && type != REDIRECT_TYPE_LOCATION) {
9365 memprintf(errmsg, "response only supports redirect type 'location'");
9366 return NULL;
9367 }
9368
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02009369 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +01009370 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009371 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009372
9373 if (!use_fmt) {
9374 /* old-style static redirect rule */
9375 rule->rdr_str = strdup(destination);
9376 rule->rdr_len = strlen(destination);
9377 }
9378 else {
9379 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009380
9381 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9382 * if prefix == "/", we don't want to add anything, otherwise it
9383 * makes it hard for the user to configure a self-redirection.
9384 */
Godbachd9722032014-12-18 15:44:58 +08009385 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009386 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009387 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
9388 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 +01009389 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9390 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009391 return NULL;
9392 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009393 free(curproxy->conf.lfs_file);
9394 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9395 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009396 }
9397 }
9398
Willy Tarreau4baae242012-12-27 12:00:31 +01009399 if (cookie) {
9400 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9401 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9402 */
9403 rule->cookie_len = strlen(cookie);
9404 if (cookie_set) {
9405 rule->cookie_str = malloc(rule->cookie_len + 10);
9406 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9407 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9408 rule->cookie_len += 9;
9409 } else {
9410 rule->cookie_str = malloc(rule->cookie_len + 21);
9411 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9412 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9413 rule->cookie_len += 20;
9414 }
9415 }
9416 rule->type = type;
9417 rule->code = code;
9418 rule->flags = flags;
9419 LIST_INIT(&rule->list);
9420 return rule;
9421
9422 missing_arg:
9423 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9424 return NULL;
9425}
9426
Willy Tarreau8797c062007-05-07 00:55:35 +02009427/************************************************************************/
9428/* The code below is dedicated to ACL parsing and matching */
9429/************************************************************************/
9430
9431
Willy Tarreau14174bc2012-04-16 14:34:04 +02009432/* This function ensures that the prerequisites for an L7 fetch are ready,
9433 * which means that a request or response is ready. If some data is missing,
9434 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009435 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9436 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009437 *
9438 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009439 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9440 * decide whether or not an HTTP message is present ;
9441 * 0 if the requested data cannot be fetched or if it is certain that
9442 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009443 * 1 if an HTTP message is ready
9444 */
James Rosewell91a41cb2015-09-18 17:11:16 +01009445int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009446 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009447{
Willy Tarreau192252e2015-04-04 01:47:55 +02009448 struct http_txn *txn;
9449 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009450
Willy Tarreaube508f12016-03-10 11:47:01 +01009451 /* Note: it is possible that <s> is NULL when called before stream
9452 * initialization (eg: tcp-request connection), so this function is the
9453 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009454 */
Willy Tarreau192252e2015-04-04 01:47:55 +02009455 if (!s)
9456 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01009457
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +02009458 if (!s->txn) {
9459 if (unlikely(!http_alloc_txn(s)))
9460 return 0; /* not enough memory */
9461 http_init_txn(s);
9462 }
Willy Tarreau192252e2015-04-04 01:47:55 +02009463 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +02009464 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009465
9466 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009467 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009468
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009469 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009470 /* If the buffer does not leave enough free space at the end,
9471 * we must first realign it.
9472 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009473 if (s->req.buf->p > s->req.buf->data &&
9474 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
9475 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009476
Willy Tarreau14174bc2012-04-16 14:34:04 +02009477 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009478 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009479 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009480
9481 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009482 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009483 http_msg_analyzer(msg, &txn->hdr_idx);
9484
9485 /* Still no valid request ? */
9486 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009487 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009488 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009489 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009490 }
9491 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009492 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009493 return 0;
9494 }
9495
9496 /* OK we just got a valid HTTP request. We have some minor
9497 * preparation to perform so that further checks can rely
9498 * on HTTP tests.
9499 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009500
9501 /* If the request was parsed but was too large, we must absolutely
9502 * return an error so that it is not processed. At the moment this
9503 * cannot happen, but if the parsers are to change in the future,
9504 * we want this check to be maintained.
9505 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009506 if (unlikely(s->req.buf->i + s->req.buf->p >
9507 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01009508 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009509 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009510 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009511 return 1;
9512 }
9513
Willy Tarreau9b28e032012-10-12 23:49:43 +02009514 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009515 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02009516 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009517
Willy Tarreau506d0502013-07-06 13:29:24 +02009518 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9519 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009520 }
9521
Willy Tarreau506d0502013-07-06 13:29:24 +02009522 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009523 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009524 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009525
9526 /* otherwise everything's ready for the request */
9527 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009528 else {
9529 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009530 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9531 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009532 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009533 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009534 }
9535
9536 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009537 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009538 return 1;
9539}
Willy Tarreau8797c062007-05-07 00:55:35 +02009540
Willy Tarreau8797c062007-05-07 00:55:35 +02009541/* 1. Check on METHOD
9542 * We use the pre-parsed method if it is known, and store its number as an
9543 * integer. If it is unknown, we use the pointer and the length.
9544 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009545static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009546{
9547 int len, meth;
9548
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009549 len = strlen(text);
9550 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009551
9552 pattern->val.i = meth;
9553 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +02009554 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009555 pattern->len = len;
9556 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009557 else {
9558 pattern->ptr.str = NULL;
9559 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009560 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009561 return 1;
9562}
9563
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009564/* This function fetches the method of current HTTP request and stores
9565 * it in the global pattern struct as a chunk. There are two possibilities :
9566 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9567 * in <len> and <ptr> is NULL ;
9568 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9569 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009570 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009571 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009572static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009573smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009574{
9575 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +01009576 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009577
Willy Tarreau24e32d82012-04-23 23:55:44 +02009578 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009579
Willy Tarreaube508f12016-03-10 11:47:01 +01009580 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009581 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009582 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009583 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009584 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009585 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9586 /* ensure the indexes are not affected */
9587 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009588 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009589 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
9590 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009591 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009592 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009593 return 1;
9594}
9595
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009596/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009597static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009598{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009599 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009600 struct pattern_list *lst;
9601 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009602
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009603 list_for_each_entry(lst, &expr->patterns, list) {
9604 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009605
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009606 /* well-known method */
9607 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009608 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009609 return pattern;
9610 else
9611 continue;
9612 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009613
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009614 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009615 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009616 continue;
9617
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009618 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009619 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
9620 (!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 +01009621 return pattern;
9622 }
9623 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009624}
9625
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009626static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009627smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009628{
Willy Tarreaube508f12016-03-10 11:47:01 +01009629 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009630 char *ptr;
9631 int len;
9632
Willy Tarreauc0239e02012-04-16 14:42:55 +02009633 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009634
Willy Tarreaube508f12016-03-10 11:47:01 +01009635 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009636 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009637 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009638
9639 while ((len-- > 0) && (*ptr++ != '/'));
9640 if (len <= 0)
9641 return 0;
9642
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009643 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009644 smp->data.u.str.str = ptr;
9645 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009646
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009647 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009648 return 1;
9649}
9650
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009651static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009652smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009653{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009654 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009655 char *ptr;
9656 int len;
9657
Willy Tarreauc0239e02012-04-16 14:42:55 +02009658 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009659
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009660 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009661 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9662 return 0;
9663
Willy Tarreau8797c062007-05-07 00:55:35 +02009664 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009665 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009666
9667 while ((len-- > 0) && (*ptr++ != '/'));
9668 if (len <= 0)
9669 return 0;
9670
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009671 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009672 smp->data.u.str.str = ptr;
9673 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009674
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009675 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009676 return 1;
9677}
9678
9679/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009680static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009681smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009682{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009683 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009684 char *ptr;
9685 int len;
9686
Willy Tarreauc0239e02012-04-16 14:42:55 +02009687 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009688
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009689 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009690 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9691 return 0;
9692
Willy Tarreau8797c062007-05-07 00:55:35 +02009693 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009694 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009695
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009696 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009697 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009698 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009699 return 1;
9700}
9701
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009702static int
9703smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
9704{
9705 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
9706 return 0;
9707
9708 if (!smp->strm->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01009709 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009710 return 0;
9711 smp->strm->unique_id[0] = '\0';
9712 }
9713 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
9714 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
9715
9716 smp->data.type = SMP_T_STR;
9717 smp->data.u.str.str = smp->strm->unique_id;
9718 smp->flags = SMP_F_CONST;
9719 return 1;
9720}
9721
Thierry FOURNIERd7d88812017-04-19 15:15:14 +02009722/* Returns a string block containing all headers including the
9723 * empty line wich separes headers from the body. This is useful
9724 * form some headers analysis.
9725 */
9726static int
9727smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
9728{
9729 struct http_msg *msg;
9730 struct hdr_idx *idx;
9731 struct http_txn *txn;
9732
9733 CHECK_HTTP_MESSAGE_FIRST();
9734
9735 txn = smp->strm->txn;
9736 idx = &txn->hdr_idx;
9737 msg = &txn->req;
9738
9739 smp->data.type = SMP_T_STR;
9740 smp->data.u.str.str = msg->chn->buf->p + hdr_idx_first_pos(idx);
9741 smp->data.u.str.len = msg->eoh - hdr_idx_first_pos(idx) + 1 +
9742 (msg->chn->buf->p[msg->eoh] == '\r');
9743
9744 return 1;
9745}
9746
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009747/* Returns the header request in a length/value encoded format.
9748 * This is useful for exchanges with the SPOE.
9749 *
9750 * A "length value" is a multibyte code encoding numbers. It uses the
9751 * SPOE format. The encoding is the following:
9752 *
9753 * Each couple "header name" / "header value" is composed
9754 * like this:
9755 * "length value" "header name bytes"
9756 * "length value" "header value bytes"
9757 * When the last header is reached, the header name and the header
9758 * value are empty. Their length are 0
9759 */
9760static int
9761smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
9762{
9763 struct http_msg *msg;
9764 struct chunk *temp;
9765 struct hdr_idx *idx;
9766 const char *cur_ptr, *cur_next, *p;
9767 int old_idx, cur_idx;
9768 struct hdr_idx_elem *cur_hdr;
9769 const char *hn, *hv;
9770 int hnl, hvl;
9771 int ret;
9772 struct http_txn *txn;
9773 char *buf;
9774 char *end;
9775
9776 CHECK_HTTP_MESSAGE_FIRST();
9777
9778 temp = get_trash_chunk();
9779 buf = temp->str;
9780 end = temp->str + temp->size;
9781
9782 txn = smp->strm->txn;
9783 idx = &txn->hdr_idx;
9784 msg = &txn->req;
9785
9786 /* Build array of headers. */
9787 old_idx = 0;
9788 cur_next = msg->chn->buf->p + hdr_idx_first_pos(idx);
9789 while (1) {
9790 cur_idx = idx->v[old_idx].next;
9791 if (!cur_idx)
9792 break;
9793 old_idx = cur_idx;
9794
9795 cur_hdr = &idx->v[cur_idx];
9796 cur_ptr = cur_next;
9797 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
9798
9799 /* Now we have one full header at cur_ptr of len cur_hdr->len,
9800 * and the next header starts at cur_next. We'll check
9801 * this header in the list as well as against the default
9802 * rule.
9803 */
9804
9805 /* look for ': *'. */
9806 hn = cur_ptr;
9807 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
9808 if (p >= cur_ptr+cur_hdr->len)
9809 continue;
9810 hnl = p - hn;
9811 p++;
9812 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
9813 p++;
9814 if (p >= cur_ptr + cur_hdr->len)
9815 continue;
9816 hv = p;
9817 hvl = cur_ptr + cur_hdr->len-p;
9818
9819 /* encode the header name. */
9820 ret = encode_varint(hnl, &buf, end);
9821 if (ret == -1)
9822 return 0;
9823 if (buf + hnl > end)
9824 return 0;
9825 memcpy(buf, hn, hnl);
9826 buf += hnl;
9827
9828 /* encode and copy the value. */
9829 ret = encode_varint(hvl, &buf, end);
9830 if (ret == -1)
9831 return 0;
9832 if (buf + hvl > end)
9833 return 0;
9834 memcpy(buf, hv, hvl);
9835 buf += hvl;
9836 }
9837
9838 /* encode the end of the header list with empty
9839 * header name and header value.
9840 */
9841 ret = encode_varint(0, &buf, end);
9842 if (ret == -1)
9843 return 0;
9844 ret = encode_varint(0, &buf, end);
9845 if (ret == -1)
9846 return 0;
9847
9848 /* Initialise sample data which will be filled. */
9849 smp->data.type = SMP_T_BIN;
9850 smp->data.u.str.str = temp->str;
9851 smp->data.u.str.len = buf - temp->str;
9852 smp->data.u.str.size = temp->size;
9853
9854 return 1;
9855}
9856
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009857/* returns the longest available part of the body. This requires that the body
9858 * has been waited for using http-buffer-request.
9859 */
9860static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009861smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009862{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009863 struct http_msg *msg;
9864 unsigned long len;
9865 unsigned long block1;
9866 char *body;
9867 struct chunk *temp;
9868
9869 CHECK_HTTP_MESSAGE_FIRST();
9870
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009871 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009872 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009873 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009874 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009875
9876 len = http_body_bytes(msg);
9877 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
9878
9879 block1 = len;
9880 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
9881 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
9882
9883 if (block1 == len) {
9884 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009885 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009886 smp->data.u.str.str = body;
9887 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009888 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
9889 }
9890 else {
9891 /* buffer is wrapped, we need to defragment it */
9892 temp = get_trash_chunk();
9893 memcpy(temp->str, body, block1);
9894 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009895 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009896 smp->data.u.str.str = temp->str;
9897 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009898 smp->flags = SMP_F_VOL_TEST;
9899 }
9900 return 1;
9901}
9902
9903
9904/* returns the available length of the body. This requires that the body
9905 * has been waited for using http-buffer-request.
9906 */
9907static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009908smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009909{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009910 struct http_msg *msg;
9911
9912 CHECK_HTTP_MESSAGE_FIRST();
9913
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009914 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009915 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009916 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009917 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009918
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009919 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009920 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009921
9922 smp->flags = SMP_F_VOL_TEST;
9923 return 1;
9924}
9925
9926
9927/* returns the advertised length of the body, or the advertised size of the
9928 * chunks available in the buffer. This requires that the body has been waited
9929 * for using http-buffer-request.
9930 */
9931static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009932smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009933{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009934 struct http_msg *msg;
9935
9936 CHECK_HTTP_MESSAGE_FIRST();
9937
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009938 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009939 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009940 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009941 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009942
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009943 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009944 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009945
9946 smp->flags = SMP_F_VOL_TEST;
9947 return 1;
9948}
9949
9950
Willy Tarreau8797c062007-05-07 00:55:35 +02009951/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009952static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009953smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009954{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009955 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009956
Willy Tarreauc0239e02012-04-16 14:42:55 +02009957 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009958
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009959 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009960 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009961 smp->data.u.str.len = txn->req.sl.rq.u_l;
9962 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009963 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009964 return 1;
9965}
9966
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009967static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009968smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009969{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009970 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009971 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009972
Willy Tarreauc0239e02012-04-16 14:42:55 +02009973 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009974
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009975 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009976 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 +02009977 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009978 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009979
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009980 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009981 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009982 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009983 return 1;
9984}
9985
9986static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009987smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009988{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009989 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009990 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009991
Willy Tarreauc0239e02012-04-16 14:42:55 +02009992 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009993
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009994 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009995 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 +02009996 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9997 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009998
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009999 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010000 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010001 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010002 return 1;
10003}
10004
Willy Tarreau185b5c42012-04-26 15:11:51 +020010005/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10006 * Accepts an optional argument of type string containing the header field name,
10007 * and an optional argument of type signed or unsigned integer to request an
10008 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010009 * headers are considered from the first one. It does not stop on commas and
10010 * returns full lines instead (useful for User-Agent or Date for example).
10011 */
10012static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010013smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010014{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010015 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010016 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010017 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010018 int occ = 0;
10019 const char *name_str = NULL;
10020 int name_len = 0;
10021
10022 if (!ctx) {
10023 /* first call */
10024 ctx = &static_hdr_ctx;
10025 ctx->idx = 0;
10026 smp->ctx.a[0] = ctx;
10027 }
10028
10029 if (args) {
10030 if (args[0].type != ARGT_STR)
10031 return 0;
10032 name_str = args[0].data.str.str;
10033 name_len = args[0].data.str.len;
10034
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010035 if (args[1].type == ARGT_SINT)
10036 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010037 }
10038
10039 CHECK_HTTP_MESSAGE_FIRST();
10040
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010041 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010042 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 +020010043
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010044 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10045 /* search for header from the beginning */
10046 ctx->idx = 0;
10047
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010048 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010049 /* no explicit occurrence and single fetch => last header by default */
10050 occ = -1;
10051
10052 if (!occ)
10053 /* prepare to report multiple occurrences for ACL fetches */
10054 smp->flags |= SMP_F_NOT_LAST;
10055
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010056 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010057 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010058 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 +020010059 return 1;
10060
10061 smp->flags &= ~SMP_F_NOT_LAST;
10062 return 0;
10063}
10064
10065/* 6. Check on HTTP header count. The number of occurrences is returned.
10066 * Accepts exactly 1 argument of type string. It does not stop on commas and
10067 * returns full lines instead (useful for User-Agent or Date for example).
10068 */
10069static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010070smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010071{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010072 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010073 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010074 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010075 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010076 const char *name = NULL;
10077 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010078
Willy Tarreau601a4d12015-04-01 19:16:09 +020010079 if (args && args->type == ARGT_STR) {
10080 name = args->data.str.str;
10081 len = args->data.str.len;
10082 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010083
10084 CHECK_HTTP_MESSAGE_FIRST();
10085
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010086 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010087 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 +020010088
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010089 ctx.idx = 0;
10090 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010091 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010092 cnt++;
10093
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010094 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010095 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010096 smp->flags = SMP_F_VOL_HDR;
10097 return 1;
10098}
10099
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010100static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010101smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010102{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010103 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010104 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010105 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010106 struct chunk *temp;
10107 char del = ',';
10108
10109 if (args && args->type == ARGT_STR)
10110 del = *args[0].data.str.str;
10111
10112 CHECK_HTTP_MESSAGE_FIRST();
10113
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010114 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010115 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 +020010116
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010117 temp = get_trash_chunk();
10118
10119 ctx.idx = 0;
10120 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10121 if (temp->len)
10122 temp->str[temp->len++] = del;
10123 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10124 temp->len += ctx.del;
10125 }
10126
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010127 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010128 smp->data.u.str.str = temp->str;
10129 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010130 smp->flags = SMP_F_VOL_HDR;
10131 return 1;
10132}
10133
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010134/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10135 * Accepts an optional argument of type string containing the header field name,
10136 * and an optional argument of type signed or unsigned integer to request an
10137 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010138 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010139 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010140static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010141smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010142{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010143 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010144 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010145 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010146 int occ = 0;
10147 const char *name_str = NULL;
10148 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010149
Willy Tarreaua890d072013-04-02 12:01:06 +020010150 if (!ctx) {
10151 /* first call */
10152 ctx = &static_hdr_ctx;
10153 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010154 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010155 }
10156
Willy Tarreau185b5c42012-04-26 15:11:51 +020010157 if (args) {
10158 if (args[0].type != ARGT_STR)
10159 return 0;
10160 name_str = args[0].data.str.str;
10161 name_len = args[0].data.str.len;
10162
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010163 if (args[1].type == ARGT_SINT)
10164 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010165 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010166
Willy Tarreaue333ec92012-04-16 16:26:40 +020010167 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010168
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010169 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010170 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 +020010171
Willy Tarreau185b5c42012-04-26 15:11:51 +020010172 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010173 /* search for header from the beginning */
10174 ctx->idx = 0;
10175
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010176 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010177 /* no explicit occurrence and single fetch => last header by default */
10178 occ = -1;
10179
10180 if (!occ)
10181 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010182 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010183
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010184 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010185 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010186 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 +020010187 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010188
Willy Tarreau37406352012-04-23 16:16:37 +020010189 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010190 return 0;
10191}
10192
Willy Tarreauc11416f2007-06-17 16:58:38 +020010193/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010194 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010195 */
10196static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010197smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010198{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010199 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010200 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010201 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010202 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010203 const char *name = NULL;
10204 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010205
Willy Tarreau601a4d12015-04-01 19:16:09 +020010206 if (args && args->type == ARGT_STR) {
10207 name = args->data.str.str;
10208 len = args->data.str.len;
10209 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010210
Willy Tarreaue333ec92012-04-16 16:26:40 +020010211 CHECK_HTTP_MESSAGE_FIRST();
10212
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010213 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010214 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 +020010215
Willy Tarreau33a7e692007-06-10 19:45:56 +020010216 ctx.idx = 0;
10217 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010218 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010219 cnt++;
10220
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010221 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010222 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010223 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010224 return 1;
10225}
10226
Willy Tarreau185b5c42012-04-26 15:11:51 +020010227/* Fetch an HTTP header's integer value. The integer value is returned. It
10228 * takes a mandatory argument of type string and an optional one of type int
10229 * to designate a specific occurrence. It returns an unsigned integer, which
10230 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010231 */
10232static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010233smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010234{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010235 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010236
Willy Tarreauf853c462012-04-23 18:53:56 +020010237 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010238 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010239 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010240 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010241
Willy Tarreaud53e2422012-04-16 17:21:11 +020010242 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010243}
10244
Cyril Bonté69fa9922012-10-25 00:01:06 +020010245/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10246 * and an optional one of type int to designate a specific occurrence.
10247 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010248 */
10249static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010250smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010251{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010252 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010253
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010254 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010255 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010256 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010257 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010258 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010259 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010260 if (smp->data.u.str.len < temp->size - 1) {
10261 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10262 temp->str[smp->data.u.str.len] = '\0';
10263 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010264 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010265 break;
10266 }
10267 }
10268 }
10269
Willy Tarreaud53e2422012-04-16 17:21:11 +020010270 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010271 if (!(smp->flags & SMP_F_NOT_LAST))
10272 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010273 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010274 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010275}
10276
Willy Tarreau737b0c12007-06-10 21:28:46 +020010277/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10278 * the first '/' after the possible hostname, and ends before the possible '?'.
10279 */
10280static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010281smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010282{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010283 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010284 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010285
Willy Tarreauc0239e02012-04-16 14:42:55 +020010286 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010287
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010288 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010289 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010290 ptr = http_get_path(txn);
10291 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010292 return 0;
10293
10294 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010295 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010296 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010297
10298 while (ptr < end && *ptr != '?')
10299 ptr++;
10300
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010301 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010302 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010303 return 1;
10304}
10305
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010306/* This produces a concatenation of the first occurrence of the Host header
10307 * followed by the path component if it begins with a slash ('/'). This means
10308 * that '*' will not be added, resulting in exactly the first Host entry.
10309 * If no Host header is found, then the path is returned as-is. The returned
10310 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010311 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010312 */
10313static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010314smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010315{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010316 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010317 char *ptr, *end, *beg;
10318 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010319 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010320
10321 CHECK_HTTP_MESSAGE_FIRST();
10322
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010323 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010324 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010325 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010326 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010327
10328 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010329 temp = get_trash_chunk();
10330 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010331 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010332 smp->data.u.str.str = temp->str;
10333 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010334
10335 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010336 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010337 beg = http_get_path(txn);
10338 if (!beg)
10339 beg = end;
10340
10341 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10342
10343 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010344 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10345 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010346 }
10347
10348 smp->flags = SMP_F_VOL_1ST;
10349 return 1;
10350}
10351
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010352/* This produces a 32-bit hash of the concatenation of the first occurrence of
10353 * the Host header followed by the path component if it begins with a slash ('/').
10354 * This means that '*' will not be added, resulting in exactly the first Host
10355 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010356 * is hashed using the path hash followed by a full avalanche hash and provides a
10357 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010358 * high-traffic sites without having to store whole paths.
10359 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010360int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010361smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010362{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010363 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010364 struct hdr_ctx ctx;
10365 unsigned int hash = 0;
10366 char *ptr, *beg, *end;
10367 int len;
10368
10369 CHECK_HTTP_MESSAGE_FIRST();
10370
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010371 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010372 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010373 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010374 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10375 ptr = ctx.line + ctx.val;
10376 len = ctx.vlen;
10377 while (len--)
10378 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10379 }
10380
10381 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010382 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010383 beg = http_get_path(txn);
10384 if (!beg)
10385 beg = end;
10386
10387 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10388
10389 if (beg < ptr && *beg == '/') {
10390 while (beg < ptr)
10391 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10392 }
10393 hash = full_hash(hash);
10394
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010395 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010396 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010397 smp->flags = SMP_F_VOL_1ST;
10398 return 1;
10399}
10400
Willy Tarreau4a550602012-12-09 14:53:32 +010010401/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010402 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10403 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10404 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010405 * that in environments where IPv6 is insignificant, truncating the output to
10406 * 8 bytes would still work.
10407 */
10408static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010409smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010410{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010411 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010412 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010413
10414 if (!cli_conn)
10415 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010416
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010417 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010418 return 0;
10419
Willy Tarreau47ca5452012-12-23 20:22:19 +010010420 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010421 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010422 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010423
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010424 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010425 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010426 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010427 temp->len += 4;
10428 break;
10429 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010430 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010431 temp->len += 16;
10432 break;
10433 default:
10434 return 0;
10435 }
10436
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010437 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010438 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010439 return 1;
10440}
10441
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010442/* Extracts the query string, which comes after the question mark '?'. If no
10443 * question mark is found, nothing is returned. Otherwise it returns a sample
10444 * of type string carrying the whole query string.
10445 */
10446static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010447smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010448{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010449 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010450 char *ptr, *end;
10451
10452 CHECK_HTTP_MESSAGE_FIRST();
10453
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010454 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010455 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10456 end = ptr + txn->req.sl.rq.u_l;
10457
10458 /* look up the '?' */
10459 do {
10460 if (ptr == end)
10461 return 0;
10462 } while (*ptr++ != '?');
10463
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010464 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010465 smp->data.u.str.str = ptr;
10466 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010467 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10468 return 1;
10469}
10470
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010471static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010472smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010473{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010474 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10475 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10476 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010477
Willy Tarreau24e32d82012-04-23 23:55:44 +020010478 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010479
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010480 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010481 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010482 return 1;
10483}
10484
Willy Tarreau7f18e522010-10-22 20:04:13 +020010485/* return a valid test if the current request is the first one on the connection */
10486static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010487smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010488{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010489 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010490 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010491 return 1;
10492}
10493
Willy Tarreau34db1082012-04-19 17:16:54 +020010494/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010495static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010496smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010497{
10498
Willy Tarreau24e32d82012-04-23 23:55:44 +020010499 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010500 return 0;
10501
Willy Tarreauc0239e02012-04-16 14:42:55 +020010502 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010503
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010504 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010505 return 0;
10506
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010507 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010508 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010509 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010510 return 1;
10511}
Willy Tarreau8797c062007-05-07 00:55:35 +020010512
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010513/* Accepts exactly 1 argument of type userlist */
10514static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010515smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010516{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010517 if (!args || args->type != ARGT_USR)
10518 return 0;
10519
10520 CHECK_HTTP_MESSAGE_FIRST();
10521
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010522 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010523 return 0;
10524
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010525 /* if the user does not belong to the userlist or has a wrong password,
10526 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010527 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010528 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010529 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10530 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010531 return 0;
10532
10533 /* pat_match_auth() will need the user list */
10534 smp->ctx.a[0] = args->data.usr;
10535
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010536 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010537 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010538 smp->data.u.str.str = smp->strm->txn->auth.user;
10539 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010540
10541 return 1;
10542}
10543
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010544/* Try to find the next occurrence of a cookie name in a cookie header value.
10545 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10546 * the cookie value is returned into *value and *value_l, and the function
10547 * returns a pointer to the next pointer to search from if the value was found.
10548 * Otherwise if the cookie was not found, NULL is returned and neither value
10549 * nor value_l are touched. The input <hdr> string should first point to the
10550 * header's value, and the <hdr_end> pointer must point to the first character
10551 * not part of the value. <list> must be non-zero if value may represent a list
10552 * of values (cookie headers). This makes it faster to abort parsing when no
10553 * list is expected.
10554 */
David Carlier4686f792015-09-25 14:10:50 +010010555char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010556extract_cookie_value(char *hdr, const char *hdr_end,
10557 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010558 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010559{
10560 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10561 char *next;
10562
10563 /* we search at least a cookie name followed by an equal, and more
10564 * generally something like this :
10565 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10566 */
10567 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10568 /* Iterate through all cookies on this line */
10569
Willy Tarreau2235b262016-11-05 15:50:20 +010010570 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010571 att_beg++;
10572
10573 /* find att_end : this is the first character after the last non
10574 * space before the equal. It may be equal to hdr_end.
10575 */
10576 equal = att_end = att_beg;
10577
10578 while (equal < hdr_end) {
10579 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10580 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010010581 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010582 continue;
10583 att_end = equal;
10584 }
10585
10586 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10587 * is between <att_beg> and <equal>, both may be identical.
10588 */
10589
10590 /* look for end of cookie if there is an equal sign */
10591 if (equal < hdr_end && *equal == '=') {
10592 /* look for the beginning of the value */
10593 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010010594 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010595 val_beg++;
10596
10597 /* find the end of the value, respecting quotes */
10598 next = find_cookie_value_end(val_beg, hdr_end);
10599
10600 /* make val_end point to the first white space or delimitor after the value */
10601 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010010602 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010603 val_end--;
10604 } else {
10605 val_beg = val_end = next = equal;
10606 }
10607
10608 /* We have nothing to do with attributes beginning with '$'. However,
10609 * they will automatically be removed if a header before them is removed,
10610 * since they're supposed to be linked together.
10611 */
10612 if (*att_beg == '$')
10613 continue;
10614
10615 /* Ignore cookies with no equal sign */
10616 if (equal == next)
10617 continue;
10618
10619 /* Now we have the cookie name between att_beg and att_end, and
10620 * its value between val_beg and val_end.
10621 */
10622
10623 if (att_end - att_beg == cookie_name_l &&
10624 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10625 /* let's return this value and indicate where to go on from */
10626 *value = val_beg;
10627 *value_l = val_end - val_beg;
10628 return next + 1;
10629 }
10630
10631 /* Set-Cookie headers only have the name in the first attr=value part */
10632 if (!list)
10633 break;
10634 }
10635
10636 return NULL;
10637}
10638
William Lallemanda43ba4e2014-01-28 18:14:25 +010010639/* Fetch a captured HTTP request header. The index is the position of
10640 * the "capture" option in the configuration file
10641 */
10642static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010643smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010644{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010645 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010646 int idx;
10647
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010648 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010649 return 0;
10650
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010651 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010652
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010653 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 +010010654 return 0;
10655
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010656 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010657 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010658 smp->data.u.str.str = smp->strm->req_cap[idx];
10659 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010660
10661 return 1;
10662}
10663
10664/* Fetch a captured HTTP response header. The index is the position of
10665 * the "capture" option in the configuration file
10666 */
10667static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010668smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010669{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010670 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010671 int idx;
10672
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010673 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010674 return 0;
10675
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010676 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010677
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010678 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 +010010679 return 0;
10680
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010681 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010682 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010683 smp->data.u.str.str = smp->strm->res_cap[idx];
10684 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010685
10686 return 1;
10687}
10688
William Lallemand65ad6e12014-01-31 15:08:02 +010010689/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10690static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010691smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010692{
10693 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010694 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010010695 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010696
Willy Tarreau15e91e12015-04-04 00:52:09 +020010697 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010698 return 0;
10699
William Lallemand96a77852014-02-05 00:30:02 +010010700 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010701
William Lallemand96a77852014-02-05 00:30:02 +010010702 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10703 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010704
William Lallemand96a77852014-02-05 00:30:02 +010010705 temp = get_trash_chunk();
10706 temp->str = txn->uri;
10707 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010708 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010709 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010710 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010711
10712 return 1;
10713
10714}
10715
10716/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10717static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010718smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010719{
10720 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010721 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010010722 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010723
Willy Tarreau15e91e12015-04-04 00:52:09 +020010724 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010725 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010726
William Lallemand65ad6e12014-01-31 15:08:02 +010010727 ptr = txn->uri;
10728
10729 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10730 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010731
William Lallemand65ad6e12014-01-31 15:08:02 +010010732 if (!*ptr)
10733 return 0;
10734
10735 ptr++; /* skip the space */
10736
10737 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010738 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010739 if (!ptr)
10740 return 0;
10741 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10742 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010743
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010744 smp->data.u.str = *temp;
10745 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010746 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010747 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010748
10749 return 1;
10750}
10751
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010752/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10753 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10754 */
10755static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010756smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010757{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010758 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010759
Willy Tarreau15e91e12015-04-04 00:52:09 +020010760 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010761 return 0;
10762
10763 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010764 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010765 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010766 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010767
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010768 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010769 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010770 smp->flags = SMP_F_CONST;
10771 return 1;
10772
10773}
10774
10775/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10776 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10777 */
10778static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010779smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010780{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010781 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010782
Willy Tarreau15e91e12015-04-04 00:52:09 +020010783 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010784 return 0;
10785
10786 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010787 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010788 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010789 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010790
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010791 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010792 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010793 smp->flags = SMP_F_CONST;
10794 return 1;
10795
10796}
10797
William Lallemand65ad6e12014-01-31 15:08:02 +010010798
Willy Tarreaue333ec92012-04-16 16:26:40 +020010799/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010800 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010801 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010802 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010803 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010804 * Accepts exactly 1 argument of type string. If the input options indicate
10805 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010806 * The returned sample is of type CSTR. Can be used to parse cookies in other
10807 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010808 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010809int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010810{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010811 struct http_txn *txn;
10812 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010813 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010814 const struct http_msg *msg;
10815 const char *hdr_name;
10816 int hdr_name_len;
10817 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010818 int occ = 0;
10819 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010820
Willy Tarreau24e32d82012-04-23 23:55:44 +020010821 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010822 return 0;
10823
Willy Tarreaua890d072013-04-02 12:01:06 +020010824 if (!ctx) {
10825 /* first call */
10826 ctx = &static_hdr_ctx;
10827 ctx->idx = 0;
10828 smp->ctx.a[2] = ctx;
10829 }
10830
Willy Tarreaue333ec92012-04-16 16:26:40 +020010831 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010832
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010833 txn = smp->strm->txn;
10834 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010835
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010836 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010837 msg = &txn->req;
10838 hdr_name = "Cookie";
10839 hdr_name_len = 6;
10840 } else {
10841 msg = &txn->rsp;
10842 hdr_name = "Set-Cookie";
10843 hdr_name_len = 10;
10844 }
10845
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010846 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020010847 /* no explicit occurrence and single fetch => last cookie by default */
10848 occ = -1;
10849
10850 /* OK so basically here, either we want only one value and it's the
10851 * last one, or we want to iterate over all of them and we fetch the
10852 * next one.
10853 */
10854
Willy Tarreau9b28e032012-10-12 23:49:43 +020010855 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010856 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010857 /* search for the header from the beginning, we must first initialize
10858 * the search parameters.
10859 */
Willy Tarreau37406352012-04-23 16:16:37 +020010860 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010861 ctx->idx = 0;
10862 }
10863
Willy Tarreau28376d62012-04-26 21:26:10 +020010864 smp->flags |= SMP_F_VOL_HDR;
10865
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010866 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010867 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10868 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010869 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10870 goto out;
10871
Willy Tarreau24e32d82012-04-23 23:55:44 +020010872 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010873 continue;
10874
Willy Tarreau37406352012-04-23 16:16:37 +020010875 smp->ctx.a[0] = ctx->line + ctx->val;
10876 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010877 }
10878
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010879 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010880 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010881 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010882 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010883 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010884 &smp->data.u.str.str,
10885 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010886 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010887 found = 1;
10888 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010889 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020010890 smp->flags |= SMP_F_NOT_LAST;
10891 return 1;
10892 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010893 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010894 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010895 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010896 /* all cookie headers and values were scanned. If we're looking for the
10897 * last occurrence, we may return it now.
10898 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010899 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010900 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010901 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010902}
10903
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010904/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010905 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010906 * multiple cookies may be parsed on the same line. The returned sample is of
10907 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010908 */
10909static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010910smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010911{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010912 struct http_txn *txn;
10913 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010914 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010915 const struct http_msg *msg;
10916 const char *hdr_name;
10917 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010918 int cnt;
10919 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010920 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010921
Willy Tarreau24e32d82012-04-23 23:55:44 +020010922 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010923 return 0;
10924
Willy Tarreaue333ec92012-04-16 16:26:40 +020010925 CHECK_HTTP_MESSAGE_FIRST();
10926
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010927 txn = smp->strm->txn;
10928 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010929
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010930 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010931 msg = &txn->req;
10932 hdr_name = "Cookie";
10933 hdr_name_len = 6;
10934 } else {
10935 msg = &txn->rsp;
10936 hdr_name = "Set-Cookie";
10937 hdr_name_len = 10;
10938 }
10939
Willy Tarreau9b28e032012-10-12 23:49:43 +020010940 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010941 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010942 ctx.idx = 0;
10943 cnt = 0;
10944
10945 while (1) {
10946 /* Note: val_beg == NULL every time we need to fetch a new header */
10947 if (!val_beg) {
10948 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10949 break;
10950
Willy Tarreau24e32d82012-04-23 23:55:44 +020010951 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010952 continue;
10953
10954 val_beg = ctx.line + ctx.val;
10955 val_end = val_beg + ctx.vlen;
10956 }
10957
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010958 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010959 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010960 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010961 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010962 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010963 &smp->data.u.str.str,
10964 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010965 cnt++;
10966 }
10967 }
10968
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010969 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010970 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010971 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010972 return 1;
10973}
10974
Willy Tarreau51539362012-05-08 12:46:28 +020010975/* Fetch an cookie's integer value. The integer value is returned. It
10976 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10977 */
10978static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010979smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020010980{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010981 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020010982
10983 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010984 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010985 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020010986 }
10987
10988 return ret;
10989}
10990
Willy Tarreau8797c062007-05-07 00:55:35 +020010991/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010992/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010993/************************************************************************/
10994
David Cournapeau16023ee2010-12-23 20:55:41 +090010995/*
10996 * Given a path string and its length, find the position of beginning of the
10997 * query string. Returns NULL if no query string is found in the path.
10998 *
10999 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11000 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011001 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011002 */
bedis4c75cca2012-10-05 08:38:24 +020011003static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011004{
11005 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011006
bedis4c75cca2012-10-05 08:38:24 +020011007 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011008 return p ? p + 1 : NULL;
11009}
11010
bedis4c75cca2012-10-05 08:38:24 +020011011static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011012{
bedis4c75cca2012-10-05 08:38:24 +020011013 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011014}
11015
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011016/* after increasing a pointer value, it can exceed the first buffer
11017 * size. This function transform the value of <ptr> according with
11018 * the expected position. <chunks> is an array of the one or two
11019 * avalaible chunks. The first value is the start of the first chunk,
11020 * the second value if the end+1 of the first chunks. The third value
11021 * is NULL or the start of the second chunk and the fourth value is
11022 * the end+1 of the second chunk. The function returns 1 if does a
11023 * wrap, else returns 0.
11024 */
11025static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11026{
11027 if (*ptr < chunks[1])
11028 return 0;
11029 if (!chunks[2])
11030 return 0;
11031 *ptr = chunks[2] + ( *ptr - chunks[1] );
11032 return 1;
11033}
11034
David Cournapeau16023ee2010-12-23 20:55:41 +090011035/*
11036 * Given a url parameter, find the starting position of the first occurence,
11037 * or NULL if the parameter is not found.
11038 *
11039 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11040 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011041 *
Willy Tarreauf6625822015-12-27 14:51:01 +010011042 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011043 * or the second chunk. The caller must be check the position before using the
11044 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011045 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011046static const char *
11047find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011048 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011049 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011050{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011051 const char *pos, *last, *equal;
11052 const char **bufs = chunks;
11053 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011054
David Cournapeau16023ee2010-12-23 20:55:41 +090011055
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011056 pos = bufs[0];
11057 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010011058 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011059 /* Check the equal. */
11060 equal = pos + url_param_name_l;
11061 if (fix_pointer_if_wrap(chunks, &equal)) {
11062 if (equal >= chunks[3])
11063 return NULL;
11064 } else {
11065 if (equal >= chunks[1])
11066 return NULL;
11067 }
11068 if (*equal == '=') {
11069 if (pos + url_param_name_l > last) {
11070 /* process wrap case, we detect a wrap. In this case, the
11071 * comparison is performed in two parts.
11072 */
11073
11074 /* This is the end, we dont have any other chunk. */
11075 if (bufs != chunks || !bufs[2])
11076 return NULL;
11077
11078 /* Compute the length of each part of the comparison. */
11079 l1 = last - pos;
11080 l2 = url_param_name_l - l1;
11081
11082 /* The second buffer is too short to contain the compared string. */
11083 if (bufs[2] + l2 > bufs[3])
11084 return NULL;
11085
11086 if (memcmp(pos, url_param_name, l1) == 0 &&
11087 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11088 return pos;
11089
11090 /* Perform wrapping and jump the string who fail the comparison. */
11091 bufs += 2;
11092 pos = bufs[0] + l2;
11093 last = bufs[1];
11094
11095 } else {
11096 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011097 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11098 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011099 pos += url_param_name_l + 1;
11100 if (fix_pointer_if_wrap(chunks, &pos))
11101 last = bufs[2];
11102 }
11103 }
11104
11105 while (1) {
11106 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011107 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011108 pos++;
11109 if (pos < last)
11110 break;
11111 /* process buffer wrapping. */
11112 if (bufs != chunks || !bufs[2])
11113 return NULL;
11114 bufs += 2;
11115 pos = bufs[0];
11116 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011117 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011118 pos++;
11119 }
11120 return NULL;
11121}
11122
11123/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011124 * Given a url parameter name and a query string, find the next value.
11125 * An empty url_param_name matches the first available parameter.
11126 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11127 * respectively provide a pointer to the value and its end.
11128 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011129 */
11130static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011131find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011132 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011133 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011134{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011135 const char *arg_start, *qs_end;
11136 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011137
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011138 arg_start = chunks[0];
11139 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011140 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011141 /* Looks for an argument name. */
11142 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011143 url_param_name, url_param_name_l,
11144 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011145 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011146 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011147 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011148 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011149 if (!arg_start)
11150 return 0;
11151
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011152 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011153 while (1) {
11154 /* looks for the first argument. */
11155 value_start = memchr(arg_start, '=', qs_end - arg_start);
11156 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011157 /* Check for wrapping. */
11158 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011159 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011160 chunks[2]) {
11161 arg_start = chunks[2];
11162 qs_end = chunks[3];
11163 continue;
11164 }
11165 return 0;
11166 }
11167 break;
11168 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011169 value_start++;
11170 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011171 else {
11172 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011173 value_start = arg_start + url_param_name_l + 1;
11174
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011175 /* Check for pointer wrapping. */
11176 if (fix_pointer_if_wrap(chunks, &value_start)) {
11177 /* Update the end pointer. */
11178 qs_end = chunks[3];
11179
11180 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011181 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011182 return 0;
11183 }
11184 }
11185
David Cournapeau16023ee2010-12-23 20:55:41 +090011186 value_end = value_start;
11187
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011188 while (1) {
11189 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11190 value_end++;
11191 if (value_end < qs_end)
11192 break;
11193 /* process buffer wrapping. */
11194 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011195 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011196 chunks[2]) {
11197 value_end = chunks[2];
11198 qs_end = chunks[3];
11199 continue;
11200 }
11201 break;
11202 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011203
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011204 *vstart = value_start;
11205 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011206 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011207}
11208
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011209/* This scans a URL-encoded query string. It takes an optionally wrapping
11210 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11211 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11212 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011213 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011214static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011215smp_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 +090011216{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011217 const char *vstart, *vend;
11218 struct chunk *temp;
11219 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011220
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011221 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011222 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011223 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011224 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011225 return 0;
11226
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011227 /* Create sample. If the value is contiguous, return the pointer as CONST,
11228 * if the value is wrapped, copy-it in a buffer.
11229 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011230 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011231 if (chunks[2] &&
11232 vstart >= chunks[0] && vstart <= chunks[1] &&
11233 vend >= chunks[2] && vend <= chunks[3]) {
11234 /* Wrapped case. */
11235 temp = get_trash_chunk();
11236 memcpy(temp->str, vstart, chunks[1] - vstart);
11237 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011238 smp->data.u.str.str = temp->str;
11239 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011240 } else {
11241 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011242 smp->data.u.str.str = (char *)vstart;
11243 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011244 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11245 }
11246
11247 /* Update context, check wrapping. */
11248 chunks[0] = vend;
11249 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11250 chunks[1] = chunks[3];
11251 chunks[2] = NULL;
11252 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011253
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011254 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011255 smp->flags |= SMP_F_NOT_LAST;
11256
David Cournapeau16023ee2010-12-23 20:55:41 +090011257 return 1;
11258}
11259
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011260/* This function iterates over each parameter of the query string. It uses
11261 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011262 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11263 * An optional parameter name is passed in args[0], otherwise any parameter is
11264 * considered. It supports an optional delimiter argument for the beginning of
11265 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011266 */
11267static int
11268smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11269{
11270 struct http_msg *msg;
11271 char delim = '?';
11272 const char *name;
11273 int name_len;
11274
Dragan Dosen26f77e52015-05-25 10:02:11 +020011275 if (!args ||
11276 (args[0].type && args[0].type != ARGT_STR) ||
11277 (args[1].type && args[1].type != ARGT_STR))
11278 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011279
Dragan Dosen26f77e52015-05-25 10:02:11 +020011280 name = "";
11281 name_len = 0;
11282 if (args->type == ARGT_STR) {
11283 name = args->data.str.str;
11284 name_len = args->data.str.len;
11285 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011286
Dragan Dosen26f77e52015-05-25 10:02:11 +020011287 if (args[1].type)
11288 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011289
Dragan Dosen26f77e52015-05-25 10:02:11 +020011290 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011291 CHECK_HTTP_MESSAGE_FIRST();
11292
11293 msg = &smp->strm->txn->req;
11294
11295 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11296 msg->sl.rq.u_l, delim);
11297 if (!smp->ctx.a[0])
11298 return 0;
11299
11300 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011301
11302 /* Assume that the context is filled with NULL pointer
11303 * before the first call.
11304 * smp->ctx.a[2] = NULL;
11305 * smp->ctx.a[3] = NULL;
11306 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011307 }
11308
11309 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11310}
11311
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011312/* This function iterates over each parameter of the body. This requires
11313 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011314 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11315 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11316 * optional second part if the body wraps at the end of the buffer. An optional
11317 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011318 */
11319static int
11320smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11321{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011322 struct http_msg *msg;
11323 unsigned long len;
11324 unsigned long block1;
11325 char *body;
11326 const char *name;
11327 int name_len;
11328
11329 if (!args || (args[0].type && args[0].type != ARGT_STR))
11330 return 0;
11331
11332 name = "";
11333 name_len = 0;
11334 if (args[0].type == ARGT_STR) {
11335 name = args[0].data.str.str;
11336 name_len = args[0].data.str.len;
11337 }
11338
11339 if (!smp->ctx.a[0]) { // first call, find the query string
11340 CHECK_HTTP_MESSAGE_FIRST();
11341
11342 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011343 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011344 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011345 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011346
11347 len = http_body_bytes(msg);
11348 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11349
11350 block1 = len;
11351 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11352 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11353
11354 if (block1 == len) {
11355 /* buffer is not wrapped (or empty) */
11356 smp->ctx.a[0] = body;
11357 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011358
11359 /* Assume that the context is filled with NULL pointer
11360 * before the first call.
11361 * smp->ctx.a[2] = NULL;
11362 * smp->ctx.a[3] = NULL;
11363 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011364 }
11365 else {
11366 /* buffer is wrapped, we need to defragment it */
11367 smp->ctx.a[0] = body;
11368 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011369 smp->ctx.a[2] = msg->chn->buf->data;
11370 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011371 }
11372 }
11373 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11374}
11375
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011376/* Return the signed integer value for the specified url parameter (see url_param
11377 * above).
11378 */
11379static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011380smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011381{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011382 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011383
11384 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011385 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011386 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011387 }
11388
11389 return ret;
11390}
11391
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011392/* This produces a 32-bit hash of the concatenation of the first occurrence of
11393 * the Host header followed by the path component if it begins with a slash ('/').
11394 * This means that '*' will not be added, resulting in exactly the first Host
11395 * entry. If no Host header is found, then the path is used. The resulting value
11396 * is hashed using the url hash followed by a full avalanche hash and provides a
11397 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11398 * high-traffic sites without having to store whole paths.
11399 * this differs from the base32 functions in that it includes the url parameters
11400 * as well as the path
11401 */
11402static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011403smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011404{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011405 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011406 struct hdr_ctx ctx;
11407 unsigned int hash = 0;
11408 char *ptr, *beg, *end;
11409 int len;
11410
11411 CHECK_HTTP_MESSAGE_FIRST();
11412
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011413 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011414 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011415 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011416 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11417 ptr = ctx.line + ctx.val;
11418 len = ctx.vlen;
11419 while (len--)
11420 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11421 }
11422
11423 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011424 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 +000011425 beg = http_get_path(txn);
11426 if (!beg)
11427 beg = end;
11428
11429 for (ptr = beg; ptr < end ; ptr++);
11430
11431 if (beg < ptr && *beg == '/') {
11432 while (beg < ptr)
11433 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11434 }
11435 hash = full_hash(hash);
11436
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011437 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011438 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011439 smp->flags = SMP_F_VOL_1ST;
11440 return 1;
11441}
11442
11443/* This concatenates the source address with the 32-bit hash of the Host and
11444 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11445 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11446 * on the source address length. The URL hash is stored before the address so
11447 * that in environments where IPv6 is insignificant, truncating the output to
11448 * 8 bytes would still work.
11449 */
11450static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011451smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011452{
11453 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011454 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011455
Dragan Dosendb5af612016-06-16 11:23:01 +020011456 if (!cli_conn)
11457 return 0;
11458
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011459 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011460 return 0;
11461
11462 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020011463 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
11464 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011465
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011466 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011467 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011468 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011469 temp->len += 4;
11470 break;
11471 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011472 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011473 temp->len += 16;
11474 break;
11475 default:
11476 return 0;
11477 }
11478
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011479 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011480 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011481 return 1;
11482}
11483
Willy Tarreau185b5c42012-04-26 15:11:51 +020011484/* This function is used to validate the arguments passed to any "hdr" fetch
11485 * keyword. These keywords support an optional positive or negative occurrence
11486 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11487 * is assumed that the types are already the correct ones. Returns 0 on error,
11488 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11489 * error message in case of error, that the caller is responsible for freeing.
11490 * The initial location must either be freeable or NULL.
11491 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011492int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011493{
11494 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011495 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011496 return 0;
11497 }
11498 return 1;
11499}
11500
Willy Tarreau276fae92013-07-25 14:36:01 +020011501/* takes an UINT value on input supposed to represent the time since EPOCH,
11502 * adds an optional offset found in args[0] and emits a string representing
11503 * the date in RFC-1123/5322 format.
11504 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011505static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011506{
Cyril Bontéf78d8962016-01-22 19:40:28 +010011507 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020011508 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11509 struct chunk *temp;
11510 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011511 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011512 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011513
11514 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011515 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011516 curr_date += args[0].data.sint;
11517
11518 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011519 if (!tm)
11520 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011521
11522 temp = get_trash_chunk();
11523 temp->len = snprintf(temp->str, temp->size - temp->len,
11524 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11525 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11526 tm->tm_hour, tm->tm_min, tm->tm_sec);
11527
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011528 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011529 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011530 return 1;
11531}
11532
Thierry FOURNIERad903512014-04-11 17:51:01 +020011533/* Match language range with language tag. RFC2616 14.4:
11534 *
11535 * A language-range matches a language-tag if it exactly equals
11536 * the tag, or if it exactly equals a prefix of the tag such
11537 * that the first tag character following the prefix is "-".
11538 *
11539 * Return 1 if the strings match, else return 0.
11540 */
11541static inline int language_range_match(const char *range, int range_len,
11542 const char *tag, int tag_len)
11543{
11544 const char *end = range + range_len;
11545 const char *tend = tag + tag_len;
11546 while (range < end) {
11547 if (*range == '-' && tag == tend)
11548 return 1;
11549 if (*range != *tag || tag == tend)
11550 return 0;
11551 range++;
11552 tag++;
11553 }
11554 /* Return true only if the last char of the tag is matched. */
11555 return tag == tend;
11556}
11557
11558/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011559static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011560{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011561 const char *al = smp->data.u.str.str;
11562 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011563 const char *token;
11564 int toklen;
11565 int qvalue;
11566 const char *str;
11567 const char *w;
11568 int best_q = 0;
11569
11570 /* Set the constant to the sample, because the output of the
11571 * function will be peek in the constant configuration string.
11572 */
11573 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011574 smp->data.u.str.size = 0;
11575 smp->data.u.str.str = "";
11576 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011577
11578 /* Parse the accept language */
11579 while (1) {
11580
11581 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011582 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011583 al++;
11584 if (al >= end)
11585 break;
11586
11587 /* Start of the fisrt word. */
11588 token = al;
11589
11590 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011591 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011592 al++;
11593 if (al == token)
11594 goto expect_comma;
11595
11596 /* Length of the token. */
11597 toklen = al - token;
11598 qvalue = 1000;
11599
11600 /* Check if the token exists in the list. If the token not exists,
11601 * jump to the next token.
11602 */
11603 str = args[0].data.str.str;
11604 w = str;
11605 while (1) {
11606 if (*str == ';' || *str == '\0') {
11607 if (language_range_match(token, toklen, w, str-w))
11608 goto look_for_q;
11609 if (*str == '\0')
11610 goto expect_comma;
11611 w = str + 1;
11612 }
11613 str++;
11614 }
11615 goto expect_comma;
11616
11617look_for_q:
11618
11619 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011620 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011621 al++;
11622 if (al >= end)
11623 goto process_value;
11624
11625 /* If ',' is found, process the result */
11626 if (*al == ',')
11627 goto process_value;
11628
11629 /* If the character is different from ';', look
11630 * for the end of the header part in best effort.
11631 */
11632 if (*al != ';')
11633 goto expect_comma;
11634
11635 /* Assumes that the char is ';', now expect "q=". */
11636 al++;
11637
11638 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011639 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011640 al++;
11641 if (al >= end)
11642 goto process_value;
11643
11644 /* Expect 'q'. If no 'q', continue in best effort */
11645 if (*al != 'q')
11646 goto process_value;
11647 al++;
11648
11649 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011650 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011651 al++;
11652 if (al >= end)
11653 goto process_value;
11654
11655 /* Expect '='. If no '=', continue in best effort */
11656 if (*al != '=')
11657 goto process_value;
11658 al++;
11659
11660 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011661 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011662 al++;
11663 if (al >= end)
11664 goto process_value;
11665
11666 /* Parse the q value. */
11667 qvalue = parse_qvalue(al, &al);
11668
11669process_value:
11670
11671 /* If the new q value is the best q value, then store the associated
11672 * language in the response. If qvalue is the biggest value (1000),
11673 * break the process.
11674 */
11675 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011676 smp->data.u.str.str = (char *)w;
11677 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011678 if (qvalue >= 1000)
11679 break;
11680 best_q = qvalue;
11681 }
11682
11683expect_comma:
11684
11685 /* Expect comma or end. If the end is detected, quit the loop. */
11686 while (al < end && *al != ',')
11687 al++;
11688 if (al >= end)
11689 break;
11690
11691 /* Comma is found, jump it and restart the analyzer. */
11692 al++;
11693 }
11694
11695 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011696 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
11697 smp->data.u.str.str = args[1].data.str.str;
11698 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011699 }
11700
11701 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011702 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011703}
11704
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011705/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011706static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011707{
11708 /* If the constant flag is set or if not size is avalaible at
11709 * the end of the buffer, copy the string in other buffer
11710 * before decoding.
11711 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011712 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011713 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011714 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
11715 smp->data.u.str.str = str->str;
11716 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011717 smp->flags &= ~SMP_F_CONST;
11718 }
11719
11720 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011721 smp->data.u.str.str[smp->data.u.str.len] = '\0';
11722 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Christopher Fauleta2584792017-10-05 10:03:12 +020011723 return (smp->data.u.str.len >= 0);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011724}
11725
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011726static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
11727{
11728 struct proxy *fe = strm_fe(smp->strm);
11729 int idx, i;
11730 struct cap_hdr *hdr;
11731 int len;
11732
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011733 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011734 return 0;
11735
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011736 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011737
11738 /* Check the availibity of the capture id. */
11739 if (idx > fe->nb_req_cap - 1)
11740 return 0;
11741
11742 /* Look for the original configuration. */
11743 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
11744 hdr != NULL && i != idx ;
11745 i--, hdr = hdr->next);
11746 if (!hdr)
11747 return 0;
11748
11749 /* check for the memory allocation */
11750 if (smp->strm->req_cap[hdr->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011751 smp->strm->req_cap[hdr->index] = pool_alloc(hdr->pool);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011752 if (smp->strm->req_cap[hdr->index] == NULL)
11753 return 0;
11754
11755 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011756 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011757 if (len > hdr->len)
11758 len = hdr->len;
11759
11760 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011761 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011762 smp->strm->req_cap[idx][len] = '\0';
11763
11764 return 1;
11765}
11766
11767static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
11768{
11769 struct proxy *fe = strm_fe(smp->strm);
11770 int idx, i;
11771 struct cap_hdr *hdr;
11772 int len;
11773
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011774 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011775 return 0;
11776
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011777 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011778
11779 /* Check the availibity of the capture id. */
11780 if (idx > fe->nb_rsp_cap - 1)
11781 return 0;
11782
11783 /* Look for the original configuration. */
11784 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
11785 hdr != NULL && i != idx ;
11786 i--, hdr = hdr->next);
11787 if (!hdr)
11788 return 0;
11789
11790 /* check for the memory allocation */
11791 if (smp->strm->res_cap[hdr->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011792 smp->strm->res_cap[hdr->index] = pool_alloc(hdr->pool);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011793 if (smp->strm->res_cap[hdr->index] == NULL)
11794 return 0;
11795
11796 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011797 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011798 if (len > hdr->len)
11799 len = hdr->len;
11800
11801 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011802 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011803 smp->strm->res_cap[idx][len] = '\0';
11804
11805 return 1;
11806}
11807
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011808/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011809 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011810 * the relevant part of the request line accordingly. Then it updates various
11811 * pointers to the next elements which were moved, and the total buffer length.
11812 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011813 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11814 * error, though this can be revisited when this code is finally exploited.
11815 *
11816 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
11817 * query string and 3 to replace uri.
11818 *
11819 * In query string case, the mark question '?' must be set at the start of the
11820 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011821 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011822int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011823 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011824{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011825 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011826 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011827 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011828 int delta;
11829
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011830 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011831 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011832 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011833 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11834
11835 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011836 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011837 txn->req.sl.rq.m_l += delta;
11838 txn->req.sl.rq.u += delta;
11839 txn->req.sl.rq.v += delta;
11840 break;
11841
11842 case 1: // path
11843 cur_ptr = http_get_path(txn);
11844 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011845 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011846
11847 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011848 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 +010011849 cur_end++;
11850
11851 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011852 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011853 txn->req.sl.rq.u_l += delta;
11854 txn->req.sl.rq.v += delta;
11855 break;
11856
11857 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011858 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011859 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011860 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11861 while (cur_ptr < cur_end && *cur_ptr != '?')
11862 cur_ptr++;
11863
11864 /* skip the question mark or indicate that we must insert it
11865 * (but only if the format string is not empty then).
11866 */
11867 if (cur_ptr < cur_end)
11868 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011869 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011870 offset = 0;
11871
11872 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011873 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011874 txn->req.sl.rq.u_l += delta;
11875 txn->req.sl.rq.v += delta;
11876 break;
11877
11878 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011879 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011880 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11881
11882 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011883 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011884 txn->req.sl.rq.u_l += delta;
11885 txn->req.sl.rq.v += delta;
11886 break;
11887
11888 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011889 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011890 }
11891
11892 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011893 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020011894 txn->req.sl.rq.l += delta;
11895 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011896 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011897 return 0;
11898}
11899
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011900/* This function replace the HTTP status code and the associated message. The
11901 * variable <status> contains the new status code. This function never fails.
11902 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011903void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011904{
11905 struct http_txn *txn = s->txn;
11906 char *cur_ptr, *cur_end;
11907 int delta;
11908 char *res;
11909 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011910 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011911 int msg_len;
11912
11913 chunk_reset(&trash);
11914
11915 res = ultoa_o(status, trash.str, trash.size);
11916 c_l = res - trash.str;
11917
11918 trash.str[c_l] = ' ';
11919 trash.len = c_l + 1;
11920
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011921 /* Do we have a custom reason format string? */
11922 if (msg == NULL)
11923 msg = get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011924 msg_len = strlen(msg);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011925 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
11926 trash.len += msg_len;
11927
11928 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
11929 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
11930
11931 /* commit changes and adjust message */
11932 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
11933
11934 /* adjust res line offsets and lengths */
11935 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
11936 txn->rsp.sl.st.c_l = c_l;
11937 txn->rsp.sl.st.r_l = msg_len;
11938
11939 delta = trash.len - (cur_end - cur_ptr);
11940 txn->rsp.sl.st.l += delta;
11941 txn->hdr_idx.v[0].len += delta;
11942 http_msg_move_end(&txn->rsp, delta);
11943}
11944
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011945/* This function executes one of the set-{method,path,query,uri} actions. It
11946 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011947 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011948 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011949 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
11950 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011951 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011952enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011953 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011954{
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011955 struct chunk *replace;
11956 enum act_return ret = ACT_RET_ERR;
11957
11958 replace = alloc_trash_chunk();
11959 if (!replace)
11960 goto leave;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011961
11962 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011963 if (rule->arg.http.action == 2)
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011964 replace->str[replace->len++] = '?';
11965 replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len,
11966 &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011967
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011968 http_replace_req_line(rule->arg.http.action, replace->str, replace->len, px, s);
11969
11970 ret = ACT_RET_CONT;
11971
11972leave:
11973 free_trash_chunk(replace);
11974 return ret;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011975}
11976
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011977/* This function is just a compliant action wrapper for "set-status". */
11978enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011979 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011980{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011981 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011982 return ACT_RET_CONT;
11983}
11984
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011985/* parse an http-request action among :
11986 * set-method
11987 * set-path
11988 * set-query
11989 * set-uri
11990 *
11991 * All of them accept a single argument of type string representing a log-format.
11992 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
11993 * 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 +020011994 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011995 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011996enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
11997 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011998{
11999 int cur_arg = *orig_arg;
12000
Thierry FOURNIER42148732015-09-02 17:17:33 +020012001 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012002
12003 switch (args[0][4]) {
12004 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012005 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012006 rule->action_ptr = http_action_set_req_line;
12007 break;
12008 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012009 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012010 rule->action_ptr = http_action_set_req_line;
12011 break;
12012 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012013 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012014 rule->action_ptr = http_action_set_req_line;
12015 break;
12016 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012017 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012018 rule->action_ptr = http_action_set_req_line;
12019 break;
12020 default:
12021 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012022 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012023 }
12024
12025 if (!*args[cur_arg] ||
12026 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12027 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012028 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012029 }
12030
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012031 LIST_INIT(&rule->arg.http.logfmt);
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012032 px->conf.args.ctx = ARGC_HRQ;
12033 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.logfmt, LOG_OPT_HTTP,
12034 (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 +010012035 return ACT_RET_PRS_ERR;
12036 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012037
12038 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012039 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012040}
12041
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012042/* parse set-status action:
12043 * This action accepts a single argument of type int representing
12044 * an http status code. It returns ACT_RET_PRS_OK on success,
12045 * ACT_RET_PRS_ERR on error.
12046 */
12047enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12048 struct act_rule *rule, char **err)
12049{
12050 char *error;
12051
Thierry FOURNIER42148732015-09-02 17:17:33 +020012052 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012053 rule->action_ptr = action_http_set_status;
12054
12055 /* Check if an argument is available */
12056 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012057 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012058 return ACT_RET_PRS_ERR;
12059 }
12060
12061 /* convert status code as integer */
12062 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12063 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12064 memprintf(err, "expects an integer status code between 100 and 999");
12065 return ACT_RET_PRS_ERR;
12066 }
12067
12068 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012069
12070 /* set custom reason string */
12071 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
12072 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
12073 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
12074 (*orig_arg)++;
12075 rule->arg.status.reason = strdup(args[*orig_arg]);
12076 (*orig_arg)++;
12077 }
12078
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012079 return ACT_RET_PRS_OK;
12080}
12081
Willy Tarreau53275e82017-11-24 07:52:01 +010012082/* This function executes the "reject" HTTP action. It clears the request and
12083 * response buffer without sending any response. It can be useful as an HTTP
12084 * alternative to the silent-drop action to defend against DoS attacks, and may
12085 * also be used with HTTP/2 to close a connection instead of just a stream.
12086 * The txn status is unchanged, indicating no response was sent. The termination
12087 * flags will indicate "PR". It always returns ACT_RET_STOP.
12088 */
12089enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
12090 struct session *sess, struct stream *s, int flags)
12091{
12092 channel_abort(&s->req);
12093 channel_abort(&s->res);
12094 s->req.analysers = 0;
12095 s->res.analysers = 0;
12096
12097 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
12098 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
12099 if (sess->listener && sess->listener->counters)
12100 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
12101
12102 if (!(s->flags & SF_ERR_MASK))
12103 s->flags |= SF_ERR_PRXCOND;
12104 if (!(s->flags & SF_FINST_MASK))
12105 s->flags |= SF_FINST_R;
12106
12107 return ACT_RET_CONT;
12108}
12109
12110/* parse the "reject" action:
12111 * This action takes no argument and returns ACT_RET_PRS_OK on success,
12112 * ACT_RET_PRS_ERR on error.
12113 */
12114enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
12115 struct act_rule *rule, char **err)
12116{
12117 rule->action = ACT_CUSTOM;
12118 rule->action_ptr = http_action_reject;
12119 return ACT_RET_PRS_OK;
12120}
12121
Willy Tarreaua9083d02015-05-08 15:27:59 +020012122/* This function executes the "capture" action. It executes a fetch expression,
12123 * turns the result into a string and puts it in a capture slot. It always
12124 * returns 1. If an error occurs the action is cancelled, but the rule
12125 * processing continues.
12126 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012127enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012128 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012129{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012130 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012131 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012132 char **cap = s->req_cap;
12133 int len;
12134
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012135 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 +020012136 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012137 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012138
12139 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012140 cap[h->index] = pool_alloc(h->pool);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012141
12142 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012143 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012144
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012145 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012146 if (len > h->len)
12147 len = h->len;
12148
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012149 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012150 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012151 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012152}
12153
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012154/* This function executes the "capture" action and store the result in a
12155 * capture slot if exists. It executes a fetch expression, turns the result
12156 * into a string and puts it in a capture slot. It always returns 1. If an
12157 * error occurs the action is cancelled, but the rule processing continues.
12158 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012159enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012160 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012161{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012162 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012163 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012164 char **cap = s->req_cap;
12165 struct proxy *fe = strm_fe(s);
12166 int len;
12167 int i;
12168
12169 /* Look for the original configuration. */
12170 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012171 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012172 i--, h = h->next);
12173 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012174 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012175
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012176 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 +020012177 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012178 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012179
12180 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012181 cap[h->index] = pool_alloc(h->pool);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012182
12183 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012184 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012185
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012186 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012187 if (len > h->len)
12188 len = h->len;
12189
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012190 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012191 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012192 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012193}
12194
Christopher Faulet29730ba2017-09-18 15:26:32 +020012195/* Check an "http-request capture" action.
12196 *
12197 * The function returns 1 in success case, otherwise, it returns 0 and err is
12198 * filled.
12199 */
12200int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
12201{
Christopher Fauletfd608dd2017-12-04 09:45:15 +010012202 if (rule->action_ptr != http_action_req_capture_by_id)
12203 return 1;
12204
Christopher Faulet29730ba2017-09-18 15:26:32 +020012205 if (rule->arg.capid.idx >= px->nb_req_cap) {
12206 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
12207 rule->arg.capid.idx);
12208 return 0;
12209 }
12210
12211 return 1;
12212}
12213
Willy Tarreaua9083d02015-05-08 15:27:59 +020012214/* parse an "http-request capture" action. It takes a single argument which is
12215 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012216 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012217 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012218 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012219enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12220 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012221{
12222 struct sample_expr *expr;
12223 struct cap_hdr *hdr;
12224 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012225 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012226
12227 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12228 if (strcmp(args[cur_arg], "if") == 0 ||
12229 strcmp(args[cur_arg], "unless") == 0)
12230 break;
12231
12232 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012233 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012234 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012235 }
12236
Willy Tarreaua9083d02015-05-08 15:27:59 +020012237 cur_arg = *orig_arg;
12238 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12239 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012240 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012241
12242 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12243 memprintf(err,
12244 "fetch method '%s' extracts information from '%s', none of which is available here",
12245 args[cur_arg-1], sample_src_names(expr->fetch->use));
12246 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012247 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012248 }
12249
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012250 if (!args[cur_arg] || !*args[cur_arg]) {
12251 memprintf(err, "expects 'len or 'id'");
12252 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012253 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012254 }
12255
Willy Tarreaua9083d02015-05-08 15:27:59 +020012256 if (strcmp(args[cur_arg], "len") == 0) {
12257 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012258
12259 if (!(px->cap & PR_CAP_FE)) {
12260 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012261 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012262 }
12263
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012264 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012265
Willy Tarreaua9083d02015-05-08 15:27:59 +020012266 if (!args[cur_arg]) {
12267 memprintf(err, "missing length value");
12268 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012269 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012270 }
12271 /* we copy the table name for now, it will be resolved later */
12272 len = atoi(args[cur_arg]);
12273 if (len <= 0) {
12274 memprintf(err, "length must be > 0");
12275 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012276 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012277 }
12278 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012279
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012280 if (!len) {
12281 memprintf(err, "a positive 'len' argument is mandatory");
12282 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012283 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012284 }
12285
Vincent Bernat02779b62016-04-03 13:48:43 +020012286 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012287 hdr->next = px->req_cap;
12288 hdr->name = NULL; /* not a header capture */
12289 hdr->namelen = 0;
12290 hdr->len = len;
12291 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12292 hdr->index = px->nb_req_cap++;
12293
12294 px->req_cap = hdr;
12295 px->to_log |= LW_REQHDR;
12296
Thierry FOURNIER42148732015-09-02 17:17:33 +020012297 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012298 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012299 rule->arg.cap.expr = expr;
12300 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012301 }
12302
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012303 else if (strcmp(args[cur_arg], "id") == 0) {
12304 int id;
12305 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012306
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012307 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012308
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012309 if (!args[cur_arg]) {
12310 memprintf(err, "missing id value");
12311 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012312 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012313 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012314
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012315 id = strtol(args[cur_arg], &error, 10);
12316 if (*error != '\0') {
12317 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12318 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012319 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012320 }
12321 cur_arg++;
12322
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012323 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012324
Thierry FOURNIER42148732015-09-02 17:17:33 +020012325 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012326 rule->action_ptr = http_action_req_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012327 rule->check_ptr = check_http_req_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012328 rule->arg.capid.expr = expr;
12329 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012330 }
12331
12332 else {
12333 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12334 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012335 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012336 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012337
12338 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012339 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012340}
12341
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012342/* This function executes the "capture" action and store the result in a
12343 * capture slot if exists. It executes a fetch expression, turns the result
12344 * into a string and puts it in a capture slot. It always returns 1. If an
12345 * error occurs the action is cancelled, but the rule processing continues.
12346 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012347enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012348 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012349{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012350 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012351 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012352 char **cap = s->res_cap;
12353 struct proxy *fe = strm_fe(s);
12354 int len;
12355 int i;
12356
12357 /* Look for the original configuration. */
12358 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012359 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012360 i--, h = h->next);
12361 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012362 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012363
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012364 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 +020012365 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012366 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012367
12368 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012369 cap[h->index] = pool_alloc(h->pool);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012370
12371 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012372 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012373
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012374 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012375 if (len > h->len)
12376 len = h->len;
12377
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012378 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012379 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012380 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012381}
12382
Christopher Faulet29730ba2017-09-18 15:26:32 +020012383/* Check an "http-response capture" action.
12384 *
12385 * The function returns 1 in success case, otherwise, it returns 0 and err is
12386 * filled.
12387 */
12388int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
12389{
Christopher Fauletfd608dd2017-12-04 09:45:15 +010012390 if (rule->action_ptr != http_action_res_capture_by_id)
12391 return 1;
12392
Christopher Faulet29730ba2017-09-18 15:26:32 +020012393 if (rule->arg.capid.idx >= px->nb_rsp_cap) {
12394 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
12395 rule->arg.capid.idx);
12396 return 0;
12397 }
12398
12399 return 1;
12400}
12401
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012402/* parse an "http-response capture" action. It takes a single argument which is
12403 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12404 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012405 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012406 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012407enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12408 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012409{
12410 struct sample_expr *expr;
12411 int cur_arg;
12412 int id;
12413 char *error;
12414
12415 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12416 if (strcmp(args[cur_arg], "if") == 0 ||
12417 strcmp(args[cur_arg], "unless") == 0)
12418 break;
12419
12420 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012421 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012422 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012423 }
12424
12425 cur_arg = *orig_arg;
12426 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12427 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012428 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012429
12430 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12431 memprintf(err,
12432 "fetch method '%s' extracts information from '%s', none of which is available here",
12433 args[cur_arg-1], sample_src_names(expr->fetch->use));
12434 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 (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012439 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012440 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 if (strcmp(args[cur_arg], "id") != 0) {
12445 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12446 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012447 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012448 }
12449
12450 cur_arg++;
12451
12452 if (!args[cur_arg]) {
12453 memprintf(err, "missing id value");
12454 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012455 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012456 }
12457
12458 id = strtol(args[cur_arg], &error, 10);
12459 if (*error != '\0') {
12460 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12461 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012462 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012463 }
12464 cur_arg++;
12465
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012466 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012467
Thierry FOURNIER42148732015-09-02 17:17:33 +020012468 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012469 rule->action_ptr = http_action_res_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012470 rule->check_ptr = check_http_res_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012471 rule->arg.capid.expr = expr;
12472 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012473
12474 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012475 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012476}
12477
William Lallemand73025dd2014-04-24 14:38:37 +020012478/*
12479 * Return the struct http_req_action_kw associated to a keyword.
12480 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012481struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012482{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012483 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012484}
12485
12486/*
12487 * Return the struct http_res_action_kw associated to a keyword.
12488 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012489struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012490{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012491 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012492}
12493
Willy Tarreau12207b32016-11-22 19:48:51 +010012494
12495/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
12496 * now.
12497 */
12498static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private)
12499{
12500 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
12501 return 1;
12502
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012503 if (*args[2]) {
12504 struct proxy *px;
12505
12506 px = proxy_find_by_name(args[2], 0, 0);
12507 if (px)
12508 appctx->ctx.errors.iid = px->uuid;
12509 else
12510 appctx->ctx.errors.iid = atoi(args[2]);
12511
12512 if (!appctx->ctx.errors.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +020012513 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012514 appctx->ctx.cli.msg = "No such proxy.\n";
12515 appctx->st0 = CLI_ST_PRINT;
12516 return 1;
12517 }
12518 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012519 else
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012520 appctx->ctx.errors.iid = -1; // dump all proxies
12521
Willy Tarreau35069f82016-11-25 09:16:37 +010012522 appctx->ctx.errors.flag = 0;
12523 if (strcmp(args[3], "request") == 0)
12524 appctx->ctx.errors.flag |= 4; // ignore response
12525 else if (strcmp(args[3], "response") == 0)
12526 appctx->ctx.errors.flag |= 2; // ignore request
Willy Tarreau12207b32016-11-22 19:48:51 +010012527 appctx->ctx.errors.px = NULL;
Willy Tarreau12207b32016-11-22 19:48:51 +010012528 return 0;
12529}
12530
12531/* This function dumps all captured errors onto the stream interface's
12532 * read buffer. It returns 0 if the output buffer is full and it needs
12533 * to be called again, otherwise non-zero.
12534 */
12535static int cli_io_handler_show_errors(struct appctx *appctx)
12536{
12537 struct stream_interface *si = appctx->owner;
12538 extern const char *monthname[12];
12539
12540 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
12541 return 1;
12542
12543 chunk_reset(&trash);
12544
12545 if (!appctx->ctx.errors.px) {
12546 /* the function had not been called yet, let's prepare the
12547 * buffer for a response.
12548 */
12549 struct tm tm;
12550
12551 get_localtime(date.tv_sec, &tm);
12552 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
12553 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12554 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
12555 error_snapshot_id);
12556
Willy Tarreau06d80a92017-10-19 14:32:15 +020012557 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012558 /* Socket buffer full. Let's try again later from the same point */
12559 si_applet_cant_put(si);
12560 return 0;
12561 }
12562
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012563 appctx->ctx.errors.px = proxies_list;
Willy Tarreau12207b32016-11-22 19:48:51 +010012564 appctx->ctx.errors.bol = 0;
12565 appctx->ctx.errors.ptr = -1;
12566 }
12567
12568 /* we have two inner loops here, one for the proxy, the other one for
12569 * the buffer.
12570 */
12571 while (appctx->ctx.errors.px) {
12572 struct error_snapshot *es;
12573
Willy Tarreau35069f82016-11-25 09:16:37 +010012574 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012575 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau35069f82016-11-25 09:16:37 +010012576 if (appctx->ctx.errors.flag & 2) // skip req
12577 goto next;
12578 }
12579 else {
Willy Tarreau12207b32016-11-22 19:48:51 +010012580 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau35069f82016-11-25 09:16:37 +010012581 if (appctx->ctx.errors.flag & 4) // skip resp
12582 goto next;
12583 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012584
12585 if (!es->when.tv_sec)
12586 goto next;
12587
12588 if (appctx->ctx.errors.iid >= 0 &&
12589 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
12590 es->oe->uuid != appctx->ctx.errors.iid)
12591 goto next;
12592
12593 if (appctx->ctx.errors.ptr < 0) {
12594 /* just print headers now */
12595
12596 char pn[INET6_ADDRSTRLEN];
12597 struct tm tm;
12598 int port;
12599
12600 get_localtime(es->when.tv_sec, &tm);
12601 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
12602 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12603 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
12604
12605 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
12606 case AF_INET:
12607 case AF_INET6:
12608 port = get_host_port(&es->src);
12609 break;
12610 default:
12611 port = 0;
12612 }
12613
Willy Tarreau35069f82016-11-25 09:16:37 +010012614 switch (appctx->ctx.errors.flag & 1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012615 case 0:
12616 chunk_appendf(&trash,
12617 " frontend %s (#%d): invalid request\n"
12618 " backend %s (#%d)",
12619 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12620 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
12621 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
12622 break;
12623 case 1:
12624 chunk_appendf(&trash,
12625 " backend %s (#%d): invalid response\n"
12626 " frontend %s (#%d)",
12627 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12628 es->oe->id, es->oe->uuid);
12629 break;
12630 }
12631
12632 chunk_appendf(&trash,
12633 ", server %s (#%d), event #%u\n"
12634 " src %s:%d, session #%d, session flags 0x%08x\n"
Willy Tarreau10e61cb2017-01-04 14:51:22 +010012635 " HTTP msg state %s(%d), msg flags 0x%08x, tx flags 0x%08x\n"
Willy Tarreau12207b32016-11-22 19:48:51 +010012636 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
12637 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
12638 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
12639 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
12640 es->ev_id,
12641 pn, port, es->sid, es->s_flags,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020012642 h1_msg_state_str(es->state), es->state, es->m_flags, es->t_flags,
Willy Tarreau12207b32016-11-22 19:48:51 +010012643 es->m_clen, es->m_blen,
12644 es->b_flags, es->b_out, es->b_tot,
12645 es->len, es->b_wrap, es->pos);
12646
Willy Tarreau06d80a92017-10-19 14:32:15 +020012647 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012648 /* Socket buffer full. Let's try again later from the same point */
12649 si_applet_cant_put(si);
12650 return 0;
12651 }
12652 appctx->ctx.errors.ptr = 0;
12653 appctx->ctx.errors.sid = es->sid;
12654 }
12655
12656 if (appctx->ctx.errors.sid != es->sid) {
12657 /* the snapshot changed while we were dumping it */
12658 chunk_appendf(&trash,
12659 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +020012660 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012661 si_applet_cant_put(si);
12662 return 0;
12663 }
12664 goto next;
12665 }
12666
12667 /* OK, ptr >= 0, so we have to dump the current line */
12668 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
12669 int newptr;
12670 int newline;
12671
12672 newline = appctx->ctx.errors.bol;
12673 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
12674 if (newptr == appctx->ctx.errors.ptr)
12675 return 0;
12676
Willy Tarreau06d80a92017-10-19 14:32:15 +020012677 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012678 /* Socket buffer full. Let's try again later from the same point */
12679 si_applet_cant_put(si);
12680 return 0;
12681 }
12682 appctx->ctx.errors.ptr = newptr;
12683 appctx->ctx.errors.bol = newline;
12684 };
12685 next:
12686 appctx->ctx.errors.bol = 0;
12687 appctx->ctx.errors.ptr = -1;
Willy Tarreau35069f82016-11-25 09:16:37 +010012688 appctx->ctx.errors.flag ^= 1;
12689 if (!(appctx->ctx.errors.flag & 1))
Willy Tarreau12207b32016-11-22 19:48:51 +010012690 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau12207b32016-11-22 19:48:51 +010012691 }
12692
12693 /* dump complete */
12694 return 1;
12695}
12696
12697/* register cli keywords */
12698static struct cli_kw_list cli_kws = {{ },{
12699 { { "show", "errors", NULL },
12700 "show errors : report last request and response errors for each proxy",
12701 cli_parse_show_errors, cli_io_handler_show_errors, NULL,
12702 },
12703 {{},}
12704}};
12705
Willy Tarreau4a568972010-05-12 08:08:50 +020012706/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012707/* All supported ACL keywords must be declared here. */
12708/************************************************************************/
12709
12710/* Note: must not be declared <const> as its list will be overwritten.
12711 * Please take care of keeping this list alphabetically sorted.
12712 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012713static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012714 { "base", "base", PAT_MATCH_STR },
12715 { "base_beg", "base", PAT_MATCH_BEG },
12716 { "base_dir", "base", PAT_MATCH_DIR },
12717 { "base_dom", "base", PAT_MATCH_DOM },
12718 { "base_end", "base", PAT_MATCH_END },
12719 { "base_len", "base", PAT_MATCH_LEN },
12720 { "base_reg", "base", PAT_MATCH_REG },
12721 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012722
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012723 { "cook", "req.cook", PAT_MATCH_STR },
12724 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12725 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12726 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12727 { "cook_end", "req.cook", PAT_MATCH_END },
12728 { "cook_len", "req.cook", PAT_MATCH_LEN },
12729 { "cook_reg", "req.cook", PAT_MATCH_REG },
12730 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012731
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012732 { "hdr", "req.hdr", PAT_MATCH_STR },
12733 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12734 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12735 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12736 { "hdr_end", "req.hdr", PAT_MATCH_END },
12737 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12738 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12739 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012740
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012741 /* these two declarations uses strings with list storage (in place
12742 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12743 * and delete functions are relative to the list management. The parse
12744 * and match method are related to the corresponding fetch methods. This
12745 * is very particular ACL declaration mode.
12746 */
12747 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12748 { "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 +020012749
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012750 { "path", "path", PAT_MATCH_STR },
12751 { "path_beg", "path", PAT_MATCH_BEG },
12752 { "path_dir", "path", PAT_MATCH_DIR },
12753 { "path_dom", "path", PAT_MATCH_DOM },
12754 { "path_end", "path", PAT_MATCH_END },
12755 { "path_len", "path", PAT_MATCH_LEN },
12756 { "path_reg", "path", PAT_MATCH_REG },
12757 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012758
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012759 { "req_ver", "req.ver", PAT_MATCH_STR },
12760 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012761
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012762 { "scook", "res.cook", PAT_MATCH_STR },
12763 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12764 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12765 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12766 { "scook_end", "res.cook", PAT_MATCH_END },
12767 { "scook_len", "res.cook", PAT_MATCH_LEN },
12768 { "scook_reg", "res.cook", PAT_MATCH_REG },
12769 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012770
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012771 { "shdr", "res.hdr", PAT_MATCH_STR },
12772 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12773 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12774 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12775 { "shdr_end", "res.hdr", PAT_MATCH_END },
12776 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12777 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12778 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012779
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012780 { "url", "url", PAT_MATCH_STR },
12781 { "url_beg", "url", PAT_MATCH_BEG },
12782 { "url_dir", "url", PAT_MATCH_DIR },
12783 { "url_dom", "url", PAT_MATCH_DOM },
12784 { "url_end", "url", PAT_MATCH_END },
12785 { "url_len", "url", PAT_MATCH_LEN },
12786 { "url_reg", "url", PAT_MATCH_REG },
12787 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012788
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012789 { "urlp", "urlp", PAT_MATCH_STR },
12790 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12791 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12792 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12793 { "urlp_end", "urlp", PAT_MATCH_END },
12794 { "urlp_len", "urlp", PAT_MATCH_LEN },
12795 { "urlp_reg", "urlp", PAT_MATCH_REG },
12796 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012797
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012798 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012799}};
12800
12801/************************************************************************/
12802/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012803/************************************************************************/
12804/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012805static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012806 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012807 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012808 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12809
Willy Tarreau87b09662015-04-03 00:22:06 +020012810 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012811 { "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 +020012812
12813 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012814 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12815 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12816 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012817
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012818 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12819 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012820
Willy Tarreau409bcde2013-01-08 00:31:00 +010012821 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12822 * are only here to match the ACL's name, are request-only and are used
12823 * for ACL compatibility only.
12824 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012825 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12826 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012827 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12828 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012829
12830 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12831 * only here to match the ACL's name, are request-only and are used for
12832 * ACL compatibility only.
12833 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012834 { "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 +020012835 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012836 { "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 +020012837 { "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 +010012838
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012839 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012840 { "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 +010012841 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012842 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012843 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012844 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012845
12846 /* HTTP protocol on the request path */
12847 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012848 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012849
12850 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012851 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12852 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012853
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012854 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012855 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12856 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012857 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012858
Thierry FOURNIERd7d88812017-04-19 15:15:14 +020012859 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER5617dce2017-04-09 05:38:19 +020012860 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12861
Willy Tarreau18ed2562013-01-14 15:56:36 +010012862 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012863 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12864 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012865
Willy Tarreau18ed2562013-01-14 15:56:36 +010012866 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012867 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012868 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12869 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012870
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012871 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012872 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012873 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012874 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012875 { "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 +010012876 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012877 { "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 +010012878
12879 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012880 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012881 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12882 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012883
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012884 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012885 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012886 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012887 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012888 { "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 +010012889 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012890 { "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 +010012891
Willy Tarreau409bcde2013-01-08 00:31:00 +010012892 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012893 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012894 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12895 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012896 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012897
12898 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012899 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012900 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012901 { "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 +020012902 { "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 +010012903
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012904 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020012905 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012906 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012907 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012908 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012909 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012910 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020012911 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12912 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012913 { "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 +010012914 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012915}};
12916
Willy Tarreau8797c062007-05-07 00:55:35 +020012917
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012918/************************************************************************/
12919/* All supported converter keywords must be declared here. */
12920/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012921/* Note: must not be declared <const> as its list will be overwritten */
12922static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012923 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020012924 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012925 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
12926 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012927 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012928 { NULL, NULL, 0, 0, 0 },
12929}};
12930
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012931
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012932/************************************************************************/
12933/* All supported http-request action keywords must be declared here. */
12934/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012935struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012936 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020012937 { "capture", parse_http_req_capture },
Willy Tarreau53275e82017-11-24 07:52:01 +010012938 { "reject", parse_http_action_reject },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012939 { "set-method", parse_set_req_line },
12940 { "set-path", parse_set_req_line },
12941 { "set-query", parse_set_req_line },
12942 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012943 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012944 }
12945};
12946
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012947struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012948 .kw = {
12949 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012950 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012951 { NULL, NULL }
12952 }
12953};
12954
Willy Tarreau8797c062007-05-07 00:55:35 +020012955__attribute__((constructor))
12956static void __http_protocol_init(void)
12957{
12958 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012959 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012960 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012961 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012962 http_res_keywords_register(&http_res_actions);
Willy Tarreau12207b32016-11-22 19:48:51 +010012963 cli_register_kw(&cli_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020012964}
12965
12966
Willy Tarreau58f10d72006-12-04 02:26:12 +010012967/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012968 * Local variables:
12969 * c-indent-level: 8
12970 * c-basic-offset: 8
12971 * End:
12972 */