blob: 05731793926882db42afa1adae01ec9753e7a0c1 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010028#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020029#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/compat.h>
31#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010032#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020033#include <common/memory.h>
34#include <common/mini-clist.h>
35#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020036#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020037#include <common/time.h>
38#include <common/uri_auth.h>
39#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
41#include <types/capture.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010042#include <types/cli.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020043#include <types/filters.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044#include <types/global.h>
William Lallemand9ed62032016-11-21 17:49:11 +010045#include <types/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020046
Willy Tarreau8797c062007-05-07 00:55:35 +020047#include <proto/acl.h>
Thierry FOURNIER322a1242015-08-19 09:07:47 +020048#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020049#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010050#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020051#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020052#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010053#include <proto/checks.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010054#include <proto/cli.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020055#include <proto/compression.h>
William Lallemand9ed62032016-11-21 17:49:11 +010056#include <proto/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020057#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020058#include <proto/filters.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020059#include <proto/frontend.h>
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020060#include <proto/h1.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010062#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010063#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020064#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020065#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010066#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020067#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020068#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010069#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020070#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010071#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020072#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020073#include <proto/pattern.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020074#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020075
Willy Tarreau522d6c02009-12-06 18:49:18 +010076const char HTTP_100[] =
77 "HTTP/1.1 100 Continue\r\n\r\n";
78
79const struct chunk http_100_chunk = {
80 .str = (char *)&HTTP_100,
81 .len = sizeof(HTTP_100)-1
82};
83
Willy Tarreaua9679ac2010-01-03 17:32:57 +010084/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020085const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010086 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010087 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020088 "Location: "; /* not terminated since it will be concatenated with the URL */
89
Willy Tarreau0f772532006-12-23 20:51:41 +010090const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010091 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010092 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010093 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010094 "Location: "; /* not terminated since it will be concatenated with the URL */
95
96/* same as 302 except that the browser MUST retry with the GET method */
97const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010098 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010099 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +0100100 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100101 "Location: "; /* not terminated since it will be concatenated with the URL */
102
Yves Lafon3e8d1ae2013-03-11 11:06:05 -0400103
104/* same as 302 except that the browser MUST retry with the same method */
105const char *HTTP_307 =
106 "HTTP/1.1 307 Temporary Redirect\r\n"
107 "Cache-Control: no-cache\r\n"
108 "Content-length: 0\r\n"
109 "Location: "; /* not terminated since it will be concatenated with the URL */
110
111/* same as 301 except that the browser MUST retry with the same method */
112const char *HTTP_308 =
113 "HTTP/1.1 308 Permanent Redirect\r\n"
114 "Content-length: 0\r\n"
115 "Location: "; /* not terminated since it will be concatenated with the URL */
116
Willy Tarreaubaaee002006-06-26 02:48:02 +0200117/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
118const char *HTTP_401_fmt =
119 "HTTP/1.0 401 Unauthorized\r\n"
120 "Cache-Control: no-cache\r\n"
121 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200122 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200123 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
124 "\r\n"
125 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
126
Willy Tarreau844a7e72010-01-31 21:46:18 +0100127const char *HTTP_407_fmt =
128 "HTTP/1.0 407 Unauthorized\r\n"
129 "Cache-Control: no-cache\r\n"
130 "Connection: close\r\n"
131 "Content-Type: text/html\r\n"
132 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
133 "\r\n"
Godbach1f1fae62014-12-17 16:32:05 +0800134 "<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 +0100135
Willy Tarreau0f772532006-12-23 20:51:41 +0100136
137const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200138 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100139 [HTTP_ERR_400] = 400,
140 [HTTP_ERR_403] = 403,
CJ Ess108b1dd2015-04-07 12:03:37 -0400141 [HTTP_ERR_405] = 405,
Willy Tarreau0f772532006-12-23 20:51:41 +0100142 [HTTP_ERR_408] = 408,
Olivier Houchard51a76d82017-10-02 16:12:07 +0200143 [HTTP_ERR_425] = 425,
CJ Ess108b1dd2015-04-07 12:03:37 -0400144 [HTTP_ERR_429] = 429,
Willy Tarreau0f772532006-12-23 20:51:41 +0100145 [HTTP_ERR_500] = 500,
146 [HTTP_ERR_502] = 502,
147 [HTTP_ERR_503] = 503,
148 [HTTP_ERR_504] = 504,
149};
150
Willy Tarreau80587432006-12-24 17:47:20 +0100151static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200152 [HTTP_ERR_200] =
153 "HTTP/1.0 200 OK\r\n"
154 "Cache-Control: no-cache\r\n"
155 "Connection: close\r\n"
156 "Content-Type: text/html\r\n"
157 "\r\n"
158 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
159
Willy Tarreau0f772532006-12-23 20:51:41 +0100160 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100161 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100162 "Cache-Control: no-cache\r\n"
163 "Connection: close\r\n"
164 "Content-Type: text/html\r\n"
165 "\r\n"
166 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
167
168 [HTTP_ERR_403] =
169 "HTTP/1.0 403 Forbidden\r\n"
170 "Cache-Control: no-cache\r\n"
171 "Connection: close\r\n"
172 "Content-Type: text/html\r\n"
173 "\r\n"
174 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
175
CJ Ess108b1dd2015-04-07 12:03:37 -0400176 [HTTP_ERR_405] =
177 "HTTP/1.0 405 Method Not Allowed\r\n"
178 "Cache-Control: no-cache\r\n"
179 "Connection: close\r\n"
180 "Content-Type: text/html\r\n"
181 "\r\n"
182 "<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",
183
Willy Tarreau0f772532006-12-23 20:51:41 +0100184 [HTTP_ERR_408] =
185 "HTTP/1.0 408 Request Time-out\r\n"
186 "Cache-Control: no-cache\r\n"
187 "Connection: close\r\n"
188 "Content-Type: text/html\r\n"
189 "\r\n"
190 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
191
Olivier Houchard51a76d82017-10-02 16:12:07 +0200192 [HTTP_ERR_425] =
193 "HTTP/1.0 425 Too Early\r\n"
194 "Cache-Control: no-cache\r\n"
195 "Connection: close\r\n"
196 "Content-Type: text/html\r\n"
197 "\r\n"
198 "<html><body><h1>425 Too Early</h1>\nYour browser sent early data.\n</body></html>\n",
199
CJ Ess108b1dd2015-04-07 12:03:37 -0400200 [HTTP_ERR_429] =
201 "HTTP/1.0 429 Too Many Requests\r\n"
202 "Cache-Control: no-cache\r\n"
203 "Connection: close\r\n"
204 "Content-Type: text/html\r\n"
205 "\r\n"
206 "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n",
207
Willy Tarreau0f772532006-12-23 20:51:41 +0100208 [HTTP_ERR_500] =
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200209 "HTTP/1.0 500 Internal Server Error\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100210 "Cache-Control: no-cache\r\n"
211 "Connection: close\r\n"
212 "Content-Type: text/html\r\n"
213 "\r\n"
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200214 "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
Willy Tarreau0f772532006-12-23 20:51:41 +0100215
216 [HTTP_ERR_502] =
217 "HTTP/1.0 502 Bad Gateway\r\n"
218 "Cache-Control: no-cache\r\n"
219 "Connection: close\r\n"
220 "Content-Type: text/html\r\n"
221 "\r\n"
222 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
223
224 [HTTP_ERR_503] =
225 "HTTP/1.0 503 Service Unavailable\r\n"
226 "Cache-Control: no-cache\r\n"
227 "Connection: close\r\n"
228 "Content-Type: text/html\r\n"
229 "\r\n"
230 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
231
232 [HTTP_ERR_504] =
233 "HTTP/1.0 504 Gateway Time-out\r\n"
234 "Cache-Control: no-cache\r\n"
235 "Connection: close\r\n"
236 "Content-Type: text/html\r\n"
237 "\r\n"
238 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
239
240};
241
Cyril Bonté19979e12012-04-04 12:57:21 +0200242/* status codes available for the stats admin page (strictly 4 chars length) */
243const char *stat_status_codes[STAT_STATUS_SIZE] = {
244 [STAT_STATUS_DENY] = "DENY",
245 [STAT_STATUS_DONE] = "DONE",
246 [STAT_STATUS_ERRP] = "ERRP",
247 [STAT_STATUS_EXCD] = "EXCD",
248 [STAT_STATUS_NONE] = "NONE",
249 [STAT_STATUS_PART] = "PART",
250 [STAT_STATUS_UNKN] = "UNKN",
251};
252
253
William Lallemand73025dd2014-04-24 14:38:37 +0200254/* List head of all known action keywords for "http-request" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200255struct action_kw_list http_req_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200256 .list = LIST_HEAD_INIT(http_req_keywords.list)
257};
258
259/* List head of all known action keywords for "http-response" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200260struct action_kw_list http_res_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200261 .list = LIST_HEAD_INIT(http_res_keywords.list)
262};
263
Willy Tarreau80587432006-12-24 17:47:20 +0100264/* We must put the messages here since GCC cannot initialize consts depending
265 * on strlen().
266 */
267struct chunk http_err_chunks[HTTP_ERR_SIZE];
268
Willy Tarreaua890d072013-04-02 12:01:06 +0200269/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100270static THREAD_LOCAL struct hdr_ctx static_hdr_ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +0200271
Willy Tarreau42250582007-04-01 01:30:43 +0200272#define FD_SETS_ARE_BITFIELDS
273#ifdef FD_SETS_ARE_BITFIELDS
274/*
275 * This map is used with all the FD_* macros to check whether a particular bit
276 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
277 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
278 * byte should be encoded. Be careful to always pass bytes from 0 to 255
279 * exclusively to the macros.
280 */
281fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
282fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100283fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200284
285#else
286#error "Check if your OS uses bitfields for fd_sets"
287#endif
288
Willy Tarreau87b09662015-04-03 00:22:06 +0200289static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn);
Willy Tarreau0b748332014-04-29 00:13:29 +0200290
David Carlier7365f7d2016-04-04 11:54:42 +0100291static inline int http_msg_forward_body(struct stream *s, struct http_msg *msg);
292static inline int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +0100293
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200294/* This function returns a reason associated with the HTTP status.
295 * This function never fails, a message is always returned.
296 */
297const char *get_reason(unsigned int status)
298{
299 switch (status) {
300 case 100: return "Continue";
301 case 101: return "Switching Protocols";
302 case 102: return "Processing";
303 case 200: return "OK";
304 case 201: return "Created";
305 case 202: return "Accepted";
306 case 203: return "Non-Authoritative Information";
307 case 204: return "No Content";
308 case 205: return "Reset Content";
309 case 206: return "Partial Content";
310 case 207: return "Multi-Status";
311 case 210: return "Content Different";
312 case 226: return "IM Used";
313 case 300: return "Multiple Choices";
314 case 301: return "Moved Permanently";
315 case 302: return "Moved Temporarily";
316 case 303: return "See Other";
317 case 304: return "Not Modified";
318 case 305: return "Use Proxy";
319 case 307: return "Temporary Redirect";
320 case 308: return "Permanent Redirect";
321 case 310: return "Too many Redirects";
322 case 400: return "Bad Request";
323 case 401: return "Unauthorized";
324 case 402: return "Payment Required";
325 case 403: return "Forbidden";
326 case 404: return "Not Found";
327 case 405: return "Method Not Allowed";
328 case 406: return "Not Acceptable";
329 case 407: return "Proxy Authentication Required";
330 case 408: return "Request Time-out";
331 case 409: return "Conflict";
332 case 410: return "Gone";
333 case 411: return "Length Required";
334 case 412: return "Precondition Failed";
335 case 413: return "Request Entity Too Large";
336 case 414: return "Request-URI Too Long";
337 case 415: return "Unsupported Media Type";
338 case 416: return "Requested range unsatisfiable";
339 case 417: return "Expectation failed";
340 case 418: return "I'm a teapot";
341 case 422: return "Unprocessable entity";
342 case 423: return "Locked";
343 case 424: return "Method failure";
Olivier Houchard51a76d82017-10-02 16:12:07 +0200344 case 425: return "Too Early";
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200345 case 426: return "Upgrade Required";
346 case 428: return "Precondition Required";
347 case 429: return "Too Many Requests";
348 case 431: return "Request Header Fields Too Large";
349 case 449: return "Retry With";
350 case 450: return "Blocked by Windows Parental Controls";
351 case 451: return "Unavailable For Legal Reasons";
352 case 456: return "Unrecoverable Error";
353 case 499: return "client has closed connection";
354 case 500: return "Internal Server Error";
355 case 501: return "Not Implemented";
Jarno Huuskonen59af2df2016-12-28 10:49:01 +0200356 case 502: return "Bad Gateway or Proxy Error";
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200357 case 503: return "Service Unavailable";
358 case 504: return "Gateway Time-out";
359 case 505: return "HTTP Version not supported";
360 case 506: return "Variant also negociate";
361 case 507: return "Insufficient storage";
362 case 508: return "Loop detected";
363 case 509: return "Bandwidth Limit Exceeded";
364 case 510: return "Not extended";
365 case 511: return "Network authentication required";
366 case 520: return "Web server is returning an unknown error";
367 default:
368 switch (status) {
369 case 100 ... 199: return "Informational";
370 case 200 ... 299: return "Success";
371 case 300 ... 399: return "Redirection";
372 case 400 ... 499: return "Client Error";
373 case 500 ... 599: return "Server Error";
374 default: return "Other";
375 }
376 }
377}
378
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200379/* This function returns HTTP_ERR_<num> (enum) matching http status code.
380 * Returned value should match codes from http_err_codes.
381 */
382static const int http_get_status_idx(unsigned int status)
383{
384 switch (status) {
385 case 200: return HTTP_ERR_200;
386 case 400: return HTTP_ERR_400;
387 case 403: return HTTP_ERR_403;
388 case 405: return HTTP_ERR_405;
389 case 408: return HTTP_ERR_408;
Olivier Houchard51a76d82017-10-02 16:12:07 +0200390 case 425: return HTTP_ERR_425;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200391 case 429: return HTTP_ERR_429;
392 case 500: return HTTP_ERR_500;
393 case 502: return HTTP_ERR_502;
394 case 503: return HTTP_ERR_503;
395 case 504: return HTTP_ERR_504;
396 default: return HTTP_ERR_500;
397 }
398}
399
Willy Tarreau80587432006-12-24 17:47:20 +0100400void init_proto_http()
401{
Willy Tarreau42250582007-04-01 01:30:43 +0200402 int i;
403 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100404 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200405
Willy Tarreau80587432006-12-24 17:47:20 +0100406 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
407 if (!http_err_msgs[msg]) {
408 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
409 abort();
410 }
411
412 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
413 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
414 }
Willy Tarreau42250582007-04-01 01:30:43 +0200415
416 /* initialize the log header encoding map : '{|}"#' should be encoded with
417 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
418 * URL encoding only requires '"', '#' to be encoded as well as non-
419 * printable characters above.
420 */
421 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
422 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100423 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200424 for (i = 0; i < 32; i++) {
425 FD_SET(i, hdr_encode_map);
426 FD_SET(i, url_encode_map);
427 }
428 for (i = 127; i < 256; i++) {
429 FD_SET(i, hdr_encode_map);
430 FD_SET(i, url_encode_map);
431 }
432
433 tmp = "\"#{|}";
434 while (*tmp) {
435 FD_SET(*tmp, hdr_encode_map);
436 tmp++;
437 }
438
439 tmp = "\"#";
440 while (*tmp) {
441 FD_SET(*tmp, url_encode_map);
442 tmp++;
443 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200444
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100445 /* initialize the http header encoding map. The draft httpbis define the
446 * header content as:
447 *
448 * HTTP-message = start-line
449 * *( header-field CRLF )
450 * CRLF
451 * [ message-body ]
452 * header-field = field-name ":" OWS field-value OWS
453 * field-value = *( field-content / obs-fold )
454 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
455 * obs-fold = CRLF 1*( SP / HTAB )
456 * field-vchar = VCHAR / obs-text
457 * VCHAR = %x21-7E
458 * obs-text = %x80-FF
459 *
460 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
461 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
462 * "obs-fold" is volontary forgotten because haproxy remove this.
463 */
464 memset(http_encode_map, 0, sizeof(http_encode_map));
465 for (i = 0x00; i <= 0x08; i++)
466 FD_SET(i, http_encode_map);
467 for (i = 0x0a; i <= 0x1f; i++)
468 FD_SET(i, http_encode_map);
469 FD_SET(0x7f, http_encode_map);
470
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200471 /* memory allocations */
Willy Tarreau63986c72015-04-03 22:55:33 +0200472 pool2_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100473 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100474}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200475
Willy Tarreau53b6c742006-12-17 13:37:46 +0100476/*
477 * We have 26 list of methods (1 per first letter), each of which can have
478 * up to 3 entries (2 valid, 1 null).
479 */
480struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100481 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100482 int len;
483 const char text[8];
484};
485
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100486const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100487 ['C' - 'A'] = {
488 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
489 },
490 ['D' - 'A'] = {
491 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
492 },
493 ['G' - 'A'] = {
494 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
495 },
496 ['H' - 'A'] = {
497 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
498 },
Christopher Fauletd57ad642015-07-31 14:26:57 +0200499 ['O' - 'A'] = {
500 [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" },
501 },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100502 ['P' - 'A'] = {
503 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
504 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
505 },
506 ['T' - 'A'] = {
507 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
508 },
509 /* rest is empty like this :
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200510 * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100511 */
512};
513
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100514const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100515 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
516 [HTTP_METH_GET] = { "GET", 3 },
517 [HTTP_METH_HEAD] = { "HEAD", 4 },
518 [HTTP_METH_POST] = { "POST", 4 },
519 [HTTP_METH_PUT] = { "PUT", 3 },
520 [HTTP_METH_DELETE] = { "DELETE", 6 },
521 [HTTP_METH_TRACE] = { "TRACE", 5 },
522 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
523};
524
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100525/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100526 * Adds a header and its CRLF at the tail of the message's buffer, just before
527 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100528 * The header is also automatically added to the index <hdr_idx>, and the end
529 * of headers is automatically adjusted. The number of bytes added is returned
530 * on success, otherwise <0 is returned indicating an error.
531 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100532int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100533{
534 int bytes, len;
535
536 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200537 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100538 if (!bytes)
539 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100540 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100541 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
542}
543
544/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100545 * Adds a header and its CRLF at the tail of the message's buffer, just before
546 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100547 * the buffer is only opened and the space reserved, but nothing is copied.
548 * The header is also automatically added to the index <hdr_idx>, and the end
549 * of headers is automatically adjusted. The number of bytes added is returned
550 * on success, otherwise <0 is returned indicating an error.
551 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100552int http_header_add_tail2(struct http_msg *msg,
553 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100554{
555 int bytes;
556
Willy Tarreau9b28e032012-10-12 23:49:43 +0200557 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100558 if (!bytes)
559 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100560 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100561 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
562}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200563
564/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100565 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
566 * If so, returns the position of the first non-space character relative to
567 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
568 * to return a pointer to the place after the first space. Returns 0 if the
569 * header name does not match. Checks are case-insensitive.
570 */
571int http_header_match2(const char *hdr, const char *end,
572 const char *name, int len)
573{
574 const char *val;
575
576 if (hdr + len >= end)
577 return 0;
578 if (hdr[len] != ':')
579 return 0;
580 if (strncasecmp(hdr, name, len) != 0)
581 return 0;
582 val = hdr + len + 1;
583 while (val < end && HTTP_IS_SPHT(*val))
584 val++;
585 if ((val >= end) && (len + 2 <= end - hdr))
586 return len + 2; /* we may replace starting from second space */
587 return val - hdr;
588}
589
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200590/* Find the first or next occurrence of header <name> in message buffer <sol>
591 * using headers index <idx>, and return it in the <ctx> structure. This
592 * structure holds everything necessary to use the header and find next
593 * occurrence. If its <idx> member is 0, the header is searched from the
594 * beginning. Otherwise, the next occurrence is returned. The function returns
595 * 1 when it finds a value, and 0 when there is no more. It is very similar to
596 * http_find_header2() except that it is designed to work with full-line headers
597 * whose comma is not a delimiter but is part of the syntax. As a special case,
598 * if ctx->val is NULL when searching for a new values of a header, the current
599 * header is rescanned. This allows rescanning after a header deletion.
600 */
601int http_find_full_header2(const char *name, int len,
602 char *sol, struct hdr_idx *idx,
603 struct hdr_ctx *ctx)
604{
605 char *eol, *sov;
606 int cur_idx, old_idx;
607
608 cur_idx = ctx->idx;
609 if (cur_idx) {
610 /* We have previously returned a header, let's search another one */
611 sol = ctx->line;
612 eol = sol + idx->v[cur_idx].len;
613 goto next_hdr;
614 }
615
616 /* first request for this header */
617 sol += hdr_idx_first_pos(idx);
618 old_idx = 0;
619 cur_idx = hdr_idx_first_idx(idx);
620 while (cur_idx) {
621 eol = sol + idx->v[cur_idx].len;
622
623 if (len == 0) {
624 /* No argument was passed, we want any header.
625 * To achieve this, we simply build a fake request. */
626 while (sol + len < eol && sol[len] != ':')
627 len++;
628 name = sol;
629 }
630
631 if ((len < eol - sol) &&
632 (sol[len] == ':') &&
633 (strncasecmp(sol, name, len) == 0)) {
634 ctx->del = len;
635 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100636 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200637 sov++;
638
639 ctx->line = sol;
640 ctx->prev = old_idx;
641 ctx->idx = cur_idx;
642 ctx->val = sov - sol;
643 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100644 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200645 eol--;
646 ctx->tws++;
647 }
648 ctx->vlen = eol - sov;
649 return 1;
650 }
651 next_hdr:
652 sol = eol + idx->v[cur_idx].cr + 1;
653 old_idx = cur_idx;
654 cur_idx = idx->v[cur_idx].next;
655 }
656 return 0;
657}
658
Willy Tarreauc90dc232015-02-20 13:51:36 +0100659/* Find the first or next header field in message buffer <sol> using headers
660 * index <idx>, and return it in the <ctx> structure. This structure holds
661 * everything necessary to use the header and find next occurrence. If its
662 * <idx> member is 0, the first header is retrieved. Otherwise, the next
663 * occurrence is returned. The function returns 1 when it finds a value, and
664 * 0 when there is no more. It is equivalent to http_find_full_header2() with
665 * no header name.
666 */
667int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
668{
669 char *eol, *sov;
670 int cur_idx, old_idx;
671 int len;
672
673 cur_idx = ctx->idx;
674 if (cur_idx) {
675 /* We have previously returned a header, let's search another one */
676 sol = ctx->line;
677 eol = sol + idx->v[cur_idx].len;
678 goto next_hdr;
679 }
680
681 /* first request for this header */
682 sol += hdr_idx_first_pos(idx);
683 old_idx = 0;
684 cur_idx = hdr_idx_first_idx(idx);
685 while (cur_idx) {
686 eol = sol + idx->v[cur_idx].len;
687
688 len = 0;
689 while (1) {
690 if (len >= eol - sol)
691 goto next_hdr;
692 if (sol[len] == ':')
693 break;
694 len++;
695 }
696
697 ctx->del = len;
698 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100699 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreauc90dc232015-02-20 13:51:36 +0100700 sov++;
701
702 ctx->line = sol;
703 ctx->prev = old_idx;
704 ctx->idx = cur_idx;
705 ctx->val = sov - sol;
706 ctx->tws = 0;
707
Willy Tarreau2235b262016-11-05 15:50:20 +0100708 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreauc90dc232015-02-20 13:51:36 +0100709 eol--;
710 ctx->tws++;
711 }
712 ctx->vlen = eol - sov;
713 return 1;
714
715 next_hdr:
716 sol = eol + idx->v[cur_idx].cr + 1;
717 old_idx = cur_idx;
718 cur_idx = idx->v[cur_idx].next;
719 }
720 return 0;
721}
722
Lukas Tribus23953682017-04-28 13:24:30 +0000723/* Find the end of the header value contained between <s> and <e>. See RFC7230,
724 * par 3.2 for more information. Note that it requires a valid header to return
Willy Tarreau68085d82010-01-18 14:54:04 +0100725 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200726 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100727char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200728{
729 int quoted, qdpair;
730
731 quoted = qdpair = 0;
Willy Tarreaue6d9c212016-11-05 18:23:38 +0100732
733#if defined(__x86_64__) || \
734 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
735 defined(__ARM_ARCH_7A__)
736 /* speedup: skip everything not a comma nor a double quote */
737 for (; s <= e - sizeof(int); s += sizeof(int)) {
738 unsigned int c = *(int *)s; // comma
739 unsigned int q = c; // quote
740
741 c ^= 0x2c2c2c2c; // contains one zero on a comma
742 q ^= 0x22222222; // contains one zero on a quote
743
744 c = (c - 0x01010101) & ~c; // contains 0x80 below a comma
745 q = (q - 0x01010101) & ~q; // contains 0x80 below a quote
746
747 if ((c | q) & 0x80808080)
748 break; // found a comma or a quote
749 }
750#endif
Willy Tarreau33a7e692007-06-10 19:45:56 +0200751 for (; s < e; s++) {
752 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200753 else if (quoted) {
754 if (*s == '\\') qdpair = 1;
755 else if (*s == '"') quoted = 0;
756 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200757 else if (*s == '"') quoted = 1;
758 else if (*s == ',') return s;
759 }
760 return s;
761}
762
763/* Find the first or next occurrence of header <name> in message buffer <sol>
764 * using headers index <idx>, and return it in the <ctx> structure. This
765 * structure holds everything necessary to use the header and find next
766 * occurrence. If its <idx> member is 0, the header is searched from the
767 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100768 * 1 when it finds a value, and 0 when there is no more. It is designed to work
769 * with headers defined as comma-separated lists. As a special case, if ctx->val
770 * is NULL when searching for a new values of a header, the current header is
771 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200772 */
773int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100774 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200775 struct hdr_ctx *ctx)
776{
Willy Tarreau68085d82010-01-18 14:54:04 +0100777 char *eol, *sov;
778 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200779
Willy Tarreau68085d82010-01-18 14:54:04 +0100780 cur_idx = ctx->idx;
781 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200782 /* We have previously returned a value, let's search
783 * another one on the same line.
784 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200785 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200786 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100787 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200788 eol = sol + idx->v[cur_idx].len;
789
790 if (sov >= eol)
791 /* no more values in this header */
792 goto next_hdr;
793
Willy Tarreau68085d82010-01-18 14:54:04 +0100794 /* values remaining for this header, skip the comma but save it
795 * for later use (eg: for header deletion).
796 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200797 sov++;
Willy Tarreau2235b262016-11-05 15:50:20 +0100798 while (sov < eol && HTTP_IS_LWS((*sov)))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200799 sov++;
800
801 goto return_hdr;
802 }
803
804 /* first request for this header */
805 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100806 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200807 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200808 while (cur_idx) {
809 eol = sol + idx->v[cur_idx].len;
810
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200811 if (len == 0) {
812 /* No argument was passed, we want any header.
813 * To achieve this, we simply build a fake request. */
814 while (sol + len < eol && sol[len] != ':')
815 len++;
816 name = sol;
817 }
818
Willy Tarreau33a7e692007-06-10 19:45:56 +0200819 if ((len < eol - sol) &&
820 (sol[len] == ':') &&
821 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100822 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200823 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100824 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200825 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100826
Willy Tarreau33a7e692007-06-10 19:45:56 +0200827 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100828 ctx->prev = old_idx;
829 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200830 ctx->idx = cur_idx;
831 ctx->val = sov - sol;
832
833 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200834 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100835 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200836 eol--;
837 ctx->tws++;
838 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200839 ctx->vlen = eol - sov;
840 return 1;
841 }
842 next_hdr:
843 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100844 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200845 cur_idx = idx->v[cur_idx].next;
846 }
847 return 0;
848}
849
850int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100851 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200852 struct hdr_ctx *ctx)
853{
854 return http_find_header2(name, strlen(name), sol, idx, ctx);
855}
856
Willy Tarreau68085d82010-01-18 14:54:04 +0100857/* Remove one value of a header. This only works on a <ctx> returned by one of
858 * the http_find_header functions. The value is removed, as well as surrounding
859 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100860 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100861 * message <msg>. The new index is returned. If it is zero, it means there is
862 * no more header, so any processing may stop. The ctx is always left in a form
863 * that can be handled by http_find_header2() to find next occurrence.
864 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100865int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100866{
867 int cur_idx = ctx->idx;
868 char *sol = ctx->line;
869 struct hdr_idx_elem *hdr;
870 int delta, skip_comma;
871
872 if (!cur_idx)
873 return 0;
874
875 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200876 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100877 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200878 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100879 http_msg_move_end(msg, delta);
880 idx->used--;
881 hdr->len = 0; /* unused entry */
882 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100883 if (idx->tail == ctx->idx)
884 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100885 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100886 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100887 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200888 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100889 return ctx->idx;
890 }
891
892 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200893 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
894 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100895 */
896
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200897 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200898 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200899 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100900 NULL, 0);
901 hdr->len += delta;
902 http_msg_move_end(msg, delta);
903 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200904 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100905 return ctx->idx;
906}
907
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100908/* This function handles a server error at the stream interface level. The
909 * stream interface is assumed to be already in a closed state. An optional
Willy Tarreau2019f952017-03-14 11:07:31 +0100910 * message is copied into the input buffer.
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100911 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100912 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200913 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200914static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2019f952017-03-14 11:07:31 +0100915 int err, int finst, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200916{
Willy Tarreau2019f952017-03-14 11:07:31 +0100917 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100918 channel_auto_read(si_oc(si));
919 channel_abort(si_oc(si));
920 channel_auto_close(si_oc(si));
921 channel_erase(si_oc(si));
922 channel_auto_close(si_ic(si));
923 channel_auto_read(si_ic(si));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200924 if (msg)
Willy Tarreau06d80a92017-10-19 14:32:15 +0200925 co_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaue7dff022015-04-03 01:14:29 +0200926 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200927 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200928 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200929 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200930}
931
Willy Tarreau87b09662015-04-03 00:22:06 +0200932/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100933 * and message.
934 */
935
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200936struct chunk *http_error_message(struct stream *s)
Willy Tarreau80587432006-12-24 17:47:20 +0100937{
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200938 const int msgnum = http_get_status_idx(s->txn->status);
939
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200940 if (s->be->errmsg[msgnum].str)
941 return &s->be->errmsg[msgnum];
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200942 else if (strm_fe(s)->errmsg[msgnum].str)
943 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100944 else
945 return &http_err_chunks[msgnum];
946}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200947
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100948void
949http_reply_and_close(struct stream *s, short status, struct chunk *msg)
950{
Christopher Fauletd7c91962015-04-30 11:48:27 +0200951 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +0100952 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100953 stream_int_retnclose(&s->si[0], msg);
954}
955
Willy Tarreau53b6c742006-12-17 13:37:46 +0100956/*
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200957 * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown
958 * ones.
Willy Tarreau53b6c742006-12-17 13:37:46 +0100959 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100960enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100961{
962 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100963 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100964
965 m = ((unsigned)*str - 'A');
966
967 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100968 for (h = http_methods[m]; h->len > 0; h++) {
969 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100970 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100971 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100972 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100973 };
Willy Tarreau53b6c742006-12-17 13:37:46 +0100974 }
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200975 return HTTP_METH_OTHER;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100976}
977
Willy Tarreau21d2af32008-02-14 20:25:24 +0100978/* Parse the URI from the given transaction (which is assumed to be in request
979 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
980 * It is returned otherwise.
981 */
Thierry FOURNIER3c331782015-09-17 19:33:35 +0200982char *http_get_path(struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +0100983{
984 char *ptr, *end;
985
Willy Tarreau9b28e032012-10-12 23:49:43 +0200986 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100987 end = ptr + txn->req.sl.rq.u_l;
988
989 if (ptr >= end)
990 return NULL;
991
Lukas Tribus23953682017-04-28 13:24:30 +0000992 /* RFC7230, par. 2.7 :
Willy Tarreau21d2af32008-02-14 20:25:24 +0100993 * Request-URI = "*" | absuri | abspath | authority
994 */
995
996 if (*ptr == '*')
997 return NULL;
998
999 if (isalpha((unsigned char)*ptr)) {
1000 /* this is a scheme as described by RFC3986, par. 3.1 */
1001 ptr++;
1002 while (ptr < end &&
1003 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
1004 ptr++;
1005 /* skip '://' */
1006 if (ptr == end || *ptr++ != ':')
1007 return NULL;
1008 if (ptr == end || *ptr++ != '/')
1009 return NULL;
1010 if (ptr == end || *ptr++ != '/')
1011 return NULL;
1012 }
1013 /* skip [user[:passwd]@]host[:[port]] */
1014
1015 while (ptr < end && *ptr != '/')
1016 ptr++;
1017
1018 if (ptr == end)
1019 return NULL;
1020
1021 /* OK, we got the '/' ! */
1022 return ptr;
1023}
1024
William Lallemand65ad6e12014-01-31 15:08:02 +01001025/* Parse the URI from the given string and look for the "/" beginning the PATH.
1026 * If not found, return NULL. It is returned otherwise.
1027 */
1028static char *
1029http_get_path_from_string(char *str)
1030{
1031 char *ptr = str;
1032
1033 /* RFC2616, par. 5.1.2 :
1034 * Request-URI = "*" | absuri | abspath | authority
1035 */
1036
1037 if (*ptr == '*')
1038 return NULL;
1039
1040 if (isalpha((unsigned char)*ptr)) {
1041 /* this is a scheme as described by RFC3986, par. 3.1 */
1042 ptr++;
1043 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
1044 ptr++;
1045 /* skip '://' */
1046 if (*ptr == '\0' || *ptr++ != ':')
1047 return NULL;
1048 if (*ptr == '\0' || *ptr++ != '/')
1049 return NULL;
1050 if (*ptr == '\0' || *ptr++ != '/')
1051 return NULL;
1052 }
1053 /* skip [user[:passwd]@]host[:[port]] */
1054
1055 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
1056 ptr++;
1057
1058 if (*ptr == '\0' || *ptr == ' ')
1059 return NULL;
1060
1061 /* OK, we got the '/' ! */
1062 return ptr;
1063}
1064
Willy Tarreau71241ab2012-12-27 11:30:54 +01001065/* Returns a 302 for a redirectable request that reaches a server working in
1066 * in redirect mode. This may only be called just after the stream interface
1067 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
1068 * follow normal proxy processing. NOTE: this function is designed to support
1069 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001070 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001071void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001072{
1073 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001074 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001075 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001076 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001077
1078 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001079 trash.len = strlen(HTTP_302);
1080 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001081
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001082 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001083
Willy Tarreauefb453c2008-10-26 20:49:47 +01001084 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001085 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001086 return;
1087
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001088 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001089 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001090 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
1091 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001092 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001093
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001094 /* 3: add the request URI. Since it was already forwarded, we need
1095 * to temporarily rewind the buffer.
1096 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001097 txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001098 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001099
Willy Tarreauefb453c2008-10-26 20:49:47 +01001100 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001101 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 +02001102
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001103 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001104
Willy Tarreauefb453c2008-10-26 20:49:47 +01001105 if (!path)
1106 return;
1107
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001108 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001109 return;
1110
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001111 memcpy(trash.str + trash.len, path, len);
1112 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001113
1114 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001115 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1116 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001117 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001118 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1119 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001120 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001121
1122 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001123 si_shutr(si);
1124 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001125 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001126 si->state = SI_ST_CLO;
1127
1128 /* send the message */
Willy Tarreau2019f952017-03-14 11:07:31 +01001129 txn->status = 302;
1130 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001131
1132 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001133 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001134 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001135}
1136
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001137/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001138 * that the server side is closed. Note that err_type is actually a
1139 * bitmask, where almost only aborts may be cumulated with other
1140 * values. We consider that aborted operations are more important
1141 * than timeouts or errors due to the fact that nobody else in the
1142 * logs might explain incomplete retries. All others should avoid
1143 * being cumulated. It should normally not be possible to have multiple
1144 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001145 * Note that connection errors appearing on the second request of a keep-alive
1146 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001147 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001148void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001149{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001150 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001151
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001152 /* set s->txn->status for http_error_message(s) */
1153 s->txn->status = 503;
1154
Willy Tarreauefb453c2008-10-26 20:49:47 +01001155 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001156 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001157 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001158 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001159 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001160 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001161 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001162 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001163 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001164 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001165 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001166 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001167 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001168 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001169 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001170 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001171 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001172 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001173 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001174 (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001175 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001176 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001177 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001178 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001179 http_error_message(s));
1180 else { /* SI_ET_CONN_OTHER and others */
1181 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001182 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001183 http_error_message(s));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001184 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001185}
1186
Willy Tarreau42250582007-04-01 01:30:43 +02001187extern const char sess_term_cond[8];
1188extern const char sess_fin_state[8];
1189extern const char *monthname[12];
Willy Tarreau63986c72015-04-03 22:55:33 +02001190struct pool_head *pool2_http_txn;
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001191struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001192struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001193struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001194
Willy Tarreau117f59e2007-03-04 18:17:17 +01001195/*
1196 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001197 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001198 */
1199void capture_headers(char *som, struct hdr_idx *idx,
1200 char **cap, struct cap_hdr *cap_hdr)
1201{
1202 char *eol, *sol, *col, *sov;
1203 int cur_idx;
1204 struct cap_hdr *h;
1205 int len;
1206
1207 sol = som + hdr_idx_first_pos(idx);
1208 cur_idx = hdr_idx_first_idx(idx);
1209
1210 while (cur_idx) {
1211 eol = sol + idx->v[cur_idx].len;
1212
1213 col = sol;
1214 while (col < eol && *col != ':')
1215 col++;
1216
1217 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01001218 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001219 sov++;
1220
1221 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001222 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001223 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1224 if (cap[h->index] == NULL)
1225 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001226 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001227
1228 if (cap[h->index] == NULL) {
1229 Alert("HTTP capture : out of memory.\n");
1230 continue;
1231 }
1232
1233 len = eol - sov;
1234 if (len > h->len)
1235 len = h->len;
1236
1237 memcpy(cap[h->index], sov, len);
1238 cap[h->index][len]=0;
1239 }
1240 }
1241 sol = eol + idx->v[cur_idx].cr + 1;
1242 cur_idx = idx->v[cur_idx].next;
1243 }
1244}
1245
Willy Tarreaubaaee002006-06-26 02:48:02 +02001246/*
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001247 * Returns the data from Authorization header. Function may be called more
1248 * than once so data is stored in txn->auth_data. When no header is found
1249 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001250 * searching again for something we are unable to find anyway. However, if
1251 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001252 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001253 */
1254
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001255int
Willy Tarreau87b09662015-04-03 00:22:06 +02001256get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001257{
1258
Willy Tarreaueee5b512015-04-03 23:46:31 +02001259 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001260 struct chunk auth_method;
1261 struct hdr_ctx ctx;
1262 char *h, *p;
1263 int len;
1264
1265#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001266 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001267#endif
1268
1269 if (txn->auth.method == HTTP_AUTH_WRONG)
1270 return 0;
1271
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001272 txn->auth.method = HTTP_AUTH_WRONG;
1273
1274 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001275
1276 if (txn->flags & TX_USE_PX_CONN) {
1277 h = "Proxy-Authorization";
1278 len = strlen(h);
1279 } else {
1280 h = "Authorization";
1281 len = strlen(h);
1282 }
1283
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001284 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001285 return 0;
1286
1287 h = ctx.line + ctx.val;
1288
1289 p = memchr(h, ' ', ctx.vlen);
Willy Tarreau5c557d12016-03-13 08:17:02 +01001290 len = p - h;
1291 if (!p || len <= 0)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001292 return 0;
1293
David Carlier7365f7d2016-04-04 11:54:42 +01001294 if (chunk_initlen(&auth_method, h, 0, len) != 1)
1295 return 0;
1296
Willy Tarreau5c557d12016-03-13 08:17:02 +01001297 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001298
1299 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
Christopher Faulet6988f672017-07-27 15:18:52 +02001300 struct chunk *http_auth = get_trash_chunk();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001301
1302 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Christopher Faulet6988f672017-07-27 15:18:52 +02001303 http_auth->str, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001304
1305 if (len < 0)
1306 return 0;
1307
1308
Christopher Faulet6988f672017-07-27 15:18:52 +02001309 http_auth->str[len] = '\0';
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001310
Christopher Faulet6988f672017-07-27 15:18:52 +02001311 p = strchr(http_auth->str, ':');
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001312
1313 if (!p)
1314 return 0;
1315
Christopher Faulet6988f672017-07-27 15:18:52 +02001316 txn->auth.user = http_auth->str;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001317 *p = '\0';
1318 txn->auth.pass = p+1;
1319
1320 txn->auth.method = HTTP_AUTH_BASIC;
1321 return 1;
1322 }
1323
1324 return 0;
1325}
1326
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001327
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001328/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1329 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1330 * nothing is done and 1 is returned.
1331 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001332static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001333{
1334 int delta;
1335 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001336 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001337
1338 if (msg->sl.rq.v_l != 0)
1339 return 1;
1340
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001341 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1342 if (txn->meth != HTTP_METH_GET)
1343 return 0;
1344
Willy Tarreau9b28e032012-10-12 23:49:43 +02001345 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001346
1347 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001348 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1349 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001350 }
1351 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001352 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001353 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001354 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001355 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001356 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001357 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001358 NULL, NULL);
1359 if (unlikely(!cur_end))
1360 return 0;
1361
1362 /* we have a full HTTP/1.0 request now and we know that
1363 * we have either a CR or an LF at <ptr>.
1364 */
1365 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1366 return 1;
1367}
1368
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001369/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001370 * and "keep-alive" values. If we already know that some headers may safely
1371 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001372 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1373 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001374 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001375 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1376 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1377 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001378 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001379 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001380void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001381{
Willy Tarreau5b154472009-12-21 20:11:07 +01001382 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001383 const char *hdr_val = "Connection";
1384 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001385
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001386 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001387 return;
1388
Willy Tarreau88d349d2010-01-25 12:15:43 +01001389 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1390 hdr_val = "Proxy-Connection";
1391 hdr_len = 16;
1392 }
1393
Willy Tarreau5b154472009-12-21 20:11:07 +01001394 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001395 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001396 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001397 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1398 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001399 if (to_del & 2)
1400 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001401 else
1402 txn->flags |= TX_CON_KAL_SET;
1403 }
1404 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1405 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001406 if (to_del & 1)
1407 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001408 else
1409 txn->flags |= TX_CON_CLO_SET;
1410 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001411 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1412 txn->flags |= TX_HDR_CONN_UPG;
1413 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001414 }
1415
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001416 txn->flags |= TX_HDR_CONN_PRS;
1417 return;
1418}
Willy Tarreau5b154472009-12-21 20:11:07 +01001419
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001420/* Apply desired changes on the Connection: header. Values may be removed and/or
1421 * added depending on the <wanted> flags, which are exclusively composed of
1422 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1423 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1424 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001425void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001426{
1427 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001428 const char *hdr_val = "Connection";
1429 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001430
1431 ctx.idx = 0;
1432
Willy Tarreau88d349d2010-01-25 12:15:43 +01001433
1434 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1435 hdr_val = "Proxy-Connection";
1436 hdr_len = 16;
1437 }
1438
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001439 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001440 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001441 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1442 if (wanted & TX_CON_KAL_SET)
1443 txn->flags |= TX_CON_KAL_SET;
1444 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001445 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001446 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001447 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1448 if (wanted & TX_CON_CLO_SET)
1449 txn->flags |= TX_CON_CLO_SET;
1450 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001451 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001452 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001453 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001454
1455 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1456 return;
1457
1458 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1459 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001460 hdr_val = "Connection: close";
1461 hdr_len = 17;
1462 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1463 hdr_val = "Proxy-Connection: close";
1464 hdr_len = 23;
1465 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001466 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001467 }
1468
1469 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1470 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001471 hdr_val = "Connection: keep-alive";
1472 hdr_len = 22;
1473 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1474 hdr_val = "Proxy-Connection: keep-alive";
1475 hdr_len = 28;
1476 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001477 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001478 }
1479 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001480}
1481
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001482/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
1483 * value is larger than 1000, it is bound to 1000. The parser consumes up to
1484 * 1 digit, one dot and 3 digits and stops on the first invalid character.
1485 * Unparsable qvalues return 1000 as "q=1.000".
1486 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02001487int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001488{
1489 int q = 1000;
1490
Willy Tarreau506c69a2014-07-08 00:59:48 +02001491 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001492 goto out;
1493 q = (*qvalue++ - '0') * 1000;
1494
1495 if (*qvalue++ != '.')
1496 goto out;
1497
Willy Tarreau506c69a2014-07-08 00:59:48 +02001498 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001499 goto out;
1500 q += (*qvalue++ - '0') * 100;
1501
Willy Tarreau506c69a2014-07-08 00:59:48 +02001502 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001503 goto out;
1504 q += (*qvalue++ - '0') * 10;
1505
Willy Tarreau506c69a2014-07-08 00:59:48 +02001506 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001507 goto out;
1508 q += (*qvalue++ - '0') * 1;
1509 out:
1510 if (q > 1000)
1511 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02001512 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02001513 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001514 return q;
1515}
William Lallemand82fe75c2012-10-23 10:25:10 +02001516
Willy Tarreau87b09662015-04-03 00:22:06 +02001517void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001518{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001519 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001520 int tmp = TX_CON_WANT_KAL;
1521
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001522 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
1523 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001524 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
1525 tmp = TX_CON_WANT_TUN;
1526
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001527 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001528 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
1529 tmp = TX_CON_WANT_TUN;
1530 }
1531
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001532 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001533 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
1534 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001535 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001536 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
1537 tmp = TX_CON_WANT_CLO;
1538 else
1539 tmp = TX_CON_WANT_SCL;
1540 }
1541
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001542 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001543 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
1544 tmp = TX_CON_WANT_CLO;
1545
1546 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
1547 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
1548
1549 if (!(txn->flags & TX_HDR_CONN_PRS) &&
1550 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
1551 /* parse the Connection header and possibly clean it */
1552 int to_del = 0;
1553 if ((msg->flags & HTTP_MSGF_VER_11) ||
1554 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001555 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001556 to_del |= 2; /* remove "keep-alive" */
1557 if (!(msg->flags & HTTP_MSGF_VER_11))
1558 to_del |= 1; /* remove "close" */
1559 http_parse_connection_header(txn, msg, to_del);
1560 }
1561
1562 /* check if client or config asks for explicit close in KAL/SCL */
1563 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
1564 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
1565 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
1566 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
1567 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001568 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001569 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1570}
William Lallemand82fe75c2012-10-23 10:25:10 +02001571
Willy Tarreaud787e662009-07-07 10:14:51 +02001572/* This stream analyser waits for a complete HTTP request. It returns 1 if the
1573 * processing can continue on next analysers, or zero if it either needs more
1574 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001575 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02001576 * when it has nothing left to do, and may remove any analyser when it wants to
1577 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001578 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001579int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001580{
Willy Tarreau59234e92008-11-30 23:51:27 +01001581 /*
1582 * We will parse the partial (or complete) lines.
1583 * We will check the request syntax, and also join multi-line
1584 * headers. An index of all the lines will be elaborated while
1585 * parsing.
1586 *
1587 * For the parsing, we use a 28 states FSM.
1588 *
1589 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02001590 * req->buf->p = beginning of request
1591 * req->buf->p + msg->eoh = end of processed headers / start of current one
1592 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02001593 * msg->eol = end of current header or line (LF or CRLF)
1594 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02001595 *
1596 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02001597 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02001598 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
1599 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01001600 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001601
Willy Tarreau59234e92008-11-30 23:51:27 +01001602 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001603 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001604 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01001605 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001606 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001607
Willy Tarreau87b09662015-04-03 00:22:06 +02001608 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 +01001609 now_ms, __FUNCTION__,
1610 s,
1611 req,
1612 req->rex, req->wex,
1613 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001614 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01001615 req->analysers);
1616
Willy Tarreau52a0c602009-08-16 22:45:38 +02001617 /* we're speaking HTTP here, so let's speak HTTP to the client */
1618 s->srv_error = http_return_srv_error;
1619
Willy Tarreau83e3af02009-12-28 17:39:57 +01001620 /* There's a protected area at the end of the buffer for rewriting
1621 * purposes. We don't want to start to parse the request if the
1622 * protected area is affected, because we may have to move processed
1623 * data later, which is much more complicated.
1624 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001625 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001626
1627 /* This point is executed when some data is avalaible for analysis,
1628 * so we log the end of the idle time. */
1629 if (s->logs.t_idle == -1)
1630 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
1631
Willy Tarreau379357a2013-06-08 12:55:46 +02001632 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01001633 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001634 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01001635 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01001636 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001637 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001638 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01001639 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01001640 return 0;
1641 }
Willy Tarreau379357a2013-06-08 12:55:46 +02001642 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
1643 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
1644 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001645 }
1646
Willy Tarreau9b28e032012-10-12 23:49:43 +02001647 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01001648 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001649 }
1650
Willy Tarreau59234e92008-11-30 23:51:27 +01001651 /* 1: we might have to print this header in debug mode */
1652 if (unlikely((global.mode & MODE_DEBUG) &&
1653 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02001654 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001655 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001656
Willy Tarreau9b28e032012-10-12 23:49:43 +02001657 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001658 /* this is a bit complex : in case of error on the request line,
1659 * we know that rq.l is still zero, so we display only the part
1660 * up to the end of the line (truncated by debug_hdr).
1661 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001662 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01001663 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001664
Willy Tarreau59234e92008-11-30 23:51:27 +01001665 sol += hdr_idx_first_pos(&txn->hdr_idx);
1666 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001667
Willy Tarreau59234e92008-11-30 23:51:27 +01001668 while (cur_idx) {
1669 eol = sol + txn->hdr_idx.v[cur_idx].len;
1670 debug_hdr("clihdr", s, sol, eol);
1671 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1672 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001673 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001674 }
1675
Willy Tarreau58f10d72006-12-04 02:26:12 +01001676
Willy Tarreau59234e92008-11-30 23:51:27 +01001677 /*
1678 * Now we quickly check if we have found a full valid request.
1679 * If not so, we check the FD and buffer states before leaving.
1680 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01001681 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001682 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02001683 * on a keep-alive stream, if we encounter and error, close, t/o,
1684 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001685 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02001686 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02001687 * Last, we may increase some tracked counters' http request errors on
1688 * the cases that are deliberately the client's fault. For instance,
1689 * a timeout or connection reset is not counted as an error. However
1690 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01001691 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001692
Willy Tarreau655dce92009-11-08 13:10:58 +01001693 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001694 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001695 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001696 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01001697 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001698 stream_inc_http_req_ctr(s);
1699 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001700 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001701 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01001702 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001703
Willy Tarreau59234e92008-11-30 23:51:27 +01001704 /* 1: Since we are in header mode, if there's no space
1705 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02001706 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01001707 * must terminate it now.
1708 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001709 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001710 /* FIXME: check if URI is set and return Status
1711 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001712 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001713 stream_inc_http_req_ctr(s);
1714 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001715 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02001716 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02001717 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01001718 goto return_bad_req;
1719 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001720
Willy Tarreau59234e92008-11-30 23:51:27 +01001721 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001722 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001723 if (!(s->flags & SF_ERR_MASK))
1724 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001725
Willy Tarreaufcffa692010-01-10 14:21:19 +01001726 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001727 goto failed_keep_alive;
1728
Willy Tarreau0f228a02015-05-01 15:37:53 +02001729 if (sess->fe->options & PR_O_IGNORE_PRB)
1730 goto failed_keep_alive;
1731
Willy Tarreau59234e92008-11-30 23:51:27 +01001732 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001733 if (msg->err_pos >= 0) {
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001734 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001735 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001736 }
1737
Willy Tarreaudc979f22012-12-04 10:39:01 +01001738 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001739 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001740 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001741 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01001742 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001743 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001744 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001745 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001746 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001747 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001748
Willy Tarreaue7dff022015-04-03 01:14:29 +02001749 if (!(s->flags & SF_FINST_MASK))
1750 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001751 return 0;
1752 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001753
Willy Tarreau59234e92008-11-30 23:51:27 +01001754 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001755 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001756 if (!(s->flags & SF_ERR_MASK))
1757 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001758
Willy Tarreaufcffa692010-01-10 14:21:19 +01001759 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001760 goto failed_keep_alive;
1761
Willy Tarreau0f228a02015-05-01 15:37:53 +02001762 if (sess->fe->options & PR_O_IGNORE_PRB)
1763 goto failed_keep_alive;
1764
Willy Tarreau59234e92008-11-30 23:51:27 +01001765 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001766 if (msg->err_pos >= 0) {
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001767 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001768 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001769 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001770 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001771 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001772 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001773 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001774 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001775
Willy Tarreau87b09662015-04-03 00:22:06 +02001776 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001777 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001778 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001779 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001780 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001781
Willy Tarreaue7dff022015-04-03 01:14:29 +02001782 if (!(s->flags & SF_FINST_MASK))
1783 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001784 return 0;
1785 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001786
Willy Tarreau59234e92008-11-30 23:51:27 +01001787 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001788 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001789 if (!(s->flags & SF_ERR_MASK))
1790 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001791
Willy Tarreaufcffa692010-01-10 14:21:19 +01001792 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001793 goto failed_keep_alive;
1794
Willy Tarreau0f228a02015-05-01 15:37:53 +02001795 if (sess->fe->options & PR_O_IGNORE_PRB)
1796 goto failed_keep_alive;
1797
Willy Tarreau4076a152009-04-02 15:18:36 +02001798 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001799 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001800 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001801 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001802 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001803 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001804 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001805 stream_inc_http_err_ctr(s);
1806 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001807 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001808 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001809 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001810 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001811
Willy Tarreaue7dff022015-04-03 01:14:29 +02001812 if (!(s->flags & SF_FINST_MASK))
1813 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001814 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001815 }
1816
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001817 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001818 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001819 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01001820#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001821 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
1822 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01001823 /* We need more data, we have to re-enable quick-ack in case we
1824 * previously disabled it, otherwise we might cause the client
1825 * to delay next data.
1826 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001827 setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01001828 }
1829#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001830
Willy Tarreaufcffa692010-01-10 14:21:19 +01001831 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
1832 /* If the client starts to talk, let's fall back to
1833 * request timeout processing.
1834 */
1835 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01001836 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01001837 }
1838
Willy Tarreau59234e92008-11-30 23:51:27 +01001839 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01001840 if (!tick_isset(req->analyse_exp)) {
1841 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
1842 (txn->flags & TX_WAIT_NEXT_RQ) &&
1843 tick_isset(s->be->timeout.httpka))
1844 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
1845 else
1846 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1847 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001848
Willy Tarreau59234e92008-11-30 23:51:27 +01001849 /* we're not ready yet */
1850 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001851
1852 failed_keep_alive:
1853 /* Here we process low-level errors for keep-alive requests. In
1854 * short, if the request is not the first one and it experiences
1855 * a timeout, read error or shutdown, we just silently close so
1856 * that the client can try again.
1857 */
1858 txn->status = 0;
1859 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001860 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001861 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02001862 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001863 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001864 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01001865 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01001866 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001867
Willy Tarreaud787e662009-07-07 10:14:51 +02001868 /* OK now we have a complete HTTP request with indexed headers. Let's
1869 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02001870 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01001871 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01001872 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001873 * byte after the last LF. msg->sov points to the first byte of data.
1874 * msg->eol cannot be trusted because it may have been left uninitialized
1875 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02001876 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001877
Willy Tarreau87b09662015-04-03 00:22:06 +02001878 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001879 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001880
Willy Tarreaub16a5742010-01-10 14:46:16 +01001881 if (txn->flags & TX_WAIT_NEXT_RQ) {
1882 /* kill the pending keep-alive timeout */
1883 txn->flags &= ~TX_WAIT_NEXT_RQ;
1884 req->analyse_exp = TICK_ETERNITY;
1885 }
1886
1887
Willy Tarreaud787e662009-07-07 10:14:51 +02001888 /* Maybe we found in invalid header name while we were configured not
1889 * to block on that, so we have to capture it now.
1890 */
1891 if (unlikely(msg->err_pos >= 0))
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001892 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02001893
Willy Tarreau59234e92008-11-30 23:51:27 +01001894 /*
1895 * 1: identify the method
1896 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001897 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01001898
1899 /* we can make use of server redirect on GET and HEAD */
1900 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001901 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001902
Willy Tarreau59234e92008-11-30 23:51:27 +01001903 /*
1904 * 2: check if the URI matches the monitor_uri.
1905 * We have to do this for every request which gets in, because
1906 * the monitor-uri is defined by the frontend.
1907 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001908 if (unlikely((sess->fe->monitor_uri_len != 0) &&
1909 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02001910 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001911 sess->fe->monitor_uri,
1912 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001913 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001914 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001915 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001916 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001917
Willy Tarreaue7dff022015-04-03 01:14:29 +02001918 s->flags |= SF_MONITOR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001919 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001920
Willy Tarreau59234e92008-11-30 23:51:27 +01001921 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001922 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001923 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02001924
Willy Tarreau59234e92008-11-30 23:51:27 +01001925 ret = acl_pass(ret);
1926 if (cond->pol == ACL_COND_UNLESS)
1927 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001928
Willy Tarreau59234e92008-11-30 23:51:27 +01001929 if (ret) {
1930 /* we fail this request, let's return 503 service unavail */
1931 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001932 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001933 if (!(s->flags & SF_ERR_MASK))
1934 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001935 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001936 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001937 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001938
Willy Tarreau59234e92008-11-30 23:51:27 +01001939 /* nothing to fail, let's reply normaly */
1940 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001941 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001942 if (!(s->flags & SF_ERR_MASK))
1943 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001944 goto return_prx_cond;
1945 }
1946
1947 /*
1948 * 3: Maybe we have to copy the original REQURI for the logs ?
1949 * Note: we cannot log anymore if the request has been
1950 * classified as invalid.
1951 */
1952 if (unlikely(s->logs.logwait & LW_REQ)) {
1953 /* we have a complete HTTP request that we must log */
1954 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
1955 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001956
Stéphane Cottin23e9e932017-05-18 08:58:41 +02001957 if (urilen >= global.tune.requri_len )
1958 urilen = global.tune.requri_len - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001959 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01001960 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001961
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001962 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01001963 s->do_log(s);
1964 } else {
1965 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001966 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001967 }
Willy Tarreau06619262006-12-17 08:37:22 +01001968
Willy Tarreau91852eb2015-05-01 13:26:00 +02001969 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
1970 * exactly one digit "." one digit. This check may be disabled using
1971 * option accept-invalid-http-request.
1972 */
1973 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
1974 if (msg->sl.rq.v_l != 8) {
1975 msg->err_pos = msg->sl.rq.v;
1976 goto return_bad_req;
1977 }
1978
1979 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
1980 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
1981 req->buf->p[msg->sl.rq.v + 6] != '.' ||
1982 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
1983 msg->err_pos = msg->sl.rq.v + 4;
1984 goto return_bad_req;
1985 }
1986 }
Willy Tarreau13317662015-05-01 13:47:08 +02001987 else {
1988 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1989 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
1990 goto return_bad_req;
1991 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02001992
Willy Tarreau5b154472009-12-21 20:11:07 +01001993 /* ... and check if the request is HTTP/1.1 or above */
1994 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02001995 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
1996 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
1997 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001998 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01001999
2000 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002001 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 +01002002
Willy Tarreau88d349d2010-01-25 12:15:43 +01002003 /* if the frontend has "option http-use-proxy-header", we'll check if
2004 * we have what looks like a proxied connection instead of a connection,
2005 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2006 * Note that this is *not* RFC-compliant, however browsers and proxies
2007 * happen to do that despite being non-standard :-(
2008 * We consider that a request not beginning with either '/' or '*' is
2009 * a proxied connection, which covers both "scheme://location" and
2010 * CONNECT ip:port.
2011 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002012 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002013 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002014 txn->flags |= TX_USE_PX_CONN;
2015
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002016 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002017 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002018
Willy Tarreau59234e92008-11-30 23:51:27 +01002019 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002020 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002021 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002022 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002023
Willy Tarreau557f1992015-05-01 10:05:17 +02002024 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
2025 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002026 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002027 * The length of a message body is determined by one of the following
2028 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02002029 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002030 * 1. Any response to a HEAD request and any response with a 1xx
2031 * (Informational), 204 (No Content), or 304 (Not Modified) status
2032 * code is always terminated by the first empty line after the
2033 * header fields, regardless of the header fields present in the
2034 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002035 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002036 * 2. Any 2xx (Successful) response to a CONNECT request implies that
2037 * the connection will become a tunnel immediately after the empty
2038 * line that concludes the header fields. A client MUST ignore any
2039 * Content-Length or Transfer-Encoding header fields received in
2040 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002041 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002042 * 3. If a Transfer-Encoding header field is present and the chunked
2043 * transfer coding (Section 4.1) is the final encoding, the message
2044 * body length is determined by reading and decoding the chunked
2045 * data until the transfer coding indicates the data is complete.
2046 *
2047 * If a Transfer-Encoding header field is present in a response and
2048 * the chunked transfer coding is not the final encoding, the
2049 * message body length is determined by reading the connection until
2050 * it is closed by the server. If a Transfer-Encoding header field
2051 * is present in a request and the chunked transfer coding is not
2052 * the final encoding, the message body length cannot be determined
2053 * reliably; the server MUST respond with the 400 (Bad Request)
2054 * status code and then close the connection.
2055 *
2056 * If a message is received with both a Transfer-Encoding and a
2057 * Content-Length header field, the Transfer-Encoding overrides the
2058 * Content-Length. Such a message might indicate an attempt to
2059 * perform request smuggling (Section 9.5) or response splitting
2060 * (Section 9.4) and ought to be handled as an error. A sender MUST
2061 * remove the received Content-Length field prior to forwarding such
2062 * a message downstream.
2063 *
2064 * 4. If a message is received without Transfer-Encoding and with
2065 * either multiple Content-Length header fields having differing
2066 * field-values or a single Content-Length header field having an
2067 * invalid value, then the message framing is invalid and the
2068 * recipient MUST treat it as an unrecoverable error. If this is a
2069 * request message, the server MUST respond with a 400 (Bad Request)
2070 * status code and then close the connection. If this is a response
2071 * message received by a proxy, the proxy MUST close the connection
2072 * to the server, discard the received response, and send a 502 (Bad
2073 * Gateway) response to the client. If this is a response message
2074 * received by a user agent, the user agent MUST close the
2075 * connection to the server and discard the received response.
2076 *
2077 * 5. If a valid Content-Length header field is present without
2078 * Transfer-Encoding, its decimal value defines the expected message
2079 * body length in octets. If the sender closes the connection or
2080 * the recipient times out before the indicated number of octets are
2081 * received, the recipient MUST consider the message to be
2082 * incomplete and close the connection.
2083 *
2084 * 6. If this is a request message and none of the above are true, then
2085 * the message body length is zero (no message body is present).
2086 *
2087 * 7. Otherwise, this is a response message without a declared message
2088 * body length, so the message body length is determined by the
2089 * number of octets received prior to the server closing the
2090 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002091 */
2092
Willy Tarreau32b47f42009-10-18 20:55:02 +02002093 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002094 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02002095 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002096 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02002097 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002098 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02002099 /* chunked not last, return badreq */
2100 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002101 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002102 }
2103
Willy Tarreau1c913912015-04-30 10:57:51 +02002104 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02002105 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02002106 if (msg->flags & HTTP_MSGF_TE_CHNK) {
2107 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
2108 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2109 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02002110 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002111 signed long long cl;
2112
Willy Tarreauad14f752011-09-02 20:33:27 +02002113 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002114 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002115 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002116 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002117
Willy Tarreauad14f752011-09-02 20:33:27 +02002118 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002119 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002120 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002121 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002122
Willy Tarreauad14f752011-09-02 20:33:27 +02002123 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002124 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002125 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002126 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002127
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002128 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002129 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002130 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002131 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002132
Christopher Fauletbe821b92017-03-30 11:21:53 +02002133 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002134 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002135 }
2136
Willy Tarreau34dfc602015-05-01 10:09:49 +02002137 /* even bodyless requests have a known length */
2138 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002139
Willy Tarreau179085c2014-04-28 16:48:56 +02002140 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
2141 * only change if both the request and the config reference something else.
2142 * Option httpclose by itself sets tunnel mode where headers are mangled.
2143 * However, if another mode is set, it will affect it (eg: server-close/
2144 * keep-alive + httpclose = close). Note that we avoid to redo the same work
2145 * if FE and BE have the same settings (common). The method consists in
2146 * checking if options changed between the two calls (implying that either
2147 * one is non-null, or one of them is non-null and we are there for the first
2148 * time.
2149 */
2150 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002151 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002152 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02002153
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02002154 /* we may have to wait for the request's body */
2155 if ((s->be->options & PR_O_WREQ_BODY) &&
2156 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
2157 req->analysers |= AN_REQ_HTTP_BODY;
2158
Willy Tarreaud787e662009-07-07 10:14:51 +02002159 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002160 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002161 req->analyse_exp = TICK_ETERNITY;
2162 return 1;
2163
2164 return_bad_req:
2165 /* We centralize bad requests processing here */
2166 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2167 /* we detected a parsing error. We want to archive this request
2168 * in the dedicated proxy area for later troubleshooting.
2169 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02002170 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002171 }
2172
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002173 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02002174 txn->req.msg_state = HTTP_MSG_ERROR;
2175 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002176 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002177
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002178 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002179 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002180 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02002181
2182 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02002183 if (!(s->flags & SF_ERR_MASK))
2184 s->flags |= SF_ERR_PRXCOND;
2185 if (!(s->flags & SF_FINST_MASK))
2186 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02002187
Christopher Faulet0184ea72017-01-05 14:06:34 +01002188 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02002189 req->analyse_exp = TICK_ETERNITY;
2190 return 0;
2191}
2192
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002193
Willy Tarreau347a35d2013-11-22 17:51:09 +01002194/* This function prepares an applet to handle the stats. It can deal with the
2195 * "100-continue" expectation, check that admin rules are met for POST requests,
2196 * and program a response message if something was unexpected. It cannot fail
2197 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002198 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002199 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02002200 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002201 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002202int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002203{
2204 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01002205 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02002206 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002207 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002208 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002209 struct uri_auth *uri_auth = s->be->uri_auth;
2210 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002211 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002212
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002213 appctx = si_appctx(si);
2214 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2215 appctx->st1 = appctx->st2 = 0;
2216 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
2217 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02002218 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02002219 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002220
2221 uri = msg->chn->buf->p + msg->sl.rq.u;
2222 lookup = uri + uri_auth->uri_len;
2223
2224 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
2225 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002226 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002227 break;
2228 }
2229 }
2230
2231 if (uri_auth->refresh) {
2232 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
2233 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002234 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002235 break;
2236 }
2237 }
2238 }
2239
2240 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
2241 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002242 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002243 break;
2244 }
2245 }
2246
Willy Tarreau1e62df92016-01-11 18:57:53 +01002247 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
2248 if (memcmp(h, ";typed", 6) == 0) {
2249 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
2250 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
2251 break;
2252 }
2253 }
2254
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002255 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2256 if (memcmp(h, ";st=", 4) == 0) {
2257 int i;
2258 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002259 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002260 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
2261 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002262 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002263 break;
2264 }
2265 }
2266 break;
2267 }
2268 }
2269
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002270 appctx->ctx.stats.scope_str = 0;
2271 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002272 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2273 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
2274 int itx = 0;
2275 const char *h2;
2276 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
2277 const char *err;
2278
2279 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
2280 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002281 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002282 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
2283 itx++;
2284 h++;
2285 }
2286
2287 if (itx > STAT_SCOPE_TXT_MAXLEN)
2288 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002289 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002290
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002291 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002292 memcpy(scope_txt, h2, itx);
2293 scope_txt[itx] = '\0';
2294 err = invalid_char(scope_txt);
2295 if (err) {
2296 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002297 appctx->ctx.stats.scope_str = 0;
2298 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002299 }
2300 break;
2301 }
2302 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002303
2304 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002305 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002306 int ret = 1;
2307
2308 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002309 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 +01002310 ret = acl_pass(ret);
2311 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2312 ret = !ret;
2313 }
2314
2315 if (ret) {
2316 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002317 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002318 break;
2319 }
2320 }
2321
2322 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02002323 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002324 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02002325 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02002326 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
2327 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002328 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002329 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002330 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002331 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
2332 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002333 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002334 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002335 else {
2336 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002337 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002338 }
2339
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002340 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002341 return 1;
2342}
2343
Lukas Tribus67db8df2013-06-23 17:37:13 +02002344/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
2345 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
2346 */
Vincent Bernat6e615892016-05-18 16:17:44 +02002347void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02002348{
2349#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02002350 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02002351 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2352#endif
2353#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02002354 if (from->ss_family == AF_INET6) {
2355 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02002356 /* v4-mapped addresses need IP_TOS */
2357 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2358 else
2359 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
2360 }
2361#endif
2362}
2363
Willy Tarreau87b09662015-04-03 00:22:06 +02002364int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002365 const char* name, unsigned int name_len,
2366 const char *str, struct my_regex *re,
2367 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06002368{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002369 struct hdr_ctx ctx;
2370 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002371 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002372 int (*http_find_hdr_func)(const char *name, int len, char *sol,
2373 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002374 struct chunk *output = get_trash_chunk();
2375
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002376 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06002377
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002378 /* Choose the header browsing function. */
2379 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002380 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002381 http_find_hdr_func = http_find_header2;
2382 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002383 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002384 http_find_hdr_func = http_find_full_header2;
2385 break;
2386 default: /* impossible */
2387 return -1;
2388 }
2389
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002390 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
2391 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06002392 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002393 char *val = ctx.line + ctx.val;
2394 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06002395
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002396 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
2397 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06002398
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002399 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002400 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06002401 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06002402
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002403 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06002404
2405 hdr->len += delta;
2406 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002407
2408 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002409 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06002410 }
2411
2412 return 0;
2413}
2414
Willy Tarreau87b09662015-04-03 00:22:06 +02002415static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002416 const char* name, unsigned int name_len,
2417 struct list *fmt, struct my_regex *re,
2418 int action)
2419{
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002420 struct chunk *replace;
2421 int ret = -1;
2422
2423 replace = alloc_trash_chunk();
2424 if (!replace)
2425 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002426
2427 replace->len = build_logline(s, replace->str, replace->size, fmt);
2428 if (replace->len >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002429 goto leave;
2430
2431 ret = http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002432
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002433 leave:
2434 free_trash_chunk(replace);
2435 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002436}
2437
Willy Tarreau87b09662015-04-03 00:22:06 +02002438/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02002439 * transaction <txn>. Returns the verdict of the first rule that prevents
2440 * further processing of the request (auth, deny, ...), and defaults to
2441 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2442 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02002443 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2444 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2445 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002446 */
Willy Tarreau0b748332014-04-29 00:13:29 +02002447enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02002448http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002449{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002450 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002451 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002452 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002453 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01002454 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02002455 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02002456 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02002457 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002458
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002459 /* If "the current_rule_list" match the executed rule list, we are in
2460 * resume condition. If a resume is needed it is always in the action
2461 * and never in the ACL or converters. In this case, we initialise the
2462 * current rule, and go to the action execution point.
2463 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002464 if (s->current_rule) {
2465 rule = s->current_rule;
2466 s->current_rule = NULL;
2467 if (s->current_rule_list == rules)
2468 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002469 }
2470 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002471
Willy Tarreauff011f22011-01-06 17:51:27 +01002472 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002473
Willy Tarreau96257ec2012-12-27 10:46:37 +01002474 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01002475 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01002476 int ret;
2477
Willy Tarreau192252e2015-04-04 01:47:55 +02002478 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002479 ret = acl_pass(ret);
2480
Willy Tarreauff011f22011-01-06 17:51:27 +01002481 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002482 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002483
2484 if (!ret) /* condition not matched */
2485 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002486 }
2487
Willy Tarreauacc98002015-09-27 23:34:39 +02002488 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002489resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01002490 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002491 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02002492 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002493
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002494 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02002495 if (deny_status)
2496 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02002497 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002498
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002499 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01002500 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002501 if (deny_status)
2502 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02002503 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01002504
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002505 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02002506 /* Auth might be performed on regular http-req rules as well as on stats */
2507 auth_realm = rule->arg.auth.realm;
2508 if (!auth_realm) {
2509 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2510 auth_realm = STATS_DEFAULT_REALM;
2511 else
2512 auth_realm = px->id;
2513 }
2514 /* send 401/407 depending on whether we use a proxy or not. We still
2515 * count one error, because normal browsing won't significantly
2516 * increase the counter but brute force attempts will.
2517 */
2518 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
2519 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002520 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02002521 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02002522 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002523
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002524 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02002525 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
2526 return HTTP_RULE_RES_BADREQ;
2527 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002528
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002529 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002530 s->task->nice = rule->arg.nice;
2531 break;
2532
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002533 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002534 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002535 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002536 break;
2537
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002538 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002539#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002540 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002541 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002542#endif
2543 break;
2544
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002545 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002546 s->logs.level = rule->arg.loglevel;
2547 break;
2548
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002549 case ACT_HTTP_REPLACE_HDR:
2550 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002551 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
2552 rule->arg.hdr_add.name_len,
2553 &rule->arg.hdr_add.fmt,
2554 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06002555 return HTTP_RULE_RES_BADREQ;
2556 break;
2557
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002558 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01002559 ctx.idx = 0;
2560 /* remove all occurrences of the header */
2561 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2562 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
2563 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01002564 }
Willy Tarreau85603282015-01-21 20:39:27 +01002565 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002566
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002567 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002568 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02002569 /* The scope of the trash buffer must be limited to this function. The
2570 * build_logline() function can execute a lot of other function which
2571 * can use the trash buffer. So for limiting the scope of this global
2572 * buffer, we build first the header value using build_logline, and
2573 * after we store the header name.
2574 */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002575 struct chunk *replace;
2576
2577 replace = alloc_trash_chunk();
2578 if (!replace)
2579 return HTTP_RULE_RES_BADREQ;
2580
Thierry Fournier4b788f72016-06-01 13:35:36 +02002581 len = rule->arg.hdr_add.name_len + 2,
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002582 len += build_logline(s, replace->str + len, replace->size - len, &rule->arg.hdr_add.fmt);
2583 memcpy(replace->str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2584 replace->str[rule->arg.hdr_add.name_len] = ':';
2585 replace->str[rule->arg.hdr_add.name_len + 1] = ' ';
2586 replace->len = len;
Willy Tarreau85603282015-01-21 20:39:27 +01002587
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002588 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002589 /* remove all occurrences of the header */
2590 ctx.idx = 0;
2591 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2592 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
2593 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
2594 }
2595 }
2596
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002597 http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->str, replace->len);
2598
2599 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01002600 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002601 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002602
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002603 case ACT_HTTP_DEL_ACL:
2604 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002605 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002606 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002607
2608 /* collect reference */
2609 ref = pat_ref_lookup(rule->arg.map.ref);
2610 if (!ref)
2611 continue;
2612
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002613 /* allocate key */
2614 key = alloc_trash_chunk();
2615 if (!key)
2616 return HTTP_RULE_RES_BADREQ;
2617
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002618 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002619 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2620 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002621
2622 /* perform update */
2623 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002624 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002625 pat_ref_delete(ref, key->str);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002626 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002627
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002628 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002629 break;
2630 }
2631
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002632 case ACT_HTTP_ADD_ACL: {
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 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002652 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002653 if (pat_ref_find_elt(ref, key->str) == NULL)
2654 pat_ref_add(ref, key->str, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002655 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002656
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002657 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002658 break;
2659 }
2660
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002661 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002662 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002663 struct chunk *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002664
2665 /* collect reference */
2666 ref = pat_ref_lookup(rule->arg.map.ref);
2667 if (!ref)
2668 continue;
2669
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002670 /* allocate key */
2671 key = alloc_trash_chunk();
2672 if (!key)
2673 return HTTP_RULE_RES_BADREQ;
2674
2675 /* allocate value */
2676 value = alloc_trash_chunk();
2677 if (!value) {
2678 free_trash_chunk(key);
2679 return HTTP_RULE_RES_BADREQ;
2680 }
2681
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002682 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002683 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2684 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002685
2686 /* collect value */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002687 value->len = build_logline(s, value->str, value->size, &rule->arg.map.value);
2688 value->str[value->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002689
2690 /* perform update */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002691 if (pat_ref_find_elt(ref, key->str) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002692 /* update entry if it exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002693 pat_ref_set(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002694 else
2695 /* insert a new entry */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002696 pat_ref_add(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002697
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002698 free_trash_chunk(key);
2699 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002700 break;
2701 }
William Lallemand73025dd2014-04-24 14:38:37 +02002702
Thierry FOURNIER42148732015-09-02 17:17:33 +02002703 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02002704 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
2705 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002706
Willy Tarreauacc98002015-09-27 23:34:39 +02002707 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002708 case ACT_RET_ERR:
2709 case ACT_RET_CONT:
2710 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002711 case ACT_RET_STOP:
2712 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002713 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002714 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002715 return HTTP_RULE_RES_YIELD;
2716 }
William Lallemand73025dd2014-04-24 14:38:37 +02002717 break;
2718
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002719 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002720 /* Note: only the first valid tracking parameter of each
2721 * applies.
2722 */
2723
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002724 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002725 struct stktable *t;
2726 struct stksess *ts;
2727 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002728 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02002729
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002730 t = rule->arg.trk_ctr.table.t;
2731 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 +02002732
2733 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002734 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002735
2736 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002737 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2738 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2739 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002740 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02002741
Emeric Brun819fc6f2017-06-13 19:37:32 +02002742 if (ptr1)
2743 stktable_data_cast(ptr1, http_req_cnt)++;
2744
2745 if (ptr2)
2746 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2747 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2748
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002749 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002750 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002751
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002752 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002753 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002754 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002755 }
2756 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002757 break;
2758
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002759 /* other flags exists, but normaly, they never be matched. */
2760 default:
2761 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002762 }
2763 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002764
2765 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02002766 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002767}
2768
Willy Tarreau71241ab2012-12-27 11:30:54 +01002769
Willy Tarreau51d861a2015-05-22 17:30:48 +02002770/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2771 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2772 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2773 * is returned, the process can continue the evaluation of next rule list. If
2774 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2775 * is returned, it means the operation could not be processed and a server error
2776 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2777 * deny rule. If *YIELD is returned, the caller must call again the function
2778 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002779 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002780static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002781http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002782{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002783 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002784 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002785 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002786 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002787 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02002788 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002789
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002790 /* If "the current_rule_list" match the executed rule list, we are in
2791 * resume condition. If a resume is needed it is always in the action
2792 * and never in the ACL or converters. In this case, we initialise the
2793 * current rule, and go to the action execution point.
2794 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002795 if (s->current_rule) {
2796 rule = s->current_rule;
2797 s->current_rule = NULL;
2798 if (s->current_rule_list == rules)
2799 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002800 }
2801 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002802
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002803 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002804
2805 /* check optional condition */
2806 if (rule->cond) {
2807 int ret;
2808
Willy Tarreau192252e2015-04-04 01:47:55 +02002809 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002810 ret = acl_pass(ret);
2811
2812 if (rule->cond->pol == ACL_COND_UNLESS)
2813 ret = !ret;
2814
2815 if (!ret) /* condition not matched */
2816 continue;
2817 }
2818
Willy Tarreauacc98002015-09-27 23:34:39 +02002819 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002820resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002821 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002822 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002823 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002824
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002825 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002826 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002827 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002828
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002829 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002830 s->task->nice = rule->arg.nice;
2831 break;
2832
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002833 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002834 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002835 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002836 break;
2837
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002838 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002839#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002840 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002841 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002842#endif
2843 break;
2844
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002845 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002846 s->logs.level = rule->arg.loglevel;
2847 break;
2848
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002849 case ACT_HTTP_REPLACE_HDR:
2850 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002851 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2852 rule->arg.hdr_add.name_len,
2853 &rule->arg.hdr_add.fmt,
2854 &rule->arg.hdr_add.re, rule->action))
Christopher Fauletcdade942017-02-08 12:41:31 +01002855 return HTTP_RULE_RES_BADREQ;
Sasha Pachev218f0642014-06-16 12:05:59 -06002856 break;
2857
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002858 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002859 ctx.idx = 0;
2860 /* remove all occurrences of the header */
2861 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2862 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
2863 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2864 }
Willy Tarreau85603282015-01-21 20:39:27 +01002865 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002866
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002867 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002868 case ACT_HTTP_ADD_HDR: {
2869 struct chunk *replace;
2870
2871 replace = alloc_trash_chunk();
2872 if (!replace)
2873 return HTTP_RULE_RES_BADREQ;
2874
2875 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
2876 memcpy(replace->str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2877 replace->len = rule->arg.hdr_add.name_len;
2878 replace->str[replace->len++] = ':';
2879 replace->str[replace->len++] = ' ';
2880 replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len,
2881 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002882
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002883 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002884 /* remove all occurrences of the header */
2885 ctx.idx = 0;
2886 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2887 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
2888 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2889 }
2890 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002891 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->str, replace->len);
2892
2893 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002894 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002895 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002896
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002897 case ACT_HTTP_DEL_ACL:
2898 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002899 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002900 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002901
2902 /* collect reference */
2903 ref = pat_ref_lookup(rule->arg.map.ref);
2904 if (!ref)
2905 continue;
2906
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002907 /* allocate key */
2908 key = alloc_trash_chunk();
2909 if (!key)
2910 return HTTP_RULE_RES_BADREQ;
2911
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002912 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002913 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2914 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002915
2916 /* perform update */
2917 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002918 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002919 pat_ref_delete(ref, key->str);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002920 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002921
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002922 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002923 break;
2924 }
2925
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002926 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002927 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002928 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002929
2930 /* collect reference */
2931 ref = pat_ref_lookup(rule->arg.map.ref);
2932 if (!ref)
2933 continue;
2934
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002935 /* allocate key */
2936 key = alloc_trash_chunk();
2937 if (!key)
2938 return HTTP_RULE_RES_BADREQ;
2939
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002940 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002941 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2942 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002943
2944 /* perform update */
2945 /* check if the entry already exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002946 if (pat_ref_find_elt(ref, key->str) == NULL)
2947 pat_ref_add(ref, key->str, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002948
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002949 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002950 break;
2951 }
2952
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002953 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002954 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002955 struct chunk *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002956
2957 /* collect reference */
2958 ref = pat_ref_lookup(rule->arg.map.ref);
2959 if (!ref)
2960 continue;
2961
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002962 /* allocate key */
2963 key = alloc_trash_chunk();
2964 if (!key)
2965 return HTTP_RULE_RES_BADREQ;
2966
2967 /* allocate value */
2968 value = alloc_trash_chunk();
2969 if (!value) {
2970 free_trash_chunk(key);
2971 return HTTP_RULE_RES_BADREQ;
2972 }
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 /* collect value */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002979 value->len = build_logline(s, value->str, value->size, &rule->arg.map.value);
2980 value->str[value->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002981
2982 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002983 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002984 if (pat_ref_find_elt(ref, key->str) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002985 /* update entry if it exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002986 pat_ref_set(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002987 else
2988 /* insert a new entry */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002989 pat_ref_add(ref, key->str, value->str, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002990 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002991 free_trash_chunk(key);
2992 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002993 break;
2994 }
William Lallemand73025dd2014-04-24 14:38:37 +02002995
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002996 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02002997 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
2998 return HTTP_RULE_RES_BADREQ;
2999 return HTTP_RULE_RES_DONE;
3000
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003001 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3002 /* Note: only the first valid tracking parameter of each
3003 * applies.
3004 */
3005
Christopher Faulet4fce0d82017-09-18 11:57:31 +02003006 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003007 struct stktable *t;
3008 struct stksess *ts;
3009 struct stktable_key *key;
3010 void *ptr;
3011
3012 t = rule->arg.trk_ctr.table.t;
3013 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
3014
3015 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02003016 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003017
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003018 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003019
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003020 /* let's count a new HTTP request as it's the first time we do it */
3021 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3022 if (ptr)
3023 stktable_data_cast(ptr, http_req_cnt)++;
3024
3025 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3026 if (ptr)
3027 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3028 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3029
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003030 /* When the client triggers a 4xx from the server, it's most often due
3031 * to a missing object or permission. These events should be tracked
3032 * because if they happen often, it may indicate a brute force or a
3033 * vulnerability scan. Normally this is done when receiving the response
3034 * but here we're tracking after this ought to have been done so we have
3035 * to do it on purpose.
3036 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02003037 if ((unsigned)(txn->status - 400) < 100) {
3038 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3039 if (ptr)
3040 stktable_data_cast(ptr, http_err_cnt)++;
3041
3042 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3043 if (ptr)
3044 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3045 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3046 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02003047
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003048 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003049
3050 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3051 if (sess->fe != s->be)
3052 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3053
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003054 }
3055 }
3056 break;
3057
Thierry FOURNIER42148732015-09-02 17:17:33 +02003058 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003059 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3060 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003061
Willy Tarreauacc98002015-09-27 23:34:39 +02003062 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003063 case ACT_RET_ERR:
3064 case ACT_RET_CONT:
3065 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003066 case ACT_RET_STOP:
3067 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003068 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003069 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003070 return HTTP_RULE_RES_YIELD;
3071 }
William Lallemand73025dd2014-04-24 14:38:37 +02003072 break;
3073
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003074 /* other flags exists, but normaly, they never be matched. */
3075 default:
3076 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003077 }
3078 }
3079
3080 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003081 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003082}
3083
3084
Willy Tarreau71241ab2012-12-27 11:30:54 +01003085/* Perform an HTTP redirect based on the information in <rule>. The function
3086 * returns non-zero on success, or zero in case of a, irrecoverable error such
3087 * as too large a request to build a valid response.
3088 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003089static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003090{
Willy Tarreaub329a312015-05-22 16:27:37 +02003091 struct http_msg *req = &txn->req;
3092 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003093 const char *msg_fmt;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003094 struct chunk *chunk;
3095 int ret = 0;
3096
3097 chunk = alloc_trash_chunk();
3098 if (!chunk)
3099 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003100
3101 /* build redirect message */
3102 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003103 case 308:
3104 msg_fmt = HTTP_308;
3105 break;
3106 case 307:
3107 msg_fmt = HTTP_307;
3108 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003109 case 303:
3110 msg_fmt = HTTP_303;
3111 break;
3112 case 301:
3113 msg_fmt = HTTP_301;
3114 break;
3115 case 302:
3116 default:
3117 msg_fmt = HTTP_302;
3118 break;
3119 }
3120
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003121 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
3122 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003123
3124 switch(rule->type) {
3125 case REDIRECT_TYPE_SCHEME: {
3126 const char *path;
3127 const char *host;
3128 struct hdr_ctx ctx;
3129 int pathlen;
3130 int hostlen;
3131
3132 host = "";
3133 hostlen = 0;
3134 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02003135 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003136 host = ctx.line + ctx.val;
3137 hostlen = ctx.vlen;
3138 }
3139
3140 path = http_get_path(txn);
3141 /* build message using path */
3142 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003143 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003144 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3145 int qs = 0;
3146 while (qs < pathlen) {
3147 if (path[qs] == '?') {
3148 pathlen = qs;
3149 break;
3150 }
3151 qs++;
3152 }
3153 }
3154 } else {
3155 path = "/";
3156 pathlen = 1;
3157 }
3158
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003159 if (rule->rdr_str) { /* this is an old "redirect" rule */
3160 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003161 if (chunk->len + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
3162 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003163
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003164 /* add scheme */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003165 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3166 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003167 }
3168 else {
3169 /* add scheme with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003170 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003171
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003172 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003173 if (chunk->len + 3 + hostlen + pathlen > chunk->size - 4)
3174 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003175 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003176 /* add "://" */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003177 memcpy(chunk->str + chunk->len, "://", 3);
3178 chunk->len += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003179
3180 /* add host */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003181 memcpy(chunk->str + chunk->len, host, hostlen);
3182 chunk->len += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003183
3184 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003185 memcpy(chunk->str + chunk->len, path, pathlen);
3186 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003187
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003188 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003189 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003190 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003191 if (chunk->len > chunk->size - 5)
3192 goto leave;
3193 chunk->str[chunk->len] = '/';
3194 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003195 }
3196
3197 break;
3198 }
3199 case REDIRECT_TYPE_PREFIX: {
3200 const char *path;
3201 int pathlen;
3202
3203 path = http_get_path(txn);
3204 /* build message using path */
3205 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003206 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003207 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3208 int qs = 0;
3209 while (qs < pathlen) {
3210 if (path[qs] == '?') {
3211 pathlen = qs;
3212 break;
3213 }
3214 qs++;
3215 }
3216 }
3217 } else {
3218 path = "/";
3219 pathlen = 1;
3220 }
3221
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003222 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003223 if (chunk->len + rule->rdr_len + pathlen > chunk->size - 4)
3224 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003225
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003226 /* add prefix. Note that if prefix == "/", we don't want to
3227 * add anything, otherwise it makes it hard for the user to
3228 * configure a self-redirection.
3229 */
3230 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003231 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3232 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003233 }
3234 }
3235 else {
3236 /* add prefix with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003237 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003238
3239 /* Check length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003240 if (chunk->len + pathlen > chunk->size - 4)
3241 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003242 }
3243
3244 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003245 memcpy(chunk->str + chunk->len, path, pathlen);
3246 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003247
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003248 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003249 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003250 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003251 if (chunk->len > chunk->size - 5)
3252 goto leave;
3253 chunk->str[chunk->len] = '/';
3254 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003255 }
3256
3257 break;
3258 }
3259 case REDIRECT_TYPE_LOCATION:
3260 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003261 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003262 if (chunk->len + rule->rdr_len > chunk->size - 4)
3263 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003264
3265 /* add location */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003266 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3267 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003268 }
3269 else {
3270 /* add location with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003271 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003272
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003273 /* Check left length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003274 if (chunk->len > chunk->size - 4)
3275 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003276 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003277 break;
3278 }
3279
3280 if (rule->cookie_len) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003281 memcpy(chunk->str + chunk->len, "\r\nSet-Cookie: ", 14);
3282 chunk->len += 14;
3283 memcpy(chunk->str + chunk->len, rule->cookie_str, rule->cookie_len);
3284 chunk->len += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003285 }
3286
Willy Tarreau19b14122017-02-28 09:48:11 +01003287 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003288 txn->status = rule->code;
3289 /* let's log the request time */
3290 s->logs.tv_request = now;
3291
Christopher Fauletbe821b92017-03-30 11:21:53 +02003292 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003293 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3294 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3295 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02003296 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003297 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003298 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: keep-alive", 30);
3299 chunk->len += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003300 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003301 memcpy(chunk->str + chunk->len, "\r\nConnection: keep-alive", 24);
3302 chunk->len += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003303 }
3304 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003305 memcpy(chunk->str + chunk->len, "\r\n\r\n", 4);
3306 chunk->len += 4;
3307 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau06d80a92017-10-19 14:32:15 +02003308 co_inject(res->chn, chunk->str, chunk->len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003309 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02003310 bi_fast_delete(req->chn->buf, req->sov);
3311 req->next -= req->sov;
3312 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003313 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01003314 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02003315 req->msg_state = HTTP_MSG_CLOSED;
3316 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02003317 /* Trim any possible response */
3318 res->chn->buf->i = 0;
3319 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02003320 /* let the server side turn to SI_ST_CLO */
3321 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003322 } else {
3323 /* keep-alive not possible */
3324 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003325 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3326 chunk->len += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003327 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003328 memcpy(chunk->str + chunk->len, "\r\nConnection: close\r\n\r\n", 23);
3329 chunk->len += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003330 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003331 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01003332 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003333 }
3334
Willy Tarreaue7dff022015-04-03 01:14:29 +02003335 if (!(s->flags & SF_ERR_MASK))
3336 s->flags |= SF_ERR_LOCAL;
3337 if (!(s->flags & SF_FINST_MASK))
3338 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003339
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003340 ret = 1;
3341 leave:
3342 free_trash_chunk(chunk);
3343 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003344}
3345
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003346/* This stream analyser runs all HTTP request processing which is common to
3347 * frontends and backends, which means blocking ACLs, filters, connection-close,
3348 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003349 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003350 * either needs more data or wants to immediately abort the request (eg: deny,
3351 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003352 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003353int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003354{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003355 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003356 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02003357 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003358 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003359 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02003360 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003361 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02003362 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02003363
Willy Tarreau655dce92009-11-08 13:10:58 +01003364 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003365 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003366 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02003367 }
3368
Willy Tarreau87b09662015-04-03 00:22:06 +02003369 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 +02003370 now_ms, __FUNCTION__,
3371 s,
3372 req,
3373 req->rex, req->wex,
3374 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003375 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003376 req->analysers);
3377
Willy Tarreau65410832014-04-28 21:25:43 +02003378 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02003379 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02003380
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003381 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02003382 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02003383 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01003384
Willy Tarreau0b748332014-04-29 00:13:29 +02003385 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003386 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
3387 goto return_prx_yield;
3388
Willy Tarreau0b748332014-04-29 00:13:29 +02003389 case HTTP_RULE_RES_CONT:
3390 case HTTP_RULE_RES_STOP: /* nothing to do */
3391 break;
Willy Tarreau52542592014-04-28 18:33:26 +02003392
Willy Tarreau0b748332014-04-29 00:13:29 +02003393 case HTTP_RULE_RES_DENY: /* deny or tarpit */
3394 if (txn->flags & TX_CLTARPIT)
3395 goto tarpit;
3396 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003397
Willy Tarreau0b748332014-04-29 00:13:29 +02003398 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
3399 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02003400
Willy Tarreau0b748332014-04-29 00:13:29 +02003401 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02003402 goto done;
3403
Willy Tarreau0b748332014-04-29 00:13:29 +02003404 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
3405 goto return_bad_req;
3406 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003407 }
3408
Olivier Houchardc2aae742017-09-22 18:26:28 +02003409 if (conn && conn->flags & CO_FL_EARLY_DATA) {
3410 struct hdr_ctx ctx;
3411
3412 ctx.idx = 0;
3413 if (!http_find_header2("Early-Data", strlen("Early-Data"),
3414 s->req.buf->p, &txn->hdr_idx, &ctx)) {
3415 if (unlikely(http_header_add_tail2(&txn->req,
3416 &txn->hdr_idx, "Early-Data: 1",
3417 strlen("Early-Data: 1"))) < 0) {
3418 goto return_bad_req;
3419 }
3420 }
3421
3422 }
3423
Willy Tarreau52542592014-04-28 18:33:26 +02003424 /* OK at this stage, we know that the request was accepted according to
3425 * the http-request rules, we can check for the stats. Note that the
3426 * URI is detected *before* the req* rules in order not to be affected
3427 * by a possible reqrep, while they are processed *after* so that a
3428 * reqdeny can still block them. This clearly needs to change in 1.6!
3429 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003430 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02003431 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01003432 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02003433 txn->status = 500;
3434 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003435 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003436
Willy Tarreaue7dff022015-04-03 01:14:29 +02003437 if (!(s->flags & SF_ERR_MASK))
3438 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02003439 goto return_prx_cond;
3440 }
3441
3442 /* parse the whole stats request and extract the relevant information */
3443 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02003444 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02003445 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003446
Willy Tarreau0b748332014-04-29 00:13:29 +02003447 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
3448 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003449
Willy Tarreau0b748332014-04-29 00:13:29 +02003450 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
3451 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003452 }
3453
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003454 /* evaluate the req* rules except reqadd */
3455 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003456 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003457 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003458
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003459 if (txn->flags & TX_CLDENY)
3460 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003461
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003462 if (txn->flags & TX_CLTARPIT) {
3463 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003464 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003465 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003466 }
Willy Tarreau06619262006-12-17 08:37:22 +01003467
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003468 /* add request headers from the rule sets in the same order */
3469 list_for_each_entry(wl, &px->req_add, list) {
3470 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003471 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003472 ret = acl_pass(ret);
3473 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3474 ret = !ret;
3475 if (!ret)
3476 continue;
3477 }
3478
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003479 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003480 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003481 }
3482
Willy Tarreau52542592014-04-28 18:33:26 +02003483
3484 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003485 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003486 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003487 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003488 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01003489
Willy Tarreaue7dff022015-04-03 01:14:29 +02003490 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
3491 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
3492 if (!(s->flags & SF_FINST_MASK))
3493 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003494
Willy Tarreau70730dd2014-04-24 18:06:27 +02003495 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01003496 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
3497 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003498 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003499 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003500 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003501
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003502 /* check whether we have some ACLs set to redirect this request */
3503 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003504 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003505 int ret;
3506
Willy Tarreau192252e2015-04-04 01:47:55 +02003507 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003508 ret = acl_pass(ret);
3509 if (rule->cond->pol == ACL_COND_UNLESS)
3510 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003511 if (!ret)
3512 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003513 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003514 if (!http_apply_redirect_rule(rule, s, txn))
3515 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003516 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003517 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003518
Willy Tarreau2be39392010-01-03 17:24:51 +01003519 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3520 * If this happens, then the data will not come immediately, so we must
3521 * send all what we have without waiting. Note that due to the small gain
3522 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003523 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003524 * itself once used.
3525 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003526 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003527
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003528 done: /* done with this analyser, continue with next ones that the calling
3529 * points will have set, if any.
3530 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003531 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003532 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
3533 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003534 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003535
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003536 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02003537 /* Allow cookie logging
3538 */
3539 if (s->be->cookie_name || sess->fe->capture_name)
3540 manage_client_side_cookies(s, req);
3541
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003542 /* When a connection is tarpitted, we use the tarpit timeout,
3543 * which may be the same as the connect timeout if unspecified.
3544 * If unset, then set it to zero because we really want it to
3545 * eventually expire. We build the tarpit as an analyser.
3546 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003547 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003548
3549 /* wipe the request out so that we can drop the connection early
3550 * if the client closes first.
3551 */
3552 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003553
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003554 txn->status = http_err_codes[deny_status];
3555
Christopher Faulet0184ea72017-01-05 14:06:34 +01003556 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003557 req->analysers |= AN_REQ_HTTP_TARPIT;
3558 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3559 if (!req->analyse_exp)
3560 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02003561 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003562 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003563 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003564 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003565 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003566 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003567 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003568
3569 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003570
3571 /* Allow cookie logging
3572 */
3573 if (s->be->cookie_name || sess->fe->capture_name)
3574 manage_client_side_cookies(s, req);
3575
Willy Tarreau0b748332014-04-29 00:13:29 +02003576 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003577 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003578 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003579 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02003580 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003581 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003582 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003583 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003584 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003585 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003586 goto return_prx_cond;
3587
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003588 return_bad_req:
3589 /* We centralize bad requests processing here */
3590 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3591 /* we detected a parsing error. We want to archive this request
3592 * in the dedicated proxy area for later troubleshooting.
3593 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02003594 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003595 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003596
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003597 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003598 txn->req.msg_state = HTTP_MSG_ERROR;
3599 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003600 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003601
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003602 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003603 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003604 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003605
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003606 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003607 if (!(s->flags & SF_ERR_MASK))
3608 s->flags |= SF_ERR_PRXCOND;
3609 if (!(s->flags & SF_FINST_MASK))
3610 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003611
Christopher Faulet0184ea72017-01-05 14:06:34 +01003612 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003613 req->analyse_exp = TICK_ETERNITY;
3614 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003615
3616 return_prx_yield:
3617 channel_dont_connect(req);
3618 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003619}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003620
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003621/* This function performs all the processing enabled for the current request.
3622 * It returns 1 if the processing can continue on next analysers, or zero if it
3623 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003624 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003625 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003626int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003627{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003628 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003629 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003630 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003631 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003632
Willy Tarreau655dce92009-11-08 13:10:58 +01003633 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003634 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003635 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003636 return 0;
3637 }
3638
Willy Tarreau87b09662015-04-03 00:22:06 +02003639 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 +02003640 now_ms, __FUNCTION__,
3641 s,
3642 req,
3643 req->rex, req->wex,
3644 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003645 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003646 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003647
Willy Tarreau59234e92008-11-30 23:51:27 +01003648 /*
3649 * Right now, we know that we have processed the entire headers
3650 * and that unwanted requests have been filtered out. We can do
3651 * whatever we want with the remaining request. Also, now we
3652 * may have separate values for ->fe, ->be.
3653 */
Willy Tarreau06619262006-12-17 08:37:22 +01003654
Willy Tarreau59234e92008-11-30 23:51:27 +01003655 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003656 * If HTTP PROXY is set we simply get remote server address parsing
3657 * incoming request. Note that this requires that a connection is
3658 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003659 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003660 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003661 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003662 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003663
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003664 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003665 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003666 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003667 txn->req.msg_state = HTTP_MSG_ERROR;
3668 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003669 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003670 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003671
Willy Tarreaue7dff022015-04-03 01:14:29 +02003672 if (!(s->flags & SF_ERR_MASK))
3673 s->flags |= SF_ERR_RESOURCE;
3674 if (!(s->flags & SF_FINST_MASK))
3675 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003676
3677 return 0;
3678 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003679
3680 path = http_get_path(txn);
3681 url2sa(req->buf->p + msg->sl.rq.u,
3682 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01003683 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003684 /* if the path was found, we have to remove everything between
3685 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
3686 * found, we need to replace from req->buf->p + msg->sl.rq.u for
3687 * u_l characters by a single "/".
3688 */
3689 if (path) {
3690 char *cur_ptr = req->buf->p;
3691 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3692 int delta;
3693
3694 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
3695 http_msg_move_end(&txn->req, delta);
3696 cur_end += delta;
3697 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3698 goto return_bad_req;
3699 }
3700 else {
3701 char *cur_ptr = req->buf->p;
3702 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3703 int delta;
3704
3705 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
3706 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
3707 http_msg_move_end(&txn->req, delta);
3708 cur_end += delta;
3709 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3710 goto return_bad_req;
3711 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003712 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003713
Willy Tarreau59234e92008-11-30 23:51:27 +01003714 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003715 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003716 * Note that doing so might move headers in the request, but
3717 * the fields will stay coherent and the URI will not move.
3718 * This should only be performed in the backend.
3719 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003720 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003721 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003722
William Lallemanda73203e2012-03-12 12:48:57 +01003723 /* add unique-id if "header-unique-id" is specified */
3724
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003725 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003726 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
3727 goto return_bad_req;
3728 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003729 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003730 }
William Lallemanda73203e2012-03-12 12:48:57 +01003731
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003732 if (sess->fe->header_unique_id && s->unique_id) {
3733 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003734 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003735 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003736 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003737 goto return_bad_req;
3738 }
3739
Cyril Bontéb21570a2009-11-29 20:04:48 +01003740 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003741 * 9: add X-Forwarded-For if either the frontend or the backend
3742 * asks for it.
3743 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003744 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003745 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003746 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3747 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3748 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003749 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003750 /* The header is set to be added only if none is present
3751 * and we found it, so don't do anything.
3752 */
3753 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003754 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003755 /* Add an X-Forwarded-For header unless the source IP is
3756 * in the 'except' network range.
3757 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003758 if ((!sess->fe->except_mask.s_addr ||
3759 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3760 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003761 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003762 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003763 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003764 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003765 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003766 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003767
3768 /* Note: we rely on the backend to get the header name to be used for
3769 * x-forwarded-for, because the header is really meant for the backends.
3770 * However, if the backend did not specify any option, we have to rely
3771 * on the frontend's header name.
3772 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003773 if (s->be->fwdfor_hdr_len) {
3774 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003775 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003776 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003777 len = sess->fe->fwdfor_hdr_len;
3778 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003779 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003780 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 +01003781
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003782 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003783 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003784 }
3785 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003786 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003787 /* FIXME: for the sake of completeness, we should also support
3788 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003789 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003790 int len;
3791 char pn[INET6_ADDRSTRLEN];
3792 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003793 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003794 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003795
Willy Tarreau59234e92008-11-30 23:51:27 +01003796 /* Note: we rely on the backend to get the header name to be used for
3797 * x-forwarded-for, because the header is really meant for the backends.
3798 * However, if the backend did not specify any option, we have to rely
3799 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003800 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003801 if (s->be->fwdfor_hdr_len) {
3802 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003803 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003804 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003805 len = sess->fe->fwdfor_hdr_len;
3806 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003807 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003808 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003809
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003810 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003811 goto return_bad_req;
3812 }
3813 }
3814
3815 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003816 * 10: add X-Original-To if either the frontend or the backend
3817 * asks for it.
3818 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003819 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003820
3821 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003822 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003823 /* Add an X-Original-To header unless the destination IP is
3824 * in the 'except' network range.
3825 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003826 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003827
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003828 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003829 ((!sess->fe->except_mask_to.s_addr ||
3830 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3831 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003832 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003833 (((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 +02003834 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003835 int len;
3836 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003837 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003838
3839 /* Note: we rely on the backend to get the header name to be used for
3840 * x-original-to, because the header is really meant for the backends.
3841 * However, if the backend did not specify any option, we have to rely
3842 * on the frontend's header name.
3843 */
3844 if (s->be->orgto_hdr_len) {
3845 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003846 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003847 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003848 len = sess->fe->orgto_hdr_len;
3849 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003850 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003851 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 +02003852
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003853 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003854 goto return_bad_req;
3855 }
3856 }
3857 }
3858
Willy Tarreau50fc7772012-11-11 22:19:57 +01003859 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3860 * If an "Upgrade" token is found, the header is left untouched in order not to have
3861 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3862 * "Upgrade" is present in the Connection header.
3863 */
3864 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3865 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003866 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003867 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003868 unsigned int want_flags = 0;
3869
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003870 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003871 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003872 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003873 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003874 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003875 want_flags |= TX_CON_CLO_SET;
3876 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003877 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003878 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003879 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003880 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003881 want_flags |= TX_CON_KAL_SET;
3882 }
3883
3884 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003885 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003886 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003887
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003888
Willy Tarreau522d6c02009-12-06 18:49:18 +01003889 /* If we have no server assigned yet and we're balancing on url_param
3890 * with a POST request, we may be interested in checking the body for
3891 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003892 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003893 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003894 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003895 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003896 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003897 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003898 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003899
Christopher Fauletbe821b92017-03-30 11:21:53 +02003900 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3901 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003902#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003903 /* We expect some data from the client. Unless we know for sure
3904 * we already have a full request, we have to re-enable quick-ack
3905 * in case we previously disabled it, otherwise we might cause
3906 * the client to delay further data.
3907 */
3908 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3909 cli_conn && conn_ctrl_ready(cli_conn) &&
3910 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
3911 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003912 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003913#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003914
Willy Tarreau59234e92008-11-30 23:51:27 +01003915 /*************************************************************
3916 * OK, that's finished for the headers. We have done what we *
3917 * could. Let's switch to the DATA state. *
3918 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003919 req->analyse_exp = TICK_ETERNITY;
3920 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003921
Willy Tarreau59234e92008-11-30 23:51:27 +01003922 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003923 /* OK let's go on with the BODY now */
3924 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003925
Willy Tarreau59234e92008-11-30 23:51:27 +01003926 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003927 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003928 /* we detected a parsing error. We want to archive this request
3929 * in the dedicated proxy area for later troubleshooting.
3930 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02003931 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003932 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003933
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003934 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003935 txn->req.msg_state = HTTP_MSG_ERROR;
3936 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003937 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003938 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003939
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003940 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003941 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003942 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003943
Willy Tarreaue7dff022015-04-03 01:14:29 +02003944 if (!(s->flags & SF_ERR_MASK))
3945 s->flags |= SF_ERR_PRXCOND;
3946 if (!(s->flags & SF_FINST_MASK))
3947 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003948 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003949}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003950
Willy Tarreau60b85b02008-11-30 23:28:40 +01003951/* This function is an analyser which processes the HTTP tarpit. It always
3952 * returns zero, at the beginning because it prevents any other processing
3953 * from occurring, and at the end because it terminates the request.
3954 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003955int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003956{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003957 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003958
3959 /* This connection is being tarpitted. The CLIENT side has
3960 * already set the connect expiration date to the right
3961 * timeout. We just have to check that the client is still
3962 * there and that the timeout has not expired.
3963 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003964 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003965 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003966 !tick_is_expired(req->analyse_exp, now_ms))
3967 return 0;
3968
3969 /* We will set the queue timer to the time spent, just for
3970 * logging purposes. We fake a 500 server error, so that the
3971 * attacker will not suspect his connection has been tarpitted.
3972 * It will not cause trouble to the logs because we can exclude
3973 * the tarpitted connections by filtering on the 'PT' status flags.
3974 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003975 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3976
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003977 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003978 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003979
Christopher Faulet0184ea72017-01-05 14:06:34 +01003980 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003981 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003982
Willy Tarreaue7dff022015-04-03 01:14:29 +02003983 if (!(s->flags & SF_ERR_MASK))
3984 s->flags |= SF_ERR_PRXCOND;
3985 if (!(s->flags & SF_FINST_MASK))
3986 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003987 return 0;
3988}
3989
Willy Tarreau5a8f9472014-04-10 11:16:06 +02003990/* This function is an analyser which waits for the HTTP request body. It waits
3991 * for either the buffer to be full, or the full advertised contents to have
3992 * reached the buffer. It must only be called after the standard HTTP request
3993 * processing has occurred, because it expects the request to be parsed and will
3994 * look for the Expect header. It may send a 100-Continue interim response. It
3995 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
3996 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
3997 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01003998 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003999int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004000{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004001 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004002 struct http_txn *txn = s->txn;
4003 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004004
4005 /* We have to parse the HTTP request body to find any required data.
4006 * "balance url_param check_post" should have been the only way to get
4007 * into this. We were brought here after HTTP header analysis, so all
4008 * related structures are ready.
4009 */
4010
Willy Tarreau890988f2014-04-10 11:59:33 +02004011 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4012 /* This is the first call */
4013 if (msg->msg_state < HTTP_MSG_BODY)
4014 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004015
Willy Tarreau890988f2014-04-10 11:59:33 +02004016 if (msg->msg_state < HTTP_MSG_100_SENT) {
4017 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4018 * send an HTTP/1.1 100 Continue intermediate response.
4019 */
4020 if (msg->flags & HTTP_MSGF_VER_11) {
4021 struct hdr_ctx ctx;
4022 ctx.idx = 0;
4023 /* Expect is allowed in 1.1, look for it */
4024 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4025 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02004026 co_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01004027 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02004028 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004029 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004030 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004031 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004032
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004033 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004034 * req->buf->p still points to the beginning of the message. We
4035 * must save the body in msg->next because it survives buffer
4036 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004037 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004038 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004039
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004040 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004041 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4042 else
4043 msg->msg_state = HTTP_MSG_DATA;
4044 }
4045
Willy Tarreau890988f2014-04-10 11:59:33 +02004046 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4047 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004048 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004049 goto missing_data;
4050
4051 /* OK we have everything we need now */
4052 goto http_end;
4053 }
4054
4055 /* OK here we're parsing a chunked-encoded message */
4056
Willy Tarreau522d6c02009-12-06 18:49:18 +01004057 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004058 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004059 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004060 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004061 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004062 unsigned int chunk;
4063 int ret = h1_parse_chunk_size(req->buf, msg->next, req->buf->i, &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01004064
Willy Tarreau115acb92009-12-26 13:56:06 +01004065 if (!ret)
4066 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004067 else if (ret < 0) {
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004068 msg->err_pos = req->buf->i + ret;
4069 if (msg->err_pos < 0)
4070 msg->err_pos += req->buf->size;
Willy Tarreau87b09662015-04-03 00:22:06 +02004071 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004072 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004073 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004074
4075 msg->chunk_len = chunk;
4076 msg->body_len += chunk;
4077
4078 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01004079 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004080 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01004081 }
4082
Willy Tarreaud98cf932009-12-27 22:54:55 +01004083 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004084 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4085 * for at least a whole chunk or the whole content length bytes after
4086 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004087 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004088 if (msg->msg_state == HTTP_MSG_TRAILERS)
4089 goto http_end;
4090
Willy Tarreaue115b492015-05-01 23:05:14 +02004091 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004092 goto http_end;
4093
4094 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004095 /* we get here if we need to wait for more data. If the buffer is full,
4096 * we have the maximum we can expect.
4097 */
4098 if (buffer_full(req->buf, global.tune.maxrewrite))
4099 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004100
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004101 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004102 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004103 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004104
Willy Tarreaue7dff022015-04-03 01:14:29 +02004105 if (!(s->flags & SF_ERR_MASK))
4106 s->flags |= SF_ERR_CLITO;
4107 if (!(s->flags & SF_FINST_MASK))
4108 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004109 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004110 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004111
4112 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004113 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004114 /* Not enough data. We'll re-use the http-request
4115 * timeout here. Ideally, we should set the timeout
4116 * relative to the accept() date. We just set the
4117 * request timeout once at the beginning of the
4118 * request.
4119 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004120 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004121 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004122 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004123 return 0;
4124 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004125
4126 http_end:
4127 /* The situation will not evolve, so let's give up on the analysis. */
4128 s->logs.tv_request = now; /* update the request timer to reflect full request */
4129 req->analysers &= ~an_bit;
4130 req->analyse_exp = TICK_ETERNITY;
4131 return 1;
4132
4133 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004134 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004135 txn->req.msg_state = HTTP_MSG_ERROR;
4136 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004137 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004138
Willy Tarreaue7dff022015-04-03 01:14:29 +02004139 if (!(s->flags & SF_ERR_MASK))
4140 s->flags |= SF_ERR_PRXCOND;
4141 if (!(s->flags & SF_FINST_MASK))
4142 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004143
Willy Tarreau522d6c02009-12-06 18:49:18 +01004144 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01004145 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004146 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004147 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004148 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004149 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004150}
4151
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004152/* send a server's name with an outgoing request over an established connection.
4153 * Note: this function is designed to be called once the request has been scheduled
4154 * for being forwarded. This is the reason why it rewinds the buffer before
4155 * proceeding.
4156 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004157int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004158
4159 struct hdr_ctx ctx;
4160
Mark Lamourinec2247f02012-01-04 13:02:01 -05004161 char *hdr_name = be->server_id_hdr_name;
4162 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004163 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004164 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004165 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004166
William Lallemandd9e90662012-01-30 17:27:17 +01004167 ctx.idx = 0;
4168
Willy Tarreau211cdec2014-04-17 20:18:08 +02004169 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004170 if (old_o) {
4171 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004172 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004173 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004174 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004175 }
4176
Willy Tarreau9b28e032012-10-12 23:49:43 +02004177 old_i = chn->buf->i;
4178 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 -05004179 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004180 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004181 }
4182
4183 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004184 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004185 memcpy(hdr_val, hdr_name, hdr_name_len);
4186 hdr_val += hdr_name_len;
4187 *hdr_val++ = ':';
4188 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004189 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4190 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004191
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004192 if (old_o) {
4193 /* If this was a forwarded request, we must readjust the amount of
4194 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004195 * variations. Note that the current state is >= HTTP_MSG_BODY,
4196 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004197 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004198 old_o += chn->buf->i - old_i;
4199 b_adv(chn->buf, old_o);
4200 txn->req.next -= old_o;
4201 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004202 }
4203
Mark Lamourinec2247f02012-01-04 13:02:01 -05004204 return 0;
4205}
4206
Willy Tarreau610ecce2010-01-04 21:15:02 +01004207/* Terminate current transaction and prepare a new one. This is very tricky
4208 * right now but it works.
4209 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004210void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004211{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004212 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02004213 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01004214 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004215 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02004216 struct connection *srv_conn;
4217 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02004218 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01004219
Willy Tarreau610ecce2010-01-04 21:15:02 +01004220 /* FIXME: We need a more portable way of releasing a backend's and a
4221 * server's connections. We need a safer way to reinitialize buffer
4222 * flags. We also need a more accurate method for computing per-request
4223 * data.
4224 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004225 /*
4226 * XXX cognet: This is probably wrong, this is killing a whole
4227 * connection, in the new world order, we probably want to just kill
4228 * the stream, this is to be revisited the day we handle multiple
4229 * streams in one server connection.
4230 */
4231 cs = objt_cs(s->si[1].end);
4232 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004233
Willy Tarreau4213a112013-12-15 10:25:42 +01004234 /* unless we're doing keep-alive, we want to quickly close the connection
4235 * to the server.
4236 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004237 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004238 !si_conn_ready(&s->si[1])) {
4239 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4240 si_shutr(&s->si[1]);
4241 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004242 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004243
Willy Tarreaue7dff022015-04-03 01:14:29 +02004244 if (s->flags & SF_BE_ASSIGNED) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004245 HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004246 if (unlikely(s->srv_conn))
4247 sess_change_server(s, NULL);
4248 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004249
4250 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02004251 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004252
Willy Tarreaueee5b512015-04-03 23:46:31 +02004253 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004254 int n;
4255
Willy Tarreaueee5b512015-04-03 23:46:31 +02004256 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004257 if (n < 1 || n > 5)
4258 n = 0;
4259
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004260 if (fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004261 HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004262 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02004263 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01004264 (be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004265 HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
4266 HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004267 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004268 }
4269
4270 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004271 s->logs.bytes_in -= s->req.buf->i;
4272 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004273
4274 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004275 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02004276 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004277 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004278 s->do_log(s);
4279 }
4280
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004281 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02004282 stream_stop_content_counters(s);
4283 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004284
Willy Tarreau610ecce2010-01-04 21:15:02 +01004285 s->logs.accept_date = date; /* user-visible date for logging */
4286 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02004287 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
4288 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004289 tv_zero(&s->logs.tv_request);
4290 s->logs.t_queue = -1;
4291 s->logs.t_connect = -1;
4292 s->logs.t_data = -1;
4293 s->logs.t_close = 0;
4294 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4295 s->logs.srv_queue_size = 0; /* we will get this number soon */
4296
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004297 s->logs.bytes_in = s->req.total = s->req.buf->i;
4298 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004299
4300 if (s->pend_pos)
4301 pendconn_free(s->pend_pos);
4302
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004303 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004304 if (s->flags & SF_CURR_SESS) {
4305 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +02004306 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004307 }
Willy Tarreau858b1032015-12-07 17:04:59 +01004308 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004309 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004310 }
4311
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004312 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004313
Willy Tarreau4213a112013-12-15 10:25:42 +01004314 /* only release our endpoint if we don't intend to reuse the
4315 * connection.
4316 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004317 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004318 !si_conn_ready(&s->si[1])) {
4319 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02004320 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004321 }
4322
Willy Tarreau350f4872014-11-28 14:42:25 +01004323 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
4324 s->si[1].err_type = SI_ET_NONE;
4325 s->si[1].conn_retries = 0; /* used for logging too */
4326 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02004327 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 +01004328 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);
4329 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);
Willy Tarreaue7dff022015-04-03 01:14:29 +02004330 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
4331 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
4332 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01004333
Willy Tarreaueee5b512015-04-03 23:46:31 +02004334 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004335 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004336 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004337
4338 if (prev_status == 401 || prev_status == 407) {
4339 /* In HTTP keep-alive mode, if we receive a 401, we still have
4340 * a chance of being able to send the visitor again to the same
4341 * server over the same connection. This is required by some
4342 * broken protocols such as NTLM, and anyway whenever there is
4343 * an opportunity for sending the challenge to the proper place,
4344 * it's better to do it (at least it helps with debugging).
4345 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004346 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02004347 if (srv_conn)
4348 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01004349 }
4350
Willy Tarreau53f96852016-02-02 18:50:47 +01004351 /* Never ever allow to reuse a connection from a non-reuse backend */
4352 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
4353 srv_conn->flags |= CO_FL_PRIVATE;
4354
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004355 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01004356 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004357
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004358 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004359 s->req.flags |= CF_NEVER_WAIT;
4360 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004361 }
4362
Willy Tarreau714ea782015-11-25 20:11:11 +01004363 /* we're removing the analysers, we MUST re-enable events detection.
4364 * We don't enable close on the response channel since it's either
4365 * already closed, or in keep-alive with an idle connection handler.
4366 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004367 channel_auto_read(&s->req);
4368 channel_auto_close(&s->req);
4369 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004370
Willy Tarreau1c59bd52015-11-02 20:20:11 +01004371 /* we're in keep-alive with an idle connection, monitor it if not already done */
4372 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02004373 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02004374 if (!srv)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004375 si_idle_cs(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01004376 else if (srv_conn->flags & CO_FL_PRIVATE)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004377 si_idle_cs(&s->si[1], (srv->priv_conns ? &srv->priv_conns[tid] : NULL));
Willy Tarreau449d74a2015-08-05 17:16:33 +02004378 else if (prev_flags & TX_NOT_FIRST)
4379 /* note: we check the request, not the connection, but
4380 * this is valid for strategies SAFE and AGGR, and in
4381 * case of ALWS, we don't care anyway.
4382 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004383 si_idle_cs(&s->si[1], (srv->safe_conns ? &srv->safe_conns[tid] : NULL));
Willy Tarreau8dff9982015-08-04 20:45:52 +02004384 else
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004385 si_idle_cs(&s->si[1], (srv->idle_conns ? &srv->idle_conns[tid] : NULL));
Willy Tarreau4320eaa2015-08-05 11:08:30 +02004386 }
Christopher Faulete6006242017-03-10 11:52:44 +01004387 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
4388 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004389}
4390
4391
4392/* This function updates the request state machine according to the response
4393 * state machine and buffer flags. It returns 1 if it changes anything (flag
4394 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4395 * it is only used to find when a request/response couple is complete. Both
4396 * this function and its equivalent should loop until both return zero. It
4397 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4398 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004399int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004400{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004401 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004402 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004403 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004404 unsigned int old_state = txn->req.msg_state;
4405
Christopher Faulet4be98032017-07-18 10:48:24 +02004406 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004407 return 0;
4408
4409 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004410 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004411 * We can shut the read side unless we want to abort_on_close,
4412 * or we have a POST request. The issue with POST requests is
4413 * that some browsers still send a CRLF after the request, and
4414 * this CRLF must be read so that it does not remain in the kernel
4415 * buffers, otherwise a close could cause an RST on some systems
4416 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004417 * Note that if we're using keep-alive on the client side, we'd
4418 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02004419 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01004420 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004421 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004422 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4423 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4424 !(s->be->options & PR_O_ABRT_CLOSE) &&
4425 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004426 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004427
Willy Tarreau40f151a2012-12-20 12:10:09 +01004428 /* if the server closes the connection, we want to immediately react
4429 * and close the socket to save packets and syscalls.
4430 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004431 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01004432
Willy Tarreau7f876a12015-11-18 11:59:55 +01004433 /* In any case we've finished parsing the request so we must
4434 * disable Nagle when sending data because 1) we're not going
4435 * to shut this side, and 2) the server is waiting for us to
4436 * send pending data.
4437 */
4438 chn->flags |= CF_NEVER_WAIT;
4439
Willy Tarreau610ecce2010-01-04 21:15:02 +01004440 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4441 goto wait_other_side;
4442
4443 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4444 /* The server has not finished to respond, so we
4445 * don't want to move in order not to upset it.
4446 */
4447 goto wait_other_side;
4448 }
4449
Willy Tarreau610ecce2010-01-04 21:15:02 +01004450 /* When we get here, it means that both the request and the
4451 * response have finished receiving. Depending on the connection
4452 * mode, we'll have to wait for the last bytes to leave in either
4453 * direction, and sometimes for a close to be effective.
4454 */
4455
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004456 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4457 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004458 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4459 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004460 }
4461 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4462 /* Option forceclose is set, or either side wants to close,
4463 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004464 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004465 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004466 *
4467 * However, there is an exception if the response
4468 * length is undefined. In this case, we need to wait
4469 * the close from the server. The response will be
4470 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004471 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004472 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4473 txn->rsp.msg_state != HTTP_MSG_CLOSED)
4474 goto check_channel_flags;
4475
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004476 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4477 channel_shutr_now(chn);
4478 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004479 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004480 }
4481 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004482 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4483 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004484 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004485 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4486 channel_auto_read(chn);
4487 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004488 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004489 }
4490
Christopher Faulet4be98032017-07-18 10:48:24 +02004491 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004492 }
4493
4494 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4495 http_msg_closing:
4496 /* nothing else to forward, just waiting for the output buffer
4497 * to be empty and for the shutw_now to take effect.
4498 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004499 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004500 txn->req.msg_state = HTTP_MSG_CLOSED;
4501 goto http_msg_closed;
4502 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004503 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004504 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004505 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004506 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004507 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004508 }
4509
4510 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4511 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01004512 /* see above in MSG_DONE why we only do this in these states */
4513 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4514 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4515 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004516 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004517 goto wait_other_side;
4518 }
4519
Christopher Faulet4be98032017-07-18 10:48:24 +02004520 check_channel_flags:
4521 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4522 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4523 /* if we've just closed an output, let's switch */
4524 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4525 txn->req.msg_state = HTTP_MSG_CLOSING;
4526 goto http_msg_closing;
4527 }
4528
4529
Willy Tarreau610ecce2010-01-04 21:15:02 +01004530 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004531 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004532}
4533
4534
4535/* This function updates the response state machine according to the request
4536 * state machine and buffer flags. It returns 1 if it changes anything (flag
4537 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4538 * it is only used to find when a request/response couple is complete. Both
4539 * this function and its equivalent should loop until both return zero. It
4540 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4541 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004542int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004543{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004544 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004545 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004546 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004547 unsigned int old_state = txn->rsp.msg_state;
4548
Christopher Faulet4be98032017-07-18 10:48:24 +02004549 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004550 return 0;
4551
4552 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4553 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004554 * still monitor the server connection for a possible close
4555 * while the request is being uploaded, so we don't disable
4556 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004557 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004558 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004559
4560 if (txn->req.msg_state == HTTP_MSG_ERROR)
4561 goto wait_other_side;
4562
4563 if (txn->req.msg_state < HTTP_MSG_DONE) {
4564 /* The client seems to still be sending data, probably
4565 * because we got an error response during an upload.
4566 * We have the choice of either breaking the connection
4567 * or letting it pass through. Let's do the later.
4568 */
4569 goto wait_other_side;
4570 }
4571
Willy Tarreau610ecce2010-01-04 21:15:02 +01004572 /* When we get here, it means that both the request and the
4573 * response have finished receiving. Depending on the connection
4574 * mode, we'll have to wait for the last bytes to leave in either
4575 * direction, and sometimes for a close to be effective.
4576 */
4577
4578 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4579 /* Server-close mode : shut read and wait for the request
4580 * side to close its output buffer. The caller will detect
4581 * when we're in DONE and the other is in CLOSED and will
4582 * catch that for the final cleanup.
4583 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004584 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4585 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004586 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004587 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4588 /* Option forceclose is set, or either side wants to close,
4589 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004590 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004591 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004592 *
4593 * However, there is an exception if the response length
4594 * is undefined. In this case, we switch in TUNNEL mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004595 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004596 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN)) {
4597 channel_auto_read(chn);
4598 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4599 chn->flags |= CF_NEVER_WAIT;
4600 }
4601 else if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004602 channel_shutr_now(chn);
4603 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004604 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004605 }
4606 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004607 /* The last possible modes are keep-alive and tunnel. Tunnel will
4608 * need to forward remaining data. Keep-alive will need to monitor
4609 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004610 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004611 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004612 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004613 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4614 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004615 }
4616
Christopher Faulet4be98032017-07-18 10:48:24 +02004617 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004618 }
4619
4620 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4621 http_msg_closing:
4622 /* nothing else to forward, just waiting for the output buffer
4623 * to be empty and for the shutw_now to take effect.
4624 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004625 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004626 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4627 goto http_msg_closed;
4628 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004629 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004630 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004631 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004632 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004633 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004634 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004635 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004636 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004637 }
4638
4639 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4640 http_msg_closed:
4641 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004642 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004643 channel_auto_close(chn);
4644 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004645 goto wait_other_side;
4646 }
4647
Christopher Faulet4be98032017-07-18 10:48:24 +02004648 check_channel_flags:
4649 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4650 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4651 /* if we've just closed an output, let's switch */
4652 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4653 goto http_msg_closing;
4654 }
4655
Willy Tarreau610ecce2010-01-04 21:15:02 +01004656 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004657 /* We force the response to leave immediately if we're waiting for the
4658 * other side, since there is no pending shutdown to push it out.
4659 */
4660 if (!channel_is_empty(chn))
4661 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004662 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004663}
4664
4665
Christopher Faulet894da4c2017-07-18 11:29:07 +02004666/* Resync the request and response state machines. */
4667void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004668{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004669 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004670#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004671 int old_req_state = txn->req.msg_state;
4672 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004673#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004674
Willy Tarreau610ecce2010-01-04 21:15:02 +01004675 http_sync_req_state(s);
4676 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004677 if (!http_sync_res_state(s))
4678 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004679 if (!http_sync_req_state(s))
4680 break;
4681 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004682
Christopher Faulet894da4c2017-07-18 11:29:07 +02004683 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4684 "req->analysers=0x%08x res->analysers=0x%08x\n",
4685 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004686 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4687 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004688 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004689
4690
Willy Tarreau610ecce2010-01-04 21:15:02 +01004691 /* OK, both state machines agree on a compatible state.
4692 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004693 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4694 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004695 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4696 * means we must abort the request.
4697 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4698 * corresponding channel.
4699 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4700 * on the response with server-close mode means we've completed one
4701 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004702 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004703 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4704 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004705 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004706 channel_auto_close(&s->req);
4707 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004708 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004709 channel_auto_close(&s->res);
4710 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004711 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004712 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4713 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004714 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004715 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004716 channel_auto_close(&s->res);
4717 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004718 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004719 channel_abort(&s->req);
4720 channel_auto_close(&s->req);
4721 channel_auto_read(&s->req);
4722 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004723 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004724 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4725 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4726 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4727 s->req.analysers &= AN_REQ_FLT_END;
4728 if (HAS_REQ_DATA_FILTERS(s))
4729 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4730 }
4731 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4732 s->res.analysers &= AN_RES_FLT_END;
4733 if (HAS_RSP_DATA_FILTERS(s))
4734 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4735 }
4736 channel_auto_close(&s->req);
4737 channel_auto_read(&s->req);
4738 channel_auto_close(&s->res);
4739 channel_auto_read(&s->res);
4740 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004741 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4742 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004743 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004744 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4745 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4746 /* server-close/keep-alive: terminate this transaction,
4747 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004748 * a fresh-new transaction, but only once we're sure there's
4749 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004750 * another request. They must not hold any pending output data
4751 * and the response buffer must realigned
4752 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004753 */
Christopher Faulet894da4c2017-07-18 11:29:07 +02004754 if (s->req.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004755 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004756 else if (s->res.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004757 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004758 else {
4759 s->req.analysers = AN_REQ_FLT_END;
4760 s->res.analysers = AN_RES_FLT_END;
4761 txn->flags |= TX_WAIT_CLEANUP;
4762 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004763 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004764}
4765
Willy Tarreaud98cf932009-12-27 22:54:55 +01004766/* This function is an analyser which forwards request body (including chunk
4767 * sizes if any). It is called as soon as we must forward, even if we forward
4768 * zero byte. The only situation where it must not be called is when we're in
4769 * tunnel mode and we want to forward till the close. It's used both to forward
4770 * remaining data and to resync after end of body. It expects the msg_state to
4771 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004772 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004773 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004774 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004775 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004776int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004777{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004778 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004779 struct http_txn *txn = s->txn;
4780 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004781 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004782
Christopher Faulet814d2702017-03-30 11:33:44 +02004783 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
4784 now_ms, __FUNCTION__,
4785 s,
4786 req,
4787 req->rex, req->wex,
4788 req->flags,
4789 req->buf->i,
4790 req->analysers);
4791
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004792 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4793 return 0;
4794
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004795 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004796 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004797 /* Output closed while we were sending data. We must abort and
4798 * wake the other side up.
4799 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004800 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004801 msg->msg_state = HTTP_MSG_ERROR;
4802 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004803 return 1;
4804 }
4805
Willy Tarreaud98cf932009-12-27 22:54:55 +01004806 /* Note that we don't have to send 100-continue back because we don't
4807 * need the data to complete our job, and it's up to the server to
4808 * decide whether to return 100, 417 or anything else in return of
4809 * an "Expect: 100-continue" header.
4810 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004811 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004812 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4813 ? HTTP_MSG_CHUNK_SIZE
4814 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004815
4816 /* TODO/filters: when http-buffer-request option is set or if a
4817 * rule on url_param exists, the first chunk size could be
4818 * already parsed. In that case, msg->next is after the chunk
4819 * size (including the CRLF after the size). So this case should
4820 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004821 }
4822
Willy Tarreau7ba23542014-04-17 21:50:00 +02004823 /* Some post-connect processing might want us to refrain from starting to
4824 * forward data. Currently, the only reason for this is "balance url_param"
4825 * whichs need to parse/process the request after we've enabled forwarding.
4826 */
4827 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004828 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004829 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004830 req->flags |= CF_WAKE_CONNECT;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004831 goto missing_data_or_waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004832 }
4833 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4834 }
4835
Willy Tarreau80a92c02014-03-12 10:41:13 +01004836 /* in most states, we should abort in case of early close */
4837 channel_auto_close(req);
4838
Willy Tarreauefdf0942014-04-24 20:08:57 +02004839 if (req->to_forward) {
4840 /* We can't process the buffer's contents yet */
4841 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004842 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004843 }
4844
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004845 if (msg->msg_state < HTTP_MSG_DONE) {
4846 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4847 ? http_msg_forward_chunked_body(s, msg)
4848 : http_msg_forward_body(s, msg));
4849 if (!ret)
4850 goto missing_data_or_waiting;
4851 if (ret < 0)
4852 goto return_bad_req;
4853 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004854
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004855 /* other states, DONE...TUNNEL */
4856 /* we don't want to forward closes on DONE except in tunnel mode. */
4857 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4858 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004859
Christopher Faulet894da4c2017-07-18 11:29:07 +02004860 http_resync_states(s);
4861 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004862 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4863 if (req->flags & CF_SHUTW) {
4864 /* request errors are most likely due to the
4865 * server aborting the transfer. */
4866 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004867 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004868 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02004869 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004870 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004871 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004872 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004873 }
4874
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004875 /* If "option abortonclose" is set on the backend, we want to monitor
4876 * the client's connection and forward any shutdown notification to the
4877 * server, which will decide whether to close or to go on processing the
4878 * request. We only do that in tunnel mode, and not in other modes since
4879 * it can be abused to exhaust source ports. */
4880 if (s->be->options & PR_O_ABRT_CLOSE) {
4881 channel_auto_read(req);
4882 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4883 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4884 s->si[1].flags |= SI_FL_NOLINGER;
4885 channel_auto_close(req);
4886 }
4887 else if (s->txn->meth == HTTP_METH_POST) {
4888 /* POST requests may require to read extra CRLF sent by broken
4889 * browsers and which could cause an RST to be sent upon close
4890 * on some systems (eg: Linux). */
4891 channel_auto_read(req);
4892 }
4893 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004894
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004895 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004896 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004897 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004898 if (!(s->flags & SF_ERR_MASK))
4899 s->flags |= SF_ERR_CLICL;
4900 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004901 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004902 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004903 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004904 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004905 }
4906
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004907 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4908 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004909 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004910 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004911
4912 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004913 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004914
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004915 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004916 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004917 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004918
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004919 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004920 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004921 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004922 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004923 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004924
Willy Tarreau5c620922011-05-11 19:56:11 +02004925 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004926 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004927 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004928 * modes are already handled by the stream sock layer. We must not do
4929 * this in content-length mode because it could present the MSG_MORE
4930 * flag with the last block of forwarded data, which would cause an
4931 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004932 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004933 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004934 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004935
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004936 return 0;
4937
Willy Tarreaud98cf932009-12-27 22:54:55 +01004938 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004939 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004940 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004941 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02004942
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004943 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004944 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004945 txn->req.msg_state = HTTP_MSG_ERROR;
4946 if (txn->status) {
4947 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004948 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004949 } else {
4950 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004951 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004952 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004953 req->analysers &= AN_REQ_FLT_END;
4954 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 +01004955
Willy Tarreaue7dff022015-04-03 01:14:29 +02004956 if (!(s->flags & SF_ERR_MASK))
4957 s->flags |= SF_ERR_PRXCOND;
4958 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004959 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004960 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004961 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004962 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004963 }
4964 return 0;
4965
4966 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004967 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004968 txn->req.msg_state = HTTP_MSG_ERROR;
4969 if (txn->status) {
4970 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004971 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004972 } else {
4973 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004974 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004975 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004976 req->analysers &= AN_REQ_FLT_END;
4977 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 +01004978
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004979 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
4980 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004981 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004982 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004983
Willy Tarreaue7dff022015-04-03 01:14:29 +02004984 if (!(s->flags & SF_ERR_MASK))
4985 s->flags |= SF_ERR_SRVCL;
4986 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004987 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004988 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004989 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004990 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004991 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004992 return 0;
4993}
4994
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004995/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4996 * processing can continue on next analysers, or zero if it either needs more
4997 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004998 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004999 * when it has nothing left to do, and may remove any analyser when it wants to
5000 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005001 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005002int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005003{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005004 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005005 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005006 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005007 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005008 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005009 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005010 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005011
Willy Tarreau87b09662015-04-03 00:22:06 +02005012 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 +02005013 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005014 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005015 rep,
5016 rep->rex, rep->wex,
5017 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005018 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005019 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005020
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005021 /*
5022 * Now parse the partial (or complete) lines.
5023 * We will check the response syntax, and also join multi-line
5024 * headers. An index of all the lines will be elaborated while
5025 * parsing.
5026 *
5027 * For the parsing, we use a 28 states FSM.
5028 *
5029 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005030 * rep->buf->p = beginning of response
5031 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5032 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005033 * msg->eol = end of current header or line (LF or CRLF)
5034 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005035 */
5036
Willy Tarreau628c40c2014-04-24 19:11:26 +02005037 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005038 /* There's a protected area at the end of the buffer for rewriting
5039 * purposes. We don't want to start to parse the request if the
5040 * protected area is affected, because we may have to move processed
5041 * data later, which is much more complicated.
5042 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005043 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005044 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005045 /* some data has still not left the buffer, wake us once that's done */
5046 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5047 goto abort_response;
5048 channel_dont_close(rep);
5049 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005050 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005051 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005052 }
5053
Willy Tarreau379357a2013-06-08 12:55:46 +02005054 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5055 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5056 buffer_slow_realign(rep->buf);
5057
Willy Tarreau9b28e032012-10-12 23:49:43 +02005058 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005059 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005060 }
5061
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005062 /* 1: we might have to print this header in debug mode */
5063 if (unlikely((global.mode & MODE_DEBUG) &&
5064 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005065 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005066 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005067
Willy Tarreau9b28e032012-10-12 23:49:43 +02005068 sol = rep->buf->p;
5069 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005070 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005071
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005072 sol += hdr_idx_first_pos(&txn->hdr_idx);
5073 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005074
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005075 while (cur_idx) {
5076 eol = sol + txn->hdr_idx.v[cur_idx].len;
5077 debug_hdr("srvhdr", s, sol, eol);
5078 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5079 cur_idx = txn->hdr_idx.v[cur_idx].next;
5080 }
5081 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005082
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005083 /*
5084 * Now we quickly check if we have found a full valid response.
5085 * If not so, we check the FD and buffer states before leaving.
5086 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005087 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005088 * responses are checked first.
5089 *
5090 * Depending on whether the client is still there or not, we
5091 * may send an error response back or not. Note that normally
5092 * we should only check for HTTP status there, and check I/O
5093 * errors somewhere else.
5094 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005095
Willy Tarreau655dce92009-11-08 13:10:58 +01005096 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005097 /* Invalid response */
5098 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5099 /* we detected a parsing error. We want to archive this response
5100 * in the dedicated proxy area for later troubleshooting.
5101 */
5102 hdr_response_bad:
5103 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005104 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005105
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005106 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005107 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005108 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005109 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005110 }
Willy Tarreau64648412010-03-05 10:41:54 +01005111 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005112 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005113 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005114 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005115 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005116 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005117 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005118
Willy Tarreaue7dff022015-04-03 01:14:29 +02005119 if (!(s->flags & SF_ERR_MASK))
5120 s->flags |= SF_ERR_PRXCOND;
5121 if (!(s->flags & SF_FINST_MASK))
5122 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005123
5124 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005125 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005126
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005127 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005128 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005129 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005130 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005131 goto hdr_response_bad;
5132 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005133
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005134 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005135 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005136 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005137 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005138 else if (txn->flags & TX_NOT_FIRST)
5139 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005140
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005141 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005142 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005143 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005144 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005145 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005146
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005147 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005148 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005149 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01005150
5151 /* Check to see if the server refused the early data.
5152 * If so, just send a 425
5153 */
5154 if (objt_cs(s->si[1].end)) {
5155 struct connection *conn = objt_cs(s->si[1].end)->conn;
5156
5157 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
5158 txn->status = 425;
5159 }
5160
Willy Tarreau350f4872014-11-28 14:42:25 +01005161 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005162 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005163 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02005164
Willy Tarreaue7dff022015-04-03 01:14:29 +02005165 if (!(s->flags & SF_ERR_MASK))
5166 s->flags |= SF_ERR_SRVCL;
5167 if (!(s->flags & SF_FINST_MASK))
5168 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005169 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005170 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005171
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005172 /* read timeout : return a 504 to the client. */
5173 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005174 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005175 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005176
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005177 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005178 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005179 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005180 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005181 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005182
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005183 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005184 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005185 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005186 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005187 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005188 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02005189
Willy Tarreaue7dff022015-04-03 01:14:29 +02005190 if (!(s->flags & SF_ERR_MASK))
5191 s->flags |= SF_ERR_SRVTO;
5192 if (!(s->flags & SF_FINST_MASK))
5193 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005194 return 0;
5195 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005196
Willy Tarreauf003d372012-11-26 13:35:37 +01005197 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005198 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005199 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5200 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01005201 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005202 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01005203
Christopher Faulet0184ea72017-01-05 14:06:34 +01005204 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01005205 channel_auto_close(rep);
5206
5207 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005208 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005209 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01005210
Willy Tarreaue7dff022015-04-03 01:14:29 +02005211 if (!(s->flags & SF_ERR_MASK))
5212 s->flags |= SF_ERR_CLICL;
5213 if (!(s->flags & SF_FINST_MASK))
5214 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01005215
Willy Tarreau87b09662015-04-03 00:22:06 +02005216 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01005217 return 0;
5218 }
5219
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005220 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005221 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005222 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005223 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005224 else if (txn->flags & TX_NOT_FIRST)
5225 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005226
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005227 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005228 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005229 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005230 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005231 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005232
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005233 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005234 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005235 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005236 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005237 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005238 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005239
Willy Tarreaue7dff022015-04-03 01:14:29 +02005240 if (!(s->flags & SF_ERR_MASK))
5241 s->flags |= SF_ERR_SRVCL;
5242 if (!(s->flags & SF_FINST_MASK))
5243 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005244 return 0;
5245 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005246
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005247 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005248 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005249 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005250 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005251 else if (txn->flags & TX_NOT_FIRST)
5252 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005253
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005254 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005255 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005256 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005257
Willy Tarreaue7dff022015-04-03 01:14:29 +02005258 if (!(s->flags & SF_ERR_MASK))
5259 s->flags |= SF_ERR_CLICL;
5260 if (!(s->flags & SF_FINST_MASK))
5261 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005262
Willy Tarreau87b09662015-04-03 00:22:06 +02005263 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005264 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005265 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005266
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005267 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005268 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005269 return 0;
5270 }
5271
5272 /* More interesting part now : we know that we have a complete
5273 * response which at least looks like HTTP. We have an indicator
5274 * of each header's length, so we can parse them quickly.
5275 */
5276
5277 if (unlikely(msg->err_pos >= 0))
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005278 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005279
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005280 /*
5281 * 1: get the status code
5282 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005283 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005284 if (n < 1 || n > 5)
5285 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005286 /* when the client triggers a 4xx from the server, it's most often due
5287 * to a missing object or permission. These events should be tracked
5288 * because if they happen often, it may indicate a brute force or a
5289 * vulnerability scan.
5290 */
5291 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02005292 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02005293
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005294 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005295 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005296
Willy Tarreau91852eb2015-05-01 13:26:00 +02005297 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
5298 * exactly one digit "." one digit. This check may be disabled using
5299 * option accept-invalid-http-response.
5300 */
5301 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
5302 if (msg->sl.st.v_l != 8) {
5303 msg->err_pos = 0;
5304 goto hdr_response_bad;
5305 }
5306
5307 if (rep->buf->p[4] != '/' ||
5308 !isdigit((unsigned char)rep->buf->p[5]) ||
5309 rep->buf->p[6] != '.' ||
5310 !isdigit((unsigned char)rep->buf->p[7])) {
5311 msg->err_pos = 4;
5312 goto hdr_response_bad;
5313 }
5314 }
5315
Willy Tarreau5b154472009-12-21 20:11:07 +01005316 /* check if the response is HTTP/1.1 or above */
5317 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005318 ((rep->buf->p[5] > '1') ||
5319 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005320 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005321
5322 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005323 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 +01005324
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005325 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005326 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005327
Willy Tarreau9b28e032012-10-12 23:49:43 +02005328 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005329
Willy Tarreau39650402010-03-15 19:44:39 +01005330 /* Adjust server's health based on status code. Note: status codes 501
5331 * and 505 are triggered on demand by client request, so we must not
5332 * count them as server failures.
5333 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005334 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005335 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005336 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005337 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005338 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005339 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005340
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005341 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02005342 * We may be facing a 100-continue response, or any other informational
5343 * 1xx response which is non-final, in which case this is not the right
5344 * response, and we're waiting for the next one. Let's allow this response
5345 * to go to the client and wait for the next one. There's an exception for
5346 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005347 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02005348 if (txn->status < 200 &&
5349 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005350 hdr_idx_init(&txn->hdr_idx);
5351 msg->next -= channel_forward(rep, msg->next);
5352 msg->msg_state = HTTP_MSG_RPBEFORE;
5353 txn->status = 0;
5354 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01005355 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02005356 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02005357 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02005358
Willy Tarreaua14ad722017-07-07 11:36:32 +02005359 /*
5360 * 2: check for cacheability.
5361 */
5362
5363 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005364 case 200:
5365 case 203:
5366 case 206:
5367 case 300:
5368 case 301:
5369 case 410:
5370 /* RFC2616 @13.4:
5371 * "A response received with a status code of
5372 * 200, 203, 206, 300, 301 or 410 MAY be stored
5373 * by a cache (...) unless a cache-control
5374 * directive prohibits caching."
5375 *
5376 * RFC2616 @9.5: POST method :
5377 * "Responses to this method are not cacheable,
5378 * unless the response includes appropriate
5379 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005380 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005381 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005382 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005383 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5384 break;
5385 default:
5386 break;
5387 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005388
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005389 /*
5390 * 3: we may need to capture headers
5391 */
5392 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005393 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005394 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005395 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005396
Willy Tarreau557f1992015-05-01 10:05:17 +02005397 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
5398 * by RFC7230#3.3.3 :
5399 *
5400 * The length of a message body is determined by one of the following
5401 * (in order of precedence):
5402 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005403 * 1. Any 2xx (Successful) response to a CONNECT request implies that
5404 * the connection will become a tunnel immediately after the empty
5405 * line that concludes the header fields. A client MUST ignore
5406 * any Content-Length or Transfer-Encoding header fields received
5407 * in such a message. Any 101 response (Switching Protocols) is
5408 * managed in the same manner.
5409 *
5410 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02005411 * (Informational), 204 (No Content), or 304 (Not Modified) status
5412 * code is always terminated by the first empty line after the
5413 * header fields, regardless of the header fields present in the
5414 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005415 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005416 * 3. If a Transfer-Encoding header field is present and the chunked
5417 * transfer coding (Section 4.1) is the final encoding, the message
5418 * body length is determined by reading and decoding the chunked
5419 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005420 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005421 * If a Transfer-Encoding header field is present in a response and
5422 * the chunked transfer coding is not the final encoding, the
5423 * message body length is determined by reading the connection until
5424 * it is closed by the server. If a Transfer-Encoding header field
5425 * is present in a request and the chunked transfer coding is not
5426 * the final encoding, the message body length cannot be determined
5427 * reliably; the server MUST respond with the 400 (Bad Request)
5428 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005429 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005430 * If a message is received with both a Transfer-Encoding and a
5431 * Content-Length header field, the Transfer-Encoding overrides the
5432 * Content-Length. Such a message might indicate an attempt to
5433 * perform request smuggling (Section 9.5) or response splitting
5434 * (Section 9.4) and ought to be handled as an error. A sender MUST
5435 * remove the received Content-Length field prior to forwarding such
5436 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005437 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005438 * 4. If a message is received without Transfer-Encoding and with
5439 * either multiple Content-Length header fields having differing
5440 * field-values or a single Content-Length header field having an
5441 * invalid value, then the message framing is invalid and the
5442 * recipient MUST treat it as an unrecoverable error. If this is a
5443 * request message, the server MUST respond with a 400 (Bad Request)
5444 * status code and then close the connection. If this is a response
5445 * message received by a proxy, the proxy MUST close the connection
5446 * to the server, discard the received response, and send a 502 (Bad
5447 * Gateway) response to the client. If this is a response message
5448 * received by a user agent, the user agent MUST close the
5449 * connection to the server and discard the received response.
5450 *
5451 * 5. If a valid Content-Length header field is present without
5452 * Transfer-Encoding, its decimal value defines the expected message
5453 * body length in octets. If the sender closes the connection or
5454 * the recipient times out before the indicated number of octets are
5455 * received, the recipient MUST consider the message to be
5456 * incomplete and close the connection.
5457 *
5458 * 6. If this is a request message and none of the above are true, then
5459 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005460 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005461 * 7. Otherwise, this is a response message without a declared message
5462 * body length, so the message body length is determined by the
5463 * number of octets received prior to the server closing the
5464 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005465 */
5466
5467 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005468 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005469 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005470 * FIXME: should we parse anyway and return an error on chunked encoding ?
5471 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005472 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5473 txn->status == 101)) {
5474 /* Either we've established an explicit tunnel, or we're
5475 * switching the protocol. In both cases, we're very unlikely
5476 * to understand the next protocols. We have to switch to tunnel
5477 * mode, so that we transfer the request and responses then let
5478 * this protocol pass unmodified. When we later implement specific
5479 * parsers for such protocols, we'll want to check the Upgrade
5480 * header which contains information about that protocol for
5481 * responses with status 101 (eg: see RFC2817 about TLS).
5482 */
5483 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5484 msg->flags |= HTTP_MSGF_XFER_LEN;
5485 goto end;
5486 }
5487
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005488 if (txn->meth == HTTP_METH_HEAD ||
5489 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005490 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005491 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005492 goto skip_content_length;
5493 }
5494
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005495 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005496 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02005497 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005498 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005499 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5500 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005501 /* bad transfer-encoding (chunked followed by something else) */
5502 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005503 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005504 break;
5505 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005506 }
5507
Willy Tarreau1c913912015-04-30 10:57:51 +02005508 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005509 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005510 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02005511 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
5512 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5513 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005514 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005515 signed long long cl;
5516
Willy Tarreauad14f752011-09-02 20:33:27 +02005517 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005518 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005519 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005520 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005521
Willy Tarreauad14f752011-09-02 20:33:27 +02005522 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005523 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005524 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005525 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005526
Willy Tarreauad14f752011-09-02 20:33:27 +02005527 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005528 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005529 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005530 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005531
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005532 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005533 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005534 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005535 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005536
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005537 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005538 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005539 }
5540
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005541 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005542 /* Now we have to check if we need to modify the Connection header.
5543 * This is more difficult on the response than it is on the request,
5544 * because we can have two different HTTP versions and we don't know
5545 * how the client will interprete a response. For instance, let's say
5546 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5547 * HTTP/1.1 response without any header. Maybe it will bound itself to
5548 * HTTP/1.0 because it only knows about it, and will consider the lack
5549 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5550 * the lack of header as a keep-alive. Thus we will use two flags
5551 * indicating how a request MAY be understood by the client. In case
5552 * of multiple possibilities, we'll fix the header to be explicit. If
5553 * ambiguous cases such as both close and keepalive are seen, then we
5554 * will fall back to explicit close. Note that we won't take risks with
5555 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005556 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005557 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005558 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5559 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5560 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5561 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005562 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005563
Willy Tarreau70dffda2014-01-30 03:07:23 +01005564 /* this situation happens when combining pretend-keepalive with httpclose. */
5565 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005566 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005567 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01005568 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5569
Willy Tarreau60466522010-01-18 19:08:45 +01005570 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005571 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005572 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5573 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005574
Willy Tarreau60466522010-01-18 19:08:45 +01005575 /* now adjust header transformations depending on current state */
5576 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5577 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5578 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005579 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005580 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005581 }
Willy Tarreau60466522010-01-18 19:08:45 +01005582 else { /* SCL / KAL */
5583 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005584 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005585 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005586 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005587
Willy Tarreau60466522010-01-18 19:08:45 +01005588 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005589 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005590
Willy Tarreau60466522010-01-18 19:08:45 +01005591 /* Some keep-alive responses are converted to Server-close if
5592 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005593 */
Willy Tarreau60466522010-01-18 19:08:45 +01005594 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5595 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005596 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005597 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005598 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005599 }
5600
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005601 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005602 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005603 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005604
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005605 /* end of job, return OK */
5606 rep->analysers &= ~an_bit;
5607 rep->analyse_exp = TICK_ETERNITY;
5608 channel_auto_close(rep);
5609 return 1;
5610
5611 abort_keep_alive:
5612 /* A keep-alive request to the server failed on a network error.
5613 * The client is required to retry. We need to close without returning
5614 * any other information so that the client retries.
5615 */
5616 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005617 rep->analysers &= AN_RES_FLT_END;
5618 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005619 channel_auto_close(rep);
5620 s->logs.logwait = 0;
5621 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005622 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005623 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005624 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005625 return 0;
5626}
5627
5628/* This function performs all the processing enabled for the current response.
5629 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005630 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005631 * other functions. It works like process_request (see indications above).
5632 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005633int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005634{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005635 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005636 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005637 struct http_msg *msg = &txn->rsp;
5638 struct proxy *cur_proxy;
5639 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005640 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005641
Willy Tarreau87b09662015-04-03 00:22:06 +02005642 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 +02005643 now_ms, __FUNCTION__,
5644 s,
5645 rep,
5646 rep->rex, rep->wex,
5647 rep->flags,
5648 rep->buf->i,
5649 rep->analysers);
5650
5651 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5652 return 0;
5653
Willy Tarreau70730dd2014-04-24 18:06:27 +02005654 /* The stats applet needs to adjust the Connection header but we don't
5655 * apply any filter there.
5656 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005657 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5658 rep->analysers &= ~an_bit;
5659 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005660 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005661 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005662
Willy Tarreau58975672014-04-24 21:13:57 +02005663 /*
5664 * We will have to evaluate the filters.
5665 * As opposed to version 1.2, now they will be evaluated in the
5666 * filters order and not in the header order. This means that
5667 * each filter has to be validated among all headers.
5668 *
5669 * Filters are tried with ->be first, then with ->fe if it is
5670 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005671 *
5672 * Maybe we are in resume condiion. In this case I choose the
5673 * "struct proxy" which contains the rule list matching the resume
5674 * pointer. If none of theses "struct proxy" match, I initialise
5675 * the process with the first one.
5676 *
5677 * In fact, I check only correspondance betwwen the current list
5678 * pointer and the ->fe rule list. If it doesn't match, I initialize
5679 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005680 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005681 if (s->current_rule_list == &sess->fe->http_res_rules)
5682 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005683 else
5684 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005685 while (1) {
5686 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005687
Willy Tarreau58975672014-04-24 21:13:57 +02005688 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005689 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005690 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005691
Willy Tarreau51d861a2015-05-22 17:30:48 +02005692 if (ret == HTTP_RULE_RES_BADREQ)
5693 goto return_srv_prx_502;
5694
5695 if (ret == HTTP_RULE_RES_DONE) {
5696 rep->analysers &= ~an_bit;
5697 rep->analyse_exp = TICK_ETERNITY;
5698 return 1;
5699 }
5700 }
5701
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005702 /* we need to be called again. */
5703 if (ret == HTTP_RULE_RES_YIELD) {
5704 channel_dont_close(rep);
5705 return 0;
5706 }
5707
Willy Tarreau58975672014-04-24 21:13:57 +02005708 /* try headers filters */
5709 if (rule_set->rsp_exp != NULL) {
5710 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5711 return_bad_resp:
5712 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005713 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005714 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005715 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005716 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005717 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005718 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005719 txn->status = 502;
5720 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005721 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005722 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005723 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005724 if (!(s->flags & SF_ERR_MASK))
5725 s->flags |= SF_ERR_PRXCOND;
5726 if (!(s->flags & SF_FINST_MASK))
5727 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005728 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005729 }
Willy Tarreau58975672014-04-24 21:13:57 +02005730 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005731
Willy Tarreau58975672014-04-24 21:13:57 +02005732 /* has the response been denied ? */
5733 if (txn->flags & TX_SVDENY) {
5734 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005735 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005736
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005737 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5738 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005739 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005740 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005741
Willy Tarreau58975672014-04-24 21:13:57 +02005742 goto return_srv_prx_502;
5743 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005744
Willy Tarreau58975672014-04-24 21:13:57 +02005745 /* add response headers from the rule sets in the same order */
5746 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005747 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005748 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005749 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005750 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005751 ret = acl_pass(ret);
5752 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5753 ret = !ret;
5754 if (!ret)
5755 continue;
5756 }
5757 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
5758 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005759 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005760
Willy Tarreau58975672014-04-24 21:13:57 +02005761 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005762 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005763 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005764 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005765 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005766
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005767 /* After this point, this anayzer can't return yield, so we can
5768 * remove the bit corresponding to this analyzer from the list.
5769 *
5770 * Note that the intermediate returns and goto found previously
5771 * reset the analyzers.
5772 */
5773 rep->analysers &= ~an_bit;
5774 rep->analyse_exp = TICK_ETERNITY;
5775
Willy Tarreau58975672014-04-24 21:13:57 +02005776 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005777 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005778 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005779
Willy Tarreau58975672014-04-24 21:13:57 +02005780 /*
5781 * Now check for a server cookie.
5782 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005783 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005784 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005785
Willy Tarreau58975672014-04-24 21:13:57 +02005786 /*
5787 * Check for cache-control or pragma headers if required.
5788 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005789 if (((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)) && txn->status != 101)
Willy Tarreau58975672014-04-24 21:13:57 +02005790 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005791
Willy Tarreau58975672014-04-24 21:13:57 +02005792 /*
5793 * Add server cookie in the response if needed
5794 */
5795 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5796 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005797 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005798 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5799 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5800 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5801 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5802 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005803 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005804 /* the server is known, it's not the one the client requested, or the
5805 * cookie's last seen date needs to be refreshed. We have to
5806 * insert a set-cookie here, except if we want to insert only on POST
5807 * requests and this one isn't. Note that servers which don't have cookies
5808 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005809 */
Willy Tarreau58975672014-04-24 21:13:57 +02005810 if (!objt_server(s->target)->cookie) {
5811 chunk_printf(&trash,
5812 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5813 s->be->cookie_name);
5814 }
5815 else {
5816 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005817
Willy Tarreau58975672014-04-24 21:13:57 +02005818 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5819 /* emit last_date, which is mandatory */
5820 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5821 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5822 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005823
Willy Tarreau58975672014-04-24 21:13:57 +02005824 if (s->be->cookie_maxlife) {
5825 /* emit first_date, which is either the original one or
5826 * the current date.
5827 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005828 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005829 s30tob64(txn->cookie_first_date ?
5830 txn->cookie_first_date >> 2 :
5831 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005832 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005833 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005834 }
Willy Tarreau58975672014-04-24 21:13:57 +02005835 chunk_appendf(&trash, "; path=/");
5836 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005837
Willy Tarreau58975672014-04-24 21:13:57 +02005838 if (s->be->cookie_domain)
5839 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005840
Willy Tarreau58975672014-04-24 21:13:57 +02005841 if (s->be->ck_opts & PR_CK_HTTPONLY)
5842 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005843
Willy Tarreau58975672014-04-24 21:13:57 +02005844 if (s->be->ck_opts & PR_CK_SECURE)
5845 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005846
Willy Tarreau58975672014-04-24 21:13:57 +02005847 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
5848 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005849
Willy Tarreau58975672014-04-24 21:13:57 +02005850 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005851 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005852 /* the server did not change, only the date was updated */
5853 txn->flags |= TX_SCK_UPDATED;
5854 else
5855 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005856
Willy Tarreau58975672014-04-24 21:13:57 +02005857 /* Here, we will tell an eventual cache on the client side that we don't
5858 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5859 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5860 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005861 */
Willy Tarreau58975672014-04-24 21:13:57 +02005862 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005863
Willy Tarreau58975672014-04-24 21:13:57 +02005864 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005865
Willy Tarreau58975672014-04-24 21:13:57 +02005866 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5867 "Cache-control: private", 22) < 0))
5868 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005869 }
Willy Tarreau58975672014-04-24 21:13:57 +02005870 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005871
Willy Tarreau58975672014-04-24 21:13:57 +02005872 /*
5873 * Check if result will be cacheable with a cookie.
5874 * We'll block the response if security checks have caught
5875 * nasty things such as a cacheable cookie.
5876 */
5877 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5878 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5879 (s->be->options & PR_O_CHK_CACHE)) {
5880 /* we're in presence of a cacheable response containing
5881 * a set-cookie header. We'll block it as requested by
5882 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005883 */
Willy Tarreau58975672014-04-24 21:13:57 +02005884 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005885 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005886
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005887 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5888 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005889 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005890 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005891
Willy Tarreau58975672014-04-24 21:13:57 +02005892 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5893 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5894 send_log(s->be, LOG_ALERT,
5895 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5896 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5897 goto return_srv_prx_502;
5898 }
Willy Tarreau03945942009-12-22 16:50:27 +01005899
Willy Tarreau70730dd2014-04-24 18:06:27 +02005900 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005901 /*
5902 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5903 * If an "Upgrade" token is found, the header is left untouched in order
5904 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005905 * if anything but "Upgrade" is present in the Connection header. We don't
5906 * want to touch any 101 response either since it's switching to another
5907 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005908 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005909 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02005910 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005911 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02005912 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
5913 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005914
Willy Tarreau58975672014-04-24 21:13:57 +02005915 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5916 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5917 /* we want a keep-alive response here. Keep-alive header
5918 * required if either side is not 1.1.
5919 */
5920 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5921 want_flags |= TX_CON_KAL_SET;
5922 }
5923 else {
5924 /* we want a close response here. Close header required if
5925 * the server is 1.1, regardless of the client.
5926 */
5927 if (msg->flags & HTTP_MSGF_VER_11)
5928 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005929 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005930
Willy Tarreau58975672014-04-24 21:13:57 +02005931 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5932 http_change_connection_header(txn, msg, want_flags);
5933 }
5934
5935 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005936 /* Always enter in the body analyzer */
5937 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5938 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005939
Willy Tarreau58975672014-04-24 21:13:57 +02005940 /* if the user wants to log as soon as possible, without counting
5941 * bytes from the server, then this is the right moment. We have
5942 * to temporarily assign bytes_out to log what we currently have.
5943 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005944 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005945 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5946 s->logs.bytes_out = txn->rsp.eoh;
5947 s->do_log(s);
5948 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005949 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005950 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005951}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005952
Willy Tarreaud98cf932009-12-27 22:54:55 +01005953/* This function is an analyser which forwards response body (including chunk
5954 * sizes if any). It is called as soon as we must forward, even if we forward
5955 * zero byte. The only situation where it must not be called is when we're in
5956 * tunnel mode and we want to forward till the close. It's used both to forward
5957 * remaining data and to resync after end of body. It expects the msg_state to
5958 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005959 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005960 *
5961 * It is capable of compressing response data both in content-length mode and
5962 * in chunked mode. The state machines follows different flows depending on
5963 * whether content-length and chunked modes are used, since there are no
5964 * trailers in content-length :
5965 *
5966 * chk-mode cl-mode
5967 * ,----- BODY -----.
5968 * / \
5969 * V size > 0 V chk-mode
5970 * .--> SIZE -------------> DATA -------------> CRLF
5971 * | | size == 0 | last byte |
5972 * | v final crlf v inspected |
5973 * | TRAILERS -----------> DONE |
5974 * | |
5975 * `----------------------------------------------'
5976 *
5977 * Compression only happens in the DATA state, and must be flushed in final
5978 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5979 * is performed at once on final states for all bytes parsed, or when leaving
5980 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005981 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005982int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005983{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005984 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005985 struct http_txn *txn = s->txn;
5986 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01005987 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005988
Christopher Faulet814d2702017-03-30 11:33:44 +02005989 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
5990 now_ms, __FUNCTION__,
5991 s,
5992 res,
5993 res->rex, res->wex,
5994 res->flags,
5995 res->buf->i,
5996 res->analysers);
5997
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005998 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5999 return 0;
6000
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006001 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006002 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006003 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006004 /* Output closed while we were sending data. We must abort and
6005 * wake the other side up.
6006 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006007 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006008 msg->msg_state = HTTP_MSG_ERROR;
6009 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006010 return 1;
6011 }
6012
Willy Tarreau4fe41902010-06-07 22:27:41 +02006013 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006014 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006015
Christopher Fauletd7c91962015-04-30 11:48:27 +02006016 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006017 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
6018 ? HTTP_MSG_CHUNK_SIZE
6019 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006020 }
6021
Willy Tarreauefdf0942014-04-24 20:08:57 +02006022 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006023 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02006024 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006025 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006026 }
6027
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006028 if (msg->msg_state < HTTP_MSG_DONE) {
6029 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6030 ? http_msg_forward_chunked_body(s, msg)
6031 : http_msg_forward_body(s, msg));
6032 if (!ret)
6033 goto missing_data_or_waiting;
6034 if (ret < 0)
6035 goto return_bad_res;
6036 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006037
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006038 /* other states, DONE...TUNNEL */
6039 /* for keep-alive we don't want to forward closes on DONE */
6040 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6041 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6042 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006043
Christopher Faulet894da4c2017-07-18 11:29:07 +02006044 http_resync_states(s);
6045 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006046 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6047 if (res->flags & CF_SHUTW) {
6048 /* response errors are most likely due to the
6049 * client aborting the transfer. */
6050 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006051 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006052 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006053 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 +01006054 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006055 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006056 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006057 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006058 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006059
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006060 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006061 if (res->flags & CF_SHUTW)
6062 goto aborted_xfer;
6063
6064 /* stop waiting for data if the input is closed before the end. If the
6065 * client side was already closed, it means that the client has aborted,
6066 * so we don't want to count this as a server abort. Otherwise it's a
6067 * server abort.
6068 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02006069 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006070 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006071 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006072 /* If we have some pending data, we continue the processing */
6073 if (!buffer_pending(res->buf)) {
6074 if (!(s->flags & SF_ERR_MASK))
6075 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006076 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006077 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006078 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006079 goto return_bad_res_stats_ok;
6080 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006081 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006082
Willy Tarreau40dba092010-03-04 18:14:51 +01006083 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006084 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006085 goto return_bad_res;
6086
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006087 /* When TE: chunked is used, we need to get there again to parse
6088 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02006089 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
6090 * or if there are filters registered on the stream, we don't want to
6091 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006092 */
Christopher Faulet69744d92017-03-30 10:54:35 +02006093 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006094 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006095 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6096 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006097 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006098
Willy Tarreau5c620922011-05-11 19:56:11 +02006099 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006100 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006101 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006102 * modes are already handled by the stream sock layer. We must not do
6103 * this in content-length mode because it could present the MSG_MORE
6104 * flag with the last block of forwarded data, which would cause an
6105 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006106 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006107 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006108 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006109
Willy Tarreau87b09662015-04-03 00:22:06 +02006110 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006111 return 0;
6112
Willy Tarreau40dba092010-03-04 18:14:51 +01006113 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006114 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006115 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006116 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006117
6118 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006119 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006120 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006121 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006122 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006123 res->analysers &= AN_RES_FLT_END;
6124 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 +01006125 if (objt_server(s->target))
6126 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006127
Willy Tarreaue7dff022015-04-03 01:14:29 +02006128 if (!(s->flags & SF_ERR_MASK))
6129 s->flags |= SF_ERR_PRXCOND;
6130 if (!(s->flags & SF_FINST_MASK))
6131 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006132 return 0;
6133
6134 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006135 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006136 txn->rsp.msg_state = HTTP_MSG_ERROR;
6137 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006138 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006139 res->analysers &= AN_RES_FLT_END;
6140 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 +01006141
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006142 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
6143 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006144 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006145 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006146
Willy Tarreaue7dff022015-04-03 01:14:29 +02006147 if (!(s->flags & SF_ERR_MASK))
6148 s->flags |= SF_ERR_CLICL;
6149 if (!(s->flags & SF_FINST_MASK))
6150 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006151 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006152}
6153
6154
6155static inline int
6156http_msg_forward_body(struct stream *s, struct http_msg *msg)
6157{
6158 struct channel *chn = msg->chn;
6159 int ret;
6160
6161 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
6162
6163 if (msg->msg_state == HTTP_MSG_ENDING)
6164 goto ending;
6165
6166 /* Neither content-length, nor transfer-encoding was found, so we must
6167 * read the body until the server connection is closed. In that case, we
6168 * eat data as they come. Of course, this happens for response only. */
6169 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6170 unsigned long long len = (chn->buf->i - msg->next);
6171 msg->chunk_len += len;
6172 msg->body_len += len;
6173 }
Christopher Fauletda02e172015-12-04 09:25:05 +01006174 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6175 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6176 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006177 msg->next += ret;
6178 msg->chunk_len -= ret;
6179 if (msg->chunk_len) {
6180 /* input empty or output full */
6181 if (chn->buf->i > msg->next)
6182 chn->flags |= CF_WAKE_WRITE;
6183 goto missing_data_or_waiting;
6184 }
6185
Christopher Faulet1486b0a2017-07-18 11:42:08 +02006186 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
6187 * always set for a request. */
6188 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6189 /* The server still sending data that should be filtered */
6190 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
6191 goto missing_data_or_waiting;
6192 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006193
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006194 msg->msg_state = HTTP_MSG_ENDING;
6195
6196 ending:
6197 /* we may have some pending data starting at res->buf->p such as a last
6198 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006199 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6200 /* default_ret */ msg->next,
6201 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006202 b_adv(chn->buf, ret);
6203 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006204 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6205 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006206 if (msg->next)
6207 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006208
Christopher Fauletda02e172015-12-04 09:25:05 +01006209 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
6210 /* default_ret */ 1,
6211 /* on_error */ goto error,
6212 /* on_wait */ goto waiting);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006213 msg->msg_state = HTTP_MSG_DONE;
6214 return 1;
6215
6216 missing_data_or_waiting:
6217 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006218 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6219 /* default_ret */ msg->next,
6220 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006221 b_adv(chn->buf, ret);
6222 msg->next -= ret;
6223 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6224 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006225 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006226 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006227 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006228 return 0;
6229 error:
6230 return -1;
6231}
6232
6233static inline int
6234http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
6235{
6236 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006237 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006238 int ret;
6239
6240 /* Here we have the guarantee to be in one of the following state:
6241 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
6242 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
6243
6244 switch_states:
6245 switch (msg->msg_state) {
6246 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01006247 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6248 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6249 /* on_error */ goto error);
6250 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006251 msg->chunk_len -= ret;
6252 if (msg->chunk_len) {
6253 /* input empty or output full */
6254 if (chn->buf->i > msg->next)
6255 chn->flags |= CF_WAKE_WRITE;
6256 goto missing_data_or_waiting;
6257 }
6258
6259 /* nothing left to forward for this chunk*/
6260 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
6261 /* fall through for HTTP_MSG_CHUNK_CRLF */
6262
6263 case HTTP_MSG_CHUNK_CRLF:
6264 /* we want the CRLF after the data */
Willy Tarreaub2892562017-09-21 11:33:54 +02006265 ret = h1_skip_chunk_crlf(chn->buf, msg->next, chn->buf->i);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006266 if (ret == 0)
6267 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02006268 if (ret < 0) {
6269 msg->err_pos = chn->buf->i + ret;
6270 if (msg->err_pos < 0)
6271 msg->err_pos += chn->buf->size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006272 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02006273 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01006274 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006275 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6276 /* fall through for HTTP_MSG_CHUNK_SIZE */
6277
6278 case HTTP_MSG_CHUNK_SIZE:
6279 /* read the chunk size and assign it to ->chunk_len,
6280 * then set ->next to point to the body and switch to
6281 * DATA or TRAILERS state.
6282 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006283 ret = h1_parse_chunk_size(chn->buf, msg->next, chn->buf->i, &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006284 if (ret == 0)
6285 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006286 if (ret < 0) {
6287 msg->err_pos = chn->buf->i + ret;
6288 if (msg->err_pos < 0)
6289 msg->err_pos += chn->buf->size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006290 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006291 }
6292
6293 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01006294 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006295 msg->chunk_len = chunk;
6296 msg->body_len += chunk;
6297
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006298 if (msg->chunk_len) {
6299 msg->msg_state = HTTP_MSG_DATA;
6300 goto switch_states;
6301 }
6302 msg->msg_state = HTTP_MSG_TRAILERS;
6303 /* fall through for HTTP_MSG_TRAILERS */
6304
6305 case HTTP_MSG_TRAILERS:
6306 ret = http_forward_trailers(msg);
6307 if (ret < 0)
6308 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01006309 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
6310 /* default_ret */ 1,
6311 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006312 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006313 if (!ret)
6314 goto missing_data_or_waiting;
6315 break;
6316
6317 case HTTP_MSG_ENDING:
6318 goto ending;
6319
6320 default:
6321 /* This should no happen in this function */
6322 goto error;
6323 }
6324
6325 msg->msg_state = HTTP_MSG_ENDING;
6326 ending:
6327 /* we may have some pending data starting at res->buf->p such as a last
6328 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006329 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006330 /* default_ret */ msg->next,
6331 /* on_error */ goto error);
6332 b_adv(chn->buf, ret);
6333 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006334 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6335 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006336 if (msg->next)
6337 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006338
Christopher Fauletda02e172015-12-04 09:25:05 +01006339 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006340 /* default_ret */ 1,
6341 /* on_error */ goto error,
6342 /* on_wait */ goto waiting);
6343 msg->msg_state = HTTP_MSG_DONE;
6344 return 1;
6345
6346 missing_data_or_waiting:
6347 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006348 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006349 /* default_ret */ msg->next,
6350 /* on_error */ goto error);
6351 b_adv(chn->buf, ret);
6352 msg->next -= ret;
6353 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6354 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006355 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006356 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006357 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006358 return 0;
6359
6360 chunk_parsing_error:
6361 if (msg->err_pos >= 0) {
6362 if (chn->flags & CF_ISRESP)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006363 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006364 msg->msg_state, strm_fe(s));
6365 else
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006366 http_capture_bad_message(strm_fe(s), &strm_fe(s)->invalid_req, s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006367 msg, msg->msg_state, s->be);
6368 }
6369 error:
6370 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006371}
6372
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006373
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006374/* Iterate the same filter through all request headers.
6375 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006376 * Since it can manage the switch to another backend, it updates the per-proxy
6377 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006378 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006379int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006380{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006381 char *cur_ptr, *cur_end, *cur_next;
6382 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006383 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006384 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006385 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006386
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006387 last_hdr = 0;
6388
Willy Tarreau9b28e032012-10-12 23:49:43 +02006389 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006390 old_idx = 0;
6391
6392 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006393 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006394 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006395 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006396 (exp->action == ACT_ALLOW ||
6397 exp->action == ACT_DENY ||
6398 exp->action == ACT_TARPIT))
6399 return 0;
6400
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006401 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006402 if (!cur_idx)
6403 break;
6404
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006405 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006406 cur_ptr = cur_next;
6407 cur_end = cur_ptr + cur_hdr->len;
6408 cur_next = cur_end + cur_hdr->cr + 1;
6409
6410 /* Now we have one header between cur_ptr and cur_end,
6411 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006412 */
6413
Willy Tarreau15a53a42015-01-21 13:39:42 +01006414 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006415 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006416 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006417 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006418 last_hdr = 1;
6419 break;
6420
6421 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006422 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006423 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006424 break;
6425
6426 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006427 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006428 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006429 break;
6430
6431 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006432 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6433 if (trash.len < 0)
6434 return -1;
6435
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006436 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006437 /* FIXME: if the user adds a newline in the replacement, the
6438 * index will not be recalculated for now, and the new line
6439 * will not be counted as a new header.
6440 */
6441
6442 cur_end += delta;
6443 cur_next += delta;
6444 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006445 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006446 break;
6447
6448 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006449 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006450 cur_next += delta;
6451
Willy Tarreaufa355d42009-11-29 18:12:29 +01006452 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006453 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6454 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006455 cur_hdr->len = 0;
6456 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006457 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006458 break;
6459
6460 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006461 }
6462
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006463 /* keep the link from this header to next one in case of later
6464 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006465 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006466 old_idx = cur_idx;
6467 }
6468 return 0;
6469}
6470
6471
6472/* Apply the filter to the request line.
6473 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6474 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006475 * Since it can manage the switch to another backend, it updates the per-proxy
6476 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006477 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006478int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006479{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006480 char *cur_ptr, *cur_end;
6481 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006482 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006483 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006484
Willy Tarreau3d300592007-03-18 18:34:41 +01006485 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006486 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006487 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006488 (exp->action == ACT_ALLOW ||
6489 exp->action == ACT_DENY ||
6490 exp->action == ACT_TARPIT))
6491 return 0;
6492 else if (exp->action == ACT_REMOVE)
6493 return 0;
6494
6495 done = 0;
6496
Willy Tarreau9b28e032012-10-12 23:49:43 +02006497 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006498 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006499
6500 /* Now we have the request line between cur_ptr and cur_end */
6501
Willy Tarreau15a53a42015-01-21 13:39:42 +01006502 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006503 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006504 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006505 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006506 done = 1;
6507 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006508
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006509 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006510 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006511 done = 1;
6512 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006513
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006514 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006515 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006516 done = 1;
6517 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006518
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006519 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006520 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6521 if (trash.len < 0)
6522 return -1;
6523
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006524 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006525 /* FIXME: if the user adds a newline in the replacement, the
6526 * index will not be recalculated for now, and the new line
6527 * will not be counted as a new header.
6528 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006529
Willy Tarreaufa355d42009-11-29 18:12:29 +01006530 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006531 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006532 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006533 HTTP_MSG_RQMETH,
6534 cur_ptr, cur_end + 1,
6535 NULL, NULL);
6536 if (unlikely(!cur_end))
6537 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006538
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006539 /* we have a full request and we know that we have either a CR
6540 * or an LF at <ptr>.
6541 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006542 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6543 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006544 /* there is no point trying this regex on headers */
6545 return 1;
6546 }
6547 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006548 return done;
6549}
Willy Tarreau97de6242006-12-27 17:18:38 +01006550
Willy Tarreau58f10d72006-12-04 02:26:12 +01006551
Willy Tarreau58f10d72006-12-04 02:26:12 +01006552
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006553/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006554 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006555 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006556 * unparsable request. Since it can manage the switch to another backend, it
6557 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006558 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006559int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006560{
Willy Tarreau192252e2015-04-04 01:47:55 +02006561 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006562 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006563 struct hdr_exp *exp;
6564
6565 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006566 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006567
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006568 /*
6569 * The interleaving of transformations and verdicts
6570 * makes it difficult to decide to continue or stop
6571 * the evaluation.
6572 */
6573
Willy Tarreau6c123b12010-01-28 20:22:06 +01006574 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6575 break;
6576
Willy Tarreau3d300592007-03-18 18:34:41 +01006577 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006578 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006579 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006580 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006581
6582 /* if this filter had a condition, evaluate it now and skip to
6583 * next filter if the condition does not match.
6584 */
6585 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006586 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006587 ret = acl_pass(ret);
6588 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6589 ret = !ret;
6590
6591 if (!ret)
6592 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006593 }
6594
6595 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006596 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006597 if (unlikely(ret < 0))
6598 return -1;
6599
6600 if (likely(ret == 0)) {
6601 /* The filter did not match the request, it can be
6602 * iterated through all headers.
6603 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006604 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6605 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006606 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006607 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006608 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006609}
6610
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006611
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006612/* Find the end of a cookie value contained between <s> and <e>. It works the
6613 * same way as with headers above except that the semi-colon also ends a token.
6614 * See RFC2965 for more information. Note that it requires a valid header to
6615 * return a valid result.
6616 */
6617char *find_cookie_value_end(char *s, const char *e)
6618{
6619 int quoted, qdpair;
6620
6621 quoted = qdpair = 0;
6622 for (; s < e; s++) {
6623 if (qdpair) qdpair = 0;
6624 else if (quoted) {
6625 if (*s == '\\') qdpair = 1;
6626 else if (*s == '"') quoted = 0;
6627 }
6628 else if (*s == '"') quoted = 1;
6629 else if (*s == ',' || *s == ';') return s;
6630 }
6631 return s;
6632}
6633
6634/* Delete a value in a header between delimiters <from> and <next> in buffer
6635 * <buf>. The number of characters displaced is returned, and the pointer to
6636 * the first delimiter is updated if required. The function tries as much as
6637 * possible to respect the following principles :
6638 * - replace <from> delimiter by the <next> one unless <from> points to a
6639 * colon, in which case <next> is simply removed
6640 * - set exactly one space character after the new first delimiter, unless
6641 * there are not enough characters in the block being moved to do so.
6642 * - remove unneeded spaces before the previous delimiter and after the new
6643 * one.
6644 *
6645 * It is the caller's responsibility to ensure that :
6646 * - <from> points to a valid delimiter or the colon ;
6647 * - <next> points to a valid delimiter or the final CR/LF ;
6648 * - there are non-space chars before <from> ;
6649 * - there is a CR/LF at or after <next>.
6650 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006651int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006652{
6653 char *prev = *from;
6654
6655 if (*prev == ':') {
6656 /* We're removing the first value, preserve the colon and add a
6657 * space if possible.
6658 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006659 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006660 next++;
6661 prev++;
6662 if (prev < next)
6663 *prev++ = ' ';
6664
Willy Tarreau2235b262016-11-05 15:50:20 +01006665 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006666 next++;
6667 } else {
6668 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006669 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006670 prev--;
6671 *from = prev;
6672
6673 /* copy the delimiter and if possible a space if we're
6674 * not at the end of the line.
6675 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006676 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006677 *prev++ = *next++;
6678 if (prev + 1 < next)
6679 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006680 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006681 next++;
6682 }
6683 }
6684 return buffer_replace2(buf, prev, next, NULL, 0);
6685}
6686
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006687/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006688 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006689 * desirable to call it only when needed. This code is quite complex because
6690 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6691 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006692 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006693void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006694{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006695 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006696 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006697 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006698 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006699 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6700 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006701
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006702 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006703 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006704 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006705
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006706 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006707 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006708 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006709
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006710 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006711 hdr_beg = hdr_next;
6712 hdr_end = hdr_beg + cur_hdr->len;
6713 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006714
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006715 /* We have one full header between hdr_beg and hdr_end, and the
6716 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006717 * "Cookie:" headers.
6718 */
6719
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006720 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006721 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006722 old_idx = cur_idx;
6723 continue;
6724 }
6725
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006726 del_from = NULL; /* nothing to be deleted */
6727 preserve_hdr = 0; /* assume we may kill the whole header */
6728
Willy Tarreau58f10d72006-12-04 02:26:12 +01006729 /* Now look for cookies. Conforming to RFC2109, we have to support
6730 * attributes whose name begin with a '$', and associate them with
6731 * the right cookie, if we want to delete this cookie.
6732 * So there are 3 cases for each cookie read :
6733 * 1) it's a special attribute, beginning with a '$' : ignore it.
6734 * 2) it's a server id cookie that we *MAY* want to delete : save
6735 * some pointers on it (last semi-colon, beginning of cookie...)
6736 * 3) it's an application cookie : we *MAY* have to delete a previous
6737 * "special" cookie.
6738 * At the end of loop, if a "special" cookie remains, we may have to
6739 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006740 * *MUST* delete it.
6741 *
6742 * Note: RFC2965 is unclear about the processing of spaces around
6743 * the equal sign in the ATTR=VALUE form. A careful inspection of
6744 * the RFC explicitly allows spaces before it, and not within the
6745 * tokens (attrs or values). An inspection of RFC2109 allows that
6746 * too but section 10.1.3 lets one think that spaces may be allowed
6747 * after the equal sign too, resulting in some (rare) buggy
6748 * implementations trying to do that. So let's do what servers do.
6749 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6750 * allowed quoted strings in values, with any possible character
6751 * after a backslash, including control chars and delimitors, which
6752 * causes parsing to become ambiguous. Browsers also allow spaces
6753 * within values even without quotes.
6754 *
6755 * We have to keep multiple pointers in order to support cookie
6756 * removal at the beginning, middle or end of header without
6757 * corrupting the header. All of these headers are valid :
6758 *
6759 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6760 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6761 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6762 * | | | | | | | | |
6763 * | | | | | | | | hdr_end <--+
6764 * | | | | | | | +--> next
6765 * | | | | | | +----> val_end
6766 * | | | | | +-----------> val_beg
6767 * | | | | +--------------> equal
6768 * | | | +----------------> att_end
6769 * | | +---------------------> att_beg
6770 * | +--------------------------> prev
6771 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006772 */
6773
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006774 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6775 /* Iterate through all cookies on this line */
6776
6777 /* find att_beg */
6778 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006779 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006780 att_beg++;
6781
6782 /* find att_end : this is the first character after the last non
6783 * space before the equal. It may be equal to hdr_end.
6784 */
6785 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006786
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006787 while (equal < hdr_end) {
6788 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006789 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006790 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006791 continue;
6792 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006793 }
6794
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006795 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6796 * is between <att_beg> and <equal>, both may be identical.
6797 */
6798
6799 /* look for end of cookie if there is an equal sign */
6800 if (equal < hdr_end && *equal == '=') {
6801 /* look for the beginning of the value */
6802 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006803 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006804 val_beg++;
6805
6806 /* find the end of the value, respecting quotes */
6807 next = find_cookie_value_end(val_beg, hdr_end);
6808
6809 /* make val_end point to the first white space or delimitor after the value */
6810 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006811 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006812 val_end--;
6813 } else {
6814 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006815 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006816
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006817 /* We have nothing to do with attributes beginning with '$'. However,
6818 * they will automatically be removed if a header before them is removed,
6819 * since they're supposed to be linked together.
6820 */
6821 if (*att_beg == '$')
6822 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006823
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006824 /* Ignore cookies with no equal sign */
6825 if (equal == next) {
6826 /* This is not our cookie, so we must preserve it. But if we already
6827 * scheduled another cookie for removal, we cannot remove the
6828 * complete header, but we can remove the previous block itself.
6829 */
6830 preserve_hdr = 1;
6831 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006832 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006833 val_end += delta;
6834 next += delta;
6835 hdr_end += delta;
6836 hdr_next += delta;
6837 cur_hdr->len += delta;
6838 http_msg_move_end(&txn->req, delta);
6839 prev = del_from;
6840 del_from = NULL;
6841 }
6842 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006843 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006844
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006845 /* if there are spaces around the equal sign, we need to
6846 * strip them otherwise we'll get trouble for cookie captures,
6847 * or even for rewrites. Since this happens extremely rarely,
6848 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006849 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006850 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6851 int stripped_before = 0;
6852 int stripped_after = 0;
6853
6854 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006855 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006856 equal += stripped_before;
6857 val_beg += stripped_before;
6858 }
6859
6860 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006861 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006862 val_beg += stripped_after;
6863 stripped_before += stripped_after;
6864 }
6865
6866 val_end += stripped_before;
6867 next += stripped_before;
6868 hdr_end += stripped_before;
6869 hdr_next += stripped_before;
6870 cur_hdr->len += stripped_before;
6871 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006872 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006873 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006874
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006875 /* First, let's see if we want to capture this cookie. We check
6876 * that we don't already have a client side cookie, because we
6877 * can only capture one. Also as an optimisation, we ignore
6878 * cookies shorter than the declared name.
6879 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006880 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6881 (val_end - att_beg >= sess->fe->capture_namelen) &&
6882 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006883 int log_len = val_end - att_beg;
6884
6885 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6886 Alert("HTTP logging : out of memory.\n");
6887 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006888 if (log_len > sess->fe->capture_len)
6889 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006890 memcpy(txn->cli_cookie, att_beg, log_len);
6891 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006892 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006893 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006894
Willy Tarreaubca99692010-10-06 19:25:55 +02006895 /* Persistence cookies in passive, rewrite or insert mode have the
6896 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006897 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006898 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006899 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006900 * For cookies in prefix mode, the form is :
6901 *
6902 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006903 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006904 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6905 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6906 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006907 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006908
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006909 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6910 * have the server ID between val_beg and delim, and the original cookie between
6911 * delim+1 and val_end. Otherwise, delim==val_end :
6912 *
6913 * Cookie: NAME=SRV; # in all but prefix modes
6914 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6915 * | || || | |+-> next
6916 * | || || | +--> val_end
6917 * | || || +---------> delim
6918 * | || |+------------> val_beg
6919 * | || +-------------> att_end = equal
6920 * | |+-----------------> att_beg
6921 * | +------------------> prev
6922 * +-------------------------> hdr_beg
6923 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006924
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006925 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006926 for (delim = val_beg; delim < val_end; delim++)
6927 if (*delim == COOKIE_DELIM)
6928 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006929 } else {
6930 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006931 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006932 /* Now check if the cookie contains a date field, which would
6933 * appear after a vertical bar ('|') just after the server name
6934 * and before the delimiter.
6935 */
6936 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6937 if (vbar1) {
6938 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006939 * right is the last seen date. It is a base64 encoded
6940 * 30-bit value representing the UNIX date since the
6941 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006942 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006943 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006944 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006945 if (val_end - vbar1 >= 5) {
6946 val = b64tos30(vbar1);
6947 if (val > 0)
6948 txn->cookie_last_date = val << 2;
6949 }
6950 /* look for a second vertical bar */
6951 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6952 if (vbar1 && (val_end - vbar1 > 5)) {
6953 val = b64tos30(vbar1 + 1);
6954 if (val > 0)
6955 txn->cookie_first_date = val << 2;
6956 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006957 }
6958 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006959
Willy Tarreauf64d1412010-10-07 20:06:11 +02006960 /* if the cookie has an expiration date and the proxy wants to check
6961 * it, then we do that now. We first check if the cookie is too old,
6962 * then only if it has expired. We detect strict overflow because the
6963 * time resolution here is not great (4 seconds). Cookies with dates
6964 * in the future are ignored if their offset is beyond one day. This
6965 * allows an admin to fix timezone issues without expiring everyone
6966 * and at the same time avoids keeping unwanted side effects for too
6967 * long.
6968 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006969 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6970 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006971 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006972 txn->flags &= ~TX_CK_MASK;
6973 txn->flags |= TX_CK_OLD;
6974 delim = val_beg; // let's pretend we have not found the cookie
6975 txn->cookie_first_date = 0;
6976 txn->cookie_last_date = 0;
6977 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006978 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6979 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006980 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006981 txn->flags &= ~TX_CK_MASK;
6982 txn->flags |= TX_CK_EXPIRED;
6983 delim = val_beg; // let's pretend we have not found the cookie
6984 txn->cookie_first_date = 0;
6985 txn->cookie_last_date = 0;
6986 }
6987
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006988 /* Here, we'll look for the first running server which supports the cookie.
6989 * This allows to share a same cookie between several servers, for example
6990 * to dedicate backup servers to specific servers only.
6991 * However, to prevent clients from sticking to cookie-less backup server
6992 * when they have incidentely learned an empty cookie, we simply ignore
6993 * empty cookies and mark them as invalid.
6994 * The same behaviour is applied when persistence must be ignored.
6995 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006996 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006997 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006998
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006999 while (srv) {
7000 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7001 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02007002 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007003 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007004 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007005 /* we found the server and we can use it */
7006 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02007007 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007008 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007009 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007010 break;
7011 } else {
7012 /* we found a server, but it's down,
7013 * mark it as such and go on in case
7014 * another one is available.
7015 */
7016 txn->flags &= ~TX_CK_MASK;
7017 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007018 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007019 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007020 srv = srv->next;
7021 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007022
Willy Tarreauf64d1412010-10-07 20:06:11 +02007023 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007024 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007025 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007026 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007027 txn->flags |= TX_CK_UNUSED;
7028 else
7029 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007030 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007031
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007032 /* depending on the cookie mode, we may have to either :
7033 * - delete the complete cookie if we're in insert+indirect mode, so that
7034 * the server never sees it ;
7035 * - remove the server id from the cookie value, and tag the cookie as an
7036 * application cookie so that it does not get accidentely removed later,
7037 * if we're in cookie prefix mode
7038 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007039 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007040 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007041
Willy Tarreau9b28e032012-10-12 23:49:43 +02007042 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007043 val_end += delta;
7044 next += delta;
7045 hdr_end += delta;
7046 hdr_next += delta;
7047 cur_hdr->len += delta;
7048 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007049
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007050 del_from = NULL;
7051 preserve_hdr = 1; /* we want to keep this cookie */
7052 }
7053 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007054 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007055 del_from = prev;
7056 }
7057 } else {
7058 /* This is not our cookie, so we must preserve it. But if we already
7059 * scheduled another cookie for removal, we cannot remove the
7060 * complete header, but we can remove the previous block itself.
7061 */
7062 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007063
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007064 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007065 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007066 if (att_beg >= del_from)
7067 att_beg += delta;
7068 if (att_end >= del_from)
7069 att_end += delta;
7070 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007071 val_end += delta;
7072 next += delta;
7073 hdr_end += delta;
7074 hdr_next += delta;
7075 cur_hdr->len += delta;
7076 http_msg_move_end(&txn->req, delta);
7077 prev = del_from;
7078 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007079 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007080 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007081
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007082 /* continue with next cookie on this header line */
7083 att_beg = next;
7084 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007085
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007086 /* There are no more cookies on this line.
7087 * We may still have one (or several) marked for deletion at the
7088 * end of the line. We must do this now in two ways :
7089 * - if some cookies must be preserved, we only delete from the
7090 * mark to the end of line ;
7091 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007092 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007093 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007094 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007095 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007096 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007097 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007098 cur_hdr->len += delta;
7099 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007100 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007101
7102 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007103 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7104 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007105 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007106 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007107 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007108 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007109 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007110 }
7111
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007112 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007113 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007114 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007115}
7116
7117
Willy Tarreaua15645d2007-03-18 16:22:39 +01007118/* Iterate the same filter through all response headers contained in <rtr>.
7119 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7120 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007121int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007122{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007123 char *cur_ptr, *cur_end, *cur_next;
7124 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007125 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007126 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007127 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007128
7129 last_hdr = 0;
7130
Willy Tarreau9b28e032012-10-12 23:49:43 +02007131 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007132 old_idx = 0;
7133
7134 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007135 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007136 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007137 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007138 (exp->action == ACT_ALLOW ||
7139 exp->action == ACT_DENY))
7140 return 0;
7141
7142 cur_idx = txn->hdr_idx.v[old_idx].next;
7143 if (!cur_idx)
7144 break;
7145
7146 cur_hdr = &txn->hdr_idx.v[cur_idx];
7147 cur_ptr = cur_next;
7148 cur_end = cur_ptr + cur_hdr->len;
7149 cur_next = cur_end + cur_hdr->cr + 1;
7150
7151 /* Now we have one header between cur_ptr and cur_end,
7152 * and the next header starts at cur_next.
7153 */
7154
Willy Tarreau15a53a42015-01-21 13:39:42 +01007155 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007156 switch (exp->action) {
7157 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007158 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007159 last_hdr = 1;
7160 break;
7161
7162 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007163 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007164 last_hdr = 1;
7165 break;
7166
7167 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007168 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7169 if (trash.len < 0)
7170 return -1;
7171
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007172 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007173 /* FIXME: if the user adds a newline in the replacement, the
7174 * index will not be recalculated for now, and the new line
7175 * will not be counted as a new header.
7176 */
7177
7178 cur_end += delta;
7179 cur_next += delta;
7180 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007181 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007182 break;
7183
7184 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007185 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007186 cur_next += delta;
7187
Willy Tarreaufa355d42009-11-29 18:12:29 +01007188 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007189 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7190 txn->hdr_idx.used--;
7191 cur_hdr->len = 0;
7192 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007193 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007194 break;
7195
7196 }
7197 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007198
7199 /* keep the link from this header to next one in case of later
7200 * removal of next header.
7201 */
7202 old_idx = cur_idx;
7203 }
7204 return 0;
7205}
7206
7207
7208/* Apply the filter to the status line in the response buffer <rtr>.
7209 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7210 * or -1 if a replacement resulted in an invalid status line.
7211 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007212int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007213{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007214 char *cur_ptr, *cur_end;
7215 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007216 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007217 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007218
7219
Willy Tarreau3d300592007-03-18 18:34:41 +01007220 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007221 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007222 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007223 (exp->action == ACT_ALLOW ||
7224 exp->action == ACT_DENY))
7225 return 0;
7226 else if (exp->action == ACT_REMOVE)
7227 return 0;
7228
7229 done = 0;
7230
Willy Tarreau9b28e032012-10-12 23:49:43 +02007231 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007232 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007233
7234 /* Now we have the status line between cur_ptr and cur_end */
7235
Willy Tarreau15a53a42015-01-21 13:39:42 +01007236 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007237 switch (exp->action) {
7238 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007239 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007240 done = 1;
7241 break;
7242
7243 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007244 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007245 done = 1;
7246 break;
7247
7248 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007249 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7250 if (trash.len < 0)
7251 return -1;
7252
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007253 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007254 /* FIXME: if the user adds a newline in the replacement, the
7255 * index will not be recalculated for now, and the new line
7256 * will not be counted as a new header.
7257 */
7258
Willy Tarreaufa355d42009-11-29 18:12:29 +01007259 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007260 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007261 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007262 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007263 cur_ptr, cur_end + 1,
7264 NULL, NULL);
7265 if (unlikely(!cur_end))
7266 return -1;
7267
7268 /* we have a full respnse and we know that we have either a CR
7269 * or an LF at <ptr>.
7270 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007271 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007272 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007273 /* there is no point trying this regex on headers */
7274 return 1;
7275 }
7276 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007277 return done;
7278}
7279
7280
7281
7282/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007283 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007284 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7285 * unparsable response.
7286 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007287int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007288{
Willy Tarreau192252e2015-04-04 01:47:55 +02007289 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007290 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007291 struct hdr_exp *exp;
7292
7293 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007294 int ret;
7295
7296 /*
7297 * The interleaving of transformations and verdicts
7298 * makes it difficult to decide to continue or stop
7299 * the evaluation.
7300 */
7301
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007302 if (txn->flags & TX_SVDENY)
7303 break;
7304
Willy Tarreau3d300592007-03-18 18:34:41 +01007305 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007306 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7307 exp->action == ACT_PASS)) {
7308 exp = exp->next;
7309 continue;
7310 }
7311
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007312 /* if this filter had a condition, evaluate it now and skip to
7313 * next filter if the condition does not match.
7314 */
7315 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007316 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007317 ret = acl_pass(ret);
7318 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7319 ret = !ret;
7320 if (!ret)
7321 continue;
7322 }
7323
Willy Tarreaua15645d2007-03-18 16:22:39 +01007324 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007325 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007326 if (unlikely(ret < 0))
7327 return -1;
7328
7329 if (likely(ret == 0)) {
7330 /* The filter did not match the response, it can be
7331 * iterated through all headers.
7332 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007333 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7334 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007335 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007336 }
7337 return 0;
7338}
7339
7340
Willy Tarreaua15645d2007-03-18 16:22:39 +01007341/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007342 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007343 * desirable to call it only when needed. This function is also used when we
7344 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007345 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007346void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007347{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007348 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007349 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01007350 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007351 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007352 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007353 char *hdr_beg, *hdr_end, *hdr_next;
7354 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007355
Willy Tarreaua15645d2007-03-18 16:22:39 +01007356 /* Iterate through the headers.
7357 * we start with the start line.
7358 */
7359 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007360 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007361
7362 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7363 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007364 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007365
7366 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007367 hdr_beg = hdr_next;
7368 hdr_end = hdr_beg + cur_hdr->len;
7369 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007370
Willy Tarreau24581ba2010-08-31 22:39:35 +02007371 /* We have one full header between hdr_beg and hdr_end, and the
7372 * next header starts at hdr_next. We're only interested in
7373 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007374 */
7375
Willy Tarreau24581ba2010-08-31 22:39:35 +02007376 is_cookie2 = 0;
7377 prev = hdr_beg + 10;
7378 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007379 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007380 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7381 if (!val) {
7382 old_idx = cur_idx;
7383 continue;
7384 }
7385 is_cookie2 = 1;
7386 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007387 }
7388
Willy Tarreau24581ba2010-08-31 22:39:35 +02007389 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7390 * <prev> points to the colon.
7391 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007392 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007393
Willy Tarreau24581ba2010-08-31 22:39:35 +02007394 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7395 * check-cache is enabled) and we are not interested in checking
7396 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007397 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02007398 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007399 return;
7400
Willy Tarreau24581ba2010-08-31 22:39:35 +02007401 /* OK so now we know we have to process this response cookie.
7402 * The format of the Set-Cookie header is slightly different
7403 * from the format of the Cookie header in that it does not
7404 * support the comma as a cookie delimiter (thus the header
7405 * cannot be folded) because the Expires attribute described in
7406 * the original Netscape's spec may contain an unquoted date
7407 * with a comma inside. We have to live with this because
7408 * many browsers don't support Max-Age and some browsers don't
7409 * support quoted strings. However the Set-Cookie2 header is
7410 * clean.
7411 *
7412 * We have to keep multiple pointers in order to support cookie
7413 * removal at the beginning, middle or end of header without
7414 * corrupting the header (in case of set-cookie2). A special
7415 * pointer, <scav> points to the beginning of the set-cookie-av
7416 * fields after the first semi-colon. The <next> pointer points
7417 * either to the end of line (set-cookie) or next unquoted comma
7418 * (set-cookie2). All of these headers are valid :
7419 *
7420 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7421 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7422 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7423 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7424 * | | | | | | | | | |
7425 * | | | | | | | | +-> next hdr_end <--+
7426 * | | | | | | | +------------> scav
7427 * | | | | | | +--------------> val_end
7428 * | | | | | +--------------------> val_beg
7429 * | | | | +----------------------> equal
7430 * | | | +------------------------> att_end
7431 * | | +----------------------------> att_beg
7432 * | +------------------------------> prev
7433 * +-----------------------------------------> hdr_beg
7434 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007435
Willy Tarreau24581ba2010-08-31 22:39:35 +02007436 for (; prev < hdr_end; prev = next) {
7437 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007438
Willy Tarreau24581ba2010-08-31 22:39:35 +02007439 /* find att_beg */
7440 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007441 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007442 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007443
Willy Tarreau24581ba2010-08-31 22:39:35 +02007444 /* find att_end : this is the first character after the last non
7445 * space before the equal. It may be equal to hdr_end.
7446 */
7447 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007448
Willy Tarreau24581ba2010-08-31 22:39:35 +02007449 while (equal < hdr_end) {
7450 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7451 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007452 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007453 continue;
7454 att_end = equal;
7455 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007456
Willy Tarreau24581ba2010-08-31 22:39:35 +02007457 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7458 * is between <att_beg> and <equal>, both may be identical.
7459 */
7460
7461 /* look for end of cookie if there is an equal sign */
7462 if (equal < hdr_end && *equal == '=') {
7463 /* look for the beginning of the value */
7464 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007465 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007466 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007467
Willy Tarreau24581ba2010-08-31 22:39:35 +02007468 /* find the end of the value, respecting quotes */
7469 next = find_cookie_value_end(val_beg, hdr_end);
7470
7471 /* make val_end point to the first white space or delimitor after the value */
7472 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007473 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007474 val_end--;
7475 } else {
7476 /* <equal> points to next comma, semi-colon or EOL */
7477 val_beg = val_end = next = equal;
7478 }
7479
7480 if (next < hdr_end) {
7481 /* Set-Cookie2 supports multiple cookies, and <next> points to
7482 * a colon or semi-colon before the end. So skip all attr-value
7483 * pairs and look for the next comma. For Set-Cookie, since
7484 * commas are permitted in values, skip to the end.
7485 */
7486 if (is_cookie2)
7487 next = find_hdr_value_end(next, hdr_end);
7488 else
7489 next = hdr_end;
7490 }
7491
7492 /* Now everything is as on the diagram above */
7493
7494 /* Ignore cookies with no equal sign */
7495 if (equal == val_end)
7496 continue;
7497
7498 /* If there are spaces around the equal sign, we need to
7499 * strip them otherwise we'll get trouble for cookie captures,
7500 * or even for rewrites. Since this happens extremely rarely,
7501 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007502 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007503 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7504 int stripped_before = 0;
7505 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007506
Willy Tarreau24581ba2010-08-31 22:39:35 +02007507 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007508 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007509 equal += stripped_before;
7510 val_beg += stripped_before;
7511 }
7512
7513 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007514 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007515 val_beg += stripped_after;
7516 stripped_before += stripped_after;
7517 }
7518
7519 val_end += stripped_before;
7520 next += stripped_before;
7521 hdr_end += stripped_before;
7522 hdr_next += stripped_before;
7523 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007524 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007525 }
7526
7527 /* First, let's see if we want to capture this cookie. We check
7528 * that we don't already have a server side cookie, because we
7529 * can only capture one. Also as an optimisation, we ignore
7530 * cookies shorter than the declared name.
7531 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007532 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007533 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007534 (val_end - att_beg >= sess->fe->capture_namelen) &&
7535 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007536 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007537 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007538 Alert("HTTP logging : out of memory.\n");
7539 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007540 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007541 if (log_len > sess->fe->capture_len)
7542 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007543 memcpy(txn->srv_cookie, att_beg, log_len);
7544 txn->srv_cookie[log_len] = 0;
7545 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007546 }
7547
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007548 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007549 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007550 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007551 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7552 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007553 /* assume passive cookie by default */
7554 txn->flags &= ~TX_SCK_MASK;
7555 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007556
7557 /* If the cookie is in insert mode on a known server, we'll delete
7558 * this occurrence because we'll insert another one later.
7559 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007560 * a direct access.
7561 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007562 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007563 /* The "preserve" flag was set, we don't want to touch the
7564 * server's cookie.
7565 */
7566 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007567 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007568 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007569 /* this cookie must be deleted */
7570 if (*prev == ':' && next == hdr_end) {
7571 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007572 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007573 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7574 txn->hdr_idx.used--;
7575 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007576 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007577 hdr_next += delta;
7578 http_msg_move_end(&txn->rsp, delta);
7579 /* note: while both invalid now, <next> and <hdr_end>
7580 * are still equal, so the for() will stop as expected.
7581 */
7582 } else {
7583 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007584 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007585 next = prev;
7586 hdr_end += delta;
7587 hdr_next += delta;
7588 cur_hdr->len += delta;
7589 http_msg_move_end(&txn->rsp, delta);
7590 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007591 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007592 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007593 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007594 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007595 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007596 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007597 * with this server since we know it.
7598 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007599 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007600 next += delta;
7601 hdr_end += delta;
7602 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007603 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007604 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007605
Willy Tarreauf1348312010-10-07 15:54:11 +02007606 txn->flags &= ~TX_SCK_MASK;
7607 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007608 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007609 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007610 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007611 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007612 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007613 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007614 next += delta;
7615 hdr_end += delta;
7616 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007617 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007618 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007619
Willy Tarreau827aee92011-03-10 16:55:02 +01007620 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007621 txn->flags &= ~TX_SCK_MASK;
7622 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007623 }
7624 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007625 /* that's done for this cookie, check the next one on the same
7626 * line when next != hdr_end (only if is_cookie2).
7627 */
7628 }
7629 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007630 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007631 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007632}
7633
7634
Willy Tarreaua15645d2007-03-18 16:22:39 +01007635/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007636 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007637 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007638void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007639{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007640 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007641 char *p1, *p2;
7642
7643 char *cur_ptr, *cur_end, *cur_next;
7644 int cur_idx;
7645
Willy Tarreau5df51872007-11-25 16:20:08 +01007646 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007647 return;
7648
7649 /* Iterate through the headers.
7650 * we start with the start line.
7651 */
7652 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007653 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007654
7655 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7656 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007657 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007658
7659 cur_hdr = &txn->hdr_idx.v[cur_idx];
7660 cur_ptr = cur_next;
7661 cur_end = cur_ptr + cur_hdr->len;
7662 cur_next = cur_end + cur_hdr->cr + 1;
7663
7664 /* We have one full header between cur_ptr and cur_end, and the
7665 * next header starts at cur_next. We're only interested in
7666 * "Cookie:" headers.
7667 */
7668
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007669 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7670 if (val) {
7671 if ((cur_end - (cur_ptr + val) >= 8) &&
7672 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7673 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7674 return;
7675 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007676 }
7677
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007678 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7679 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007680 continue;
7681
7682 /* OK, right now we know we have a cache-control header at cur_ptr */
7683
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007684 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007685
7686 if (p1 >= cur_end) /* no more info */
7687 continue;
7688
7689 /* p1 is at the beginning of the value */
7690 p2 = p1;
7691
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007692 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007693 p2++;
7694
7695 /* we have a complete value between p1 and p2 */
7696 if (p2 < cur_end && *p2 == '=') {
7697 /* we have something of the form no-cache="set-cookie" */
7698 if ((cur_end - p1 >= 21) &&
7699 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7700 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007701 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007702 continue;
7703 }
7704
7705 /* OK, so we know that either p2 points to the end of string or to a comma */
7706 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007707 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01007708 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7709 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7710 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007711 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007712 return;
7713 }
7714
7715 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007716 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007717 continue;
7718 }
7719 }
7720}
7721
Willy Tarreau58f10d72006-12-04 02:26:12 +01007722
Willy Tarreaub2513902006-12-17 14:52:38 +01007723/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007724 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007725 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007726 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007727 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007728 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007729 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007730 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007731 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007732int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007733{
7734 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007735 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007736 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007737
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007738 if (!uri_auth)
7739 return 0;
7740
Cyril Bonté70be45d2010-10-12 00:14:35 +02007741 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007742 return 0;
7743
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007744 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007745 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007746 return 0;
7747
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007748 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007749 return 0;
7750
Willy Tarreaub2513902006-12-17 14:52:38 +01007751 return 1;
7752}
7753
Willy Tarreau4076a152009-04-02 15:18:36 +02007754/*
7755 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007756 * By default it tries to report the error position as msg->err_pos. However if
7757 * this one is not set, it will then report msg->next, which is the last known
7758 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007759 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007760 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02007761void http_capture_bad_message(struct proxy *proxy, struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007762 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007763 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007764{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007765 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007766 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007767 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007768
Christopher Faulet2a944ee2017-11-07 10:42:54 +01007769 HA_SPIN_LOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreauf3764b72016-03-31 13:45:10 +02007770 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02007771 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007772 len1 = MIN(len1, es->len);
7773 len2 = es->len - len1; /* remaining data if buffer wraps */
7774
Willy Tarreauf3764b72016-03-31 13:45:10 +02007775 if (!es->buf)
7776 es->buf = malloc(global.tune.bufsize);
7777
7778 if (es->buf) {
7779 memcpy(es->buf, chn->buf->p, len1);
7780 if (len2)
7781 memcpy(es->buf + len1, chn->buf->data, len2);
7782 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007783
Willy Tarreau4076a152009-04-02 15:18:36 +02007784 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007785 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007786 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007787 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007788
Willy Tarreau4076a152009-04-02 15:18:36 +02007789 es->when = date; // user-visible date
7790 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007791 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007792 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007793 if (objt_conn(sess->origin))
7794 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007795 else
7796 memset(&es->src, 0, sizeof(es->src));
7797
Willy Tarreau078272e2010-12-12 12:46:33 +01007798 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007799 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007800 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007801 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007802 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007803 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007804 es->b_out = chn->buf->o;
7805 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007806 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007807 es->m_clen = msg->chunk_len;
7808 es->m_blen = msg->body_len;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01007809 HA_SPIN_UNLOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreau4076a152009-04-02 15:18:36 +02007810}
Willy Tarreaub2513902006-12-17 14:52:38 +01007811
Willy Tarreau294c4732011-12-16 21:35:50 +01007812/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7813 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7814 * performed over the whole headers. Otherwise it must contain a valid header
7815 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7816 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7817 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7818 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007819 * -1. The value fetch stops at commas, so this function is suited for use with
7820 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007821 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007822 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007823unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007824 struct hdr_idx *idx, int occ,
7825 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007826{
Willy Tarreau294c4732011-12-16 21:35:50 +01007827 struct hdr_ctx local_ctx;
7828 char *ptr_hist[MAX_HDR_HISTORY];
7829 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007830 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007831 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007832
Willy Tarreau294c4732011-12-16 21:35:50 +01007833 if (!ctx) {
7834 local_ctx.idx = 0;
7835 ctx = &local_ctx;
7836 }
7837
Willy Tarreaubce70882009-09-07 11:51:47 +02007838 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007839 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007840 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007841 occ--;
7842 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007843 *vptr = ctx->line + ctx->val;
7844 *vlen = ctx->vlen;
7845 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007846 }
7847 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007848 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007849 }
7850
7851 /* negative occurrence, we scan all the list then walk back */
7852 if (-occ > MAX_HDR_HISTORY)
7853 return 0;
7854
Willy Tarreau294c4732011-12-16 21:35:50 +01007855 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007856 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007857 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7858 len_hist[hist_ptr] = ctx->vlen;
7859 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007860 hist_ptr = 0;
7861 found++;
7862 }
7863 if (-occ > found)
7864 return 0;
7865 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007866 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7867 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7868 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007869 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007870 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007871 if (hist_ptr >= MAX_HDR_HISTORY)
7872 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007873 *vptr = ptr_hist[hist_ptr];
7874 *vlen = len_hist[hist_ptr];
7875 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007876}
7877
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007878/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7879 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7880 * performed over the whole headers. Otherwise it must contain a valid header
7881 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7882 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7883 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7884 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7885 * -1. This function differs from http_get_hdr() in that it only returns full
7886 * line header values and does not stop at commas.
7887 * The return value is 0 if nothing was found, or non-zero otherwise.
7888 */
7889unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7890 struct hdr_idx *idx, int occ,
7891 struct hdr_ctx *ctx, char **vptr, int *vlen)
7892{
7893 struct hdr_ctx local_ctx;
7894 char *ptr_hist[MAX_HDR_HISTORY];
7895 int len_hist[MAX_HDR_HISTORY];
7896 unsigned int hist_ptr;
7897 int found;
7898
7899 if (!ctx) {
7900 local_ctx.idx = 0;
7901 ctx = &local_ctx;
7902 }
7903
7904 if (occ >= 0) {
7905 /* search from the beginning */
7906 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
7907 occ--;
7908 if (occ <= 0) {
7909 *vptr = ctx->line + ctx->val;
7910 *vlen = ctx->vlen;
7911 return 1;
7912 }
7913 }
7914 return 0;
7915 }
7916
7917 /* negative occurrence, we scan all the list then walk back */
7918 if (-occ > MAX_HDR_HISTORY)
7919 return 0;
7920
7921 found = hist_ptr = 0;
7922 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
7923 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7924 len_hist[hist_ptr] = ctx->vlen;
7925 if (++hist_ptr >= MAX_HDR_HISTORY)
7926 hist_ptr = 0;
7927 found++;
7928 }
7929 if (-occ > found)
7930 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007931
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007932 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007933 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7934 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7935 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007936 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007937 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007938 if (hist_ptr >= MAX_HDR_HISTORY)
7939 hist_ptr -= MAX_HDR_HISTORY;
7940 *vptr = ptr_hist[hist_ptr];
7941 *vlen = len_hist[hist_ptr];
7942 return 1;
7943}
7944
Willy Tarreaubaaee002006-06-26 02:48:02 +02007945/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007946 * Print a debug line with a header. Always stop at the first CR or LF char,
7947 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7948 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007949 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007950void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007951{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007952 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007953 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007954
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007955 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007956 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007957 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02007958 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 +02007959
7960 for (max = 0; start + max < end; max++)
7961 if (start[max] == '\r' || start[max] == '\n')
7962 break;
7963
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007964 UBOUND(max, trash.size - trash.len - 3);
7965 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
7966 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01007967 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007968}
7969
Willy Tarreaueee5b512015-04-03 23:46:31 +02007970
7971/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7972 * The hdr_idx is allocated as well. In case of allocation failure, everything
7973 * allocated is freed and NULL is returned. Otherwise the new transaction is
7974 * assigned to the stream and returned.
7975 */
7976struct http_txn *http_alloc_txn(struct stream *s)
7977{
7978 struct http_txn *txn = s->txn;
7979
7980 if (txn)
7981 return txn;
7982
7983 txn = pool_alloc2(pool2_http_txn);
7984 if (!txn)
7985 return txn;
7986
7987 txn->hdr_idx.size = global.tune.max_http_hdr;
7988 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
7989 if (!txn->hdr_idx.v) {
7990 pool_free2(pool2_http_txn, txn);
7991 return NULL;
7992 }
7993
7994 s->txn = txn;
7995 return txn;
7996}
7997
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007998void http_txn_reset_req(struct http_txn *txn)
7999{
8000 txn->req.flags = 0;
8001 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8002 txn->req.next = 0;
8003 txn->req.chunk_len = 0LL;
8004 txn->req.body_len = 0LL;
8005 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8006}
8007
8008void http_txn_reset_res(struct http_txn *txn)
8009{
8010 txn->rsp.flags = 0;
8011 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8012 txn->rsp.next = 0;
8013 txn->rsp.chunk_len = 0LL;
8014 txn->rsp.body_len = 0LL;
8015 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8016}
8017
Willy Tarreau0937bc42009-12-22 15:03:09 +01008018/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008019 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008020 * the required fields are properly allocated and that we only need to (re)init
8021 * them. This should be used before processing any new request.
8022 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008023void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008024{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008025 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008026 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008027
8028 txn->flags = 0;
8029 txn->status = -1;
8030
Willy Tarreauf64d1412010-10-07 20:06:11 +02008031 txn->cookie_first_date = 0;
8032 txn->cookie_last_date = 0;
8033
Willy Tarreaueee5b512015-04-03 23:46:31 +02008034 txn->srv_cookie = NULL;
8035 txn->cli_cookie = NULL;
8036 txn->uri = NULL;
8037
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008038 http_txn_reset_req(txn);
8039 http_txn_reset_res(txn);
8040
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008041 txn->req.chn = &s->req;
8042 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008043
8044 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008045
8046 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8047 if (fe->options2 & PR_O2_REQBUG_OK)
8048 txn->req.err_pos = -1; /* let buggy requests pass */
8049
Willy Tarreau0937bc42009-12-22 15:03:09 +01008050 if (txn->hdr_idx.v)
8051 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008052
8053 vars_init(&s->vars_txn, SCOPE_TXN);
8054 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008055}
8056
8057/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008058void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008059{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008060 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008061 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008062
8063 /* these ones will have been dynamically allocated */
8064 pool_free2(pool2_requri, txn->uri);
8065 pool_free2(pool2_capture, txn->cli_cookie);
8066 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008067 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008068
William Lallemanda73203e2012-03-12 12:48:57 +01008069 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008070 txn->uri = NULL;
8071 txn->srv_cookie = NULL;
8072 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008073
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008074 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008075 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008076 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008077 pool_free2(h->pool, s->req_cap[h->index]);
8078 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008079 }
8080
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008081 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008082 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008083 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008084 pool_free2(h->pool, s->res_cap[h->index]);
8085 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008086 }
8087
Willy Tarreau6204cd92016-03-10 16:33:04 +01008088 vars_prune(&s->vars_txn, s->sess, s);
8089 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008090}
8091
8092/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008093void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008094{
8095 http_end_txn(s);
8096 http_init_txn(s);
8097
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008098 /* reinitialise the current rule list pointer to NULL. We are sure that
8099 * any rulelist match the NULL pointer.
8100 */
8101 s->current_rule_list = NULL;
8102
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008103 s->be = strm_fe(s);
8104 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008105 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008106 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008107 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008108 /* re-init store persistence */
8109 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008110 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008111
Willy Tarreau0937bc42009-12-22 15:03:09 +01008112 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008113
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008114 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008115
Willy Tarreau739cfba2010-01-25 23:11:14 +01008116 /* We must trim any excess data from the response buffer, because we
8117 * may have blocked an invalid response from a server that we don't
8118 * want to accidentely forward once we disable the analysers, nor do
8119 * we want those data to come along with next response. A typical
8120 * example of such data would be from a buggy server responding to
8121 * a HEAD with some data, or sending more than the advertised
8122 * content-length.
8123 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008124 if (unlikely(s->res.buf->i))
8125 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008126
Christopher Fauletc0c672a2017-03-28 11:51:33 +02008127 /* Now we can realign the response buffer */
8128 buffer_realign(s->res.buf);
8129
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008130 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008131 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008132
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008133 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008134 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008135
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008136 s->req.rex = TICK_ETERNITY;
8137 s->req.wex = TICK_ETERNITY;
8138 s->req.analyse_exp = TICK_ETERNITY;
8139 s->res.rex = TICK_ETERNITY;
8140 s->res.wex = TICK_ETERNITY;
8141 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01008142 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008143}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008144
Sasha Pachev218f0642014-06-16 12:05:59 -06008145void free_http_res_rules(struct list *r)
8146{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008147 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008148
8149 list_for_each_entry_safe(pr, tr, r, list) {
8150 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008151 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008152 free(pr);
8153 }
8154}
8155
8156void free_http_req_rules(struct list *r)
8157{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008158 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01008159
8160 list_for_each_entry_safe(pr, tr, r, list) {
8161 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008162 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008163 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008164
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008165 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008166 free(pr);
8167 }
8168}
8169
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008170/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008171struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01008172{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008173 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008174 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008175 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008176 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008177
Vincent Bernat02779b62016-04-03 13:48:43 +02008178 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01008179 if (!rule) {
8180 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008181 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008182 }
8183
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008184 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008185 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01008186 cur_arg = 1;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008187 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block") || !strcmp(args[0], "tarpit")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04008188 int code;
8189 int hc;
8190
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008191 if (!strcmp(args[0], "tarpit")) {
8192 rule->action = ACT_HTTP_REQ_TARPIT;
8193 rule->deny_status = HTTP_ERR_500;
8194 }
8195 else {
8196 rule->action = ACT_ACTION_DENY;
8197 rule->deny_status = HTTP_ERR_403;
8198 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008199 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04008200 if (strcmp(args[cur_arg], "deny_status") == 0) {
8201 cur_arg++;
8202 if (!args[cur_arg]) {
8203 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
8204 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
8205 goto out_err;
8206 }
8207
8208 code = atol(args[cur_arg]);
8209 cur_arg++;
8210 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
8211 if (http_err_codes[hc] == code) {
8212 rule->deny_status = hc;
8213 break;
8214 }
8215 }
8216
8217 if (hc >= HTTP_ERR_SIZE) {
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008218 Warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n",
8219 file, linenum, code, http_err_codes[rule->deny_status]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008220 }
8221 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008222 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008223 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008224 cur_arg = 1;
8225
8226 while(*args[cur_arg]) {
8227 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008228 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008229 cur_arg+=2;
8230 continue;
8231 } else
8232 break;
8233 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008234 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008235 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008236 cur_arg = 1;
8237
8238 if (!*args[cur_arg] ||
8239 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8240 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8241 file, linenum, args[0]);
8242 goto out_err;
8243 }
8244 rule->arg.nice = atoi(args[cur_arg]);
8245 if (rule->arg.nice < -1024)
8246 rule->arg.nice = -1024;
8247 else if (rule->arg.nice > 1024)
8248 rule->arg.nice = 1024;
8249 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008250 } else if (!strcmp(args[0], "set-tos")) {
8251#ifdef IP_TOS
8252 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008253 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008254 cur_arg = 1;
8255
8256 if (!*args[cur_arg] ||
8257 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8258 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8259 file, linenum, args[0]);
8260 goto out_err;
8261 }
8262
8263 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8264 if (err && *err != '\0') {
8265 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8266 file, linenum, err, args[0]);
8267 goto out_err;
8268 }
8269 cur_arg++;
8270#else
8271 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8272 goto out_err;
8273#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008274 } else if (!strcmp(args[0], "set-mark")) {
8275#ifdef SO_MARK
8276 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008277 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008278 cur_arg = 1;
8279
8280 if (!*args[cur_arg] ||
8281 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8282 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8283 file, linenum, args[0]);
8284 goto out_err;
8285 }
8286
8287 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8288 if (err && *err != '\0') {
8289 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8290 file, linenum, err, args[0]);
8291 goto out_err;
8292 }
8293 cur_arg++;
8294 global.last_checks |= LSTCHK_NETADM;
8295#else
8296 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8297 goto out_err;
8298#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008299 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008300 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008301 cur_arg = 1;
8302
8303 if (!*args[cur_arg] ||
8304 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8305 bad_log_level:
8306 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8307 file, linenum, args[0]);
8308 goto out_err;
8309 }
8310 if (strcmp(args[cur_arg], "silent") == 0)
8311 rule->arg.loglevel = -1;
8312 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8313 goto bad_log_level;
8314 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008315 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008316 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008317 cur_arg = 1;
8318
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008319 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8320 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008321 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8322 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008323 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008324 }
8325
8326 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8327 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8328 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008329
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008330 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008331 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008332 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 +01008333 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8334 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8335 file, linenum, args[0], error);
8336 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008337 goto out_err;
8338 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008339 free(proxy->conf.lfs_file);
8340 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8341 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008342 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02008343 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008344 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008345 cur_arg = 1;
8346
8347 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02008348 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
Sasha Pachev218f0642014-06-16 12:05:59 -06008349 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
8350 file, linenum, args[0]);
8351 goto out_err;
8352 }
8353
8354 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8355 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8356 LIST_INIT(&rule->arg.hdr_add.fmt);
8357
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008358 error = NULL;
8359 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
8360 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8361 args[cur_arg + 1], error);
8362 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008363 goto out_err;
8364 }
8365
8366 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008367 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008368 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 +01008369 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8370 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8371 file, linenum, args[0], error);
8372 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008373 goto out_err;
8374 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008375
8376 free(proxy->conf.lfs_file);
8377 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8378 proxy->conf.lfs_line = proxy->conf.args.line;
8379 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008380 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008381 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008382 cur_arg = 1;
8383
8384 if (!*args[cur_arg] ||
8385 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8386 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8387 file, linenum, args[0]);
8388 goto out_err;
8389 }
8390
8391 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8392 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8393
8394 proxy->conf.args.ctx = ARGC_HRQ;
8395 free(proxy->conf.lfs_file);
8396 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8397 proxy->conf.lfs_line = proxy->conf.args.line;
8398 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02008399 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
8400 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02008401 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02008402 struct sample_expr *expr;
8403 unsigned int where;
8404 char *err = NULL;
8405
8406 cur_arg = 1;
8407 proxy->conf.args.ctx = ARGC_TRK;
8408
8409 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
8410 if (!expr) {
8411 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8412 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
8413 free(err);
8414 goto out_err;
8415 }
8416
8417 where = 0;
8418 if (proxy->cap & PR_CAP_FE)
8419 where |= SMP_VAL_FE_HRQ_HDR;
8420 if (proxy->cap & PR_CAP_BE)
8421 where |= SMP_VAL_BE_HRQ_HDR;
8422
8423 if (!(expr->fetch->val & where)) {
8424 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
8425 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
8426 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
8427 args[cur_arg-1], sample_src_names(expr->fetch->use));
8428 free(expr);
8429 goto out_err;
8430 }
8431
8432 if (strcmp(args[cur_arg], "table") == 0) {
8433 cur_arg++;
8434 if (!args[cur_arg]) {
8435 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
8436 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
8437 free(expr);
8438 goto out_err;
8439 }
8440 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008441 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008442 cur_arg++;
8443 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008444 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008445 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Christopher Faulet78880fb2017-09-18 14:43:55 +02008446 rule->check_ptr = check_trk_action;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008447 } else if (strcmp(args[0], "redirect") == 0) {
8448 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008449 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008450
Willy Tarreaube4653b2015-05-28 15:26:58 +02008451 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008452 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8453 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8454 goto out_err;
8455 }
8456
8457 /* this redirect rule might already contain a parsed condition which
8458 * we'll pass to the http-request rule.
8459 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008460 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008461 rule->arg.redir = redir;
8462 rule->cond = redir->cond;
8463 redir->cond = NULL;
8464 cur_arg = 2;
8465 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008466 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8467 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008468 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008469 /*
8470 * '+ 8' for 'add-acl('
8471 * '- 9' for 'add-acl(' + trailing ')'
8472 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008473 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008474
8475 cur_arg = 1;
8476
8477 if (!*args[cur_arg] ||
8478 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8479 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8480 file, linenum, args[0]);
8481 goto out_err;
8482 }
8483
8484 LIST_INIT(&rule->arg.map.key);
8485 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008486 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008487 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008488 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8489 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8490 file, linenum, args[0], error);
8491 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008492 goto out_err;
8493 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008494 free(proxy->conf.lfs_file);
8495 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8496 proxy->conf.lfs_line = proxy->conf.args.line;
8497 cur_arg += 1;
8498 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8499 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008500 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008501 /*
8502 * '+ 8' for 'del-acl('
8503 * '- 9' for 'del-acl(' + trailing ')'
8504 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008505 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008506
8507 cur_arg = 1;
8508
8509 if (!*args[cur_arg] ||
8510 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8511 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8512 file, linenum, args[0]);
8513 goto out_err;
8514 }
8515
8516 LIST_INIT(&rule->arg.map.key);
8517 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008518 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008519 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008520 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8521 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8522 file, linenum, args[0], error);
8523 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008524 goto out_err;
8525 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008526 free(proxy->conf.lfs_file);
8527 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8528 proxy->conf.lfs_line = proxy->conf.args.line;
8529 cur_arg += 1;
8530 } else if (strncmp(args[0], "del-map", 7) == 0) {
8531 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008532 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008533 /*
8534 * '+ 8' for 'del-map('
8535 * '- 9' for 'del-map(' + trailing ')'
8536 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008537 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008538
8539 cur_arg = 1;
8540
8541 if (!*args[cur_arg] ||
8542 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8543 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8544 file, linenum, args[0]);
8545 goto out_err;
8546 }
8547
8548 LIST_INIT(&rule->arg.map.key);
8549 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008550 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008551 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008552 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8553 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8554 file, linenum, args[0], error);
8555 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008556 goto out_err;
8557 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008558 free(proxy->conf.lfs_file);
8559 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8560 proxy->conf.lfs_line = proxy->conf.args.line;
8561 cur_arg += 1;
8562 } else if (strncmp(args[0], "set-map", 7) == 0) {
8563 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008564 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008565 /*
8566 * '+ 8' for 'set-map('
8567 * '- 9' for 'set-map(' + trailing ')'
8568 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008569 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008570
8571 cur_arg = 1;
8572
8573 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8574 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8575 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8576 file, linenum, args[0]);
8577 goto out_err;
8578 }
8579
8580 LIST_INIT(&rule->arg.map.key);
8581 LIST_INIT(&rule->arg.map.value);
8582 proxy->conf.args.ctx = ARGC_HRQ;
8583
8584 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008585 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008586 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008587 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8588 Alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
8589 file, linenum, args[0], error);
8590 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008591 goto out_err;
8592 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008593
8594 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008595 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008596 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 +01008597 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8598 Alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
8599 file, linenum, args[0], error);
8600 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008601 goto out_err;
8602 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008603 free(proxy->conf.lfs_file);
8604 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8605 proxy->conf.lfs_line = proxy->conf.args.line;
8606
8607 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02008608 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
8609 char *errmsg = NULL;
8610 cur_arg = 1;
8611 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02008612 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02008613 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02008614 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02008615 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8616 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8617 free(errmsg);
8618 goto out_err;
8619 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008620 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008621 action_build_list(&http_req_keywords.list, &trash);
8622 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
8623 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08008624 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02008625 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008626 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008627 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008628 }
8629
8630 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8631 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008632 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008633
Christopher Faulet1b421ea2017-09-22 14:38:56 +02008634 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008635 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8636 file, linenum, args[0], errmsg);
8637 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008638 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008639 }
8640 rule->cond = cond;
8641 }
8642 else if (*args[cur_arg]) {
8643 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8644 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8645 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008646 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008647 }
8648
8649 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008650 out_err:
8651 free(rule);
8652 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008653}
8654
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008655/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008656struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008657{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008658 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008659 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008660 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008661 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008662
8663 rule = calloc(1, sizeof(*rule));
8664 if (!rule) {
8665 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
8666 goto out_err;
8667 }
8668
8669 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008670 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008671 cur_arg = 1;
8672 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008673 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008674 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008675 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008676 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008677 cur_arg = 1;
8678
8679 if (!*args[cur_arg] ||
8680 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8681 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8682 file, linenum, args[0]);
8683 goto out_err;
8684 }
8685 rule->arg.nice = atoi(args[cur_arg]);
8686 if (rule->arg.nice < -1024)
8687 rule->arg.nice = -1024;
8688 else if (rule->arg.nice > 1024)
8689 rule->arg.nice = 1024;
8690 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008691 } else if (!strcmp(args[0], "set-tos")) {
8692#ifdef IP_TOS
8693 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008694 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008695 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)) {
8699 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8700 file, linenum, args[0]);
8701 goto out_err;
8702 }
8703
8704 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8705 if (err && *err != '\0') {
8706 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8707 file, linenum, err, args[0]);
8708 goto out_err;
8709 }
8710 cur_arg++;
8711#else
8712 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8713 goto out_err;
8714#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008715 } else if (!strcmp(args[0], "set-mark")) {
8716#ifdef SO_MARK
8717 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008718 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008719 cur_arg = 1;
8720
8721 if (!*args[cur_arg] ||
8722 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8723 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8724 file, linenum, args[0]);
8725 goto out_err;
8726 }
8727
8728 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8729 if (err && *err != '\0') {
8730 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8731 file, linenum, err, args[0]);
8732 goto out_err;
8733 }
8734 cur_arg++;
8735 global.last_checks |= LSTCHK_NETADM;
8736#else
8737 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8738 goto out_err;
8739#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008740 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008741 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008742 cur_arg = 1;
8743
8744 if (!*args[cur_arg] ||
8745 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8746 bad_log_level:
8747 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8748 file, linenum, args[0]);
8749 goto out_err;
8750 }
8751 if (strcmp(args[cur_arg], "silent") == 0)
8752 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08008753 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008754 goto bad_log_level;
8755 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008756 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008757 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008758 cur_arg = 1;
8759
8760 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8761 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8762 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8763 file, linenum, args[0]);
8764 goto out_err;
8765 }
8766
8767 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8768 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8769 LIST_INIT(&rule->arg.hdr_add.fmt);
8770
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008771 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008772 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008773 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 +01008774 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8775 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8776 file, linenum, args[0], error);
8777 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008778 goto out_err;
8779 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008780 free(proxy->conf.lfs_file);
8781 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8782 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008783 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06008784 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008785 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008786 cur_arg = 1;
8787
8788 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02008789 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
8790 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06008791 file, linenum, args[0]);
8792 goto out_err;
8793 }
8794
8795 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8796 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8797 LIST_INIT(&rule->arg.hdr_add.fmt);
8798
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008799 error = NULL;
8800 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
8801 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8802 args[cur_arg + 1], error);
8803 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008804 goto out_err;
8805 }
8806
8807 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008808 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008809 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 +01008810 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8811 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8812 file, linenum, args[0], error);
8813 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008814 goto out_err;
8815 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008816
8817 free(proxy->conf.lfs_file);
8818 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8819 proxy->conf.lfs_line = proxy->conf.args.line;
8820 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008821 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008822 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008823 cur_arg = 1;
8824
8825 if (!*args[cur_arg] ||
8826 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8827 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8828 file, linenum, args[0]);
8829 goto out_err;
8830 }
8831
8832 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8833 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8834
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008835 proxy->conf.args.ctx = ARGC_HRS;
8836 free(proxy->conf.lfs_file);
8837 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8838 proxy->conf.lfs_line = proxy->conf.args.line;
8839 cur_arg += 1;
8840 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8841 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008842 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008843 /*
8844 * '+ 8' for 'add-acl('
8845 * '- 9' for 'add-acl(' + trailing ')'
8846 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008847 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008848
8849 cur_arg = 1;
8850
8851 if (!*args[cur_arg] ||
8852 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8853 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8854 file, linenum, args[0]);
8855 goto out_err;
8856 }
8857
8858 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008859 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008860 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008861 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008862 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8863 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8864 file, linenum, args[0], error);
8865 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008866 goto out_err;
8867 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008868 free(proxy->conf.lfs_file);
8869 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8870 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008871
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008872 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008873 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8874 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008875 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008876 /*
8877 * '+ 8' for 'del-acl('
8878 * '- 9' for 'del-acl(' + trailing ')'
8879 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008880 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008881
8882 cur_arg = 1;
8883
8884 if (!*args[cur_arg] ||
8885 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8886 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8887 file, linenum, args[0]);
8888 goto out_err;
8889 }
8890
8891 LIST_INIT(&rule->arg.map.key);
8892 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008893 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008894 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008895 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8896 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8897 file, linenum, args[0], error);
8898 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008899 goto out_err;
8900 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008901 free(proxy->conf.lfs_file);
8902 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8903 proxy->conf.lfs_line = proxy->conf.args.line;
8904 cur_arg += 1;
8905 } else if (strncmp(args[0], "del-map", 7) == 0) {
8906 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008907 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008908 /*
8909 * '+ 8' for 'del-map('
8910 * '- 9' for 'del-map(' + trailing ')'
8911 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008912 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008913
8914 cur_arg = 1;
8915
8916 if (!*args[cur_arg] ||
8917 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8918 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8919 file, linenum, args[0]);
8920 goto out_err;
8921 }
8922
8923 LIST_INIT(&rule->arg.map.key);
8924 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008925 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008926 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008927 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8928 Alert("parsing [%s:%d]: 'http-response %s' %s.\n",
8929 file, linenum, args[0], error);
8930 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008931 goto out_err;
8932 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008933 free(proxy->conf.lfs_file);
8934 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8935 proxy->conf.lfs_line = proxy->conf.args.line;
8936 cur_arg += 1;
8937 } else if (strncmp(args[0], "set-map", 7) == 0) {
8938 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008939 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008940 /*
8941 * '+ 8' for 'set-map('
8942 * '- 9' for 'set-map(' + trailing ')'
8943 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008944 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008945
8946 cur_arg = 1;
8947
8948 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8949 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8950 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8951 file, linenum, args[0]);
8952 goto out_err;
8953 }
8954
8955 LIST_INIT(&rule->arg.map.key);
8956 LIST_INIT(&rule->arg.map.value);
8957
8958 proxy->conf.args.ctx = ARGC_HRS;
8959
8960 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008961 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008962 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008963 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8964 Alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
8965 file, linenum, args[0], error);
8966 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008967 goto out_err;
8968 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008969
8970 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008971 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008972 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 +01008973 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8974 Alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
8975 file, linenum, args[0], error);
8976 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008977 goto out_err;
8978 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008979
8980 free(proxy->conf.lfs_file);
8981 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8982 proxy->conf.lfs_line = proxy->conf.args.line;
8983
8984 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02008985 } else if (strcmp(args[0], "redirect") == 0) {
8986 struct redirect_rule *redir;
8987 char *errmsg = NULL;
8988
8989 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
8990 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
8991 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8992 goto out_err;
8993 }
8994
8995 /* this redirect rule might already contain a parsed condition which
8996 * we'll pass to the http-request rule.
8997 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008998 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02008999 rule->arg.redir = redir;
9000 rule->cond = redir->cond;
9001 redir->cond = NULL;
9002 cur_arg = 2;
9003 return rule;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009004 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9005 args[0][9] == '\0' && args[0][8] >= '0' &&
9006 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
9007 struct sample_expr *expr;
9008 unsigned int where;
9009 char *err = NULL;
9010
9011 cur_arg = 1;
9012 proxy->conf.args.ctx = ARGC_TRK;
9013
9014 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9015 if (!expr) {
9016 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9017 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9018 free(err);
9019 goto out_err;
9020 }
9021
9022 where = 0;
9023 if (proxy->cap & PR_CAP_FE)
9024 where |= SMP_VAL_FE_HRS_HDR;
9025 if (proxy->cap & PR_CAP_BE)
9026 where |= SMP_VAL_BE_HRS_HDR;
9027
9028 if (!(expr->fetch->val & where)) {
9029 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
9030 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9031 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9032 args[cur_arg-1], sample_src_names(expr->fetch->use));
9033 free(expr);
9034 goto out_err;
9035 }
9036
9037 if (strcmp(args[cur_arg], "table") == 0) {
9038 cur_arg++;
9039 if (!args[cur_arg]) {
9040 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9041 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9042 free(expr);
9043 goto out_err;
9044 }
9045 /* we copy the table name for now, it will be resolved later */
9046 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9047 cur_arg++;
9048 }
9049 rule->arg.trk_ctr.expr = expr;
9050 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Christopher Faulet78880fb2017-09-18 14:43:55 +02009051 rule->check_ptr = check_trk_action;
William Lallemand73025dd2014-04-24 14:38:37 +02009052 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9053 char *errmsg = NULL;
9054 cur_arg = 1;
9055 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009056 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009057 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009058 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009059 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9060 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9061 free(errmsg);
9062 goto out_err;
9063 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009064 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009065 action_build_list(&http_res_keywords.list, &trash);
9066 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9067 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009068 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009069 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009070 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009071 goto out_err;
9072 }
9073
9074 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9075 struct acl_cond *cond;
9076 char *errmsg = NULL;
9077
Christopher Faulet1b421ea2017-09-22 14:38:56 +02009078 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009079 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9080 file, linenum, args[0], errmsg);
9081 free(errmsg);
9082 goto out_err;
9083 }
9084 rule->cond = cond;
9085 }
9086 else if (*args[cur_arg]) {
9087 Alert("parsing [%s:%d]: 'http-response %s' expects"
9088 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9089 file, linenum, args[0], args[cur_arg]);
9090 goto out_err;
9091 }
9092
9093 return rule;
9094 out_err:
9095 free(rule);
9096 return NULL;
9097}
9098
Willy Tarreau4baae242012-12-27 12:00:31 +01009099/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009100 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009101 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9102 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009103 */
9104struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009105 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009106{
9107 struct redirect_rule *rule;
9108 int cur_arg;
9109 int type = REDIRECT_TYPE_NONE;
9110 int code = 302;
9111 const char *destination = NULL;
9112 const char *cookie = NULL;
9113 int cookie_set = 0;
9114 unsigned int flags = REDIRECT_FLAG_NONE;
9115 struct acl_cond *cond = NULL;
9116
9117 cur_arg = 0;
9118 while (*(args[cur_arg])) {
9119 if (strcmp(args[cur_arg], "location") == 0) {
9120 if (!*args[cur_arg + 1])
9121 goto missing_arg;
9122
9123 type = REDIRECT_TYPE_LOCATION;
9124 cur_arg++;
9125 destination = args[cur_arg];
9126 }
9127 else if (strcmp(args[cur_arg], "prefix") == 0) {
9128 if (!*args[cur_arg + 1])
9129 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009130 type = REDIRECT_TYPE_PREFIX;
9131 cur_arg++;
9132 destination = args[cur_arg];
9133 }
9134 else if (strcmp(args[cur_arg], "scheme") == 0) {
9135 if (!*args[cur_arg + 1])
9136 goto missing_arg;
9137
9138 type = REDIRECT_TYPE_SCHEME;
9139 cur_arg++;
9140 destination = args[cur_arg];
9141 }
9142 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9143 if (!*args[cur_arg + 1])
9144 goto missing_arg;
9145
9146 cur_arg++;
9147 cookie = args[cur_arg];
9148 cookie_set = 1;
9149 }
9150 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9151 if (!*args[cur_arg + 1])
9152 goto missing_arg;
9153
9154 cur_arg++;
9155 cookie = args[cur_arg];
9156 cookie_set = 0;
9157 }
9158 else if (strcmp(args[cur_arg], "code") == 0) {
9159 if (!*args[cur_arg + 1])
9160 goto missing_arg;
9161
9162 cur_arg++;
9163 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009164 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009165 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009166 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009167 args[cur_arg - 1], args[cur_arg]);
9168 return NULL;
9169 }
9170 }
9171 else if (!strcmp(args[cur_arg],"drop-query")) {
9172 flags |= REDIRECT_FLAG_DROP_QS;
9173 }
9174 else if (!strcmp(args[cur_arg],"append-slash")) {
9175 flags |= REDIRECT_FLAG_APPEND_SLASH;
9176 }
9177 else if (strcmp(args[cur_arg], "if") == 0 ||
9178 strcmp(args[cur_arg], "unless") == 0) {
Christopher Faulet1b421ea2017-09-22 14:38:56 +02009179 cond = build_acl_cond(file, linenum, &proxy->acl, curproxy, (const char **)args + cur_arg, errmsg);
Willy Tarreau4baae242012-12-27 12:00:31 +01009180 if (!cond) {
9181 memprintf(errmsg, "error in condition: %s", *errmsg);
9182 return NULL;
9183 }
9184 break;
9185 }
9186 else {
9187 memprintf(errmsg,
9188 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9189 args[cur_arg]);
9190 return NULL;
9191 }
9192 cur_arg++;
9193 }
9194
9195 if (type == REDIRECT_TYPE_NONE) {
9196 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9197 return NULL;
9198 }
9199
Willy Tarreaube4653b2015-05-28 15:26:58 +02009200 if (dir && type != REDIRECT_TYPE_LOCATION) {
9201 memprintf(errmsg, "response only supports redirect type 'location'");
9202 return NULL;
9203 }
9204
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02009205 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +01009206 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009207 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009208
9209 if (!use_fmt) {
9210 /* old-style static redirect rule */
9211 rule->rdr_str = strdup(destination);
9212 rule->rdr_len = strlen(destination);
9213 }
9214 else {
9215 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009216
9217 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9218 * if prefix == "/", we don't want to add anything, otherwise it
9219 * makes it hard for the user to configure a self-redirection.
9220 */
Godbachd9722032014-12-18 15:44:58 +08009221 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009222 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009223 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
9224 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 +01009225 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9226 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009227 return NULL;
9228 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009229 free(curproxy->conf.lfs_file);
9230 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9231 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009232 }
9233 }
9234
Willy Tarreau4baae242012-12-27 12:00:31 +01009235 if (cookie) {
9236 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9237 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9238 */
9239 rule->cookie_len = strlen(cookie);
9240 if (cookie_set) {
9241 rule->cookie_str = malloc(rule->cookie_len + 10);
9242 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9243 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9244 rule->cookie_len += 9;
9245 } else {
9246 rule->cookie_str = malloc(rule->cookie_len + 21);
9247 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9248 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9249 rule->cookie_len += 20;
9250 }
9251 }
9252 rule->type = type;
9253 rule->code = code;
9254 rule->flags = flags;
9255 LIST_INIT(&rule->list);
9256 return rule;
9257
9258 missing_arg:
9259 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9260 return NULL;
9261}
9262
Willy Tarreau8797c062007-05-07 00:55:35 +02009263/************************************************************************/
9264/* The code below is dedicated to ACL parsing and matching */
9265/************************************************************************/
9266
9267
Willy Tarreau14174bc2012-04-16 14:34:04 +02009268/* This function ensures that the prerequisites for an L7 fetch are ready,
9269 * which means that a request or response is ready. If some data is missing,
9270 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009271 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9272 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009273 *
9274 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009275 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9276 * decide whether or not an HTTP message is present ;
9277 * 0 if the requested data cannot be fetched or if it is certain that
9278 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009279 * 1 if an HTTP message is ready
9280 */
James Rosewell91a41cb2015-09-18 17:11:16 +01009281int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009282 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009283{
Willy Tarreau192252e2015-04-04 01:47:55 +02009284 struct http_txn *txn;
9285 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009286
Willy Tarreaube508f12016-03-10 11:47:01 +01009287 /* Note: it is possible that <s> is NULL when called before stream
9288 * initialization (eg: tcp-request connection), so this function is the
9289 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009290 */
Willy Tarreau192252e2015-04-04 01:47:55 +02009291 if (!s)
9292 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01009293
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +02009294 if (!s->txn) {
9295 if (unlikely(!http_alloc_txn(s)))
9296 return 0; /* not enough memory */
9297 http_init_txn(s);
9298 }
Willy Tarreau192252e2015-04-04 01:47:55 +02009299 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +02009300 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009301
9302 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009303 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009304
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009305 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009306 /* If the buffer does not leave enough free space at the end,
9307 * we must first realign it.
9308 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009309 if (s->req.buf->p > s->req.buf->data &&
9310 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
9311 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009312
Willy Tarreau14174bc2012-04-16 14:34:04 +02009313 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009314 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009315 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009316
9317 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009318 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009319 http_msg_analyzer(msg, &txn->hdr_idx);
9320
9321 /* Still no valid request ? */
9322 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009323 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009324 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009325 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009326 }
9327 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009328 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009329 return 0;
9330 }
9331
9332 /* OK we just got a valid HTTP request. We have some minor
9333 * preparation to perform so that further checks can rely
9334 * on HTTP tests.
9335 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009336
9337 /* If the request was parsed but was too large, we must absolutely
9338 * return an error so that it is not processed. At the moment this
9339 * cannot happen, but if the parsers are to change in the future,
9340 * we want this check to be maintained.
9341 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009342 if (unlikely(s->req.buf->i + s->req.buf->p >
9343 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01009344 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009345 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009346 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009347 return 1;
9348 }
9349
Willy Tarreau9b28e032012-10-12 23:49:43 +02009350 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009351 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02009352 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009353
Willy Tarreau506d0502013-07-06 13:29:24 +02009354 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9355 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009356 }
9357
Willy Tarreau506d0502013-07-06 13:29:24 +02009358 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009359 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009360 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009361
9362 /* otherwise everything's ready for the request */
9363 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009364 else {
9365 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009366 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9367 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009368 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009369 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009370 }
9371
9372 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009373 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009374 return 1;
9375}
Willy Tarreau8797c062007-05-07 00:55:35 +02009376
Willy Tarreau8797c062007-05-07 00:55:35 +02009377/* 1. Check on METHOD
9378 * We use the pre-parsed method if it is known, and store its number as an
9379 * integer. If it is unknown, we use the pointer and the length.
9380 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009381static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009382{
9383 int len, meth;
9384
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009385 len = strlen(text);
9386 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009387
9388 pattern->val.i = meth;
9389 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +02009390 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009391 pattern->len = len;
9392 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009393 else {
9394 pattern->ptr.str = NULL;
9395 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009396 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009397 return 1;
9398}
9399
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009400/* This function fetches the method of current HTTP request and stores
9401 * it in the global pattern struct as a chunk. There are two possibilities :
9402 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9403 * in <len> and <ptr> is NULL ;
9404 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9405 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009406 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009407 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009408static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009409smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009410{
9411 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +01009412 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009413
Willy Tarreau24e32d82012-04-23 23:55:44 +02009414 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009415
Willy Tarreaube508f12016-03-10 11:47:01 +01009416 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009417 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009418 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009419 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009420 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009421 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9422 /* ensure the indexes are not affected */
9423 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009424 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009425 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
9426 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009427 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009428 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009429 return 1;
9430}
9431
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009432/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009433static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009434{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009435 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009436 struct pattern_list *lst;
9437 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009438
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009439 list_for_each_entry(lst, &expr->patterns, list) {
9440 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009441
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009442 /* well-known method */
9443 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009444 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009445 return pattern;
9446 else
9447 continue;
9448 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009449
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009450 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009451 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009452 continue;
9453
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009454 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009455 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
9456 (!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 +01009457 return pattern;
9458 }
9459 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009460}
9461
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009462static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009463smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009464{
Willy Tarreaube508f12016-03-10 11:47:01 +01009465 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009466 char *ptr;
9467 int len;
9468
Willy Tarreauc0239e02012-04-16 14:42:55 +02009469 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009470
Willy Tarreaube508f12016-03-10 11:47:01 +01009471 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009472 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009473 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009474
9475 while ((len-- > 0) && (*ptr++ != '/'));
9476 if (len <= 0)
9477 return 0;
9478
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009479 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009480 smp->data.u.str.str = ptr;
9481 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009482
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009483 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009484 return 1;
9485}
9486
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009487static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009488smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009489{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009490 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009491 char *ptr;
9492 int len;
9493
Willy Tarreauc0239e02012-04-16 14:42:55 +02009494 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009495
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009496 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009497 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9498 return 0;
9499
Willy Tarreau8797c062007-05-07 00:55:35 +02009500 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009501 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009502
9503 while ((len-- > 0) && (*ptr++ != '/'));
9504 if (len <= 0)
9505 return 0;
9506
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009507 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009508 smp->data.u.str.str = ptr;
9509 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009510
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009511 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009512 return 1;
9513}
9514
9515/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009516static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009517smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009518{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009519 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009520 char *ptr;
9521 int len;
9522
Willy Tarreauc0239e02012-04-16 14:42:55 +02009523 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009524
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009525 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009526 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9527 return 0;
9528
Willy Tarreau8797c062007-05-07 00:55:35 +02009529 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009530 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009531
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009532 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009533 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009534 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009535 return 1;
9536}
9537
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009538static int
9539smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
9540{
9541 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
9542 return 0;
9543
9544 if (!smp->strm->unique_id) {
9545 if ((smp->strm->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
9546 return 0;
9547 smp->strm->unique_id[0] = '\0';
9548 }
9549 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
9550 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
9551
9552 smp->data.type = SMP_T_STR;
9553 smp->data.u.str.str = smp->strm->unique_id;
9554 smp->flags = SMP_F_CONST;
9555 return 1;
9556}
9557
Thierry FOURNIERd7d88812017-04-19 15:15:14 +02009558/* Returns a string block containing all headers including the
9559 * empty line wich separes headers from the body. This is useful
9560 * form some headers analysis.
9561 */
9562static int
9563smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
9564{
9565 struct http_msg *msg;
9566 struct hdr_idx *idx;
9567 struct http_txn *txn;
9568
9569 CHECK_HTTP_MESSAGE_FIRST();
9570
9571 txn = smp->strm->txn;
9572 idx = &txn->hdr_idx;
9573 msg = &txn->req;
9574
9575 smp->data.type = SMP_T_STR;
9576 smp->data.u.str.str = msg->chn->buf->p + hdr_idx_first_pos(idx);
9577 smp->data.u.str.len = msg->eoh - hdr_idx_first_pos(idx) + 1 +
9578 (msg->chn->buf->p[msg->eoh] == '\r');
9579
9580 return 1;
9581}
9582
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009583/* Returns the header request in a length/value encoded format.
9584 * This is useful for exchanges with the SPOE.
9585 *
9586 * A "length value" is a multibyte code encoding numbers. It uses the
9587 * SPOE format. The encoding is the following:
9588 *
9589 * Each couple "header name" / "header value" is composed
9590 * like this:
9591 * "length value" "header name bytes"
9592 * "length value" "header value bytes"
9593 * When the last header is reached, the header name and the header
9594 * value are empty. Their length are 0
9595 */
9596static int
9597smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
9598{
9599 struct http_msg *msg;
9600 struct chunk *temp;
9601 struct hdr_idx *idx;
9602 const char *cur_ptr, *cur_next, *p;
9603 int old_idx, cur_idx;
9604 struct hdr_idx_elem *cur_hdr;
9605 const char *hn, *hv;
9606 int hnl, hvl;
9607 int ret;
9608 struct http_txn *txn;
9609 char *buf;
9610 char *end;
9611
9612 CHECK_HTTP_MESSAGE_FIRST();
9613
9614 temp = get_trash_chunk();
9615 buf = temp->str;
9616 end = temp->str + temp->size;
9617
9618 txn = smp->strm->txn;
9619 idx = &txn->hdr_idx;
9620 msg = &txn->req;
9621
9622 /* Build array of headers. */
9623 old_idx = 0;
9624 cur_next = msg->chn->buf->p + hdr_idx_first_pos(idx);
9625 while (1) {
9626 cur_idx = idx->v[old_idx].next;
9627 if (!cur_idx)
9628 break;
9629 old_idx = cur_idx;
9630
9631 cur_hdr = &idx->v[cur_idx];
9632 cur_ptr = cur_next;
9633 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
9634
9635 /* Now we have one full header at cur_ptr of len cur_hdr->len,
9636 * and the next header starts at cur_next. We'll check
9637 * this header in the list as well as against the default
9638 * rule.
9639 */
9640
9641 /* look for ': *'. */
9642 hn = cur_ptr;
9643 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
9644 if (p >= cur_ptr+cur_hdr->len)
9645 continue;
9646 hnl = p - hn;
9647 p++;
9648 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
9649 p++;
9650 if (p >= cur_ptr + cur_hdr->len)
9651 continue;
9652 hv = p;
9653 hvl = cur_ptr + cur_hdr->len-p;
9654
9655 /* encode the header name. */
9656 ret = encode_varint(hnl, &buf, end);
9657 if (ret == -1)
9658 return 0;
9659 if (buf + hnl > end)
9660 return 0;
9661 memcpy(buf, hn, hnl);
9662 buf += hnl;
9663
9664 /* encode and copy the value. */
9665 ret = encode_varint(hvl, &buf, end);
9666 if (ret == -1)
9667 return 0;
9668 if (buf + hvl > end)
9669 return 0;
9670 memcpy(buf, hv, hvl);
9671 buf += hvl;
9672 }
9673
9674 /* encode the end of the header list with empty
9675 * header name and header value.
9676 */
9677 ret = encode_varint(0, &buf, end);
9678 if (ret == -1)
9679 return 0;
9680 ret = encode_varint(0, &buf, end);
9681 if (ret == -1)
9682 return 0;
9683
9684 /* Initialise sample data which will be filled. */
9685 smp->data.type = SMP_T_BIN;
9686 smp->data.u.str.str = temp->str;
9687 smp->data.u.str.len = buf - temp->str;
9688 smp->data.u.str.size = temp->size;
9689
9690 return 1;
9691}
9692
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009693/* returns the longest available part of the body. This requires that the body
9694 * has been waited for using http-buffer-request.
9695 */
9696static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009697smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009698{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009699 struct http_msg *msg;
9700 unsigned long len;
9701 unsigned long block1;
9702 char *body;
9703 struct chunk *temp;
9704
9705 CHECK_HTTP_MESSAGE_FIRST();
9706
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009707 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009708 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009709 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009710 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009711
9712 len = http_body_bytes(msg);
9713 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
9714
9715 block1 = len;
9716 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
9717 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
9718
9719 if (block1 == len) {
9720 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009721 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009722 smp->data.u.str.str = body;
9723 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009724 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
9725 }
9726 else {
9727 /* buffer is wrapped, we need to defragment it */
9728 temp = get_trash_chunk();
9729 memcpy(temp->str, body, block1);
9730 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009731 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009732 smp->data.u.str.str = temp->str;
9733 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009734 smp->flags = SMP_F_VOL_TEST;
9735 }
9736 return 1;
9737}
9738
9739
9740/* returns the available length of the body. This requires that the body
9741 * has been waited for using http-buffer-request.
9742 */
9743static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009744smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009745{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009746 struct http_msg *msg;
9747
9748 CHECK_HTTP_MESSAGE_FIRST();
9749
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009750 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009751 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009752 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009753 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009754
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009755 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009756 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009757
9758 smp->flags = SMP_F_VOL_TEST;
9759 return 1;
9760}
9761
9762
9763/* returns the advertised length of the body, or the advertised size of the
9764 * chunks available in the buffer. This requires that the body has been waited
9765 * for using http-buffer-request.
9766 */
9767static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009768smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009769{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009770 struct http_msg *msg;
9771
9772 CHECK_HTTP_MESSAGE_FIRST();
9773
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009774 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009775 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009776 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009777 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009778
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009779 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009780 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009781
9782 smp->flags = SMP_F_VOL_TEST;
9783 return 1;
9784}
9785
9786
Willy Tarreau8797c062007-05-07 00:55:35 +02009787/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009788static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009789smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009790{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009791 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009792
Willy Tarreauc0239e02012-04-16 14:42:55 +02009793 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009794
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009795 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009796 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009797 smp->data.u.str.len = txn->req.sl.rq.u_l;
9798 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009799 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009800 return 1;
9801}
9802
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009803static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009804smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009805{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009806 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009807 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009808
Willy Tarreauc0239e02012-04-16 14:42:55 +02009809 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009810
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009811 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009812 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 +02009813 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009814 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009815
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009816 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009817 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009818 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009819 return 1;
9820}
9821
9822static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009823smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009824{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009825 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009826 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009827
Willy Tarreauc0239e02012-04-16 14:42:55 +02009828 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009829
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009830 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009831 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 +02009832 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9833 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009834
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009835 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009836 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009837 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009838 return 1;
9839}
9840
Willy Tarreau185b5c42012-04-26 15:11:51 +02009841/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9842 * Accepts an optional argument of type string containing the header field name,
9843 * and an optional argument of type signed or unsigned integer to request an
9844 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009845 * headers are considered from the first one. It does not stop on commas and
9846 * returns full lines instead (useful for User-Agent or Date for example).
9847 */
9848static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009849smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009850{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009851 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009852 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +02009853 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009854 int occ = 0;
9855 const char *name_str = NULL;
9856 int name_len = 0;
9857
9858 if (!ctx) {
9859 /* first call */
9860 ctx = &static_hdr_ctx;
9861 ctx->idx = 0;
9862 smp->ctx.a[0] = ctx;
9863 }
9864
9865 if (args) {
9866 if (args[0].type != ARGT_STR)
9867 return 0;
9868 name_str = args[0].data.str.str;
9869 name_len = args[0].data.str.len;
9870
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02009871 if (args[1].type == ARGT_SINT)
9872 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009873 }
9874
9875 CHECK_HTTP_MESSAGE_FIRST();
9876
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009877 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009878 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 +02009879
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009880 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9881 /* search for header from the beginning */
9882 ctx->idx = 0;
9883
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009884 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009885 /* no explicit occurrence and single fetch => last header by default */
9886 occ = -1;
9887
9888 if (!occ)
9889 /* prepare to report multiple occurrences for ACL fetches */
9890 smp->flags |= SMP_F_NOT_LAST;
9891
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009892 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009893 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009894 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 +02009895 return 1;
9896
9897 smp->flags &= ~SMP_F_NOT_LAST;
9898 return 0;
9899}
9900
9901/* 6. Check on HTTP header count. The number of occurrences is returned.
9902 * Accepts exactly 1 argument of type string. It does not stop on commas and
9903 * returns full lines instead (useful for User-Agent or Date for example).
9904 */
9905static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009906smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009907{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009908 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009909 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009910 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009911 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009912 const char *name = NULL;
9913 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009914
Willy Tarreau601a4d12015-04-01 19:16:09 +02009915 if (args && args->type == ARGT_STR) {
9916 name = args->data.str.str;
9917 len = args->data.str.len;
9918 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009919
9920 CHECK_HTTP_MESSAGE_FIRST();
9921
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009922 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009923 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 +02009924
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009925 ctx.idx = 0;
9926 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009927 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009928 cnt++;
9929
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009930 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009931 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009932 smp->flags = SMP_F_VOL_HDR;
9933 return 1;
9934}
9935
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009936static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009937smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009938{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009939 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009940 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009941 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009942 struct chunk *temp;
9943 char del = ',';
9944
9945 if (args && args->type == ARGT_STR)
9946 del = *args[0].data.str.str;
9947
9948 CHECK_HTTP_MESSAGE_FIRST();
9949
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009950 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009951 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 +02009952
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009953 temp = get_trash_chunk();
9954
9955 ctx.idx = 0;
9956 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
9957 if (temp->len)
9958 temp->str[temp->len++] = del;
9959 memcpy(temp->str + temp->len, ctx.line, ctx.del);
9960 temp->len += ctx.del;
9961 }
9962
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009963 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009964 smp->data.u.str.str = temp->str;
9965 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009966 smp->flags = SMP_F_VOL_HDR;
9967 return 1;
9968}
9969
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009970/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9971 * Accepts an optional argument of type string containing the header field name,
9972 * and an optional argument of type signed or unsigned integer to request an
9973 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009974 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009975 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009976static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009977smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009978{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009979 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009980 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +02009981 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009982 int occ = 0;
9983 const char *name_str = NULL;
9984 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009985
Willy Tarreaua890d072013-04-02 12:01:06 +02009986 if (!ctx) {
9987 /* first call */
9988 ctx = &static_hdr_ctx;
9989 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009990 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009991 }
9992
Willy Tarreau185b5c42012-04-26 15:11:51 +02009993 if (args) {
9994 if (args[0].type != ARGT_STR)
9995 return 0;
9996 name_str = args[0].data.str.str;
9997 name_len = args[0].data.str.len;
9998
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02009999 if (args[1].type == ARGT_SINT)
10000 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010001 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010002
Willy Tarreaue333ec92012-04-16 16:26:40 +020010003 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010004
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010005 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010006 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 +020010007
Willy Tarreau185b5c42012-04-26 15:11:51 +020010008 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010009 /* search for header from the beginning */
10010 ctx->idx = 0;
10011
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010012 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010013 /* no explicit occurrence and single fetch => last header by default */
10014 occ = -1;
10015
10016 if (!occ)
10017 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010018 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010019
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010020 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010021 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010022 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 +020010023 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010024
Willy Tarreau37406352012-04-23 16:16:37 +020010025 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010026 return 0;
10027}
10028
Willy Tarreauc11416f2007-06-17 16:58:38 +020010029/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010030 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010031 */
10032static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010033smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010034{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010035 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010036 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010037 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010038 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010039 const char *name = NULL;
10040 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010041
Willy Tarreau601a4d12015-04-01 19:16:09 +020010042 if (args && args->type == ARGT_STR) {
10043 name = args->data.str.str;
10044 len = args->data.str.len;
10045 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010046
Willy Tarreaue333ec92012-04-16 16:26:40 +020010047 CHECK_HTTP_MESSAGE_FIRST();
10048
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010049 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010050 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 +020010051
Willy Tarreau33a7e692007-06-10 19:45:56 +020010052 ctx.idx = 0;
10053 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010054 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010055 cnt++;
10056
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010057 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010058 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010059 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010060 return 1;
10061}
10062
Willy Tarreau185b5c42012-04-26 15:11:51 +020010063/* Fetch an HTTP header's integer value. The integer value is returned. It
10064 * takes a mandatory argument of type string and an optional one of type int
10065 * to designate a specific occurrence. It returns an unsigned integer, which
10066 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010067 */
10068static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010069smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010070{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010071 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010072
Willy Tarreauf853c462012-04-23 18:53:56 +020010073 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010074 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010075 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010076 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010077
Willy Tarreaud53e2422012-04-16 17:21:11 +020010078 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010079}
10080
Cyril Bonté69fa9922012-10-25 00:01:06 +020010081/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10082 * and an optional one of type int to designate a specific occurrence.
10083 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010084 */
10085static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010086smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010087{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010088 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010089
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010090 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010091 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010092 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010093 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010094 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010095 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010096 if (smp->data.u.str.len < temp->size - 1) {
10097 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10098 temp->str[smp->data.u.str.len] = '\0';
10099 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010100 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010101 break;
10102 }
10103 }
10104 }
10105
Willy Tarreaud53e2422012-04-16 17:21:11 +020010106 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010107 if (!(smp->flags & SMP_F_NOT_LAST))
10108 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010109 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010110 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010111}
10112
Willy Tarreau737b0c12007-06-10 21:28:46 +020010113/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10114 * the first '/' after the possible hostname, and ends before the possible '?'.
10115 */
10116static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010117smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010118{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010119 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010120 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010121
Willy Tarreauc0239e02012-04-16 14:42:55 +020010122 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010123
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010124 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010125 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010126 ptr = http_get_path(txn);
10127 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010128 return 0;
10129
10130 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010131 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010132 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010133
10134 while (ptr < end && *ptr != '?')
10135 ptr++;
10136
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010137 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010138 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010139 return 1;
10140}
10141
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010142/* This produces a concatenation of the first occurrence of the Host header
10143 * followed by the path component if it begins with a slash ('/'). This means
10144 * that '*' will not be added, resulting in exactly the first Host entry.
10145 * If no Host header is found, then the path is returned as-is. The returned
10146 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010147 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010148 */
10149static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010150smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010151{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010152 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010153 char *ptr, *end, *beg;
10154 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010155 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010156
10157 CHECK_HTTP_MESSAGE_FIRST();
10158
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010159 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010160 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010161 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010162 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010163
10164 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010165 temp = get_trash_chunk();
10166 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010167 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010168 smp->data.u.str.str = temp->str;
10169 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010170
10171 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010172 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010173 beg = http_get_path(txn);
10174 if (!beg)
10175 beg = end;
10176
10177 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10178
10179 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010180 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10181 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010182 }
10183
10184 smp->flags = SMP_F_VOL_1ST;
10185 return 1;
10186}
10187
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010188/* This produces a 32-bit hash of the concatenation of the first occurrence of
10189 * the Host header followed by the path component if it begins with a slash ('/').
10190 * This means that '*' will not be added, resulting in exactly the first Host
10191 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010192 * is hashed using the path hash followed by a full avalanche hash and provides a
10193 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010194 * high-traffic sites without having to store whole paths.
10195 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010196int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010197smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010198{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010199 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010200 struct hdr_ctx ctx;
10201 unsigned int hash = 0;
10202 char *ptr, *beg, *end;
10203 int len;
10204
10205 CHECK_HTTP_MESSAGE_FIRST();
10206
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010207 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010208 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010209 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010210 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10211 ptr = ctx.line + ctx.val;
10212 len = ctx.vlen;
10213 while (len--)
10214 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10215 }
10216
10217 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010218 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010219 beg = http_get_path(txn);
10220 if (!beg)
10221 beg = end;
10222
10223 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10224
10225 if (beg < ptr && *beg == '/') {
10226 while (beg < ptr)
10227 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10228 }
10229 hash = full_hash(hash);
10230
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010231 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010232 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010233 smp->flags = SMP_F_VOL_1ST;
10234 return 1;
10235}
10236
Willy Tarreau4a550602012-12-09 14:53:32 +010010237/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010238 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10239 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10240 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010241 * that in environments where IPv6 is insignificant, truncating the output to
10242 * 8 bytes would still work.
10243 */
10244static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010245smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010246{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010247 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010248 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010249
10250 if (!cli_conn)
10251 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010252
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010253 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010254 return 0;
10255
Willy Tarreau47ca5452012-12-23 20:22:19 +010010256 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010257 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010258 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010259
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010260 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010261 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010262 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010263 temp->len += 4;
10264 break;
10265 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010266 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010267 temp->len += 16;
10268 break;
10269 default:
10270 return 0;
10271 }
10272
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010273 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010274 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010275 return 1;
10276}
10277
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010278/* Extracts the query string, which comes after the question mark '?'. If no
10279 * question mark is found, nothing is returned. Otherwise it returns a sample
10280 * of type string carrying the whole query string.
10281 */
10282static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010283smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010284{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010285 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010286 char *ptr, *end;
10287
10288 CHECK_HTTP_MESSAGE_FIRST();
10289
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010290 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010291 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10292 end = ptr + txn->req.sl.rq.u_l;
10293
10294 /* look up the '?' */
10295 do {
10296 if (ptr == end)
10297 return 0;
10298 } while (*ptr++ != '?');
10299
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010300 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010301 smp->data.u.str.str = ptr;
10302 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010303 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10304 return 1;
10305}
10306
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010307static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010308smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010309{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010310 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10311 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10312 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010313
Willy Tarreau24e32d82012-04-23 23:55:44 +020010314 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010315
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010316 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010317 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010318 return 1;
10319}
10320
Willy Tarreau7f18e522010-10-22 20:04:13 +020010321/* return a valid test if the current request is the first one on the connection */
10322static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010323smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010324{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010325 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010326 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010327 return 1;
10328}
10329
Willy Tarreau34db1082012-04-19 17:16:54 +020010330/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010331static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010332smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010333{
10334
Willy Tarreau24e32d82012-04-23 23:55:44 +020010335 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010336 return 0;
10337
Willy Tarreauc0239e02012-04-16 14:42:55 +020010338 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010339
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010340 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010341 return 0;
10342
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010343 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010344 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010345 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010346 return 1;
10347}
Willy Tarreau8797c062007-05-07 00:55:35 +020010348
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010349/* Accepts exactly 1 argument of type userlist */
10350static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010351smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010352{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010353 if (!args || args->type != ARGT_USR)
10354 return 0;
10355
10356 CHECK_HTTP_MESSAGE_FIRST();
10357
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010358 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010359 return 0;
10360
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010361 /* if the user does not belong to the userlist or has a wrong password,
10362 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010363 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010364 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010365 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10366 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010367 return 0;
10368
10369 /* pat_match_auth() will need the user list */
10370 smp->ctx.a[0] = args->data.usr;
10371
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010372 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010373 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010374 smp->data.u.str.str = smp->strm->txn->auth.user;
10375 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010376
10377 return 1;
10378}
10379
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010380/* Try to find the next occurrence of a cookie name in a cookie header value.
10381 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10382 * the cookie value is returned into *value and *value_l, and the function
10383 * returns a pointer to the next pointer to search from if the value was found.
10384 * Otherwise if the cookie was not found, NULL is returned and neither value
10385 * nor value_l are touched. The input <hdr> string should first point to the
10386 * header's value, and the <hdr_end> pointer must point to the first character
10387 * not part of the value. <list> must be non-zero if value may represent a list
10388 * of values (cookie headers). This makes it faster to abort parsing when no
10389 * list is expected.
10390 */
David Carlier4686f792015-09-25 14:10:50 +010010391char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010392extract_cookie_value(char *hdr, const char *hdr_end,
10393 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010394 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010395{
10396 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10397 char *next;
10398
10399 /* we search at least a cookie name followed by an equal, and more
10400 * generally something like this :
10401 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10402 */
10403 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10404 /* Iterate through all cookies on this line */
10405
Willy Tarreau2235b262016-11-05 15:50:20 +010010406 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010407 att_beg++;
10408
10409 /* find att_end : this is the first character after the last non
10410 * space before the equal. It may be equal to hdr_end.
10411 */
10412 equal = att_end = att_beg;
10413
10414 while (equal < hdr_end) {
10415 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10416 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010010417 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010418 continue;
10419 att_end = equal;
10420 }
10421
10422 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10423 * is between <att_beg> and <equal>, both may be identical.
10424 */
10425
10426 /* look for end of cookie if there is an equal sign */
10427 if (equal < hdr_end && *equal == '=') {
10428 /* look for the beginning of the value */
10429 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010010430 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010431 val_beg++;
10432
10433 /* find the end of the value, respecting quotes */
10434 next = find_cookie_value_end(val_beg, hdr_end);
10435
10436 /* make val_end point to the first white space or delimitor after the value */
10437 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010010438 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010439 val_end--;
10440 } else {
10441 val_beg = val_end = next = equal;
10442 }
10443
10444 /* We have nothing to do with attributes beginning with '$'. However,
10445 * they will automatically be removed if a header before them is removed,
10446 * since they're supposed to be linked together.
10447 */
10448 if (*att_beg == '$')
10449 continue;
10450
10451 /* Ignore cookies with no equal sign */
10452 if (equal == next)
10453 continue;
10454
10455 /* Now we have the cookie name between att_beg and att_end, and
10456 * its value between val_beg and val_end.
10457 */
10458
10459 if (att_end - att_beg == cookie_name_l &&
10460 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10461 /* let's return this value and indicate where to go on from */
10462 *value = val_beg;
10463 *value_l = val_end - val_beg;
10464 return next + 1;
10465 }
10466
10467 /* Set-Cookie headers only have the name in the first attr=value part */
10468 if (!list)
10469 break;
10470 }
10471
10472 return NULL;
10473}
10474
William Lallemanda43ba4e2014-01-28 18:14:25 +010010475/* Fetch a captured HTTP request header. The index is the position of
10476 * the "capture" option in the configuration file
10477 */
10478static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010479smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010480{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010481 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010482 int idx;
10483
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010484 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010485 return 0;
10486
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010487 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010488
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010489 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 +010010490 return 0;
10491
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010492 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010493 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010494 smp->data.u.str.str = smp->strm->req_cap[idx];
10495 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010496
10497 return 1;
10498}
10499
10500/* Fetch a captured HTTP response header. The index is the position of
10501 * the "capture" option in the configuration file
10502 */
10503static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010504smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010505{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010506 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010507 int idx;
10508
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010509 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010510 return 0;
10511
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010512 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010513
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010514 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 +010010515 return 0;
10516
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010517 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010518 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010519 smp->data.u.str.str = smp->strm->res_cap[idx];
10520 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010521
10522 return 1;
10523}
10524
William Lallemand65ad6e12014-01-31 15:08:02 +010010525/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10526static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010527smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010528{
10529 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010530 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010010531 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010532
Willy Tarreau15e91e12015-04-04 00:52:09 +020010533 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010534 return 0;
10535
William Lallemand96a77852014-02-05 00:30:02 +010010536 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010537
William Lallemand96a77852014-02-05 00:30:02 +010010538 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10539 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010540
William Lallemand96a77852014-02-05 00:30:02 +010010541 temp = get_trash_chunk();
10542 temp->str = txn->uri;
10543 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010544 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010545 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010546 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010547
10548 return 1;
10549
10550}
10551
10552/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10553static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010554smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010555{
10556 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010557 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010010558 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010559
Willy Tarreau15e91e12015-04-04 00:52:09 +020010560 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010561 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010562
William Lallemand65ad6e12014-01-31 15:08:02 +010010563 ptr = txn->uri;
10564
10565 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10566 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010567
William Lallemand65ad6e12014-01-31 15:08:02 +010010568 if (!*ptr)
10569 return 0;
10570
10571 ptr++; /* skip the space */
10572
10573 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010574 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010575 if (!ptr)
10576 return 0;
10577 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10578 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010579
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010580 smp->data.u.str = *temp;
10581 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010582 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010583 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010584
10585 return 1;
10586}
10587
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010588/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10589 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10590 */
10591static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010592smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010593{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010594 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010595
Willy Tarreau15e91e12015-04-04 00:52:09 +020010596 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010597 return 0;
10598
10599 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010600 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010601 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010602 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010603
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010604 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010605 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010606 smp->flags = SMP_F_CONST;
10607 return 1;
10608
10609}
10610
10611/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10612 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10613 */
10614static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010615smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010616{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010617 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010618
Willy Tarreau15e91e12015-04-04 00:52:09 +020010619 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010620 return 0;
10621
10622 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010623 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010624 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010625 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010626
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010627 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010628 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010629 smp->flags = SMP_F_CONST;
10630 return 1;
10631
10632}
10633
William Lallemand65ad6e12014-01-31 15:08:02 +010010634
Willy Tarreaue333ec92012-04-16 16:26:40 +020010635/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010636 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010637 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010638 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010639 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010640 * Accepts exactly 1 argument of type string. If the input options indicate
10641 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010642 * The returned sample is of type CSTR. Can be used to parse cookies in other
10643 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010644 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010645int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010646{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010647 struct http_txn *txn;
10648 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010649 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010650 const struct http_msg *msg;
10651 const char *hdr_name;
10652 int hdr_name_len;
10653 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010654 int occ = 0;
10655 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010656
Willy Tarreau24e32d82012-04-23 23:55:44 +020010657 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010658 return 0;
10659
Willy Tarreaua890d072013-04-02 12:01:06 +020010660 if (!ctx) {
10661 /* first call */
10662 ctx = &static_hdr_ctx;
10663 ctx->idx = 0;
10664 smp->ctx.a[2] = ctx;
10665 }
10666
Willy Tarreaue333ec92012-04-16 16:26:40 +020010667 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010668
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010669 txn = smp->strm->txn;
10670 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010671
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010672 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010673 msg = &txn->req;
10674 hdr_name = "Cookie";
10675 hdr_name_len = 6;
10676 } else {
10677 msg = &txn->rsp;
10678 hdr_name = "Set-Cookie";
10679 hdr_name_len = 10;
10680 }
10681
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010682 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020010683 /* no explicit occurrence and single fetch => last cookie by default */
10684 occ = -1;
10685
10686 /* OK so basically here, either we want only one value and it's the
10687 * last one, or we want to iterate over all of them and we fetch the
10688 * next one.
10689 */
10690
Willy Tarreau9b28e032012-10-12 23:49:43 +020010691 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010692 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010693 /* search for the header from the beginning, we must first initialize
10694 * the search parameters.
10695 */
Willy Tarreau37406352012-04-23 16:16:37 +020010696 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010697 ctx->idx = 0;
10698 }
10699
Willy Tarreau28376d62012-04-26 21:26:10 +020010700 smp->flags |= SMP_F_VOL_HDR;
10701
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010702 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010703 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10704 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010705 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10706 goto out;
10707
Willy Tarreau24e32d82012-04-23 23:55:44 +020010708 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010709 continue;
10710
Willy Tarreau37406352012-04-23 16:16:37 +020010711 smp->ctx.a[0] = ctx->line + ctx->val;
10712 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010713 }
10714
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010715 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010716 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010717 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010718 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010719 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010720 &smp->data.u.str.str,
10721 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010722 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010723 found = 1;
10724 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010725 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020010726 smp->flags |= SMP_F_NOT_LAST;
10727 return 1;
10728 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010729 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010730 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010731 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010732 /* all cookie headers and values were scanned. If we're looking for the
10733 * last occurrence, we may return it now.
10734 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010735 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010736 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010737 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010738}
10739
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010740/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010741 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010742 * multiple cookies may be parsed on the same line. The returned sample is of
10743 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010744 */
10745static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010746smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010747{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010748 struct http_txn *txn;
10749 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010750 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010751 const struct http_msg *msg;
10752 const char *hdr_name;
10753 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010754 int cnt;
10755 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010756 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010757
Willy Tarreau24e32d82012-04-23 23:55:44 +020010758 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010759 return 0;
10760
Willy Tarreaue333ec92012-04-16 16:26:40 +020010761 CHECK_HTTP_MESSAGE_FIRST();
10762
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010763 txn = smp->strm->txn;
10764 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010765
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010766 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010767 msg = &txn->req;
10768 hdr_name = "Cookie";
10769 hdr_name_len = 6;
10770 } else {
10771 msg = &txn->rsp;
10772 hdr_name = "Set-Cookie";
10773 hdr_name_len = 10;
10774 }
10775
Willy Tarreau9b28e032012-10-12 23:49:43 +020010776 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010777 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010778 ctx.idx = 0;
10779 cnt = 0;
10780
10781 while (1) {
10782 /* Note: val_beg == NULL every time we need to fetch a new header */
10783 if (!val_beg) {
10784 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10785 break;
10786
Willy Tarreau24e32d82012-04-23 23:55:44 +020010787 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010788 continue;
10789
10790 val_beg = ctx.line + ctx.val;
10791 val_end = val_beg + ctx.vlen;
10792 }
10793
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010794 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010795 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010796 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010797 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010798 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010799 &smp->data.u.str.str,
10800 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010801 cnt++;
10802 }
10803 }
10804
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010805 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010806 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010807 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010808 return 1;
10809}
10810
Willy Tarreau51539362012-05-08 12:46:28 +020010811/* Fetch an cookie's integer value. The integer value is returned. It
10812 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10813 */
10814static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010815smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020010816{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010817 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020010818
10819 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010820 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010821 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020010822 }
10823
10824 return ret;
10825}
10826
Willy Tarreau8797c062007-05-07 00:55:35 +020010827/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010828/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010829/************************************************************************/
10830
David Cournapeau16023ee2010-12-23 20:55:41 +090010831/*
10832 * Given a path string and its length, find the position of beginning of the
10833 * query string. Returns NULL if no query string is found in the path.
10834 *
10835 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10836 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010837 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090010838 */
bedis4c75cca2012-10-05 08:38:24 +020010839static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010840{
10841 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020010842
bedis4c75cca2012-10-05 08:38:24 +020010843 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090010844 return p ? p + 1 : NULL;
10845}
10846
bedis4c75cca2012-10-05 08:38:24 +020010847static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010848{
bedis4c75cca2012-10-05 08:38:24 +020010849 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090010850}
10851
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010852/* after increasing a pointer value, it can exceed the first buffer
10853 * size. This function transform the value of <ptr> according with
10854 * the expected position. <chunks> is an array of the one or two
10855 * avalaible chunks. The first value is the start of the first chunk,
10856 * the second value if the end+1 of the first chunks. The third value
10857 * is NULL or the start of the second chunk and the fourth value is
10858 * the end+1 of the second chunk. The function returns 1 if does a
10859 * wrap, else returns 0.
10860 */
10861static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
10862{
10863 if (*ptr < chunks[1])
10864 return 0;
10865 if (!chunks[2])
10866 return 0;
10867 *ptr = chunks[2] + ( *ptr - chunks[1] );
10868 return 1;
10869}
10870
David Cournapeau16023ee2010-12-23 20:55:41 +090010871/*
10872 * Given a url parameter, find the starting position of the first occurence,
10873 * or NULL if the parameter is not found.
10874 *
10875 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
10876 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010877 *
Willy Tarreauf6625822015-12-27 14:51:01 +010010878 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010879 * or the second chunk. The caller must be check the position before using the
10880 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090010881 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010882static const char *
10883find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010884 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010885 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010886{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010887 const char *pos, *last, *equal;
10888 const char **bufs = chunks;
10889 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090010890
David Cournapeau16023ee2010-12-23 20:55:41 +090010891
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010892 pos = bufs[0];
10893 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010010894 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010895 /* Check the equal. */
10896 equal = pos + url_param_name_l;
10897 if (fix_pointer_if_wrap(chunks, &equal)) {
10898 if (equal >= chunks[3])
10899 return NULL;
10900 } else {
10901 if (equal >= chunks[1])
10902 return NULL;
10903 }
10904 if (*equal == '=') {
10905 if (pos + url_param_name_l > last) {
10906 /* process wrap case, we detect a wrap. In this case, the
10907 * comparison is performed in two parts.
10908 */
10909
10910 /* This is the end, we dont have any other chunk. */
10911 if (bufs != chunks || !bufs[2])
10912 return NULL;
10913
10914 /* Compute the length of each part of the comparison. */
10915 l1 = last - pos;
10916 l2 = url_param_name_l - l1;
10917
10918 /* The second buffer is too short to contain the compared string. */
10919 if (bufs[2] + l2 > bufs[3])
10920 return NULL;
10921
10922 if (memcmp(pos, url_param_name, l1) == 0 &&
10923 memcmp(bufs[2], url_param_name+l1, l2) == 0)
10924 return pos;
10925
10926 /* Perform wrapping and jump the string who fail the comparison. */
10927 bufs += 2;
10928 pos = bufs[0] + l2;
10929 last = bufs[1];
10930
10931 } else {
10932 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010933 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
10934 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010935 pos += url_param_name_l + 1;
10936 if (fix_pointer_if_wrap(chunks, &pos))
10937 last = bufs[2];
10938 }
10939 }
10940
10941 while (1) {
10942 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010943 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010944 pos++;
10945 if (pos < last)
10946 break;
10947 /* process buffer wrapping. */
10948 if (bufs != chunks || !bufs[2])
10949 return NULL;
10950 bufs += 2;
10951 pos = bufs[0];
10952 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090010953 }
David Cournapeau16023ee2010-12-23 20:55:41 +090010954 pos++;
10955 }
10956 return NULL;
10957}
10958
10959/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010010960 * Given a url parameter name and a query string, find the next value.
10961 * An empty url_param_name matches the first available parameter.
10962 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
10963 * respectively provide a pointer to the value and its end.
10964 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090010965 */
10966static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010967find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010968 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010969 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010970{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010971 const char *arg_start, *qs_end;
10972 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090010973
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010974 arg_start = chunks[0];
10975 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010976 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010977 /* Looks for an argument name. */
10978 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010979 url_param_name, url_param_name_l,
10980 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010981 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010982 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010983 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010984 }
David Cournapeau16023ee2010-12-23 20:55:41 +090010985 if (!arg_start)
10986 return 0;
10987
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010988 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010989 while (1) {
10990 /* looks for the first argument. */
10991 value_start = memchr(arg_start, '=', qs_end - arg_start);
10992 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010993 /* Check for wrapping. */
10994 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010010995 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010996 chunks[2]) {
10997 arg_start = chunks[2];
10998 qs_end = chunks[3];
10999 continue;
11000 }
11001 return 0;
11002 }
11003 break;
11004 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011005 value_start++;
11006 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011007 else {
11008 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011009 value_start = arg_start + url_param_name_l + 1;
11010
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011011 /* Check for pointer wrapping. */
11012 if (fix_pointer_if_wrap(chunks, &value_start)) {
11013 /* Update the end pointer. */
11014 qs_end = chunks[3];
11015
11016 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011017 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011018 return 0;
11019 }
11020 }
11021
David Cournapeau16023ee2010-12-23 20:55:41 +090011022 value_end = value_start;
11023
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011024 while (1) {
11025 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11026 value_end++;
11027 if (value_end < qs_end)
11028 break;
11029 /* process buffer wrapping. */
11030 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011031 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011032 chunks[2]) {
11033 value_end = chunks[2];
11034 qs_end = chunks[3];
11035 continue;
11036 }
11037 break;
11038 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011039
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011040 *vstart = value_start;
11041 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011042 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011043}
11044
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011045/* This scans a URL-encoded query string. It takes an optionally wrapping
11046 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11047 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11048 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011049 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011050static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011051smp_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 +090011052{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011053 const char *vstart, *vend;
11054 struct chunk *temp;
11055 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011056
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011057 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011058 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011059 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011060 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011061 return 0;
11062
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011063 /* Create sample. If the value is contiguous, return the pointer as CONST,
11064 * if the value is wrapped, copy-it in a buffer.
11065 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011066 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011067 if (chunks[2] &&
11068 vstart >= chunks[0] && vstart <= chunks[1] &&
11069 vend >= chunks[2] && vend <= chunks[3]) {
11070 /* Wrapped case. */
11071 temp = get_trash_chunk();
11072 memcpy(temp->str, vstart, chunks[1] - vstart);
11073 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011074 smp->data.u.str.str = temp->str;
11075 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011076 } else {
11077 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011078 smp->data.u.str.str = (char *)vstart;
11079 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011080 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11081 }
11082
11083 /* Update context, check wrapping. */
11084 chunks[0] = vend;
11085 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11086 chunks[1] = chunks[3];
11087 chunks[2] = NULL;
11088 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011089
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011090 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011091 smp->flags |= SMP_F_NOT_LAST;
11092
David Cournapeau16023ee2010-12-23 20:55:41 +090011093 return 1;
11094}
11095
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011096/* This function iterates over each parameter of the query string. It uses
11097 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011098 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11099 * An optional parameter name is passed in args[0], otherwise any parameter is
11100 * considered. It supports an optional delimiter argument for the beginning of
11101 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011102 */
11103static int
11104smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11105{
11106 struct http_msg *msg;
11107 char delim = '?';
11108 const char *name;
11109 int name_len;
11110
Dragan Dosen26f77e52015-05-25 10:02:11 +020011111 if (!args ||
11112 (args[0].type && args[0].type != ARGT_STR) ||
11113 (args[1].type && args[1].type != ARGT_STR))
11114 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011115
Dragan Dosen26f77e52015-05-25 10:02:11 +020011116 name = "";
11117 name_len = 0;
11118 if (args->type == ARGT_STR) {
11119 name = args->data.str.str;
11120 name_len = args->data.str.len;
11121 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011122
Dragan Dosen26f77e52015-05-25 10:02:11 +020011123 if (args[1].type)
11124 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011125
Dragan Dosen26f77e52015-05-25 10:02:11 +020011126 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011127 CHECK_HTTP_MESSAGE_FIRST();
11128
11129 msg = &smp->strm->txn->req;
11130
11131 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11132 msg->sl.rq.u_l, delim);
11133 if (!smp->ctx.a[0])
11134 return 0;
11135
11136 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011137
11138 /* Assume that the context is filled with NULL pointer
11139 * before the first call.
11140 * smp->ctx.a[2] = NULL;
11141 * smp->ctx.a[3] = NULL;
11142 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011143 }
11144
11145 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11146}
11147
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011148/* This function iterates over each parameter of the body. This requires
11149 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011150 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11151 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11152 * optional second part if the body wraps at the end of the buffer. An optional
11153 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011154 */
11155static int
11156smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11157{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011158 struct http_msg *msg;
11159 unsigned long len;
11160 unsigned long block1;
11161 char *body;
11162 const char *name;
11163 int name_len;
11164
11165 if (!args || (args[0].type && args[0].type != ARGT_STR))
11166 return 0;
11167
11168 name = "";
11169 name_len = 0;
11170 if (args[0].type == ARGT_STR) {
11171 name = args[0].data.str.str;
11172 name_len = args[0].data.str.len;
11173 }
11174
11175 if (!smp->ctx.a[0]) { // first call, find the query string
11176 CHECK_HTTP_MESSAGE_FIRST();
11177
11178 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011179 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011180 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011181 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011182
11183 len = http_body_bytes(msg);
11184 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11185
11186 block1 = len;
11187 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11188 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11189
11190 if (block1 == len) {
11191 /* buffer is not wrapped (or empty) */
11192 smp->ctx.a[0] = body;
11193 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011194
11195 /* Assume that the context is filled with NULL pointer
11196 * before the first call.
11197 * smp->ctx.a[2] = NULL;
11198 * smp->ctx.a[3] = NULL;
11199 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011200 }
11201 else {
11202 /* buffer is wrapped, we need to defragment it */
11203 smp->ctx.a[0] = body;
11204 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011205 smp->ctx.a[2] = msg->chn->buf->data;
11206 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011207 }
11208 }
11209 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11210}
11211
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011212/* Return the signed integer value for the specified url parameter (see url_param
11213 * above).
11214 */
11215static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011216smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011217{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011218 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011219
11220 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011221 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011222 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011223 }
11224
11225 return ret;
11226}
11227
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011228/* This produces a 32-bit hash of the concatenation of the first occurrence of
11229 * the Host header followed by the path component if it begins with a slash ('/').
11230 * This means that '*' will not be added, resulting in exactly the first Host
11231 * entry. If no Host header is found, then the path is used. The resulting value
11232 * is hashed using the url hash followed by a full avalanche hash and provides a
11233 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11234 * high-traffic sites without having to store whole paths.
11235 * this differs from the base32 functions in that it includes the url parameters
11236 * as well as the path
11237 */
11238static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011239smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011240{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011241 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011242 struct hdr_ctx ctx;
11243 unsigned int hash = 0;
11244 char *ptr, *beg, *end;
11245 int len;
11246
11247 CHECK_HTTP_MESSAGE_FIRST();
11248
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011249 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011250 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011251 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011252 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11253 ptr = ctx.line + ctx.val;
11254 len = ctx.vlen;
11255 while (len--)
11256 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11257 }
11258
11259 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011260 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 +000011261 beg = http_get_path(txn);
11262 if (!beg)
11263 beg = end;
11264
11265 for (ptr = beg; ptr < end ; ptr++);
11266
11267 if (beg < ptr && *beg == '/') {
11268 while (beg < ptr)
11269 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11270 }
11271 hash = full_hash(hash);
11272
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011273 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011274 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011275 smp->flags = SMP_F_VOL_1ST;
11276 return 1;
11277}
11278
11279/* This concatenates the source address with the 32-bit hash of the Host and
11280 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11281 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11282 * on the source address length. The URL hash is stored before the address so
11283 * that in environments where IPv6 is insignificant, truncating the output to
11284 * 8 bytes would still work.
11285 */
11286static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011287smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011288{
11289 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011290 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011291
Dragan Dosendb5af612016-06-16 11:23:01 +020011292 if (!cli_conn)
11293 return 0;
11294
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011295 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011296 return 0;
11297
11298 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020011299 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
11300 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011301
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011302 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011303 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011304 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011305 temp->len += 4;
11306 break;
11307 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011308 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011309 temp->len += 16;
11310 break;
11311 default:
11312 return 0;
11313 }
11314
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011315 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011316 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011317 return 1;
11318}
11319
Willy Tarreau185b5c42012-04-26 15:11:51 +020011320/* This function is used to validate the arguments passed to any "hdr" fetch
11321 * keyword. These keywords support an optional positive or negative occurrence
11322 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11323 * is assumed that the types are already the correct ones. Returns 0 on error,
11324 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11325 * error message in case of error, that the caller is responsible for freeing.
11326 * The initial location must either be freeable or NULL.
11327 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011328int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011329{
11330 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011331 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011332 return 0;
11333 }
11334 return 1;
11335}
11336
Willy Tarreau276fae92013-07-25 14:36:01 +020011337/* takes an UINT value on input supposed to represent the time since EPOCH,
11338 * adds an optional offset found in args[0] and emits a string representing
11339 * the date in RFC-1123/5322 format.
11340 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011341static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011342{
Cyril Bontéf78d8962016-01-22 19:40:28 +010011343 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020011344 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11345 struct chunk *temp;
11346 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011347 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011348 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011349
11350 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011351 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011352 curr_date += args[0].data.sint;
11353
11354 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011355 if (!tm)
11356 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011357
11358 temp = get_trash_chunk();
11359 temp->len = snprintf(temp->str, temp->size - temp->len,
11360 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11361 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11362 tm->tm_hour, tm->tm_min, tm->tm_sec);
11363
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011364 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011365 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011366 return 1;
11367}
11368
Thierry FOURNIERad903512014-04-11 17:51:01 +020011369/* Match language range with language tag. RFC2616 14.4:
11370 *
11371 * A language-range matches a language-tag if it exactly equals
11372 * the tag, or if it exactly equals a prefix of the tag such
11373 * that the first tag character following the prefix is "-".
11374 *
11375 * Return 1 if the strings match, else return 0.
11376 */
11377static inline int language_range_match(const char *range, int range_len,
11378 const char *tag, int tag_len)
11379{
11380 const char *end = range + range_len;
11381 const char *tend = tag + tag_len;
11382 while (range < end) {
11383 if (*range == '-' && tag == tend)
11384 return 1;
11385 if (*range != *tag || tag == tend)
11386 return 0;
11387 range++;
11388 tag++;
11389 }
11390 /* Return true only if the last char of the tag is matched. */
11391 return tag == tend;
11392}
11393
11394/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011395static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011396{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011397 const char *al = smp->data.u.str.str;
11398 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011399 const char *token;
11400 int toklen;
11401 int qvalue;
11402 const char *str;
11403 const char *w;
11404 int best_q = 0;
11405
11406 /* Set the constant to the sample, because the output of the
11407 * function will be peek in the constant configuration string.
11408 */
11409 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011410 smp->data.u.str.size = 0;
11411 smp->data.u.str.str = "";
11412 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011413
11414 /* Parse the accept language */
11415 while (1) {
11416
11417 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011418 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011419 al++;
11420 if (al >= end)
11421 break;
11422
11423 /* Start of the fisrt word. */
11424 token = al;
11425
11426 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011427 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011428 al++;
11429 if (al == token)
11430 goto expect_comma;
11431
11432 /* Length of the token. */
11433 toklen = al - token;
11434 qvalue = 1000;
11435
11436 /* Check if the token exists in the list. If the token not exists,
11437 * jump to the next token.
11438 */
11439 str = args[0].data.str.str;
11440 w = str;
11441 while (1) {
11442 if (*str == ';' || *str == '\0') {
11443 if (language_range_match(token, toklen, w, str-w))
11444 goto look_for_q;
11445 if (*str == '\0')
11446 goto expect_comma;
11447 w = str + 1;
11448 }
11449 str++;
11450 }
11451 goto expect_comma;
11452
11453look_for_q:
11454
11455 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011456 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011457 al++;
11458 if (al >= end)
11459 goto process_value;
11460
11461 /* If ',' is found, process the result */
11462 if (*al == ',')
11463 goto process_value;
11464
11465 /* If the character is different from ';', look
11466 * for the end of the header part in best effort.
11467 */
11468 if (*al != ';')
11469 goto expect_comma;
11470
11471 /* Assumes that the char is ';', now expect "q=". */
11472 al++;
11473
11474 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011475 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011476 al++;
11477 if (al >= end)
11478 goto process_value;
11479
11480 /* Expect 'q'. If no 'q', continue in best effort */
11481 if (*al != 'q')
11482 goto process_value;
11483 al++;
11484
11485 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011486 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011487 al++;
11488 if (al >= end)
11489 goto process_value;
11490
11491 /* Expect '='. If no '=', continue in best effort */
11492 if (*al != '=')
11493 goto process_value;
11494 al++;
11495
11496 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011497 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011498 al++;
11499 if (al >= end)
11500 goto process_value;
11501
11502 /* Parse the q value. */
11503 qvalue = parse_qvalue(al, &al);
11504
11505process_value:
11506
11507 /* If the new q value is the best q value, then store the associated
11508 * language in the response. If qvalue is the biggest value (1000),
11509 * break the process.
11510 */
11511 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011512 smp->data.u.str.str = (char *)w;
11513 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011514 if (qvalue >= 1000)
11515 break;
11516 best_q = qvalue;
11517 }
11518
11519expect_comma:
11520
11521 /* Expect comma or end. If the end is detected, quit the loop. */
11522 while (al < end && *al != ',')
11523 al++;
11524 if (al >= end)
11525 break;
11526
11527 /* Comma is found, jump it and restart the analyzer. */
11528 al++;
11529 }
11530
11531 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011532 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
11533 smp->data.u.str.str = args[1].data.str.str;
11534 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011535 }
11536
11537 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011538 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011539}
11540
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011541/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011542static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011543{
11544 /* If the constant flag is set or if not size is avalaible at
11545 * the end of the buffer, copy the string in other buffer
11546 * before decoding.
11547 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011548 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011549 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011550 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
11551 smp->data.u.str.str = str->str;
11552 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011553 smp->flags &= ~SMP_F_CONST;
11554 }
11555
11556 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011557 smp->data.u.str.str[smp->data.u.str.len] = '\0';
11558 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Christopher Fauleta2584792017-10-05 10:03:12 +020011559 return (smp->data.u.str.len >= 0);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011560}
11561
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011562static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
11563{
11564 struct proxy *fe = strm_fe(smp->strm);
11565 int idx, i;
11566 struct cap_hdr *hdr;
11567 int len;
11568
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011569 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011570 return 0;
11571
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011572 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011573
11574 /* Check the availibity of the capture id. */
11575 if (idx > fe->nb_req_cap - 1)
11576 return 0;
11577
11578 /* Look for the original configuration. */
11579 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
11580 hdr != NULL && i != idx ;
11581 i--, hdr = hdr->next);
11582 if (!hdr)
11583 return 0;
11584
11585 /* check for the memory allocation */
11586 if (smp->strm->req_cap[hdr->index] == NULL)
11587 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
11588 if (smp->strm->req_cap[hdr->index] == NULL)
11589 return 0;
11590
11591 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011592 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011593 if (len > hdr->len)
11594 len = hdr->len;
11595
11596 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011597 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011598 smp->strm->req_cap[idx][len] = '\0';
11599
11600 return 1;
11601}
11602
11603static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
11604{
11605 struct proxy *fe = strm_fe(smp->strm);
11606 int idx, i;
11607 struct cap_hdr *hdr;
11608 int len;
11609
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011610 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011611 return 0;
11612
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011613 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011614
11615 /* Check the availibity of the capture id. */
11616 if (idx > fe->nb_rsp_cap - 1)
11617 return 0;
11618
11619 /* Look for the original configuration. */
11620 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
11621 hdr != NULL && i != idx ;
11622 i--, hdr = hdr->next);
11623 if (!hdr)
11624 return 0;
11625
11626 /* check for the memory allocation */
11627 if (smp->strm->res_cap[hdr->index] == NULL)
11628 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
11629 if (smp->strm->res_cap[hdr->index] == NULL)
11630 return 0;
11631
11632 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011633 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011634 if (len > hdr->len)
11635 len = hdr->len;
11636
11637 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011638 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011639 smp->strm->res_cap[idx][len] = '\0';
11640
11641 return 1;
11642}
11643
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011644/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011645 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011646 * the relevant part of the request line accordingly. Then it updates various
11647 * pointers to the next elements which were moved, and the total buffer length.
11648 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011649 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11650 * error, though this can be revisited when this code is finally exploited.
11651 *
11652 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
11653 * query string and 3 to replace uri.
11654 *
11655 * In query string case, the mark question '?' must be set at the start of the
11656 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011657 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011658int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011659 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011660{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011661 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011662 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011663 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011664 int delta;
11665
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011666 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011667 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011668 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011669 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11670
11671 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011672 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011673 txn->req.sl.rq.m_l += delta;
11674 txn->req.sl.rq.u += delta;
11675 txn->req.sl.rq.v += delta;
11676 break;
11677
11678 case 1: // path
11679 cur_ptr = http_get_path(txn);
11680 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011681 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011682
11683 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011684 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 +010011685 cur_end++;
11686
11687 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011688 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011689 txn->req.sl.rq.u_l += delta;
11690 txn->req.sl.rq.v += delta;
11691 break;
11692
11693 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011694 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011695 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011696 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11697 while (cur_ptr < cur_end && *cur_ptr != '?')
11698 cur_ptr++;
11699
11700 /* skip the question mark or indicate that we must insert it
11701 * (but only if the format string is not empty then).
11702 */
11703 if (cur_ptr < cur_end)
11704 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011705 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011706 offset = 0;
11707
11708 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011709 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011710 txn->req.sl.rq.u_l += delta;
11711 txn->req.sl.rq.v += delta;
11712 break;
11713
11714 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011715 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011716 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11717
11718 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011719 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011720 txn->req.sl.rq.u_l += delta;
11721 txn->req.sl.rq.v += delta;
11722 break;
11723
11724 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011725 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011726 }
11727
11728 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011729 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020011730 txn->req.sl.rq.l += delta;
11731 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011732 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011733 return 0;
11734}
11735
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011736/* This function replace the HTTP status code and the associated message. The
11737 * variable <status> contains the new status code. This function never fails.
11738 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011739void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011740{
11741 struct http_txn *txn = s->txn;
11742 char *cur_ptr, *cur_end;
11743 int delta;
11744 char *res;
11745 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011746 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011747 int msg_len;
11748
11749 chunk_reset(&trash);
11750
11751 res = ultoa_o(status, trash.str, trash.size);
11752 c_l = res - trash.str;
11753
11754 trash.str[c_l] = ' ';
11755 trash.len = c_l + 1;
11756
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011757 /* Do we have a custom reason format string? */
11758 if (msg == NULL)
11759 msg = get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011760 msg_len = strlen(msg);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011761 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
11762 trash.len += msg_len;
11763
11764 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
11765 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
11766
11767 /* commit changes and adjust message */
11768 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
11769
11770 /* adjust res line offsets and lengths */
11771 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
11772 txn->rsp.sl.st.c_l = c_l;
11773 txn->rsp.sl.st.r_l = msg_len;
11774
11775 delta = trash.len - (cur_end - cur_ptr);
11776 txn->rsp.sl.st.l += delta;
11777 txn->hdr_idx.v[0].len += delta;
11778 http_msg_move_end(&txn->rsp, delta);
11779}
11780
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011781/* This function executes one of the set-{method,path,query,uri} actions. It
11782 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011783 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011784 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011785 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
11786 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011787 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011788enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011789 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011790{
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011791 struct chunk *replace;
11792 enum act_return ret = ACT_RET_ERR;
11793
11794 replace = alloc_trash_chunk();
11795 if (!replace)
11796 goto leave;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011797
11798 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011799 if (rule->arg.http.action == 2)
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011800 replace->str[replace->len++] = '?';
11801 replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len,
11802 &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011803
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011804 http_replace_req_line(rule->arg.http.action, replace->str, replace->len, px, s);
11805
11806 ret = ACT_RET_CONT;
11807
11808leave:
11809 free_trash_chunk(replace);
11810 return ret;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011811}
11812
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011813/* This function is just a compliant action wrapper for "set-status". */
11814enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011815 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011816{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011817 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011818 return ACT_RET_CONT;
11819}
11820
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011821/* parse an http-request action among :
11822 * set-method
11823 * set-path
11824 * set-query
11825 * set-uri
11826 *
11827 * All of them accept a single argument of type string representing a log-format.
11828 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
11829 * 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 +020011830 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011831 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011832enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
11833 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011834{
11835 int cur_arg = *orig_arg;
11836
Thierry FOURNIER42148732015-09-02 17:17:33 +020011837 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011838
11839 switch (args[0][4]) {
11840 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011841 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011842 rule->action_ptr = http_action_set_req_line;
11843 break;
11844 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011845 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011846 rule->action_ptr = http_action_set_req_line;
11847 break;
11848 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011849 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011850 rule->action_ptr = http_action_set_req_line;
11851 break;
11852 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011853 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011854 rule->action_ptr = http_action_set_req_line;
11855 break;
11856 default:
11857 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011858 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011859 }
11860
11861 if (!*args[cur_arg] ||
11862 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
11863 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011864 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011865 }
11866
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011867 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011868 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010011869 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +010011870 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010011871 return ACT_RET_PRS_ERR;
11872 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011873
11874 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011875 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011876}
11877
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011878/* parse set-status action:
11879 * This action accepts a single argument of type int representing
11880 * an http status code. It returns ACT_RET_PRS_OK on success,
11881 * ACT_RET_PRS_ERR on error.
11882 */
11883enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
11884 struct act_rule *rule, char **err)
11885{
11886 char *error;
11887
Thierry FOURNIER42148732015-09-02 17:17:33 +020011888 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011889 rule->action_ptr = action_http_set_status;
11890
11891 /* Check if an argument is available */
11892 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011893 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011894 return ACT_RET_PRS_ERR;
11895 }
11896
11897 /* convert status code as integer */
11898 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
11899 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
11900 memprintf(err, "expects an integer status code between 100 and 999");
11901 return ACT_RET_PRS_ERR;
11902 }
11903
11904 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011905
11906 /* set custom reason string */
11907 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
11908 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
11909 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
11910 (*orig_arg)++;
11911 rule->arg.status.reason = strdup(args[*orig_arg]);
11912 (*orig_arg)++;
11913 }
11914
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011915 return ACT_RET_PRS_OK;
11916}
11917
Willy Tarreaua9083d02015-05-08 15:27:59 +020011918/* This function executes the "capture" action. It executes a fetch expression,
11919 * turns the result into a string and puts it in a capture slot. It always
11920 * returns 1. If an error occurs the action is cancelled, but the rule
11921 * processing continues.
11922 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011923enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011924 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020011925{
Willy Tarreaua9083d02015-05-08 15:27:59 +020011926 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020011927 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011928 char **cap = s->req_cap;
11929 int len;
11930
Thierry FOURNIER32b15002015-07-31 08:56:16 +020011931 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 +020011932 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011933 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011934
11935 if (cap[h->index] == NULL)
11936 cap[h->index] = pool_alloc2(h->pool);
11937
11938 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011939 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011940
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011941 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011942 if (len > h->len)
11943 len = h->len;
11944
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011945 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020011946 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011947 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011948}
11949
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011950/* This function executes the "capture" action and store the result in a
11951 * capture slot if exists. It executes a fetch expression, turns the result
11952 * into a string and puts it in a capture slot. It always returns 1. If an
11953 * error occurs the action is cancelled, but the rule processing continues.
11954 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011955enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011956 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011957{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011958 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011959 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011960 char **cap = s->req_cap;
11961 struct proxy *fe = strm_fe(s);
11962 int len;
11963 int i;
11964
11965 /* Look for the original configuration. */
11966 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020011967 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011968 i--, h = h->next);
11969 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011970 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011971
Thierry FOURNIERe2097972015-07-31 08:56:35 +020011972 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 +020011973 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011974 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011975
11976 if (cap[h->index] == NULL)
11977 cap[h->index] = pool_alloc2(h->pool);
11978
11979 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011980 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011981
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011982 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011983 if (len > h->len)
11984 len = h->len;
11985
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011986 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011987 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011988 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011989}
11990
Christopher Faulet29730ba2017-09-18 15:26:32 +020011991/* Check an "http-request capture" action.
11992 *
11993 * The function returns 1 in success case, otherwise, it returns 0 and err is
11994 * filled.
11995 */
11996int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
11997{
11998 if (rule->arg.capid.idx >= px->nb_req_cap) {
11999 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
12000 rule->arg.capid.idx);
12001 return 0;
12002 }
12003
12004 return 1;
12005}
12006
Willy Tarreaua9083d02015-05-08 15:27:59 +020012007/* parse an "http-request capture" action. It takes a single argument which is
12008 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012009 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012010 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012011 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012012enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12013 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012014{
12015 struct sample_expr *expr;
12016 struct cap_hdr *hdr;
12017 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012018 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012019
12020 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12021 if (strcmp(args[cur_arg], "if") == 0 ||
12022 strcmp(args[cur_arg], "unless") == 0)
12023 break;
12024
12025 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012026 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012027 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012028 }
12029
Willy Tarreaua9083d02015-05-08 15:27:59 +020012030 cur_arg = *orig_arg;
12031 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12032 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012033 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012034
12035 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12036 memprintf(err,
12037 "fetch method '%s' extracts information from '%s', none of which is available here",
12038 args[cur_arg-1], sample_src_names(expr->fetch->use));
12039 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012040 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012041 }
12042
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012043 if (!args[cur_arg] || !*args[cur_arg]) {
12044 memprintf(err, "expects 'len or 'id'");
12045 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012046 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012047 }
12048
Willy Tarreaua9083d02015-05-08 15:27:59 +020012049 if (strcmp(args[cur_arg], "len") == 0) {
12050 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012051
12052 if (!(px->cap & PR_CAP_FE)) {
12053 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012054 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012055 }
12056
12057 proxy->conf.args.ctx = ARGC_CAP;
12058
Willy Tarreaua9083d02015-05-08 15:27:59 +020012059 if (!args[cur_arg]) {
12060 memprintf(err, "missing length value");
12061 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012062 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012063 }
12064 /* we copy the table name for now, it will be resolved later */
12065 len = atoi(args[cur_arg]);
12066 if (len <= 0) {
12067 memprintf(err, "length must be > 0");
12068 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012069 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012070 }
12071 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012072
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012073 if (!len) {
12074 memprintf(err, "a positive 'len' argument is mandatory");
12075 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012076 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012077 }
12078
Vincent Bernat02779b62016-04-03 13:48:43 +020012079 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012080 hdr->next = px->req_cap;
12081 hdr->name = NULL; /* not a header capture */
12082 hdr->namelen = 0;
12083 hdr->len = len;
12084 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12085 hdr->index = px->nb_req_cap++;
12086
12087 px->req_cap = hdr;
12088 px->to_log |= LW_REQHDR;
12089
Thierry FOURNIER42148732015-09-02 17:17:33 +020012090 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012091 rule->action_ptr = http_action_req_capture;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012092 rule->check_ptr = check_http_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012093 rule->arg.cap.expr = expr;
12094 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012095 }
12096
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012097 else if (strcmp(args[cur_arg], "id") == 0) {
12098 int id;
12099 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012100
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012101 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012102
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012103 if (!args[cur_arg]) {
12104 memprintf(err, "missing id value");
12105 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012106 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012107 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012108
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012109 id = strtol(args[cur_arg], &error, 10);
12110 if (*error != '\0') {
12111 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12112 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012113 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012114 }
12115 cur_arg++;
12116
12117 proxy->conf.args.ctx = ARGC_CAP;
12118
Thierry FOURNIER42148732015-09-02 17:17:33 +020012119 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012120 rule->action_ptr = http_action_req_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012121 rule->check_ptr = check_http_req_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012122 rule->arg.capid.expr = expr;
12123 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012124 }
12125
12126 else {
12127 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12128 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012129 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012130 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012131
12132 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012133 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012134}
12135
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012136/* This function executes the "capture" action and store the result in a
12137 * capture slot if exists. It executes a fetch expression, turns the result
12138 * into a string and puts it in a capture slot. It always returns 1. If an
12139 * error occurs the action is cancelled, but the rule processing continues.
12140 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012141enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012142 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012143{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012144 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012145 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012146 char **cap = s->res_cap;
12147 struct proxy *fe = strm_fe(s);
12148 int len;
12149 int i;
12150
12151 /* Look for the original configuration. */
12152 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012153 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012154 i--, h = h->next);
12155 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012156 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012157
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012158 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 +020012159 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012160 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012161
12162 if (cap[h->index] == NULL)
12163 cap[h->index] = pool_alloc2(h->pool);
12164
12165 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012166 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012167
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012168 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012169 if (len > h->len)
12170 len = h->len;
12171
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012172 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012173 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012174 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012175}
12176
Christopher Faulet29730ba2017-09-18 15:26:32 +020012177/* Check an "http-response capture" action.
12178 *
12179 * The function returns 1 in success case, otherwise, it returns 0 and err is
12180 * filled.
12181 */
12182int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
12183{
12184 if (rule->arg.capid.idx >= px->nb_rsp_cap) {
12185 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
12186 rule->arg.capid.idx);
12187 return 0;
12188 }
12189
12190 return 1;
12191}
12192
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012193/* parse an "http-response capture" action. It takes a single argument which is
12194 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12195 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012196 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012197 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012198enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12199 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012200{
12201 struct sample_expr *expr;
12202 int cur_arg;
12203 int id;
12204 char *error;
12205
12206 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12207 if (strcmp(args[cur_arg], "if") == 0 ||
12208 strcmp(args[cur_arg], "unless") == 0)
12209 break;
12210
12211 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012212 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012213 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012214 }
12215
12216 cur_arg = *orig_arg;
12217 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12218 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012219 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012220
12221 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12222 memprintf(err,
12223 "fetch method '%s' extracts information from '%s', none of which is available here",
12224 args[cur_arg-1], sample_src_names(expr->fetch->use));
12225 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012226 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012227 }
12228
12229 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012230 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012231 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012232 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012233 }
12234
12235 if (strcmp(args[cur_arg], "id") != 0) {
12236 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12237 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012238 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012239 }
12240
12241 cur_arg++;
12242
12243 if (!args[cur_arg]) {
12244 memprintf(err, "missing id value");
12245 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012246 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012247 }
12248
12249 id = strtol(args[cur_arg], &error, 10);
12250 if (*error != '\0') {
12251 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12252 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012253 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012254 }
12255 cur_arg++;
12256
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012257 proxy->conf.args.ctx = ARGC_CAP;
12258
Thierry FOURNIER42148732015-09-02 17:17:33 +020012259 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012260 rule->action_ptr = http_action_res_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012261 rule->check_ptr = check_http_res_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012262 rule->arg.capid.expr = expr;
12263 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012264
12265 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012266 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012267}
12268
William Lallemand73025dd2014-04-24 14:38:37 +020012269/*
12270 * Return the struct http_req_action_kw associated to a keyword.
12271 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012272struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012273{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012274 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012275}
12276
12277/*
12278 * Return the struct http_res_action_kw associated to a keyword.
12279 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012280struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012281{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012282 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012283}
12284
Willy Tarreau12207b32016-11-22 19:48:51 +010012285
12286/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
12287 * now.
12288 */
12289static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private)
12290{
12291 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
12292 return 1;
12293
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012294 if (*args[2]) {
12295 struct proxy *px;
12296
12297 px = proxy_find_by_name(args[2], 0, 0);
12298 if (px)
12299 appctx->ctx.errors.iid = px->uuid;
12300 else
12301 appctx->ctx.errors.iid = atoi(args[2]);
12302
12303 if (!appctx->ctx.errors.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +020012304 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012305 appctx->ctx.cli.msg = "No such proxy.\n";
12306 appctx->st0 = CLI_ST_PRINT;
12307 return 1;
12308 }
12309 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012310 else
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012311 appctx->ctx.errors.iid = -1; // dump all proxies
12312
Willy Tarreau35069f82016-11-25 09:16:37 +010012313 appctx->ctx.errors.flag = 0;
12314 if (strcmp(args[3], "request") == 0)
12315 appctx->ctx.errors.flag |= 4; // ignore response
12316 else if (strcmp(args[3], "response") == 0)
12317 appctx->ctx.errors.flag |= 2; // ignore request
Willy Tarreau12207b32016-11-22 19:48:51 +010012318 appctx->ctx.errors.px = NULL;
Willy Tarreau12207b32016-11-22 19:48:51 +010012319 return 0;
12320}
12321
12322/* This function dumps all captured errors onto the stream interface's
12323 * read buffer. It returns 0 if the output buffer is full and it needs
12324 * to be called again, otherwise non-zero.
12325 */
12326static int cli_io_handler_show_errors(struct appctx *appctx)
12327{
12328 struct stream_interface *si = appctx->owner;
12329 extern const char *monthname[12];
12330
12331 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
12332 return 1;
12333
12334 chunk_reset(&trash);
12335
12336 if (!appctx->ctx.errors.px) {
12337 /* the function had not been called yet, let's prepare the
12338 * buffer for a response.
12339 */
12340 struct tm tm;
12341
12342 get_localtime(date.tv_sec, &tm);
12343 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
12344 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12345 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
12346 error_snapshot_id);
12347
Willy Tarreau06d80a92017-10-19 14:32:15 +020012348 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012349 /* Socket buffer full. Let's try again later from the same point */
12350 si_applet_cant_put(si);
12351 return 0;
12352 }
12353
12354 appctx->ctx.errors.px = proxy;
Willy Tarreau12207b32016-11-22 19:48:51 +010012355 appctx->ctx.errors.bol = 0;
12356 appctx->ctx.errors.ptr = -1;
12357 }
12358
12359 /* we have two inner loops here, one for the proxy, the other one for
12360 * the buffer.
12361 */
12362 while (appctx->ctx.errors.px) {
12363 struct error_snapshot *es;
12364
Willy Tarreau35069f82016-11-25 09:16:37 +010012365 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012366 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau35069f82016-11-25 09:16:37 +010012367 if (appctx->ctx.errors.flag & 2) // skip req
12368 goto next;
12369 }
12370 else {
Willy Tarreau12207b32016-11-22 19:48:51 +010012371 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau35069f82016-11-25 09:16:37 +010012372 if (appctx->ctx.errors.flag & 4) // skip resp
12373 goto next;
12374 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012375
12376 if (!es->when.tv_sec)
12377 goto next;
12378
12379 if (appctx->ctx.errors.iid >= 0 &&
12380 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
12381 es->oe->uuid != appctx->ctx.errors.iid)
12382 goto next;
12383
12384 if (appctx->ctx.errors.ptr < 0) {
12385 /* just print headers now */
12386
12387 char pn[INET6_ADDRSTRLEN];
12388 struct tm tm;
12389 int port;
12390
12391 get_localtime(es->when.tv_sec, &tm);
12392 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
12393 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12394 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
12395
12396 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
12397 case AF_INET:
12398 case AF_INET6:
12399 port = get_host_port(&es->src);
12400 break;
12401 default:
12402 port = 0;
12403 }
12404
Willy Tarreau35069f82016-11-25 09:16:37 +010012405 switch (appctx->ctx.errors.flag & 1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012406 case 0:
12407 chunk_appendf(&trash,
12408 " frontend %s (#%d): invalid request\n"
12409 " backend %s (#%d)",
12410 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12411 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
12412 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
12413 break;
12414 case 1:
12415 chunk_appendf(&trash,
12416 " backend %s (#%d): invalid response\n"
12417 " frontend %s (#%d)",
12418 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12419 es->oe->id, es->oe->uuid);
12420 break;
12421 }
12422
12423 chunk_appendf(&trash,
12424 ", server %s (#%d), event #%u\n"
12425 " src %s:%d, session #%d, session flags 0x%08x\n"
Willy Tarreau10e61cb2017-01-04 14:51:22 +010012426 " HTTP msg state %s(%d), msg flags 0x%08x, tx flags 0x%08x\n"
Willy Tarreau12207b32016-11-22 19:48:51 +010012427 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
12428 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
12429 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
12430 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
12431 es->ev_id,
12432 pn, port, es->sid, es->s_flags,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020012433 h1_msg_state_str(es->state), es->state, es->m_flags, es->t_flags,
Willy Tarreau12207b32016-11-22 19:48:51 +010012434 es->m_clen, es->m_blen,
12435 es->b_flags, es->b_out, es->b_tot,
12436 es->len, es->b_wrap, es->pos);
12437
Willy Tarreau06d80a92017-10-19 14:32:15 +020012438 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012439 /* Socket buffer full. Let's try again later from the same point */
12440 si_applet_cant_put(si);
12441 return 0;
12442 }
12443 appctx->ctx.errors.ptr = 0;
12444 appctx->ctx.errors.sid = es->sid;
12445 }
12446
12447 if (appctx->ctx.errors.sid != es->sid) {
12448 /* the snapshot changed while we were dumping it */
12449 chunk_appendf(&trash,
12450 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +020012451 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012452 si_applet_cant_put(si);
12453 return 0;
12454 }
12455 goto next;
12456 }
12457
12458 /* OK, ptr >= 0, so we have to dump the current line */
12459 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
12460 int newptr;
12461 int newline;
12462
12463 newline = appctx->ctx.errors.bol;
12464 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
12465 if (newptr == appctx->ctx.errors.ptr)
12466 return 0;
12467
Willy Tarreau06d80a92017-10-19 14:32:15 +020012468 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012469 /* Socket buffer full. Let's try again later from the same point */
12470 si_applet_cant_put(si);
12471 return 0;
12472 }
12473 appctx->ctx.errors.ptr = newptr;
12474 appctx->ctx.errors.bol = newline;
12475 };
12476 next:
12477 appctx->ctx.errors.bol = 0;
12478 appctx->ctx.errors.ptr = -1;
Willy Tarreau35069f82016-11-25 09:16:37 +010012479 appctx->ctx.errors.flag ^= 1;
12480 if (!(appctx->ctx.errors.flag & 1))
Willy Tarreau12207b32016-11-22 19:48:51 +010012481 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau12207b32016-11-22 19:48:51 +010012482 }
12483
12484 /* dump complete */
12485 return 1;
12486}
12487
12488/* register cli keywords */
12489static struct cli_kw_list cli_kws = {{ },{
12490 { { "show", "errors", NULL },
12491 "show errors : report last request and response errors for each proxy",
12492 cli_parse_show_errors, cli_io_handler_show_errors, NULL,
12493 },
12494 {{},}
12495}};
12496
Willy Tarreau4a568972010-05-12 08:08:50 +020012497/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012498/* All supported ACL keywords must be declared here. */
12499/************************************************************************/
12500
12501/* Note: must not be declared <const> as its list will be overwritten.
12502 * Please take care of keeping this list alphabetically sorted.
12503 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012504static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012505 { "base", "base", PAT_MATCH_STR },
12506 { "base_beg", "base", PAT_MATCH_BEG },
12507 { "base_dir", "base", PAT_MATCH_DIR },
12508 { "base_dom", "base", PAT_MATCH_DOM },
12509 { "base_end", "base", PAT_MATCH_END },
12510 { "base_len", "base", PAT_MATCH_LEN },
12511 { "base_reg", "base", PAT_MATCH_REG },
12512 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012513
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012514 { "cook", "req.cook", PAT_MATCH_STR },
12515 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12516 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12517 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12518 { "cook_end", "req.cook", PAT_MATCH_END },
12519 { "cook_len", "req.cook", PAT_MATCH_LEN },
12520 { "cook_reg", "req.cook", PAT_MATCH_REG },
12521 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012522
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012523 { "hdr", "req.hdr", PAT_MATCH_STR },
12524 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12525 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12526 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12527 { "hdr_end", "req.hdr", PAT_MATCH_END },
12528 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12529 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12530 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012531
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012532 /* these two declarations uses strings with list storage (in place
12533 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12534 * and delete functions are relative to the list management. The parse
12535 * and match method are related to the corresponding fetch methods. This
12536 * is very particular ACL declaration mode.
12537 */
12538 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12539 { "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 +020012540
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012541 { "path", "path", PAT_MATCH_STR },
12542 { "path_beg", "path", PAT_MATCH_BEG },
12543 { "path_dir", "path", PAT_MATCH_DIR },
12544 { "path_dom", "path", PAT_MATCH_DOM },
12545 { "path_end", "path", PAT_MATCH_END },
12546 { "path_len", "path", PAT_MATCH_LEN },
12547 { "path_reg", "path", PAT_MATCH_REG },
12548 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012549
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012550 { "req_ver", "req.ver", PAT_MATCH_STR },
12551 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012552
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012553 { "scook", "res.cook", PAT_MATCH_STR },
12554 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12555 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12556 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12557 { "scook_end", "res.cook", PAT_MATCH_END },
12558 { "scook_len", "res.cook", PAT_MATCH_LEN },
12559 { "scook_reg", "res.cook", PAT_MATCH_REG },
12560 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012561
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012562 { "shdr", "res.hdr", PAT_MATCH_STR },
12563 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12564 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12565 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12566 { "shdr_end", "res.hdr", PAT_MATCH_END },
12567 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12568 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12569 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012570
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012571 { "url", "url", PAT_MATCH_STR },
12572 { "url_beg", "url", PAT_MATCH_BEG },
12573 { "url_dir", "url", PAT_MATCH_DIR },
12574 { "url_dom", "url", PAT_MATCH_DOM },
12575 { "url_end", "url", PAT_MATCH_END },
12576 { "url_len", "url", PAT_MATCH_LEN },
12577 { "url_reg", "url", PAT_MATCH_REG },
12578 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012579
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012580 { "urlp", "urlp", PAT_MATCH_STR },
12581 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12582 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12583 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12584 { "urlp_end", "urlp", PAT_MATCH_END },
12585 { "urlp_len", "urlp", PAT_MATCH_LEN },
12586 { "urlp_reg", "urlp", PAT_MATCH_REG },
12587 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012588
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012589 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012590}};
12591
12592/************************************************************************/
12593/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012594/************************************************************************/
12595/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012596static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012597 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012598 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012599 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12600
Willy Tarreau87b09662015-04-03 00:22:06 +020012601 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012602 { "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 +020012603
12604 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012605 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12606 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12607 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012608
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012609 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12610 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012611
Willy Tarreau409bcde2013-01-08 00:31:00 +010012612 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12613 * are only here to match the ACL's name, are request-only and are used
12614 * for ACL compatibility only.
12615 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012616 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12617 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012618 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12619 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012620
12621 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12622 * only here to match the ACL's name, are request-only and are used for
12623 * ACL compatibility only.
12624 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012625 { "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 +020012626 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012627 { "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 +020012628 { "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 +010012629
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012630 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012631 { "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 +010012632 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012633 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012634 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012635 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012636
12637 /* HTTP protocol on the request path */
12638 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012639 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012640
12641 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012642 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12643 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012644
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012645 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012646 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12647 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012648 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012649
Thierry FOURNIERd7d88812017-04-19 15:15:14 +020012650 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER5617dce2017-04-09 05:38:19 +020012651 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12652
Willy Tarreau18ed2562013-01-14 15:56:36 +010012653 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012654 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12655 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012656
Willy Tarreau18ed2562013-01-14 15:56:36 +010012657 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012658 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012659 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12660 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012661
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012662 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012663 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012664 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012665 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012666 { "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 +010012667 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012668 { "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 +010012669
12670 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012671 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012672 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12673 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012674
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012675 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012676 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012677 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012678 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012679 { "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 +010012680 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012681 { "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 +010012682
Willy Tarreau409bcde2013-01-08 00:31:00 +010012683 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012684 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012685 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12686 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012687 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012688
12689 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012690 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012691 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012692 { "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 +020012693 { "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 +010012694
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012695 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020012696 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012697 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012698 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012699 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012700 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012701 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020012702 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12703 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012704 { "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 +010012705 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012706}};
12707
Willy Tarreau8797c062007-05-07 00:55:35 +020012708
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012709/************************************************************************/
12710/* All supported converter keywords must be declared here. */
12711/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012712/* Note: must not be declared <const> as its list will be overwritten */
12713static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012714 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020012715 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012716 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
12717 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012718 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012719 { NULL, NULL, 0, 0, 0 },
12720}};
12721
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012722
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012723/************************************************************************/
12724/* All supported http-request action keywords must be declared here. */
12725/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012726struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012727 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020012728 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012729 { "set-method", parse_set_req_line },
12730 { "set-path", parse_set_req_line },
12731 { "set-query", parse_set_req_line },
12732 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012733 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012734 }
12735};
12736
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012737struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012738 .kw = {
12739 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012740 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012741 { NULL, NULL }
12742 }
12743};
12744
Willy Tarreau8797c062007-05-07 00:55:35 +020012745__attribute__((constructor))
12746static void __http_protocol_init(void)
12747{
12748 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012749 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012750 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012751 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012752 http_res_keywords_register(&http_res_actions);
Willy Tarreau12207b32016-11-22 19:48:51 +010012753 cli_register_kw(&cli_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020012754}
12755
12756
Willy Tarreau58f10d72006-12-04 02:26:12 +010012757/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012758 * Local variables:
12759 * c-indent-level: 8
12760 * c-basic-offset: 8
12761 * End:
12762 */