blob: 88e2f06ebbd3fa8a562f2ccb70edb905f60d14dc [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 Tarreau91659792017-11-10 19:38:10 +01001902 else if (txn->meth == HTTP_METH_OTHER &&
1903 msg->sl.rq.m_l == 3 && memcmp(req->buf->p, "PRI", 3) == 0) {
1904 /* PRI is reserved for the HTTP/2 preface */
1905 msg->err_pos = 0;
1906 goto return_bad_req;
1907 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001908
Willy Tarreau59234e92008-11-30 23:51:27 +01001909 /*
1910 * 2: check if the URI matches the monitor_uri.
1911 * We have to do this for every request which gets in, because
1912 * the monitor-uri is defined by the frontend.
1913 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001914 if (unlikely((sess->fe->monitor_uri_len != 0) &&
1915 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02001916 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001917 sess->fe->monitor_uri,
1918 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001919 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001920 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001921 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001922 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001923
Willy Tarreaue7dff022015-04-03 01:14:29 +02001924 s->flags |= SF_MONITOR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001925 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001926
Willy Tarreau59234e92008-11-30 23:51:27 +01001927 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001928 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001929 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02001930
Willy Tarreau59234e92008-11-30 23:51:27 +01001931 ret = acl_pass(ret);
1932 if (cond->pol == ACL_COND_UNLESS)
1933 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001934
Willy Tarreau59234e92008-11-30 23:51:27 +01001935 if (ret) {
1936 /* we fail this request, let's return 503 service unavail */
1937 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001938 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001939 if (!(s->flags & SF_ERR_MASK))
1940 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001941 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001942 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001943 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001944
Willy Tarreau59234e92008-11-30 23:51:27 +01001945 /* nothing to fail, let's reply normaly */
1946 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001947 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001948 if (!(s->flags & SF_ERR_MASK))
1949 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001950 goto return_prx_cond;
1951 }
1952
1953 /*
1954 * 3: Maybe we have to copy the original REQURI for the logs ?
1955 * Note: we cannot log anymore if the request has been
1956 * classified as invalid.
1957 */
1958 if (unlikely(s->logs.logwait & LW_REQ)) {
1959 /* we have a complete HTTP request that we must log */
1960 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
1961 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001962
Stéphane Cottin23e9e932017-05-18 08:58:41 +02001963 if (urilen >= global.tune.requri_len )
1964 urilen = global.tune.requri_len - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001965 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01001966 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001967
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001968 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01001969 s->do_log(s);
1970 } else {
1971 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001972 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001973 }
Willy Tarreau06619262006-12-17 08:37:22 +01001974
Willy Tarreau91852eb2015-05-01 13:26:00 +02001975 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
1976 * exactly one digit "." one digit. This check may be disabled using
1977 * option accept-invalid-http-request.
1978 */
1979 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
1980 if (msg->sl.rq.v_l != 8) {
1981 msg->err_pos = msg->sl.rq.v;
1982 goto return_bad_req;
1983 }
1984
1985 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
1986 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
1987 req->buf->p[msg->sl.rq.v + 6] != '.' ||
1988 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
1989 msg->err_pos = msg->sl.rq.v + 4;
1990 goto return_bad_req;
1991 }
1992 }
Willy Tarreau13317662015-05-01 13:47:08 +02001993 else {
1994 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1995 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
1996 goto return_bad_req;
1997 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02001998
Willy Tarreau5b154472009-12-21 20:11:07 +01001999 /* ... and check if the request is HTTP/1.1 or above */
2000 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002001 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2002 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2003 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002004 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002005
2006 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002007 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG);
Willy Tarreau5b154472009-12-21 20:11:07 +01002008
Willy Tarreau88d349d2010-01-25 12:15:43 +01002009 /* if the frontend has "option http-use-proxy-header", we'll check if
2010 * we have what looks like a proxied connection instead of a connection,
2011 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2012 * Note that this is *not* RFC-compliant, however browsers and proxies
2013 * happen to do that despite being non-standard :-(
2014 * We consider that a request not beginning with either '/' or '*' is
2015 * a proxied connection, which covers both "scheme://location" and
2016 * CONNECT ip:port.
2017 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002018 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002019 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002020 txn->flags |= TX_USE_PX_CONN;
2021
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002022 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002023 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002024
Willy Tarreau59234e92008-11-30 23:51:27 +01002025 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002026 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002027 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002028 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002029
Willy Tarreau557f1992015-05-01 10:05:17 +02002030 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
2031 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002032 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002033 * The length of a message body is determined by one of the following
2034 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02002035 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002036 * 1. Any response to a HEAD request and any response with a 1xx
2037 * (Informational), 204 (No Content), or 304 (Not Modified) status
2038 * code is always terminated by the first empty line after the
2039 * header fields, regardless of the header fields present in the
2040 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002041 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002042 * 2. Any 2xx (Successful) response to a CONNECT request implies that
2043 * the connection will become a tunnel immediately after the empty
2044 * line that concludes the header fields. A client MUST ignore any
2045 * Content-Length or Transfer-Encoding header fields received in
2046 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002047 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002048 * 3. If a Transfer-Encoding header field is present and the chunked
2049 * transfer coding (Section 4.1) is the final encoding, the message
2050 * body length is determined by reading and decoding the chunked
2051 * data until the transfer coding indicates the data is complete.
2052 *
2053 * If a Transfer-Encoding header field is present in a response and
2054 * the chunked transfer coding is not the final encoding, the
2055 * message body length is determined by reading the connection until
2056 * it is closed by the server. If a Transfer-Encoding header field
2057 * is present in a request and the chunked transfer coding is not
2058 * the final encoding, the message body length cannot be determined
2059 * reliably; the server MUST respond with the 400 (Bad Request)
2060 * status code and then close the connection.
2061 *
2062 * If a message is received with both a Transfer-Encoding and a
2063 * Content-Length header field, the Transfer-Encoding overrides the
2064 * Content-Length. Such a message might indicate an attempt to
2065 * perform request smuggling (Section 9.5) or response splitting
2066 * (Section 9.4) and ought to be handled as an error. A sender MUST
2067 * remove the received Content-Length field prior to forwarding such
2068 * a message downstream.
2069 *
2070 * 4. If a message is received without Transfer-Encoding and with
2071 * either multiple Content-Length header fields having differing
2072 * field-values or a single Content-Length header field having an
2073 * invalid value, then the message framing is invalid and the
2074 * recipient MUST treat it as an unrecoverable error. If this is a
2075 * request message, the server MUST respond with a 400 (Bad Request)
2076 * status code and then close the connection. If this is a response
2077 * message received by a proxy, the proxy MUST close the connection
2078 * to the server, discard the received response, and send a 502 (Bad
2079 * Gateway) response to the client. If this is a response message
2080 * received by a user agent, the user agent MUST close the
2081 * connection to the server and discard the received response.
2082 *
2083 * 5. If a valid Content-Length header field is present without
2084 * Transfer-Encoding, its decimal value defines the expected message
2085 * body length in octets. If the sender closes the connection or
2086 * the recipient times out before the indicated number of octets are
2087 * received, the recipient MUST consider the message to be
2088 * incomplete and close the connection.
2089 *
2090 * 6. If this is a request message and none of the above are true, then
2091 * the message body length is zero (no message body is present).
2092 *
2093 * 7. Otherwise, this is a response message without a declared message
2094 * body length, so the message body length is determined by the
2095 * number of octets received prior to the server closing the
2096 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002097 */
2098
Willy Tarreau32b47f42009-10-18 20:55:02 +02002099 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002100 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02002101 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002102 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02002103 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002104 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02002105 /* chunked not last, return badreq */
2106 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002107 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002108 }
2109
Willy Tarreau1c913912015-04-30 10:57:51 +02002110 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02002111 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02002112 if (msg->flags & HTTP_MSGF_TE_CHNK) {
2113 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
2114 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2115 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02002116 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002117 signed long long cl;
2118
Willy Tarreauad14f752011-09-02 20:33:27 +02002119 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002120 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002121 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002122 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002123
Willy Tarreauad14f752011-09-02 20:33:27 +02002124 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002125 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002126 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002127 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002128
Willy Tarreauad14f752011-09-02 20:33:27 +02002129 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002130 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002131 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002132 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002133
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002134 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002135 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002136 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002137 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002138
Christopher Fauletbe821b92017-03-30 11:21:53 +02002139 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002140 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002141 }
2142
Willy Tarreau34dfc602015-05-01 10:09:49 +02002143 /* even bodyless requests have a known length */
2144 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002145
Willy Tarreau179085c2014-04-28 16:48:56 +02002146 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
2147 * only change if both the request and the config reference something else.
2148 * Option httpclose by itself sets tunnel mode where headers are mangled.
2149 * However, if another mode is set, it will affect it (eg: server-close/
2150 * keep-alive + httpclose = close). Note that we avoid to redo the same work
2151 * if FE and BE have the same settings (common). The method consists in
2152 * checking if options changed between the two calls (implying that either
2153 * one is non-null, or one of them is non-null and we are there for the first
2154 * time.
2155 */
2156 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002157 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002158 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02002159
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02002160 /* we may have to wait for the request's body */
2161 if ((s->be->options & PR_O_WREQ_BODY) &&
2162 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
2163 req->analysers |= AN_REQ_HTTP_BODY;
2164
Willy Tarreaud787e662009-07-07 10:14:51 +02002165 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002166 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002167 req->analyse_exp = TICK_ETERNITY;
2168 return 1;
2169
2170 return_bad_req:
2171 /* We centralize bad requests processing here */
2172 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2173 /* we detected a parsing error. We want to archive this request
2174 * in the dedicated proxy area for later troubleshooting.
2175 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02002176 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002177 }
2178
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002179 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02002180 txn->req.msg_state = HTTP_MSG_ERROR;
2181 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002182 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002183
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002184 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002185 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002186 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02002187
2188 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02002189 if (!(s->flags & SF_ERR_MASK))
2190 s->flags |= SF_ERR_PRXCOND;
2191 if (!(s->flags & SF_FINST_MASK))
2192 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02002193
Christopher Faulet0184ea72017-01-05 14:06:34 +01002194 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02002195 req->analyse_exp = TICK_ETERNITY;
2196 return 0;
2197}
2198
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002199
Willy Tarreau347a35d2013-11-22 17:51:09 +01002200/* This function prepares an applet to handle the stats. It can deal with the
2201 * "100-continue" expectation, check that admin rules are met for POST requests,
2202 * and program a response message if something was unexpected. It cannot fail
2203 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002204 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002205 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02002206 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002207 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002208int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002209{
2210 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01002211 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02002212 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002213 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002214 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002215 struct uri_auth *uri_auth = s->be->uri_auth;
2216 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002217 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002218
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002219 appctx = si_appctx(si);
2220 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2221 appctx->st1 = appctx->st2 = 0;
2222 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
2223 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02002224 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02002225 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002226
2227 uri = msg->chn->buf->p + msg->sl.rq.u;
2228 lookup = uri + uri_auth->uri_len;
2229
2230 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
2231 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002232 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002233 break;
2234 }
2235 }
2236
2237 if (uri_auth->refresh) {
2238 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
2239 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002240 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002241 break;
2242 }
2243 }
2244 }
2245
2246 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
2247 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002248 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002249 break;
2250 }
2251 }
2252
Willy Tarreau1e62df92016-01-11 18:57:53 +01002253 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
2254 if (memcmp(h, ";typed", 6) == 0) {
2255 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
2256 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
2257 break;
2258 }
2259 }
2260
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002261 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2262 if (memcmp(h, ";st=", 4) == 0) {
2263 int i;
2264 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002265 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002266 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
2267 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002268 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002269 break;
2270 }
2271 }
2272 break;
2273 }
2274 }
2275
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002276 appctx->ctx.stats.scope_str = 0;
2277 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002278 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2279 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
2280 int itx = 0;
2281 const char *h2;
2282 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
2283 const char *err;
2284
2285 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
2286 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002287 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002288 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
2289 itx++;
2290 h++;
2291 }
2292
2293 if (itx > STAT_SCOPE_TXT_MAXLEN)
2294 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002295 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002296
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002297 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002298 memcpy(scope_txt, h2, itx);
2299 scope_txt[itx] = '\0';
2300 err = invalid_char(scope_txt);
2301 if (err) {
2302 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002303 appctx->ctx.stats.scope_str = 0;
2304 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002305 }
2306 break;
2307 }
2308 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002309
2310 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002311 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002312 int ret = 1;
2313
2314 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002315 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 +01002316 ret = acl_pass(ret);
2317 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2318 ret = !ret;
2319 }
2320
2321 if (ret) {
2322 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002323 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002324 break;
2325 }
2326 }
2327
2328 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02002329 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002330 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02002331 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02002332 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
2333 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002334 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002335 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002336 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002337 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
2338 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002339 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002340 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002341 else {
2342 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002343 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002344 }
2345
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002346 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002347 return 1;
2348}
2349
Lukas Tribus67db8df2013-06-23 17:37:13 +02002350/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
2351 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
2352 */
Vincent Bernat6e615892016-05-18 16:17:44 +02002353void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02002354{
2355#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02002356 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02002357 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2358#endif
2359#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02002360 if (from->ss_family == AF_INET6) {
2361 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02002362 /* v4-mapped addresses need IP_TOS */
2363 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2364 else
2365 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
2366 }
2367#endif
2368}
2369
Willy Tarreau87b09662015-04-03 00:22:06 +02002370int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002371 const char* name, unsigned int name_len,
2372 const char *str, struct my_regex *re,
2373 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06002374{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002375 struct hdr_ctx ctx;
2376 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002377 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002378 int (*http_find_hdr_func)(const char *name, int len, char *sol,
2379 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002380 struct chunk *output = get_trash_chunk();
2381
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002382 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06002383
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002384 /* Choose the header browsing function. */
2385 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002386 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002387 http_find_hdr_func = http_find_header2;
2388 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002389 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002390 http_find_hdr_func = http_find_full_header2;
2391 break;
2392 default: /* impossible */
2393 return -1;
2394 }
2395
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002396 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
2397 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06002398 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002399 char *val = ctx.line + ctx.val;
2400 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06002401
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002402 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
2403 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06002404
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002405 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002406 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06002407 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06002408
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002409 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06002410
2411 hdr->len += delta;
2412 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002413
2414 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002415 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06002416 }
2417
2418 return 0;
2419}
2420
Willy Tarreau87b09662015-04-03 00:22:06 +02002421static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002422 const char* name, unsigned int name_len,
2423 struct list *fmt, struct my_regex *re,
2424 int action)
2425{
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002426 struct chunk *replace;
2427 int ret = -1;
2428
2429 replace = alloc_trash_chunk();
2430 if (!replace)
2431 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002432
2433 replace->len = build_logline(s, replace->str, replace->size, fmt);
2434 if (replace->len >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002435 goto leave;
2436
2437 ret = http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002438
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002439 leave:
2440 free_trash_chunk(replace);
2441 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002442}
2443
Willy Tarreau87b09662015-04-03 00:22:06 +02002444/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02002445 * transaction <txn>. Returns the verdict of the first rule that prevents
2446 * further processing of the request (auth, deny, ...), and defaults to
2447 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2448 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02002449 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2450 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2451 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002452 */
Willy Tarreau0b748332014-04-29 00:13:29 +02002453enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02002454http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002455{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002456 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002457 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002458 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002459 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01002460 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02002461 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02002462 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02002463 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002464
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002465 /* If "the current_rule_list" match the executed rule list, we are in
2466 * resume condition. If a resume is needed it is always in the action
2467 * and never in the ACL or converters. In this case, we initialise the
2468 * current rule, and go to the action execution point.
2469 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002470 if (s->current_rule) {
2471 rule = s->current_rule;
2472 s->current_rule = NULL;
2473 if (s->current_rule_list == rules)
2474 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002475 }
2476 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002477
Willy Tarreauff011f22011-01-06 17:51:27 +01002478 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002479
Willy Tarreau96257ec2012-12-27 10:46:37 +01002480 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01002481 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01002482 int ret;
2483
Willy Tarreau192252e2015-04-04 01:47:55 +02002484 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002485 ret = acl_pass(ret);
2486
Willy Tarreauff011f22011-01-06 17:51:27 +01002487 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002488 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002489
2490 if (!ret) /* condition not matched */
2491 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002492 }
2493
Willy Tarreauacc98002015-09-27 23:34:39 +02002494 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002495resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01002496 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002497 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02002498 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002499
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002500 case ACT_ACTION_DENY:
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 Tarreau96257ec2012-12-27 10:46:37 +01002504
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002505 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01002506 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002507 if (deny_status)
2508 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02002509 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01002510
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002511 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02002512 /* Auth might be performed on regular http-req rules as well as on stats */
2513 auth_realm = rule->arg.auth.realm;
2514 if (!auth_realm) {
2515 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2516 auth_realm = STATS_DEFAULT_REALM;
2517 else
2518 auth_realm = px->id;
2519 }
2520 /* send 401/407 depending on whether we use a proxy or not. We still
2521 * count one error, because normal browsing won't significantly
2522 * increase the counter but brute force attempts will.
2523 */
2524 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
2525 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002526 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02002527 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02002528 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002529
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002530 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02002531 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
2532 return HTTP_RULE_RES_BADREQ;
2533 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002534
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002535 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002536 s->task->nice = rule->arg.nice;
2537 break;
2538
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002539 case ACT_HTTP_SET_TOS:
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 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002542 break;
2543
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002544 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002545#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002546 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002547 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002548#endif
2549 break;
2550
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002551 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002552 s->logs.level = rule->arg.loglevel;
2553 break;
2554
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002555 case ACT_HTTP_REPLACE_HDR:
2556 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002557 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
2558 rule->arg.hdr_add.name_len,
2559 &rule->arg.hdr_add.fmt,
2560 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06002561 return HTTP_RULE_RES_BADREQ;
2562 break;
2563
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002564 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01002565 ctx.idx = 0;
2566 /* remove all occurrences of the header */
2567 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2568 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
2569 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01002570 }
Willy Tarreau85603282015-01-21 20:39:27 +01002571 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002572
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002573 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002574 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02002575 /* The scope of the trash buffer must be limited to this function. The
2576 * build_logline() function can execute a lot of other function which
2577 * can use the trash buffer. So for limiting the scope of this global
2578 * buffer, we build first the header value using build_logline, and
2579 * after we store the header name.
2580 */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002581 struct chunk *replace;
2582
2583 replace = alloc_trash_chunk();
2584 if (!replace)
2585 return HTTP_RULE_RES_BADREQ;
2586
Thierry Fournier4b788f72016-06-01 13:35:36 +02002587 len = rule->arg.hdr_add.name_len + 2,
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002588 len += build_logline(s, replace->str + len, replace->size - len, &rule->arg.hdr_add.fmt);
2589 memcpy(replace->str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2590 replace->str[rule->arg.hdr_add.name_len] = ':';
2591 replace->str[rule->arg.hdr_add.name_len + 1] = ' ';
2592 replace->len = len;
Willy Tarreau85603282015-01-21 20:39:27 +01002593
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002594 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002595 /* remove all occurrences of the header */
2596 ctx.idx = 0;
2597 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2598 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
2599 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
2600 }
2601 }
2602
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002603 http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->str, replace->len);
2604
2605 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01002606 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002607 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002608
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002609 case ACT_HTTP_DEL_ACL:
2610 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002611 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002612 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002613
2614 /* collect reference */
2615 ref = pat_ref_lookup(rule->arg.map.ref);
2616 if (!ref)
2617 continue;
2618
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002619 /* allocate key */
2620 key = alloc_trash_chunk();
2621 if (!key)
2622 return HTTP_RULE_RES_BADREQ;
2623
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002624 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002625 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2626 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002627
2628 /* perform update */
2629 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002630 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002631 pat_ref_delete(ref, key->str);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002632 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002633
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002634 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002635 break;
2636 }
2637
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002638 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002639 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002640 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002641
2642 /* collect reference */
2643 ref = pat_ref_lookup(rule->arg.map.ref);
2644 if (!ref)
2645 continue;
2646
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002647 /* allocate key */
2648 key = alloc_trash_chunk();
2649 if (!key)
2650 return HTTP_RULE_RES_BADREQ;
2651
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002652 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002653 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2654 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002655
2656 /* perform update */
2657 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002658 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002659 if (pat_ref_find_elt(ref, key->str) == NULL)
2660 pat_ref_add(ref, key->str, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002661 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002662
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002663 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002664 break;
2665 }
2666
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002667 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002668 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002669 struct chunk *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002670
2671 /* collect reference */
2672 ref = pat_ref_lookup(rule->arg.map.ref);
2673 if (!ref)
2674 continue;
2675
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002676 /* allocate key */
2677 key = alloc_trash_chunk();
2678 if (!key)
2679 return HTTP_RULE_RES_BADREQ;
2680
2681 /* allocate value */
2682 value = alloc_trash_chunk();
2683 if (!value) {
2684 free_trash_chunk(key);
2685 return HTTP_RULE_RES_BADREQ;
2686 }
2687
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002688 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002689 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2690 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002691
2692 /* collect value */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002693 value->len = build_logline(s, value->str, value->size, &rule->arg.map.value);
2694 value->str[value->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002695
2696 /* perform update */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002697 if (pat_ref_find_elt(ref, key->str) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002698 /* update entry if it exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002699 pat_ref_set(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002700 else
2701 /* insert a new entry */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002702 pat_ref_add(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002703
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002704 free_trash_chunk(key);
2705 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002706 break;
2707 }
William Lallemand73025dd2014-04-24 14:38:37 +02002708
Thierry FOURNIER42148732015-09-02 17:17:33 +02002709 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02002710 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
2711 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002712
Willy Tarreauacc98002015-09-27 23:34:39 +02002713 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002714 case ACT_RET_ERR:
2715 case ACT_RET_CONT:
2716 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002717 case ACT_RET_STOP:
2718 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002719 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002720 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002721 return HTTP_RULE_RES_YIELD;
2722 }
William Lallemand73025dd2014-04-24 14:38:37 +02002723 break;
2724
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002725 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002726 /* Note: only the first valid tracking parameter of each
2727 * applies.
2728 */
2729
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002730 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002731 struct stktable *t;
2732 struct stksess *ts;
2733 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002734 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02002735
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002736 t = rule->arg.trk_ctr.table.t;
2737 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 +02002738
2739 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002740 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002741
2742 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002743 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2744 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2745 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002746 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02002747
Emeric Brun819fc6f2017-06-13 19:37:32 +02002748 if (ptr1)
2749 stktable_data_cast(ptr1, http_req_cnt)++;
2750
2751 if (ptr2)
2752 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2753 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2754
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002755 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002756 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002757
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002758 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002759 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002760 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002761 }
2762 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002763 break;
2764
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002765 /* other flags exists, but normaly, they never be matched. */
2766 default:
2767 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002768 }
2769 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002770
2771 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02002772 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002773}
2774
Willy Tarreau71241ab2012-12-27 11:30:54 +01002775
Willy Tarreau51d861a2015-05-22 17:30:48 +02002776/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2777 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2778 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2779 * is returned, the process can continue the evaluation of next rule list. If
2780 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2781 * is returned, it means the operation could not be processed and a server error
2782 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2783 * deny rule. If *YIELD is returned, the caller must call again the function
2784 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002785 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002786static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002787http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002788{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002789 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002790 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002791 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002792 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002793 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02002794 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002795
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002796 /* If "the current_rule_list" match the executed rule list, we are in
2797 * resume condition. If a resume is needed it is always in the action
2798 * and never in the ACL or converters. In this case, we initialise the
2799 * current rule, and go to the action execution point.
2800 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002801 if (s->current_rule) {
2802 rule = s->current_rule;
2803 s->current_rule = NULL;
2804 if (s->current_rule_list == rules)
2805 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002806 }
2807 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002808
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002809 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002810
2811 /* check optional condition */
2812 if (rule->cond) {
2813 int ret;
2814
Willy Tarreau192252e2015-04-04 01:47:55 +02002815 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002816 ret = acl_pass(ret);
2817
2818 if (rule->cond->pol == ACL_COND_UNLESS)
2819 ret = !ret;
2820
2821 if (!ret) /* condition not matched */
2822 continue;
2823 }
2824
Willy Tarreauacc98002015-09-27 23:34:39 +02002825 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002826resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002827 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002828 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002829 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002830
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002831 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002832 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002833 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002834
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002835 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002836 s->task->nice = rule->arg.nice;
2837 break;
2838
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002839 case ACT_HTTP_SET_TOS:
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 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002842 break;
2843
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002844 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002845#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002846 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002847 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002848#endif
2849 break;
2850
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002851 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002852 s->logs.level = rule->arg.loglevel;
2853 break;
2854
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002855 case ACT_HTTP_REPLACE_HDR:
2856 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002857 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2858 rule->arg.hdr_add.name_len,
2859 &rule->arg.hdr_add.fmt,
2860 &rule->arg.hdr_add.re, rule->action))
Christopher Fauletcdade942017-02-08 12:41:31 +01002861 return HTTP_RULE_RES_BADREQ;
Sasha Pachev218f0642014-06-16 12:05:59 -06002862 break;
2863
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002864 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002865 ctx.idx = 0;
2866 /* remove all occurrences of the header */
2867 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2868 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
2869 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2870 }
Willy Tarreau85603282015-01-21 20:39:27 +01002871 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002872
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002873 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002874 case ACT_HTTP_ADD_HDR: {
2875 struct chunk *replace;
2876
2877 replace = alloc_trash_chunk();
2878 if (!replace)
2879 return HTTP_RULE_RES_BADREQ;
2880
2881 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
2882 memcpy(replace->str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2883 replace->len = rule->arg.hdr_add.name_len;
2884 replace->str[replace->len++] = ':';
2885 replace->str[replace->len++] = ' ';
2886 replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len,
2887 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002888
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002889 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002890 /* remove all occurrences of the header */
2891 ctx.idx = 0;
2892 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2893 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
2894 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2895 }
2896 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002897 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->str, replace->len);
2898
2899 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002900 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002901 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002902
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002903 case ACT_HTTP_DEL_ACL:
2904 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002905 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002906 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002907
2908 /* collect reference */
2909 ref = pat_ref_lookup(rule->arg.map.ref);
2910 if (!ref)
2911 continue;
2912
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002913 /* allocate key */
2914 key = alloc_trash_chunk();
2915 if (!key)
2916 return HTTP_RULE_RES_BADREQ;
2917
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002918 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002919 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2920 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002921
2922 /* perform update */
2923 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002924 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002925 pat_ref_delete(ref, key->str);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002926 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002927
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002928 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002929 break;
2930 }
2931
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002932 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002933 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002934 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002935
2936 /* collect reference */
2937 ref = pat_ref_lookup(rule->arg.map.ref);
2938 if (!ref)
2939 continue;
2940
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002941 /* allocate key */
2942 key = alloc_trash_chunk();
2943 if (!key)
2944 return HTTP_RULE_RES_BADREQ;
2945
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002946 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002947 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2948 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002949
2950 /* perform update */
2951 /* check if the entry already exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002952 if (pat_ref_find_elt(ref, key->str) == NULL)
2953 pat_ref_add(ref, key->str, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002954
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002955 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002956 break;
2957 }
2958
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002959 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002960 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002961 struct chunk *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002962
2963 /* collect reference */
2964 ref = pat_ref_lookup(rule->arg.map.ref);
2965 if (!ref)
2966 continue;
2967
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002968 /* allocate key */
2969 key = alloc_trash_chunk();
2970 if (!key)
2971 return HTTP_RULE_RES_BADREQ;
2972
2973 /* allocate value */
2974 value = alloc_trash_chunk();
2975 if (!value) {
2976 free_trash_chunk(key);
2977 return HTTP_RULE_RES_BADREQ;
2978 }
2979
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002980 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002981 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2982 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002983
2984 /* collect value */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002985 value->len = build_logline(s, value->str, value->size, &rule->arg.map.value);
2986 value->str[value->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002987
2988 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002989 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002990 if (pat_ref_find_elt(ref, key->str) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002991 /* update entry if it exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002992 pat_ref_set(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002993 else
2994 /* insert a new entry */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002995 pat_ref_add(ref, key->str, value->str, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002996 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002997 free_trash_chunk(key);
2998 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002999 break;
3000 }
William Lallemand73025dd2014-04-24 14:38:37 +02003001
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003002 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003003 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3004 return HTTP_RULE_RES_BADREQ;
3005 return HTTP_RULE_RES_DONE;
3006
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003007 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3008 /* Note: only the first valid tracking parameter of each
3009 * applies.
3010 */
3011
Christopher Faulet4fce0d82017-09-18 11:57:31 +02003012 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003013 struct stktable *t;
3014 struct stksess *ts;
3015 struct stktable_key *key;
3016 void *ptr;
3017
3018 t = rule->arg.trk_ctr.table.t;
3019 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
3020
3021 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02003022 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003023
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003024 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003025
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003026 /* let's count a new HTTP request as it's the first time we do it */
3027 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3028 if (ptr)
3029 stktable_data_cast(ptr, http_req_cnt)++;
3030
3031 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3032 if (ptr)
3033 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3034 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3035
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003036 /* When the client triggers a 4xx from the server, it's most often due
3037 * to a missing object or permission. These events should be tracked
3038 * because if they happen often, it may indicate a brute force or a
3039 * vulnerability scan. Normally this is done when receiving the response
3040 * but here we're tracking after this ought to have been done so we have
3041 * to do it on purpose.
3042 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02003043 if ((unsigned)(txn->status - 400) < 100) {
3044 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3045 if (ptr)
3046 stktable_data_cast(ptr, http_err_cnt)++;
3047
3048 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3049 if (ptr)
3050 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3051 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3052 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02003053
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003054 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003055
3056 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3057 if (sess->fe != s->be)
3058 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3059
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003060 }
3061 }
3062 break;
3063
Thierry FOURNIER42148732015-09-02 17:17:33 +02003064 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003065 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3066 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003067
Willy Tarreauacc98002015-09-27 23:34:39 +02003068 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003069 case ACT_RET_ERR:
3070 case ACT_RET_CONT:
3071 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003072 case ACT_RET_STOP:
3073 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003074 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003075 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003076 return HTTP_RULE_RES_YIELD;
3077 }
William Lallemand73025dd2014-04-24 14:38:37 +02003078 break;
3079
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003080 /* other flags exists, but normaly, they never be matched. */
3081 default:
3082 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003083 }
3084 }
3085
3086 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003087 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003088}
3089
3090
Willy Tarreau71241ab2012-12-27 11:30:54 +01003091/* Perform an HTTP redirect based on the information in <rule>. The function
3092 * returns non-zero on success, or zero in case of a, irrecoverable error such
3093 * as too large a request to build a valid response.
3094 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003095static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003096{
Willy Tarreaub329a312015-05-22 16:27:37 +02003097 struct http_msg *req = &txn->req;
3098 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003099 const char *msg_fmt;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003100 struct chunk *chunk;
3101 int ret = 0;
3102
3103 chunk = alloc_trash_chunk();
3104 if (!chunk)
3105 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003106
3107 /* build redirect message */
3108 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003109 case 308:
3110 msg_fmt = HTTP_308;
3111 break;
3112 case 307:
3113 msg_fmt = HTTP_307;
3114 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003115 case 303:
3116 msg_fmt = HTTP_303;
3117 break;
3118 case 301:
3119 msg_fmt = HTTP_301;
3120 break;
3121 case 302:
3122 default:
3123 msg_fmt = HTTP_302;
3124 break;
3125 }
3126
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003127 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
3128 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003129
3130 switch(rule->type) {
3131 case REDIRECT_TYPE_SCHEME: {
3132 const char *path;
3133 const char *host;
3134 struct hdr_ctx ctx;
3135 int pathlen;
3136 int hostlen;
3137
3138 host = "";
3139 hostlen = 0;
3140 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02003141 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003142 host = ctx.line + ctx.val;
3143 hostlen = ctx.vlen;
3144 }
3145
3146 path = http_get_path(txn);
3147 /* build message using path */
3148 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003149 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003150 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3151 int qs = 0;
3152 while (qs < pathlen) {
3153 if (path[qs] == '?') {
3154 pathlen = qs;
3155 break;
3156 }
3157 qs++;
3158 }
3159 }
3160 } else {
3161 path = "/";
3162 pathlen = 1;
3163 }
3164
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003165 if (rule->rdr_str) { /* this is an old "redirect" rule */
3166 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003167 if (chunk->len + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
3168 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003169
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003170 /* add scheme */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003171 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3172 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003173 }
3174 else {
3175 /* add scheme with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003176 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003177
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003178 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003179 if (chunk->len + 3 + hostlen + pathlen > chunk->size - 4)
3180 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003181 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003182 /* add "://" */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003183 memcpy(chunk->str + chunk->len, "://", 3);
3184 chunk->len += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003185
3186 /* add host */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003187 memcpy(chunk->str + chunk->len, host, hostlen);
3188 chunk->len += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003189
3190 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003191 memcpy(chunk->str + chunk->len, path, pathlen);
3192 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003193
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003194 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003195 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003196 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003197 if (chunk->len > chunk->size - 5)
3198 goto leave;
3199 chunk->str[chunk->len] = '/';
3200 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003201 }
3202
3203 break;
3204 }
3205 case REDIRECT_TYPE_PREFIX: {
3206 const char *path;
3207 int pathlen;
3208
3209 path = http_get_path(txn);
3210 /* build message using path */
3211 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003212 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003213 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3214 int qs = 0;
3215 while (qs < pathlen) {
3216 if (path[qs] == '?') {
3217 pathlen = qs;
3218 break;
3219 }
3220 qs++;
3221 }
3222 }
3223 } else {
3224 path = "/";
3225 pathlen = 1;
3226 }
3227
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003228 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003229 if (chunk->len + rule->rdr_len + pathlen > chunk->size - 4)
3230 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003231
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003232 /* add prefix. Note that if prefix == "/", we don't want to
3233 * add anything, otherwise it makes it hard for the user to
3234 * configure a self-redirection.
3235 */
3236 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003237 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3238 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003239 }
3240 }
3241 else {
3242 /* add prefix with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003243 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003244
3245 /* Check length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003246 if (chunk->len + pathlen > chunk->size - 4)
3247 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003248 }
3249
3250 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003251 memcpy(chunk->str + chunk->len, path, pathlen);
3252 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003253
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003254 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003255 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003256 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003257 if (chunk->len > chunk->size - 5)
3258 goto leave;
3259 chunk->str[chunk->len] = '/';
3260 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003261 }
3262
3263 break;
3264 }
3265 case REDIRECT_TYPE_LOCATION:
3266 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003267 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003268 if (chunk->len + rule->rdr_len > chunk->size - 4)
3269 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003270
3271 /* add location */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003272 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3273 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003274 }
3275 else {
3276 /* add location with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003277 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003278
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003279 /* Check left length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003280 if (chunk->len > chunk->size - 4)
3281 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003282 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003283 break;
3284 }
3285
3286 if (rule->cookie_len) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003287 memcpy(chunk->str + chunk->len, "\r\nSet-Cookie: ", 14);
3288 chunk->len += 14;
3289 memcpy(chunk->str + chunk->len, rule->cookie_str, rule->cookie_len);
3290 chunk->len += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003291 }
3292
Willy Tarreau19b14122017-02-28 09:48:11 +01003293 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003294 txn->status = rule->code;
3295 /* let's log the request time */
3296 s->logs.tv_request = now;
3297
Christopher Fauletbe821b92017-03-30 11:21:53 +02003298 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003299 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3300 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3301 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02003302 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003303 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003304 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: keep-alive", 30);
3305 chunk->len += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003306 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003307 memcpy(chunk->str + chunk->len, "\r\nConnection: keep-alive", 24);
3308 chunk->len += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003309 }
3310 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003311 memcpy(chunk->str + chunk->len, "\r\n\r\n", 4);
3312 chunk->len += 4;
3313 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau06d80a92017-10-19 14:32:15 +02003314 co_inject(res->chn, chunk->str, chunk->len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003315 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02003316 bi_fast_delete(req->chn->buf, req->sov);
3317 req->next -= req->sov;
3318 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003319 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01003320 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02003321 req->msg_state = HTTP_MSG_CLOSED;
3322 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02003323 /* Trim any possible response */
3324 res->chn->buf->i = 0;
3325 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02003326 /* let the server side turn to SI_ST_CLO */
3327 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003328 } else {
3329 /* keep-alive not possible */
3330 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003331 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3332 chunk->len += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003333 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003334 memcpy(chunk->str + chunk->len, "\r\nConnection: close\r\n\r\n", 23);
3335 chunk->len += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003336 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003337 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01003338 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003339 }
3340
Willy Tarreaue7dff022015-04-03 01:14:29 +02003341 if (!(s->flags & SF_ERR_MASK))
3342 s->flags |= SF_ERR_LOCAL;
3343 if (!(s->flags & SF_FINST_MASK))
3344 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003345
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003346 ret = 1;
3347 leave:
3348 free_trash_chunk(chunk);
3349 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003350}
3351
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003352/* This stream analyser runs all HTTP request processing which is common to
3353 * frontends and backends, which means blocking ACLs, filters, connection-close,
3354 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003355 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003356 * either needs more data or wants to immediately abort the request (eg: deny,
3357 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003358 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003359int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003360{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003361 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003362 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02003363 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003364 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003365 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02003366 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003367 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02003368 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02003369
Willy Tarreau655dce92009-11-08 13:10:58 +01003370 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003371 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003372 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02003373 }
3374
Willy Tarreau87b09662015-04-03 00:22:06 +02003375 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 +02003376 now_ms, __FUNCTION__,
3377 s,
3378 req,
3379 req->rex, req->wex,
3380 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003381 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003382 req->analysers);
3383
Willy Tarreau65410832014-04-28 21:25:43 +02003384 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02003385 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02003386
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003387 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02003388 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02003389 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01003390
Willy Tarreau0b748332014-04-29 00:13:29 +02003391 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003392 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
3393 goto return_prx_yield;
3394
Willy Tarreau0b748332014-04-29 00:13:29 +02003395 case HTTP_RULE_RES_CONT:
3396 case HTTP_RULE_RES_STOP: /* nothing to do */
3397 break;
Willy Tarreau52542592014-04-28 18:33:26 +02003398
Willy Tarreau0b748332014-04-29 00:13:29 +02003399 case HTTP_RULE_RES_DENY: /* deny or tarpit */
3400 if (txn->flags & TX_CLTARPIT)
3401 goto tarpit;
3402 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003403
Willy Tarreau0b748332014-04-29 00:13:29 +02003404 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
3405 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02003406
Willy Tarreau0b748332014-04-29 00:13:29 +02003407 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02003408 goto done;
3409
Willy Tarreau0b748332014-04-29 00:13:29 +02003410 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
3411 goto return_bad_req;
3412 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003413 }
3414
Olivier Houchardc2aae742017-09-22 18:26:28 +02003415 if (conn && conn->flags & CO_FL_EARLY_DATA) {
3416 struct hdr_ctx ctx;
3417
3418 ctx.idx = 0;
3419 if (!http_find_header2("Early-Data", strlen("Early-Data"),
3420 s->req.buf->p, &txn->hdr_idx, &ctx)) {
3421 if (unlikely(http_header_add_tail2(&txn->req,
3422 &txn->hdr_idx, "Early-Data: 1",
3423 strlen("Early-Data: 1"))) < 0) {
3424 goto return_bad_req;
3425 }
3426 }
3427
3428 }
3429
Willy Tarreau52542592014-04-28 18:33:26 +02003430 /* OK at this stage, we know that the request was accepted according to
3431 * the http-request rules, we can check for the stats. Note that the
3432 * URI is detected *before* the req* rules in order not to be affected
3433 * by a possible reqrep, while they are processed *after* so that a
3434 * reqdeny can still block them. This clearly needs to change in 1.6!
3435 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003436 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02003437 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01003438 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02003439 txn->status = 500;
3440 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003441 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003442
Willy Tarreaue7dff022015-04-03 01:14:29 +02003443 if (!(s->flags & SF_ERR_MASK))
3444 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02003445 goto return_prx_cond;
3446 }
3447
3448 /* parse the whole stats request and extract the relevant information */
3449 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02003450 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02003451 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003452
Willy Tarreau0b748332014-04-29 00:13:29 +02003453 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
3454 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003455
Willy Tarreau0b748332014-04-29 00:13:29 +02003456 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
3457 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003458 }
3459
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003460 /* evaluate the req* rules except reqadd */
3461 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003462 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003463 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003464
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003465 if (txn->flags & TX_CLDENY)
3466 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003467
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003468 if (txn->flags & TX_CLTARPIT) {
3469 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003470 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003471 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003472 }
Willy Tarreau06619262006-12-17 08:37:22 +01003473
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003474 /* add request headers from the rule sets in the same order */
3475 list_for_each_entry(wl, &px->req_add, list) {
3476 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003477 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003478 ret = acl_pass(ret);
3479 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3480 ret = !ret;
3481 if (!ret)
3482 continue;
3483 }
3484
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003485 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003486 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003487 }
3488
Willy Tarreau52542592014-04-28 18:33:26 +02003489
3490 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003491 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003492 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003493 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003494 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01003495
Willy Tarreaue7dff022015-04-03 01:14:29 +02003496 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
3497 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
3498 if (!(s->flags & SF_FINST_MASK))
3499 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003500
Willy Tarreau70730dd2014-04-24 18:06:27 +02003501 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01003502 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
3503 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003504 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003505 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003506 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003507
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003508 /* check whether we have some ACLs set to redirect this request */
3509 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003510 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003511 int ret;
3512
Willy Tarreau192252e2015-04-04 01:47:55 +02003513 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003514 ret = acl_pass(ret);
3515 if (rule->cond->pol == ACL_COND_UNLESS)
3516 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003517 if (!ret)
3518 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003519 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003520 if (!http_apply_redirect_rule(rule, s, txn))
3521 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003522 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003523 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003524
Willy Tarreau2be39392010-01-03 17:24:51 +01003525 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3526 * If this happens, then the data will not come immediately, so we must
3527 * send all what we have without waiting. Note that due to the small gain
3528 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003529 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003530 * itself once used.
3531 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003532 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003533
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003534 done: /* done with this analyser, continue with next ones that the calling
3535 * points will have set, if any.
3536 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003537 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003538 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
3539 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003540 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003541
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003542 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02003543 /* Allow cookie logging
3544 */
3545 if (s->be->cookie_name || sess->fe->capture_name)
3546 manage_client_side_cookies(s, req);
3547
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003548 /* When a connection is tarpitted, we use the tarpit timeout,
3549 * which may be the same as the connect timeout if unspecified.
3550 * If unset, then set it to zero because we really want it to
3551 * eventually expire. We build the tarpit as an analyser.
3552 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003553 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003554
3555 /* wipe the request out so that we can drop the connection early
3556 * if the client closes first.
3557 */
3558 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003559
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003560 txn->status = http_err_codes[deny_status];
3561
Christopher Faulet0184ea72017-01-05 14:06:34 +01003562 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003563 req->analysers |= AN_REQ_HTTP_TARPIT;
3564 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3565 if (!req->analyse_exp)
3566 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02003567 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003568 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003569 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003570 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003571 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003572 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003573 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003574
3575 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003576
3577 /* Allow cookie logging
3578 */
3579 if (s->be->cookie_name || sess->fe->capture_name)
3580 manage_client_side_cookies(s, req);
3581
Willy Tarreau0b748332014-04-29 00:13:29 +02003582 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003583 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003584 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003585 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02003586 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003587 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003588 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003589 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003590 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003591 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003592 goto return_prx_cond;
3593
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003594 return_bad_req:
3595 /* We centralize bad requests processing here */
3596 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3597 /* we detected a parsing error. We want to archive this request
3598 * in the dedicated proxy area for later troubleshooting.
3599 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02003600 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003601 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003602
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003603 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003604 txn->req.msg_state = HTTP_MSG_ERROR;
3605 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003606 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003607
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003608 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003609 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003610 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003611
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003612 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003613 if (!(s->flags & SF_ERR_MASK))
3614 s->flags |= SF_ERR_PRXCOND;
3615 if (!(s->flags & SF_FINST_MASK))
3616 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003617
Christopher Faulet0184ea72017-01-05 14:06:34 +01003618 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003619 req->analyse_exp = TICK_ETERNITY;
3620 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003621
3622 return_prx_yield:
3623 channel_dont_connect(req);
3624 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003625}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003626
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003627/* This function performs all the processing enabled for the current request.
3628 * It returns 1 if the processing can continue on next analysers, or zero if it
3629 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003630 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003631 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003632int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003633{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003634 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003635 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003636 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003637 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003638
Willy Tarreau655dce92009-11-08 13:10:58 +01003639 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003640 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003641 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003642 return 0;
3643 }
3644
Willy Tarreau87b09662015-04-03 00:22:06 +02003645 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 +02003646 now_ms, __FUNCTION__,
3647 s,
3648 req,
3649 req->rex, req->wex,
3650 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003651 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003652 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003653
Willy Tarreau59234e92008-11-30 23:51:27 +01003654 /*
3655 * Right now, we know that we have processed the entire headers
3656 * and that unwanted requests have been filtered out. We can do
3657 * whatever we want with the remaining request. Also, now we
3658 * may have separate values for ->fe, ->be.
3659 */
Willy Tarreau06619262006-12-17 08:37:22 +01003660
Willy Tarreau59234e92008-11-30 23:51:27 +01003661 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003662 * If HTTP PROXY is set we simply get remote server address parsing
3663 * incoming request. Note that this requires that a connection is
3664 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003665 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003666 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003667 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003668 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003669
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003670 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003671 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003672 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003673 txn->req.msg_state = HTTP_MSG_ERROR;
3674 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003675 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003676 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003677
Willy Tarreaue7dff022015-04-03 01:14:29 +02003678 if (!(s->flags & SF_ERR_MASK))
3679 s->flags |= SF_ERR_RESOURCE;
3680 if (!(s->flags & SF_FINST_MASK))
3681 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003682
3683 return 0;
3684 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003685
3686 path = http_get_path(txn);
3687 url2sa(req->buf->p + msg->sl.rq.u,
3688 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01003689 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003690 /* if the path was found, we have to remove everything between
3691 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
3692 * found, we need to replace from req->buf->p + msg->sl.rq.u for
3693 * u_l characters by a single "/".
3694 */
3695 if (path) {
3696 char *cur_ptr = req->buf->p;
3697 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3698 int delta;
3699
3700 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
3701 http_msg_move_end(&txn->req, delta);
3702 cur_end += delta;
3703 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3704 goto return_bad_req;
3705 }
3706 else {
3707 char *cur_ptr = req->buf->p;
3708 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3709 int delta;
3710
3711 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
3712 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
3713 http_msg_move_end(&txn->req, delta);
3714 cur_end += delta;
3715 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3716 goto return_bad_req;
3717 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003718 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003719
Willy Tarreau59234e92008-11-30 23:51:27 +01003720 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003721 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003722 * Note that doing so might move headers in the request, but
3723 * the fields will stay coherent and the URI will not move.
3724 * This should only be performed in the backend.
3725 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003726 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003727 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003728
William Lallemanda73203e2012-03-12 12:48:57 +01003729 /* add unique-id if "header-unique-id" is specified */
3730
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003731 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003732 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
3733 goto return_bad_req;
3734 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003735 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003736 }
William Lallemanda73203e2012-03-12 12:48:57 +01003737
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003738 if (sess->fe->header_unique_id && s->unique_id) {
3739 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003740 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003741 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003742 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003743 goto return_bad_req;
3744 }
3745
Cyril Bontéb21570a2009-11-29 20:04:48 +01003746 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003747 * 9: add X-Forwarded-For if either the frontend or the backend
3748 * asks for it.
3749 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003750 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003751 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003752 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3753 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3754 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003755 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003756 /* The header is set to be added only if none is present
3757 * and we found it, so don't do anything.
3758 */
3759 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003760 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003761 /* Add an X-Forwarded-For header unless the source IP is
3762 * in the 'except' network range.
3763 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003764 if ((!sess->fe->except_mask.s_addr ||
3765 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3766 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003767 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003768 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003769 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003770 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003771 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003772 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003773
3774 /* Note: we rely on the backend to get the header name to be used for
3775 * x-forwarded-for, because the header is really meant for the backends.
3776 * However, if the backend did not specify any option, we have to rely
3777 * on the frontend's header name.
3778 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003779 if (s->be->fwdfor_hdr_len) {
3780 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003781 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003782 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003783 len = sess->fe->fwdfor_hdr_len;
3784 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003785 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003786 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 +01003787
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003788 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003789 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003790 }
3791 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003792 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003793 /* FIXME: for the sake of completeness, we should also support
3794 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003795 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003796 int len;
3797 char pn[INET6_ADDRSTRLEN];
3798 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003799 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003800 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003801
Willy Tarreau59234e92008-11-30 23:51:27 +01003802 /* Note: we rely on the backend to get the header name to be used for
3803 * x-forwarded-for, because the header is really meant for the backends.
3804 * However, if the backend did not specify any option, we have to rely
3805 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003806 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003807 if (s->be->fwdfor_hdr_len) {
3808 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003809 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003810 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003811 len = sess->fe->fwdfor_hdr_len;
3812 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003813 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003814 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003815
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003816 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003817 goto return_bad_req;
3818 }
3819 }
3820
3821 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003822 * 10: add X-Original-To if either the frontend or the backend
3823 * asks for it.
3824 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003825 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003826
3827 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003828 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003829 /* Add an X-Original-To header unless the destination IP is
3830 * in the 'except' network range.
3831 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003832 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003833
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003834 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003835 ((!sess->fe->except_mask_to.s_addr ||
3836 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3837 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003838 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003839 (((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 +02003840 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003841 int len;
3842 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003843 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003844
3845 /* Note: we rely on the backend to get the header name to be used for
3846 * x-original-to, because the header is really meant for the backends.
3847 * However, if the backend did not specify any option, we have to rely
3848 * on the frontend's header name.
3849 */
3850 if (s->be->orgto_hdr_len) {
3851 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003852 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003853 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003854 len = sess->fe->orgto_hdr_len;
3855 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003856 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003857 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 +02003858
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003859 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003860 goto return_bad_req;
3861 }
3862 }
3863 }
3864
Willy Tarreau50fc7772012-11-11 22:19:57 +01003865 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3866 * If an "Upgrade" token is found, the header is left untouched in order not to have
3867 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3868 * "Upgrade" is present in the Connection header.
3869 */
3870 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3871 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
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 Tarreaubbf0b372010-01-18 16:54:40 +01003874 unsigned int want_flags = 0;
3875
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003876 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003877 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
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_CLO_SET;
3882 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003883 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003884 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003885 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003886 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003887 want_flags |= TX_CON_KAL_SET;
3888 }
3889
3890 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003891 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003892 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003893
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003894
Willy Tarreau522d6c02009-12-06 18:49:18 +01003895 /* If we have no server assigned yet and we're balancing on url_param
3896 * with a POST request, we may be interested in checking the body for
3897 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003898 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003899 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003900 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003901 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003902 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003903 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003904 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003905
Christopher Fauletbe821b92017-03-30 11:21:53 +02003906 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3907 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003908#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003909 /* We expect some data from the client. Unless we know for sure
3910 * we already have a full request, we have to re-enable quick-ack
3911 * in case we previously disabled it, otherwise we might cause
3912 * the client to delay further data.
3913 */
3914 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3915 cli_conn && conn_ctrl_ready(cli_conn) &&
3916 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
3917 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003918 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003919#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003920
Willy Tarreau59234e92008-11-30 23:51:27 +01003921 /*************************************************************
3922 * OK, that's finished for the headers. We have done what we *
3923 * could. Let's switch to the DATA state. *
3924 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003925 req->analyse_exp = TICK_ETERNITY;
3926 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003927
Willy Tarreau59234e92008-11-30 23:51:27 +01003928 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003929 /* OK let's go on with the BODY now */
3930 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003931
Willy Tarreau59234e92008-11-30 23:51:27 +01003932 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003933 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003934 /* we detected a parsing error. We want to archive this request
3935 * in the dedicated proxy area for later troubleshooting.
3936 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02003937 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003938 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003939
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003940 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003941 txn->req.msg_state = HTTP_MSG_ERROR;
3942 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003943 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003944 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003945
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003946 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003947 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003948 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003949
Willy Tarreaue7dff022015-04-03 01:14:29 +02003950 if (!(s->flags & SF_ERR_MASK))
3951 s->flags |= SF_ERR_PRXCOND;
3952 if (!(s->flags & SF_FINST_MASK))
3953 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003954 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003955}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003956
Willy Tarreau60b85b02008-11-30 23:28:40 +01003957/* This function is an analyser which processes the HTTP tarpit. It always
3958 * returns zero, at the beginning because it prevents any other processing
3959 * from occurring, and at the end because it terminates the request.
3960 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003961int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003962{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003963 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003964
3965 /* This connection is being tarpitted. The CLIENT side has
3966 * already set the connect expiration date to the right
3967 * timeout. We just have to check that the client is still
3968 * there and that the timeout has not expired.
3969 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003970 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003971 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003972 !tick_is_expired(req->analyse_exp, now_ms))
3973 return 0;
3974
3975 /* We will set the queue timer to the time spent, just for
3976 * logging purposes. We fake a 500 server error, so that the
3977 * attacker will not suspect his connection has been tarpitted.
3978 * It will not cause trouble to the logs because we can exclude
3979 * the tarpitted connections by filtering on the 'PT' status flags.
3980 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003981 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3982
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003983 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003984 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003985
Christopher Faulet0184ea72017-01-05 14:06:34 +01003986 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003987 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003988
Willy Tarreaue7dff022015-04-03 01:14:29 +02003989 if (!(s->flags & SF_ERR_MASK))
3990 s->flags |= SF_ERR_PRXCOND;
3991 if (!(s->flags & SF_FINST_MASK))
3992 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003993 return 0;
3994}
3995
Willy Tarreau5a8f9472014-04-10 11:16:06 +02003996/* This function is an analyser which waits for the HTTP request body. It waits
3997 * for either the buffer to be full, or the full advertised contents to have
3998 * reached the buffer. It must only be called after the standard HTTP request
3999 * processing has occurred, because it expects the request to be parsed and will
4000 * look for the Expect header. It may send a 100-Continue interim response. It
4001 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4002 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4003 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004004 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004005int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004006{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004007 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004008 struct http_txn *txn = s->txn;
4009 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004010
4011 /* We have to parse the HTTP request body to find any required data.
4012 * "balance url_param check_post" should have been the only way to get
4013 * into this. We were brought here after HTTP header analysis, so all
4014 * related structures are ready.
4015 */
4016
Willy Tarreau890988f2014-04-10 11:59:33 +02004017 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4018 /* This is the first call */
4019 if (msg->msg_state < HTTP_MSG_BODY)
4020 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004021
Willy Tarreau890988f2014-04-10 11:59:33 +02004022 if (msg->msg_state < HTTP_MSG_100_SENT) {
4023 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4024 * send an HTTP/1.1 100 Continue intermediate response.
4025 */
4026 if (msg->flags & HTTP_MSGF_VER_11) {
4027 struct hdr_ctx ctx;
4028 ctx.idx = 0;
4029 /* Expect is allowed in 1.1, look for it */
4030 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4031 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02004032 co_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01004033 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02004034 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004035 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004036 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004037 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004038
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004039 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004040 * req->buf->p still points to the beginning of the message. We
4041 * must save the body in msg->next because it survives buffer
4042 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004043 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004044 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004045
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004046 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004047 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4048 else
4049 msg->msg_state = HTTP_MSG_DATA;
4050 }
4051
Willy Tarreau890988f2014-04-10 11:59:33 +02004052 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4053 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004054 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004055 goto missing_data;
4056
4057 /* OK we have everything we need now */
4058 goto http_end;
4059 }
4060
4061 /* OK here we're parsing a chunked-encoded message */
4062
Willy Tarreau522d6c02009-12-06 18:49:18 +01004063 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004064 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004065 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004066 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004067 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004068 unsigned int chunk;
4069 int ret = h1_parse_chunk_size(req->buf, msg->next, req->buf->i, &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01004070
Willy Tarreau115acb92009-12-26 13:56:06 +01004071 if (!ret)
4072 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004073 else if (ret < 0) {
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004074 msg->err_pos = req->buf->i + ret;
4075 if (msg->err_pos < 0)
4076 msg->err_pos += req->buf->size;
Willy Tarreau87b09662015-04-03 00:22:06 +02004077 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004078 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004079 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004080
4081 msg->chunk_len = chunk;
4082 msg->body_len += chunk;
4083
4084 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01004085 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004086 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01004087 }
4088
Willy Tarreaud98cf932009-12-27 22:54:55 +01004089 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004090 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4091 * for at least a whole chunk or the whole content length bytes after
4092 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004093 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004094 if (msg->msg_state == HTTP_MSG_TRAILERS)
4095 goto http_end;
4096
Willy Tarreaue115b492015-05-01 23:05:14 +02004097 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004098 goto http_end;
4099
4100 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004101 /* we get here if we need to wait for more data. If the buffer is full,
4102 * we have the maximum we can expect.
4103 */
4104 if (buffer_full(req->buf, global.tune.maxrewrite))
4105 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004106
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004107 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004108 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004109 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004110
Willy Tarreaue7dff022015-04-03 01:14:29 +02004111 if (!(s->flags & SF_ERR_MASK))
4112 s->flags |= SF_ERR_CLITO;
4113 if (!(s->flags & SF_FINST_MASK))
4114 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004115 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004116 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004117
4118 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004119 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004120 /* Not enough data. We'll re-use the http-request
4121 * timeout here. Ideally, we should set the timeout
4122 * relative to the accept() date. We just set the
4123 * request timeout once at the beginning of the
4124 * request.
4125 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004126 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004127 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004128 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004129 return 0;
4130 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004131
4132 http_end:
4133 /* The situation will not evolve, so let's give up on the analysis. */
4134 s->logs.tv_request = now; /* update the request timer to reflect full request */
4135 req->analysers &= ~an_bit;
4136 req->analyse_exp = TICK_ETERNITY;
4137 return 1;
4138
4139 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004140 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004141 txn->req.msg_state = HTTP_MSG_ERROR;
4142 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004143 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004144
Willy Tarreaue7dff022015-04-03 01:14:29 +02004145 if (!(s->flags & SF_ERR_MASK))
4146 s->flags |= SF_ERR_PRXCOND;
4147 if (!(s->flags & SF_FINST_MASK))
4148 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004149
Willy Tarreau522d6c02009-12-06 18:49:18 +01004150 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01004151 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004152 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004153 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004154 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004155 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004156}
4157
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004158/* send a server's name with an outgoing request over an established connection.
4159 * Note: this function is designed to be called once the request has been scheduled
4160 * for being forwarded. This is the reason why it rewinds the buffer before
4161 * proceeding.
4162 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004163int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004164
4165 struct hdr_ctx ctx;
4166
Mark Lamourinec2247f02012-01-04 13:02:01 -05004167 char *hdr_name = be->server_id_hdr_name;
4168 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004169 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004170 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004171 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004172
William Lallemandd9e90662012-01-30 17:27:17 +01004173 ctx.idx = 0;
4174
Willy Tarreau211cdec2014-04-17 20:18:08 +02004175 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004176 if (old_o) {
4177 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004178 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004179 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004180 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004181 }
4182
Willy Tarreau9b28e032012-10-12 23:49:43 +02004183 old_i = chn->buf->i;
4184 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 -05004185 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004186 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004187 }
4188
4189 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004190 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004191 memcpy(hdr_val, hdr_name, hdr_name_len);
4192 hdr_val += hdr_name_len;
4193 *hdr_val++ = ':';
4194 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004195 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4196 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004197
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004198 if (old_o) {
4199 /* If this was a forwarded request, we must readjust the amount of
4200 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004201 * variations. Note that the current state is >= HTTP_MSG_BODY,
4202 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004203 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004204 old_o += chn->buf->i - old_i;
4205 b_adv(chn->buf, old_o);
4206 txn->req.next -= old_o;
4207 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004208 }
4209
Mark Lamourinec2247f02012-01-04 13:02:01 -05004210 return 0;
4211}
4212
Willy Tarreau610ecce2010-01-04 21:15:02 +01004213/* Terminate current transaction and prepare a new one. This is very tricky
4214 * right now but it works.
4215 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004216void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004217{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004218 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02004219 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01004220 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004221 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02004222 struct connection *srv_conn;
4223 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02004224 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01004225
Willy Tarreau610ecce2010-01-04 21:15:02 +01004226 /* FIXME: We need a more portable way of releasing a backend's and a
4227 * server's connections. We need a safer way to reinitialize buffer
4228 * flags. We also need a more accurate method for computing per-request
4229 * data.
4230 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004231 /*
4232 * XXX cognet: This is probably wrong, this is killing a whole
4233 * connection, in the new world order, we probably want to just kill
4234 * the stream, this is to be revisited the day we handle multiple
4235 * streams in one server connection.
4236 */
4237 cs = objt_cs(s->si[1].end);
4238 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004239
Willy Tarreau4213a112013-12-15 10:25:42 +01004240 /* unless we're doing keep-alive, we want to quickly close the connection
4241 * to the server.
4242 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004243 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004244 !si_conn_ready(&s->si[1])) {
4245 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4246 si_shutr(&s->si[1]);
4247 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004248 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004249
Willy Tarreaue7dff022015-04-03 01:14:29 +02004250 if (s->flags & SF_BE_ASSIGNED) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004251 HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004252 if (unlikely(s->srv_conn))
4253 sess_change_server(s, NULL);
4254 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004255
4256 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02004257 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004258
Willy Tarreaueee5b512015-04-03 23:46:31 +02004259 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004260 int n;
4261
Willy Tarreaueee5b512015-04-03 23:46:31 +02004262 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004263 if (n < 1 || n > 5)
4264 n = 0;
4265
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004266 if (fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004267 HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004268 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02004269 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01004270 (be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004271 HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
4272 HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004273 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004274 }
4275
4276 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004277 s->logs.bytes_in -= s->req.buf->i;
4278 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004279
4280 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004281 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02004282 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004283 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004284 s->do_log(s);
4285 }
4286
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004287 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02004288 stream_stop_content_counters(s);
4289 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004290
Willy Tarreau610ecce2010-01-04 21:15:02 +01004291 s->logs.accept_date = date; /* user-visible date for logging */
4292 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02004293 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
4294 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004295 tv_zero(&s->logs.tv_request);
4296 s->logs.t_queue = -1;
4297 s->logs.t_connect = -1;
4298 s->logs.t_data = -1;
4299 s->logs.t_close = 0;
4300 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4301 s->logs.srv_queue_size = 0; /* we will get this number soon */
4302
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004303 s->logs.bytes_in = s->req.total = s->req.buf->i;
4304 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004305
4306 if (s->pend_pos)
4307 pendconn_free(s->pend_pos);
4308
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004309 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004310 if (s->flags & SF_CURR_SESS) {
4311 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +02004312 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004313 }
Willy Tarreau858b1032015-12-07 17:04:59 +01004314 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004315 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004316 }
4317
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004318 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004319
Willy Tarreau4213a112013-12-15 10:25:42 +01004320 /* only release our endpoint if we don't intend to reuse the
4321 * connection.
4322 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004323 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004324 !si_conn_ready(&s->si[1])) {
4325 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02004326 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004327 }
4328
Willy Tarreau350f4872014-11-28 14:42:25 +01004329 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
4330 s->si[1].err_type = SI_ET_NONE;
4331 s->si[1].conn_retries = 0; /* used for logging too */
4332 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02004333 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 +01004334 s->req.flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT|CF_WAKE_CONNECT|CF_WROTE_DATA);
Christopher Fauletc5a9d5b2017-11-09 09:36:43 +01004335 s->res.flags &= ~(CF_SHUTR|CF_SHUTR_NOW|CF_READ_ATTACHED|CF_READ_ERROR|CF_READ_NOEXP|CF_STREAMER|CF_STREAMER_FAST|CF_WRITE_PARTIAL|CF_NEVER_WAIT|CF_WROTE_DATA|CF_WRITE_EVENT);
Willy Tarreaue7dff022015-04-03 01:14:29 +02004336 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
4337 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
4338 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01004339
Willy Tarreaueee5b512015-04-03 23:46:31 +02004340 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004341 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004342 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004343
4344 if (prev_status == 401 || prev_status == 407) {
4345 /* In HTTP keep-alive mode, if we receive a 401, we still have
4346 * a chance of being able to send the visitor again to the same
4347 * server over the same connection. This is required by some
4348 * broken protocols such as NTLM, and anyway whenever there is
4349 * an opportunity for sending the challenge to the proper place,
4350 * it's better to do it (at least it helps with debugging).
4351 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004352 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02004353 if (srv_conn)
4354 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01004355 }
4356
Willy Tarreau53f96852016-02-02 18:50:47 +01004357 /* Never ever allow to reuse a connection from a non-reuse backend */
4358 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
4359 srv_conn->flags |= CO_FL_PRIVATE;
4360
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004361 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01004362 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004363
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004364 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004365 s->req.flags |= CF_NEVER_WAIT;
4366 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004367 }
4368
Willy Tarreau714ea782015-11-25 20:11:11 +01004369 /* we're removing the analysers, we MUST re-enable events detection.
4370 * We don't enable close on the response channel since it's either
4371 * already closed, or in keep-alive with an idle connection handler.
4372 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004373 channel_auto_read(&s->req);
4374 channel_auto_close(&s->req);
4375 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004376
Willy Tarreau1c59bd52015-11-02 20:20:11 +01004377 /* we're in keep-alive with an idle connection, monitor it if not already done */
4378 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02004379 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02004380 if (!srv)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004381 si_idle_cs(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01004382 else if (srv_conn->flags & CO_FL_PRIVATE)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004383 si_idle_cs(&s->si[1], (srv->priv_conns ? &srv->priv_conns[tid] : NULL));
Willy Tarreau449d74a2015-08-05 17:16:33 +02004384 else if (prev_flags & TX_NOT_FIRST)
4385 /* note: we check the request, not the connection, but
4386 * this is valid for strategies SAFE and AGGR, and in
4387 * case of ALWS, we don't care anyway.
4388 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004389 si_idle_cs(&s->si[1], (srv->safe_conns ? &srv->safe_conns[tid] : NULL));
Willy Tarreau8dff9982015-08-04 20:45:52 +02004390 else
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004391 si_idle_cs(&s->si[1], (srv->idle_conns ? &srv->idle_conns[tid] : NULL));
Willy Tarreau4320eaa2015-08-05 11:08:30 +02004392 }
Christopher Faulete6006242017-03-10 11:52:44 +01004393 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
4394 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004395}
4396
4397
4398/* This function updates the request state machine according to the response
4399 * state machine and buffer flags. It returns 1 if it changes anything (flag
4400 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4401 * it is only used to find when a request/response couple is complete. Both
4402 * this function and its equivalent should loop until both return zero. It
4403 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4404 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004405int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004406{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004407 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004408 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004409 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004410 unsigned int old_state = txn->req.msg_state;
4411
Christopher Faulet4be98032017-07-18 10:48:24 +02004412 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004413 return 0;
4414
4415 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004416 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004417 * We can shut the read side unless we want to abort_on_close,
4418 * or we have a POST request. The issue with POST requests is
4419 * that some browsers still send a CRLF after the request, and
4420 * this CRLF must be read so that it does not remain in the kernel
4421 * buffers, otherwise a close could cause an RST on some systems
4422 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004423 * Note that if we're using keep-alive on the client side, we'd
4424 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02004425 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01004426 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004427 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004428 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4429 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4430 !(s->be->options & PR_O_ABRT_CLOSE) &&
4431 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004432 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004433
Willy Tarreau40f151a2012-12-20 12:10:09 +01004434 /* if the server closes the connection, we want to immediately react
4435 * and close the socket to save packets and syscalls.
4436 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004437 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01004438
Willy Tarreau7f876a12015-11-18 11:59:55 +01004439 /* In any case we've finished parsing the request so we must
4440 * disable Nagle when sending data because 1) we're not going
4441 * to shut this side, and 2) the server is waiting for us to
4442 * send pending data.
4443 */
4444 chn->flags |= CF_NEVER_WAIT;
4445
Willy Tarreau610ecce2010-01-04 21:15:02 +01004446 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4447 goto wait_other_side;
4448
4449 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4450 /* The server has not finished to respond, so we
4451 * don't want to move in order not to upset it.
4452 */
4453 goto wait_other_side;
4454 }
4455
Willy Tarreau610ecce2010-01-04 21:15:02 +01004456 /* When we get here, it means that both the request and the
4457 * response have finished receiving. Depending on the connection
4458 * mode, we'll have to wait for the last bytes to leave in either
4459 * direction, and sometimes for a close to be effective.
4460 */
4461
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004462 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4463 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004464 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4465 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004466 }
4467 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4468 /* Option forceclose is set, or either side wants to close,
4469 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004470 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004471 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004472 *
4473 * However, there is an exception if the response
4474 * length is undefined. In this case, we need to wait
4475 * the close from the server. The response will be
4476 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004477 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004478 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4479 txn->rsp.msg_state != HTTP_MSG_CLOSED)
4480 goto check_channel_flags;
4481
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004482 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4483 channel_shutr_now(chn);
4484 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004485 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004486 }
4487 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004488 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4489 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004490 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004491 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4492 channel_auto_read(chn);
4493 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004494 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004495 }
4496
Christopher Faulet4be98032017-07-18 10:48:24 +02004497 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004498 }
4499
4500 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4501 http_msg_closing:
4502 /* nothing else to forward, just waiting for the output buffer
4503 * to be empty and for the shutw_now to take effect.
4504 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004505 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004506 txn->req.msg_state = HTTP_MSG_CLOSED;
4507 goto http_msg_closed;
4508 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004509 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004510 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004511 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004512 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004513 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004514 }
4515
4516 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4517 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01004518 /* see above in MSG_DONE why we only do this in these states */
4519 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4520 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4521 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004522 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004523 goto wait_other_side;
4524 }
4525
Christopher Faulet4be98032017-07-18 10:48:24 +02004526 check_channel_flags:
4527 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4528 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4529 /* if we've just closed an output, let's switch */
4530 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4531 txn->req.msg_state = HTTP_MSG_CLOSING;
4532 goto http_msg_closing;
4533 }
4534
4535
Willy Tarreau610ecce2010-01-04 21:15:02 +01004536 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004537 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004538}
4539
4540
4541/* This function updates the response state machine according to the request
4542 * state machine and buffer flags. It returns 1 if it changes anything (flag
4543 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4544 * it is only used to find when a request/response couple is complete. Both
4545 * this function and its equivalent should loop until both return zero. It
4546 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4547 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004548int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004549{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004550 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004551 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004552 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004553 unsigned int old_state = txn->rsp.msg_state;
4554
Christopher Faulet4be98032017-07-18 10:48:24 +02004555 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004556 return 0;
4557
4558 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4559 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004560 * still monitor the server connection for a possible close
4561 * while the request is being uploaded, so we don't disable
4562 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004563 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004564 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004565
4566 if (txn->req.msg_state == HTTP_MSG_ERROR)
4567 goto wait_other_side;
4568
4569 if (txn->req.msg_state < HTTP_MSG_DONE) {
4570 /* The client seems to still be sending data, probably
4571 * because we got an error response during an upload.
4572 * We have the choice of either breaking the connection
4573 * or letting it pass through. Let's do the later.
4574 */
4575 goto wait_other_side;
4576 }
4577
Willy Tarreau610ecce2010-01-04 21:15:02 +01004578 /* When we get here, it means that both the request and the
4579 * response have finished receiving. Depending on the connection
4580 * mode, we'll have to wait for the last bytes to leave in either
4581 * direction, and sometimes for a close to be effective.
4582 */
4583
4584 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4585 /* Server-close mode : shut read and wait for the request
4586 * side to close its output buffer. The caller will detect
4587 * when we're in DONE and the other is in CLOSED and will
4588 * catch that for the final cleanup.
4589 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004590 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4591 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004592 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004593 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4594 /* Option forceclose is set, or either side wants to close,
4595 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004596 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004597 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004598 *
4599 * However, there is an exception if the response length
4600 * is undefined. In this case, we switch in TUNNEL mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004601 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004602 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN)) {
4603 channel_auto_read(chn);
4604 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4605 chn->flags |= CF_NEVER_WAIT;
4606 }
4607 else if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004608 channel_shutr_now(chn);
4609 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004610 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004611 }
4612 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004613 /* The last possible modes are keep-alive and tunnel. Tunnel will
4614 * need to forward remaining data. Keep-alive will need to monitor
4615 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004616 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004617 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004618 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004619 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4620 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004621 }
4622
Christopher Faulet4be98032017-07-18 10:48:24 +02004623 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004624 }
4625
4626 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4627 http_msg_closing:
4628 /* nothing else to forward, just waiting for the output buffer
4629 * to be empty and for the shutw_now to take effect.
4630 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004631 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004632 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4633 goto http_msg_closed;
4634 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004635 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004636 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004637 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004638 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004639 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004640 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004641 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004642 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004643 }
4644
4645 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4646 http_msg_closed:
4647 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004648 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004649 channel_auto_close(chn);
4650 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004651 goto wait_other_side;
4652 }
4653
Christopher Faulet4be98032017-07-18 10:48:24 +02004654 check_channel_flags:
4655 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4656 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4657 /* if we've just closed an output, let's switch */
4658 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4659 goto http_msg_closing;
4660 }
4661
Willy Tarreau610ecce2010-01-04 21:15:02 +01004662 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004663 /* We force the response to leave immediately if we're waiting for the
4664 * other side, since there is no pending shutdown to push it out.
4665 */
4666 if (!channel_is_empty(chn))
4667 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004668 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004669}
4670
4671
Christopher Faulet894da4c2017-07-18 11:29:07 +02004672/* Resync the request and response state machines. */
4673void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004674{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004675 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004676#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004677 int old_req_state = txn->req.msg_state;
4678 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004679#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004680
Willy Tarreau610ecce2010-01-04 21:15:02 +01004681 http_sync_req_state(s);
4682 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004683 if (!http_sync_res_state(s))
4684 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004685 if (!http_sync_req_state(s))
4686 break;
4687 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004688
Christopher Faulet894da4c2017-07-18 11:29:07 +02004689 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4690 "req->analysers=0x%08x res->analysers=0x%08x\n",
4691 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004692 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4693 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004694 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004695
4696
Willy Tarreau610ecce2010-01-04 21:15:02 +01004697 /* OK, both state machines agree on a compatible state.
4698 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004699 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4700 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004701 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4702 * means we must abort the request.
4703 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4704 * corresponding channel.
4705 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4706 * on the response with server-close mode means we've completed one
4707 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004708 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004709 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4710 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004711 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004712 channel_auto_close(&s->req);
4713 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004714 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004715 channel_auto_close(&s->res);
4716 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004717 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004718 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4719 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004720 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004721 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004722 channel_auto_close(&s->res);
4723 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004724 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004725 channel_abort(&s->req);
4726 channel_auto_close(&s->req);
4727 channel_auto_read(&s->req);
4728 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004729 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004730 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4731 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4732 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4733 s->req.analysers &= AN_REQ_FLT_END;
4734 if (HAS_REQ_DATA_FILTERS(s))
4735 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4736 }
4737 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4738 s->res.analysers &= AN_RES_FLT_END;
4739 if (HAS_RSP_DATA_FILTERS(s))
4740 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4741 }
4742 channel_auto_close(&s->req);
4743 channel_auto_read(&s->req);
4744 channel_auto_close(&s->res);
4745 channel_auto_read(&s->res);
4746 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004747 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4748 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004749 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004750 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4751 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4752 /* server-close/keep-alive: terminate this transaction,
4753 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004754 * a fresh-new transaction, but only once we're sure there's
4755 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004756 * another request. They must not hold any pending output data
4757 * and the response buffer must realigned
4758 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004759 */
Christopher Faulet894da4c2017-07-18 11:29:07 +02004760 if (s->req.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004761 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004762 else if (s->res.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004763 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004764 else {
4765 s->req.analysers = AN_REQ_FLT_END;
4766 s->res.analysers = AN_RES_FLT_END;
4767 txn->flags |= TX_WAIT_CLEANUP;
4768 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004769 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004770}
4771
Willy Tarreaud98cf932009-12-27 22:54:55 +01004772/* This function is an analyser which forwards request body (including chunk
4773 * sizes if any). It is called as soon as we must forward, even if we forward
4774 * zero byte. The only situation where it must not be called is when we're in
4775 * tunnel mode and we want to forward till the close. It's used both to forward
4776 * remaining data and to resync after end of body. It expects the msg_state to
4777 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004778 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004779 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004780 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004781 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004782int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004783{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004784 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004785 struct http_txn *txn = s->txn;
4786 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004787 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004788
Christopher Faulet814d2702017-03-30 11:33:44 +02004789 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
4790 now_ms, __FUNCTION__,
4791 s,
4792 req,
4793 req->rex, req->wex,
4794 req->flags,
4795 req->buf->i,
4796 req->analysers);
4797
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004798 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4799 return 0;
4800
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004801 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004802 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004803 /* Output closed while we were sending data. We must abort and
4804 * wake the other side up.
4805 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004806 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004807 msg->msg_state = HTTP_MSG_ERROR;
4808 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004809 return 1;
4810 }
4811
Willy Tarreaud98cf932009-12-27 22:54:55 +01004812 /* Note that we don't have to send 100-continue back because we don't
4813 * need the data to complete our job, and it's up to the server to
4814 * decide whether to return 100, 417 or anything else in return of
4815 * an "Expect: 100-continue" header.
4816 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004817 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004818 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4819 ? HTTP_MSG_CHUNK_SIZE
4820 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004821
4822 /* TODO/filters: when http-buffer-request option is set or if a
4823 * rule on url_param exists, the first chunk size could be
4824 * already parsed. In that case, msg->next is after the chunk
4825 * size (including the CRLF after the size). So this case should
4826 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004827 }
4828
Willy Tarreau7ba23542014-04-17 21:50:00 +02004829 /* Some post-connect processing might want us to refrain from starting to
4830 * forward data. Currently, the only reason for this is "balance url_param"
4831 * whichs need to parse/process the request after we've enabled forwarding.
4832 */
4833 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004834 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004835 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004836 req->flags |= CF_WAKE_CONNECT;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004837 goto missing_data_or_waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004838 }
4839 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4840 }
4841
Willy Tarreau80a92c02014-03-12 10:41:13 +01004842 /* in most states, we should abort in case of early close */
4843 channel_auto_close(req);
4844
Willy Tarreauefdf0942014-04-24 20:08:57 +02004845 if (req->to_forward) {
4846 /* We can't process the buffer's contents yet */
4847 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004848 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004849 }
4850
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004851 if (msg->msg_state < HTTP_MSG_DONE) {
4852 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4853 ? http_msg_forward_chunked_body(s, msg)
4854 : http_msg_forward_body(s, msg));
4855 if (!ret)
4856 goto missing_data_or_waiting;
4857 if (ret < 0)
4858 goto return_bad_req;
4859 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004860
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004861 /* other states, DONE...TUNNEL */
4862 /* we don't want to forward closes on DONE except in tunnel mode. */
4863 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4864 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004865
Christopher Faulet894da4c2017-07-18 11:29:07 +02004866 http_resync_states(s);
4867 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004868 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4869 if (req->flags & CF_SHUTW) {
4870 /* request errors are most likely due to the
4871 * server aborting the transfer. */
4872 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004873 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004874 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02004875 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004876 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004877 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004878 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004879 }
4880
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004881 /* If "option abortonclose" is set on the backend, we want to monitor
4882 * the client's connection and forward any shutdown notification to the
4883 * server, which will decide whether to close or to go on processing the
4884 * request. We only do that in tunnel mode, and not in other modes since
4885 * it can be abused to exhaust source ports. */
4886 if (s->be->options & PR_O_ABRT_CLOSE) {
4887 channel_auto_read(req);
4888 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4889 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4890 s->si[1].flags |= SI_FL_NOLINGER;
4891 channel_auto_close(req);
4892 }
4893 else if (s->txn->meth == HTTP_METH_POST) {
4894 /* POST requests may require to read extra CRLF sent by broken
4895 * browsers and which could cause an RST to be sent upon close
4896 * on some systems (eg: Linux). */
4897 channel_auto_read(req);
4898 }
4899 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004900
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004901 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004902 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004903 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004904 if (!(s->flags & SF_ERR_MASK))
4905 s->flags |= SF_ERR_CLICL;
4906 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004907 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004908 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004909 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004910 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004911 }
4912
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004913 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4914 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004915 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004916 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004917
4918 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004919 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004920
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004921 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004922 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004923 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004924
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004925 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004926 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004927 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004928 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004929 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004930
Willy Tarreau5c620922011-05-11 19:56:11 +02004931 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004932 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004933 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004934 * modes are already handled by the stream sock layer. We must not do
4935 * this in content-length mode because it could present the MSG_MORE
4936 * flag with the last block of forwarded data, which would cause an
4937 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004938 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004939 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004940 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004941
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004942 return 0;
4943
Willy Tarreaud98cf932009-12-27 22:54:55 +01004944 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004945 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004946 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004947 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02004948
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004949 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004950 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004951 txn->req.msg_state = HTTP_MSG_ERROR;
4952 if (txn->status) {
4953 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004954 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004955 } else {
4956 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004957 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004958 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004959 req->analysers &= AN_REQ_FLT_END;
4960 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 +01004961
Willy Tarreaue7dff022015-04-03 01:14:29 +02004962 if (!(s->flags & SF_ERR_MASK))
4963 s->flags |= SF_ERR_PRXCOND;
4964 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004965 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004966 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004967 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004968 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004969 }
4970 return 0;
4971
4972 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004973 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004974 txn->req.msg_state = HTTP_MSG_ERROR;
4975 if (txn->status) {
4976 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004977 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004978 } else {
4979 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004980 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004981 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004982 req->analysers &= AN_REQ_FLT_END;
4983 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 +01004984
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004985 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
4986 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004987 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004988 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004989
Willy Tarreaue7dff022015-04-03 01:14:29 +02004990 if (!(s->flags & SF_ERR_MASK))
4991 s->flags |= SF_ERR_SRVCL;
4992 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004993 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004994 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004995 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004996 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004997 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004998 return 0;
4999}
5000
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005001/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5002 * processing can continue on next analysers, or zero if it either needs more
5003 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005004 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005005 * when it has nothing left to do, and may remove any analyser when it wants to
5006 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005007 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005008int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005009{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005010 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005011 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005012 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005013 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005014 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005015 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005016 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005017
Willy Tarreau87b09662015-04-03 00:22:06 +02005018 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 +02005019 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005020 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005021 rep,
5022 rep->rex, rep->wex,
5023 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005024 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005025 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005026
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005027 /*
5028 * Now parse the partial (or complete) lines.
5029 * We will check the response syntax, and also join multi-line
5030 * headers. An index of all the lines will be elaborated while
5031 * parsing.
5032 *
5033 * For the parsing, we use a 28 states FSM.
5034 *
5035 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005036 * rep->buf->p = beginning of response
5037 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5038 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005039 * msg->eol = end of current header or line (LF or CRLF)
5040 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005041 */
5042
Willy Tarreau628c40c2014-04-24 19:11:26 +02005043 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005044 /* There's a protected area at the end of the buffer for rewriting
5045 * purposes. We don't want to start to parse the request if the
5046 * protected area is affected, because we may have to move processed
5047 * data later, which is much more complicated.
5048 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005049 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005050 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005051 /* some data has still not left the buffer, wake us once that's done */
5052 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5053 goto abort_response;
5054 channel_dont_close(rep);
5055 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005056 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005057 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005058 }
5059
Willy Tarreau379357a2013-06-08 12:55:46 +02005060 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5061 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5062 buffer_slow_realign(rep->buf);
5063
Willy Tarreau9b28e032012-10-12 23:49:43 +02005064 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005065 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005066 }
5067
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005068 /* 1: we might have to print this header in debug mode */
5069 if (unlikely((global.mode & MODE_DEBUG) &&
5070 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005071 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005072 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005073
Willy Tarreau9b28e032012-10-12 23:49:43 +02005074 sol = rep->buf->p;
5075 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005076 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005077
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005078 sol += hdr_idx_first_pos(&txn->hdr_idx);
5079 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005080
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005081 while (cur_idx) {
5082 eol = sol + txn->hdr_idx.v[cur_idx].len;
5083 debug_hdr("srvhdr", s, sol, eol);
5084 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5085 cur_idx = txn->hdr_idx.v[cur_idx].next;
5086 }
5087 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005088
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005089 /*
5090 * Now we quickly check if we have found a full valid response.
5091 * If not so, we check the FD and buffer states before leaving.
5092 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005093 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005094 * responses are checked first.
5095 *
5096 * Depending on whether the client is still there or not, we
5097 * may send an error response back or not. Note that normally
5098 * we should only check for HTTP status there, and check I/O
5099 * errors somewhere else.
5100 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005101
Willy Tarreau655dce92009-11-08 13:10:58 +01005102 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005103 /* Invalid response */
5104 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5105 /* we detected a parsing error. We want to archive this response
5106 * in the dedicated proxy area for later troubleshooting.
5107 */
5108 hdr_response_bad:
5109 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005110 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005111
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005112 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005113 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005114 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005115 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005116 }
Willy Tarreau64648412010-03-05 10:41:54 +01005117 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005118 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005119 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005120 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005121 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005122 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005123 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005124
Willy Tarreaue7dff022015-04-03 01:14:29 +02005125 if (!(s->flags & SF_ERR_MASK))
5126 s->flags |= SF_ERR_PRXCOND;
5127 if (!(s->flags & SF_FINST_MASK))
5128 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005129
5130 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005131 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005132
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005133 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005134 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005135 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005136 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005137 goto hdr_response_bad;
5138 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005139
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005140 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005141 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005142 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005143 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005144 else if (txn->flags & TX_NOT_FIRST)
5145 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005146
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005147 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005148 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005149 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005150 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005151 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005152
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005153 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005154 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005155 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01005156
5157 /* Check to see if the server refused the early data.
5158 * If so, just send a 425
5159 */
5160 if (objt_cs(s->si[1].end)) {
5161 struct connection *conn = objt_cs(s->si[1].end)->conn;
5162
5163 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
5164 txn->status = 425;
5165 }
5166
Willy Tarreau350f4872014-11-28 14:42:25 +01005167 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005168 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005169 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02005170
Willy Tarreaue7dff022015-04-03 01:14:29 +02005171 if (!(s->flags & SF_ERR_MASK))
5172 s->flags |= SF_ERR_SRVCL;
5173 if (!(s->flags & SF_FINST_MASK))
5174 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005175 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005176 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005177
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005178 /* read timeout : return a 504 to the client. */
5179 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005180 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005181 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005182
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005183 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005184 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005185 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005186 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005187 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005188
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005189 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005190 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005191 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005192 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005193 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005194 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02005195
Willy Tarreaue7dff022015-04-03 01:14:29 +02005196 if (!(s->flags & SF_ERR_MASK))
5197 s->flags |= SF_ERR_SRVTO;
5198 if (!(s->flags & SF_FINST_MASK))
5199 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005200 return 0;
5201 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005202
Willy Tarreauf003d372012-11-26 13:35:37 +01005203 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005204 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005205 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5206 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01005207 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005208 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01005209
Christopher Faulet0184ea72017-01-05 14:06:34 +01005210 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01005211 channel_auto_close(rep);
5212
5213 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005214 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005215 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01005216
Willy Tarreaue7dff022015-04-03 01:14:29 +02005217 if (!(s->flags & SF_ERR_MASK))
5218 s->flags |= SF_ERR_CLICL;
5219 if (!(s->flags & SF_FINST_MASK))
5220 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01005221
Willy Tarreau87b09662015-04-03 00:22:06 +02005222 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01005223 return 0;
5224 }
5225
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005226 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005227 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005228 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005229 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005230 else if (txn->flags & TX_NOT_FIRST)
5231 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005232
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005233 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005234 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005235 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005236 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005237 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005238
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005239 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005240 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005241 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005242 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005243 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005244 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005245
Willy Tarreaue7dff022015-04-03 01:14:29 +02005246 if (!(s->flags & SF_ERR_MASK))
5247 s->flags |= SF_ERR_SRVCL;
5248 if (!(s->flags & SF_FINST_MASK))
5249 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005250 return 0;
5251 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005252
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005253 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005254 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005255 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005256 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005257 else if (txn->flags & TX_NOT_FIRST)
5258 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005259
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005260 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005261 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005262 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005263
Willy Tarreaue7dff022015-04-03 01:14:29 +02005264 if (!(s->flags & SF_ERR_MASK))
5265 s->flags |= SF_ERR_CLICL;
5266 if (!(s->flags & SF_FINST_MASK))
5267 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005268
Willy Tarreau87b09662015-04-03 00:22:06 +02005269 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005270 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005271 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005272
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005273 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005274 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005275 return 0;
5276 }
5277
5278 /* More interesting part now : we know that we have a complete
5279 * response which at least looks like HTTP. We have an indicator
5280 * of each header's length, so we can parse them quickly.
5281 */
5282
5283 if (unlikely(msg->err_pos >= 0))
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005284 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005285
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005286 /*
5287 * 1: get the status code
5288 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005289 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005290 if (n < 1 || n > 5)
5291 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005292 /* when the client triggers a 4xx from the server, it's most often due
5293 * to a missing object or permission. These events should be tracked
5294 * because if they happen often, it may indicate a brute force or a
5295 * vulnerability scan.
5296 */
5297 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02005298 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02005299
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005300 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005301 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005302
Willy Tarreau91852eb2015-05-01 13:26:00 +02005303 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
5304 * exactly one digit "." one digit. This check may be disabled using
5305 * option accept-invalid-http-response.
5306 */
5307 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
5308 if (msg->sl.st.v_l != 8) {
5309 msg->err_pos = 0;
5310 goto hdr_response_bad;
5311 }
5312
5313 if (rep->buf->p[4] != '/' ||
5314 !isdigit((unsigned char)rep->buf->p[5]) ||
5315 rep->buf->p[6] != '.' ||
5316 !isdigit((unsigned char)rep->buf->p[7])) {
5317 msg->err_pos = 4;
5318 goto hdr_response_bad;
5319 }
5320 }
5321
Willy Tarreau5b154472009-12-21 20:11:07 +01005322 /* check if the response is HTTP/1.1 or above */
5323 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005324 ((rep->buf->p[5] > '1') ||
5325 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005326 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005327
5328 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005329 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 +01005330
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005331 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005332 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005333
Willy Tarreau9b28e032012-10-12 23:49:43 +02005334 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005335
Willy Tarreau39650402010-03-15 19:44:39 +01005336 /* Adjust server's health based on status code. Note: status codes 501
5337 * and 505 are triggered on demand by client request, so we must not
5338 * count them as server failures.
5339 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005340 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005341 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005342 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005343 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005344 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005345 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005346
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005347 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02005348 * We may be facing a 100-continue response, or any other informational
5349 * 1xx response which is non-final, in which case this is not the right
5350 * response, and we're waiting for the next one. Let's allow this response
5351 * to go to the client and wait for the next one. There's an exception for
5352 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005353 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02005354 if (txn->status < 200 &&
5355 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005356 hdr_idx_init(&txn->hdr_idx);
5357 msg->next -= channel_forward(rep, msg->next);
5358 msg->msg_state = HTTP_MSG_RPBEFORE;
5359 txn->status = 0;
5360 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01005361 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02005362 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02005363 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02005364
Willy Tarreaua14ad722017-07-07 11:36:32 +02005365 /*
5366 * 2: check for cacheability.
5367 */
5368
5369 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005370 case 200:
5371 case 203:
5372 case 206:
5373 case 300:
5374 case 301:
5375 case 410:
5376 /* RFC2616 @13.4:
5377 * "A response received with a status code of
5378 * 200, 203, 206, 300, 301 or 410 MAY be stored
5379 * by a cache (...) unless a cache-control
5380 * directive prohibits caching."
5381 *
5382 * RFC2616 @9.5: POST method :
5383 * "Responses to this method are not cacheable,
5384 * unless the response includes appropriate
5385 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005386 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005387 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005388 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005389 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5390 break;
5391 default:
5392 break;
5393 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005394
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005395 /*
5396 * 3: we may need to capture headers
5397 */
5398 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005399 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005400 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005401 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005402
Willy Tarreau557f1992015-05-01 10:05:17 +02005403 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
5404 * by RFC7230#3.3.3 :
5405 *
5406 * The length of a message body is determined by one of the following
5407 * (in order of precedence):
5408 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005409 * 1. Any 2xx (Successful) response to a CONNECT request implies that
5410 * the connection will become a tunnel immediately after the empty
5411 * line that concludes the header fields. A client MUST ignore
5412 * any Content-Length or Transfer-Encoding header fields received
5413 * in such a message. Any 101 response (Switching Protocols) is
5414 * managed in the same manner.
5415 *
5416 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02005417 * (Informational), 204 (No Content), or 304 (Not Modified) status
5418 * code is always terminated by the first empty line after the
5419 * header fields, regardless of the header fields present in the
5420 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005421 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005422 * 3. If a Transfer-Encoding header field is present and the chunked
5423 * transfer coding (Section 4.1) is the final encoding, the message
5424 * body length is determined by reading and decoding the chunked
5425 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005426 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005427 * If a Transfer-Encoding header field is present in a response and
5428 * the chunked transfer coding is not the final encoding, the
5429 * message body length is determined by reading the connection until
5430 * it is closed by the server. If a Transfer-Encoding header field
5431 * is present in a request and the chunked transfer coding is not
5432 * the final encoding, the message body length cannot be determined
5433 * reliably; the server MUST respond with the 400 (Bad Request)
5434 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005435 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005436 * If a message is received with both a Transfer-Encoding and a
5437 * Content-Length header field, the Transfer-Encoding overrides the
5438 * Content-Length. Such a message might indicate an attempt to
5439 * perform request smuggling (Section 9.5) or response splitting
5440 * (Section 9.4) and ought to be handled as an error. A sender MUST
5441 * remove the received Content-Length field prior to forwarding such
5442 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005443 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005444 * 4. If a message is received without Transfer-Encoding and with
5445 * either multiple Content-Length header fields having differing
5446 * field-values or a single Content-Length header field having an
5447 * invalid value, then the message framing is invalid and the
5448 * recipient MUST treat it as an unrecoverable error. If this is a
5449 * request message, the server MUST respond with a 400 (Bad Request)
5450 * status code and then close the connection. If this is a response
5451 * message received by a proxy, the proxy MUST close the connection
5452 * to the server, discard the received response, and send a 502 (Bad
5453 * Gateway) response to the client. If this is a response message
5454 * received by a user agent, the user agent MUST close the
5455 * connection to the server and discard the received response.
5456 *
5457 * 5. If a valid Content-Length header field is present without
5458 * Transfer-Encoding, its decimal value defines the expected message
5459 * body length in octets. If the sender closes the connection or
5460 * the recipient times out before the indicated number of octets are
5461 * received, the recipient MUST consider the message to be
5462 * incomplete and close the connection.
5463 *
5464 * 6. If this is a request message and none of the above are true, then
5465 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005466 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005467 * 7. Otherwise, this is a response message without a declared message
5468 * body length, so the message body length is determined by the
5469 * number of octets received prior to the server closing the
5470 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005471 */
5472
5473 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005474 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005475 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005476 * FIXME: should we parse anyway and return an error on chunked encoding ?
5477 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005478 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5479 txn->status == 101)) {
5480 /* Either we've established an explicit tunnel, or we're
5481 * switching the protocol. In both cases, we're very unlikely
5482 * to understand the next protocols. We have to switch to tunnel
5483 * mode, so that we transfer the request and responses then let
5484 * this protocol pass unmodified. When we later implement specific
5485 * parsers for such protocols, we'll want to check the Upgrade
5486 * header which contains information about that protocol for
5487 * responses with status 101 (eg: see RFC2817 about TLS).
5488 */
5489 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5490 msg->flags |= HTTP_MSGF_XFER_LEN;
5491 goto end;
5492 }
5493
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005494 if (txn->meth == HTTP_METH_HEAD ||
5495 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005496 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005497 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005498 goto skip_content_length;
5499 }
5500
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005501 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005502 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02005503 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005504 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005505 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5506 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005507 /* bad transfer-encoding (chunked followed by something else) */
5508 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005509 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005510 break;
5511 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005512 }
5513
Willy Tarreau1c913912015-04-30 10:57:51 +02005514 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005515 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005516 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02005517 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
5518 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5519 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005520 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005521 signed long long cl;
5522
Willy Tarreauad14f752011-09-02 20:33:27 +02005523 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005524 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005525 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005526 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005527
Willy Tarreauad14f752011-09-02 20:33:27 +02005528 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005529 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005530 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005531 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005532
Willy Tarreauad14f752011-09-02 20:33:27 +02005533 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005534 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005535 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005536 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005537
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005538 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005539 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005540 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005541 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005542
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005543 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005544 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005545 }
5546
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005547 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005548 /* Now we have to check if we need to modify the Connection header.
5549 * This is more difficult on the response than it is on the request,
5550 * because we can have two different HTTP versions and we don't know
5551 * how the client will interprete a response. For instance, let's say
5552 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5553 * HTTP/1.1 response without any header. Maybe it will bound itself to
5554 * HTTP/1.0 because it only knows about it, and will consider the lack
5555 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5556 * the lack of header as a keep-alive. Thus we will use two flags
5557 * indicating how a request MAY be understood by the client. In case
5558 * of multiple possibilities, we'll fix the header to be explicit. If
5559 * ambiguous cases such as both close and keepalive are seen, then we
5560 * will fall back to explicit close. Note that we won't take risks with
5561 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005562 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005563 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005564 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5565 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5566 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5567 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005568 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005569
Willy Tarreau70dffda2014-01-30 03:07:23 +01005570 /* this situation happens when combining pretend-keepalive with httpclose. */
5571 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005572 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005573 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01005574 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5575
Willy Tarreau60466522010-01-18 19:08:45 +01005576 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005577 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005578 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5579 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005580
Willy Tarreau60466522010-01-18 19:08:45 +01005581 /* now adjust header transformations depending on current state */
5582 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5583 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5584 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005585 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005586 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005587 }
Willy Tarreau60466522010-01-18 19:08:45 +01005588 else { /* SCL / KAL */
5589 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005590 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005591 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005592 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005593
Willy Tarreau60466522010-01-18 19:08:45 +01005594 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005595 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005596
Willy Tarreau60466522010-01-18 19:08:45 +01005597 /* Some keep-alive responses are converted to Server-close if
5598 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005599 */
Willy Tarreau60466522010-01-18 19:08:45 +01005600 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5601 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005602 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005603 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005604 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005605 }
5606
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005607 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005608 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005609 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005610
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005611 /* end of job, return OK */
5612 rep->analysers &= ~an_bit;
5613 rep->analyse_exp = TICK_ETERNITY;
5614 channel_auto_close(rep);
5615 return 1;
5616
5617 abort_keep_alive:
5618 /* A keep-alive request to the server failed on a network error.
5619 * The client is required to retry. We need to close without returning
5620 * any other information so that the client retries.
5621 */
5622 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005623 rep->analysers &= AN_RES_FLT_END;
5624 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005625 channel_auto_close(rep);
5626 s->logs.logwait = 0;
5627 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005628 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005629 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005630 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005631 return 0;
5632}
5633
5634/* This function performs all the processing enabled for the current response.
5635 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005636 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005637 * other functions. It works like process_request (see indications above).
5638 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005639int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005640{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005641 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005642 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005643 struct http_msg *msg = &txn->rsp;
5644 struct proxy *cur_proxy;
5645 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005646 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005647
Willy Tarreau87b09662015-04-03 00:22:06 +02005648 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 +02005649 now_ms, __FUNCTION__,
5650 s,
5651 rep,
5652 rep->rex, rep->wex,
5653 rep->flags,
5654 rep->buf->i,
5655 rep->analysers);
5656
5657 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5658 return 0;
5659
Willy Tarreau70730dd2014-04-24 18:06:27 +02005660 /* The stats applet needs to adjust the Connection header but we don't
5661 * apply any filter there.
5662 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005663 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5664 rep->analysers &= ~an_bit;
5665 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005666 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005667 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005668
Willy Tarreau58975672014-04-24 21:13:57 +02005669 /*
5670 * We will have to evaluate the filters.
5671 * As opposed to version 1.2, now they will be evaluated in the
5672 * filters order and not in the header order. This means that
5673 * each filter has to be validated among all headers.
5674 *
5675 * Filters are tried with ->be first, then with ->fe if it is
5676 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005677 *
5678 * Maybe we are in resume condiion. In this case I choose the
5679 * "struct proxy" which contains the rule list matching the resume
5680 * pointer. If none of theses "struct proxy" match, I initialise
5681 * the process with the first one.
5682 *
5683 * In fact, I check only correspondance betwwen the current list
5684 * pointer and the ->fe rule list. If it doesn't match, I initialize
5685 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005686 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005687 if (s->current_rule_list == &sess->fe->http_res_rules)
5688 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005689 else
5690 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005691 while (1) {
5692 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005693
Willy Tarreau58975672014-04-24 21:13:57 +02005694 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005695 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005696 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005697
Willy Tarreau51d861a2015-05-22 17:30:48 +02005698 if (ret == HTTP_RULE_RES_BADREQ)
5699 goto return_srv_prx_502;
5700
5701 if (ret == HTTP_RULE_RES_DONE) {
5702 rep->analysers &= ~an_bit;
5703 rep->analyse_exp = TICK_ETERNITY;
5704 return 1;
5705 }
5706 }
5707
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005708 /* we need to be called again. */
5709 if (ret == HTTP_RULE_RES_YIELD) {
5710 channel_dont_close(rep);
5711 return 0;
5712 }
5713
Willy Tarreau58975672014-04-24 21:13:57 +02005714 /* try headers filters */
5715 if (rule_set->rsp_exp != NULL) {
5716 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5717 return_bad_resp:
5718 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005719 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005720 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005721 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005722 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005723 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005724 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005725 txn->status = 502;
5726 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005727 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005728 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005729 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005730 if (!(s->flags & SF_ERR_MASK))
5731 s->flags |= SF_ERR_PRXCOND;
5732 if (!(s->flags & SF_FINST_MASK))
5733 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005734 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005735 }
Willy Tarreau58975672014-04-24 21:13:57 +02005736 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005737
Willy Tarreau58975672014-04-24 21:13:57 +02005738 /* has the response been denied ? */
5739 if (txn->flags & TX_SVDENY) {
5740 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005741 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005742
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005743 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5744 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005745 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005746 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005747
Willy Tarreau58975672014-04-24 21:13:57 +02005748 goto return_srv_prx_502;
5749 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005750
Willy Tarreau58975672014-04-24 21:13:57 +02005751 /* add response headers from the rule sets in the same order */
5752 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005753 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005754 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005755 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005756 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005757 ret = acl_pass(ret);
5758 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5759 ret = !ret;
5760 if (!ret)
5761 continue;
5762 }
5763 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
5764 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005765 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005766
Willy Tarreau58975672014-04-24 21:13:57 +02005767 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005768 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005769 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005770 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005771 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005772
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005773 /* After this point, this anayzer can't return yield, so we can
5774 * remove the bit corresponding to this analyzer from the list.
5775 *
5776 * Note that the intermediate returns and goto found previously
5777 * reset the analyzers.
5778 */
5779 rep->analysers &= ~an_bit;
5780 rep->analyse_exp = TICK_ETERNITY;
5781
Willy Tarreau58975672014-04-24 21:13:57 +02005782 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005783 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005784 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005785
Willy Tarreau58975672014-04-24 21:13:57 +02005786 /*
5787 * Now check for a server cookie.
5788 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005789 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005790 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005791
Willy Tarreau58975672014-04-24 21:13:57 +02005792 /*
5793 * Check for cache-control or pragma headers if required.
5794 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005795 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 +02005796 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005797
Willy Tarreau58975672014-04-24 21:13:57 +02005798 /*
5799 * Add server cookie in the response if needed
5800 */
5801 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5802 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005803 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005804 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5805 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5806 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5807 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5808 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005809 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005810 /* the server is known, it's not the one the client requested, or the
5811 * cookie's last seen date needs to be refreshed. We have to
5812 * insert a set-cookie here, except if we want to insert only on POST
5813 * requests and this one isn't. Note that servers which don't have cookies
5814 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005815 */
Willy Tarreau58975672014-04-24 21:13:57 +02005816 if (!objt_server(s->target)->cookie) {
5817 chunk_printf(&trash,
5818 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5819 s->be->cookie_name);
5820 }
5821 else {
5822 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005823
Willy Tarreau58975672014-04-24 21:13:57 +02005824 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5825 /* emit last_date, which is mandatory */
5826 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5827 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5828 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005829
Willy Tarreau58975672014-04-24 21:13:57 +02005830 if (s->be->cookie_maxlife) {
5831 /* emit first_date, which is either the original one or
5832 * the current date.
5833 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005834 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005835 s30tob64(txn->cookie_first_date ?
5836 txn->cookie_first_date >> 2 :
5837 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005838 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005839 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005840 }
Willy Tarreau58975672014-04-24 21:13:57 +02005841 chunk_appendf(&trash, "; path=/");
5842 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005843
Willy Tarreau58975672014-04-24 21:13:57 +02005844 if (s->be->cookie_domain)
5845 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005846
Willy Tarreau58975672014-04-24 21:13:57 +02005847 if (s->be->ck_opts & PR_CK_HTTPONLY)
5848 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005849
Willy Tarreau58975672014-04-24 21:13:57 +02005850 if (s->be->ck_opts & PR_CK_SECURE)
5851 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005852
Willy Tarreau58975672014-04-24 21:13:57 +02005853 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
5854 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005855
Willy Tarreau58975672014-04-24 21:13:57 +02005856 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005857 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005858 /* the server did not change, only the date was updated */
5859 txn->flags |= TX_SCK_UPDATED;
5860 else
5861 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005862
Willy Tarreau58975672014-04-24 21:13:57 +02005863 /* Here, we will tell an eventual cache on the client side that we don't
5864 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5865 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5866 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005867 */
Willy Tarreau58975672014-04-24 21:13:57 +02005868 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005869
Willy Tarreau58975672014-04-24 21:13:57 +02005870 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005871
Willy Tarreau58975672014-04-24 21:13:57 +02005872 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5873 "Cache-control: private", 22) < 0))
5874 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005875 }
Willy Tarreau58975672014-04-24 21:13:57 +02005876 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005877
Willy Tarreau58975672014-04-24 21:13:57 +02005878 /*
5879 * Check if result will be cacheable with a cookie.
5880 * We'll block the response if security checks have caught
5881 * nasty things such as a cacheable cookie.
5882 */
5883 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5884 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5885 (s->be->options & PR_O_CHK_CACHE)) {
5886 /* we're in presence of a cacheable response containing
5887 * a set-cookie header. We'll block it as requested by
5888 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005889 */
Willy Tarreau58975672014-04-24 21:13:57 +02005890 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005891 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005892
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005893 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5894 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005895 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005896 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005897
Willy Tarreau58975672014-04-24 21:13:57 +02005898 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5899 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5900 send_log(s->be, LOG_ALERT,
5901 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5902 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5903 goto return_srv_prx_502;
5904 }
Willy Tarreau03945942009-12-22 16:50:27 +01005905
Willy Tarreau70730dd2014-04-24 18:06:27 +02005906 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005907 /*
5908 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5909 * If an "Upgrade" token is found, the header is left untouched in order
5910 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005911 * if anything but "Upgrade" is present in the Connection header. We don't
5912 * want to touch any 101 response either since it's switching to another
5913 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005914 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005915 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02005916 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005917 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02005918 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
5919 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005920
Willy Tarreau58975672014-04-24 21:13:57 +02005921 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5922 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5923 /* we want a keep-alive response here. Keep-alive header
5924 * required if either side is not 1.1.
5925 */
5926 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5927 want_flags |= TX_CON_KAL_SET;
5928 }
5929 else {
5930 /* we want a close response here. Close header required if
5931 * the server is 1.1, regardless of the client.
5932 */
5933 if (msg->flags & HTTP_MSGF_VER_11)
5934 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005935 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005936
Willy Tarreau58975672014-04-24 21:13:57 +02005937 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5938 http_change_connection_header(txn, msg, want_flags);
5939 }
5940
5941 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005942 /* Always enter in the body analyzer */
5943 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5944 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005945
Willy Tarreau58975672014-04-24 21:13:57 +02005946 /* if the user wants to log as soon as possible, without counting
5947 * bytes from the server, then this is the right moment. We have
5948 * to temporarily assign bytes_out to log what we currently have.
5949 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005950 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005951 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5952 s->logs.bytes_out = txn->rsp.eoh;
5953 s->do_log(s);
5954 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005955 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005956 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005957}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005958
Willy Tarreaud98cf932009-12-27 22:54:55 +01005959/* This function is an analyser which forwards response body (including chunk
5960 * sizes if any). It is called as soon as we must forward, even if we forward
5961 * zero byte. The only situation where it must not be called is when we're in
5962 * tunnel mode and we want to forward till the close. It's used both to forward
5963 * remaining data and to resync after end of body. It expects the msg_state to
5964 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005965 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005966 *
5967 * It is capable of compressing response data both in content-length mode and
5968 * in chunked mode. The state machines follows different flows depending on
5969 * whether content-length and chunked modes are used, since there are no
5970 * trailers in content-length :
5971 *
5972 * chk-mode cl-mode
5973 * ,----- BODY -----.
5974 * / \
5975 * V size > 0 V chk-mode
5976 * .--> SIZE -------------> DATA -------------> CRLF
5977 * | | size == 0 | last byte |
5978 * | v final crlf v inspected |
5979 * | TRAILERS -----------> DONE |
5980 * | |
5981 * `----------------------------------------------'
5982 *
5983 * Compression only happens in the DATA state, and must be flushed in final
5984 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5985 * is performed at once on final states for all bytes parsed, or when leaving
5986 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005987 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005988int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005989{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005990 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005991 struct http_txn *txn = s->txn;
5992 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01005993 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005994
Christopher Faulet814d2702017-03-30 11:33:44 +02005995 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
5996 now_ms, __FUNCTION__,
5997 s,
5998 res,
5999 res->rex, res->wex,
6000 res->flags,
6001 res->buf->i,
6002 res->analysers);
6003
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006004 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6005 return 0;
6006
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006007 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006008 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006009 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006010 /* Output closed while we were sending data. We must abort and
6011 * wake the other side up.
6012 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006013 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006014 msg->msg_state = HTTP_MSG_ERROR;
6015 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006016 return 1;
6017 }
6018
Willy Tarreau4fe41902010-06-07 22:27:41 +02006019 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006020 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006021
Christopher Fauletd7c91962015-04-30 11:48:27 +02006022 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006023 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
6024 ? HTTP_MSG_CHUNK_SIZE
6025 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006026 }
6027
Willy Tarreauefdf0942014-04-24 20:08:57 +02006028 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006029 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02006030 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006031 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006032 }
6033
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006034 if (msg->msg_state < HTTP_MSG_DONE) {
6035 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6036 ? http_msg_forward_chunked_body(s, msg)
6037 : http_msg_forward_body(s, msg));
6038 if (!ret)
6039 goto missing_data_or_waiting;
6040 if (ret < 0)
6041 goto return_bad_res;
6042 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006043
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006044 /* other states, DONE...TUNNEL */
6045 /* for keep-alive we don't want to forward closes on DONE */
6046 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6047 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6048 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006049
Christopher Faulet894da4c2017-07-18 11:29:07 +02006050 http_resync_states(s);
6051 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006052 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6053 if (res->flags & CF_SHUTW) {
6054 /* response errors are most likely due to the
6055 * client aborting the transfer. */
6056 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006057 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006058 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006059 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 +01006060 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006061 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006062 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006063 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006064 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006065
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006066 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006067 if (res->flags & CF_SHUTW)
6068 goto aborted_xfer;
6069
6070 /* stop waiting for data if the input is closed before the end. If the
6071 * client side was already closed, it means that the client has aborted,
6072 * so we don't want to count this as a server abort. Otherwise it's a
6073 * server abort.
6074 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02006075 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006076 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006077 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006078 /* If we have some pending data, we continue the processing */
6079 if (!buffer_pending(res->buf)) {
6080 if (!(s->flags & SF_ERR_MASK))
6081 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006082 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006083 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006084 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006085 goto return_bad_res_stats_ok;
6086 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006087 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006088
Willy Tarreau40dba092010-03-04 18:14:51 +01006089 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006090 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006091 goto return_bad_res;
6092
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006093 /* When TE: chunked is used, we need to get there again to parse
6094 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02006095 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
6096 * or if there are filters registered on the stream, we don't want to
6097 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006098 */
Christopher Faulet69744d92017-03-30 10:54:35 +02006099 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006100 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006101 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6102 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006103 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006104
Willy Tarreau5c620922011-05-11 19:56:11 +02006105 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006106 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006107 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006108 * modes are already handled by the stream sock layer. We must not do
6109 * this in content-length mode because it could present the MSG_MORE
6110 * flag with the last block of forwarded data, which would cause an
6111 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006112 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006113 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006114 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006115
Willy Tarreau87b09662015-04-03 00:22:06 +02006116 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006117 return 0;
6118
Willy Tarreau40dba092010-03-04 18:14:51 +01006119 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006120 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006121 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006122 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006123
6124 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006125 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006126 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006127 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006128 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006129 res->analysers &= AN_RES_FLT_END;
6130 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 +01006131 if (objt_server(s->target))
6132 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006133
Willy Tarreaue7dff022015-04-03 01:14:29 +02006134 if (!(s->flags & SF_ERR_MASK))
6135 s->flags |= SF_ERR_PRXCOND;
6136 if (!(s->flags & SF_FINST_MASK))
6137 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006138 return 0;
6139
6140 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006141 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006142 txn->rsp.msg_state = HTTP_MSG_ERROR;
6143 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006144 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006145 res->analysers &= AN_RES_FLT_END;
6146 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 +01006147
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006148 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
6149 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006150 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006151 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006152
Willy Tarreaue7dff022015-04-03 01:14:29 +02006153 if (!(s->flags & SF_ERR_MASK))
6154 s->flags |= SF_ERR_CLICL;
6155 if (!(s->flags & SF_FINST_MASK))
6156 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006157 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006158}
6159
6160
6161static inline int
6162http_msg_forward_body(struct stream *s, struct http_msg *msg)
6163{
6164 struct channel *chn = msg->chn;
6165 int ret;
6166
6167 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
6168
6169 if (msg->msg_state == HTTP_MSG_ENDING)
6170 goto ending;
6171
6172 /* Neither content-length, nor transfer-encoding was found, so we must
6173 * read the body until the server connection is closed. In that case, we
6174 * eat data as they come. Of course, this happens for response only. */
6175 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6176 unsigned long long len = (chn->buf->i - msg->next);
6177 msg->chunk_len += len;
6178 msg->body_len += len;
6179 }
Christopher Fauletda02e172015-12-04 09:25:05 +01006180 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6181 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6182 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006183 msg->next += ret;
6184 msg->chunk_len -= ret;
6185 if (msg->chunk_len) {
6186 /* input empty or output full */
6187 if (chn->buf->i > msg->next)
6188 chn->flags |= CF_WAKE_WRITE;
6189 goto missing_data_or_waiting;
6190 }
6191
Christopher Faulet1486b0a2017-07-18 11:42:08 +02006192 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
6193 * always set for a request. */
6194 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6195 /* The server still sending data that should be filtered */
6196 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
6197 goto missing_data_or_waiting;
6198 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006199
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006200 msg->msg_state = HTTP_MSG_ENDING;
6201
6202 ending:
6203 /* we may have some pending data starting at res->buf->p such as a last
6204 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006205 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6206 /* default_ret */ msg->next,
6207 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006208 b_adv(chn->buf, ret);
6209 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006210 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6211 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006212 if (msg->next)
6213 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006214
Christopher Fauletda02e172015-12-04 09:25:05 +01006215 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
6216 /* default_ret */ 1,
6217 /* on_error */ goto error,
6218 /* on_wait */ goto waiting);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006219 msg->msg_state = HTTP_MSG_DONE;
6220 return 1;
6221
6222 missing_data_or_waiting:
6223 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006224 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6225 /* default_ret */ msg->next,
6226 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006227 b_adv(chn->buf, ret);
6228 msg->next -= ret;
6229 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6230 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006231 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006232 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006233 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006234 return 0;
6235 error:
6236 return -1;
6237}
6238
6239static inline int
6240http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
6241{
6242 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006243 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006244 int ret;
6245
6246 /* Here we have the guarantee to be in one of the following state:
6247 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
6248 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
6249
6250 switch_states:
6251 switch (msg->msg_state) {
6252 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01006253 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6254 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6255 /* on_error */ goto error);
6256 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006257 msg->chunk_len -= ret;
6258 if (msg->chunk_len) {
6259 /* input empty or output full */
6260 if (chn->buf->i > msg->next)
6261 chn->flags |= CF_WAKE_WRITE;
6262 goto missing_data_or_waiting;
6263 }
6264
6265 /* nothing left to forward for this chunk*/
6266 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
6267 /* fall through for HTTP_MSG_CHUNK_CRLF */
6268
6269 case HTTP_MSG_CHUNK_CRLF:
6270 /* we want the CRLF after the data */
Willy Tarreaub2892562017-09-21 11:33:54 +02006271 ret = h1_skip_chunk_crlf(chn->buf, msg->next, chn->buf->i);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006272 if (ret == 0)
6273 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02006274 if (ret < 0) {
6275 msg->err_pos = chn->buf->i + ret;
6276 if (msg->err_pos < 0)
6277 msg->err_pos += chn->buf->size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006278 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02006279 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01006280 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006281 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6282 /* fall through for HTTP_MSG_CHUNK_SIZE */
6283
6284 case HTTP_MSG_CHUNK_SIZE:
6285 /* read the chunk size and assign it to ->chunk_len,
6286 * then set ->next to point to the body and switch to
6287 * DATA or TRAILERS state.
6288 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006289 ret = h1_parse_chunk_size(chn->buf, msg->next, chn->buf->i, &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006290 if (ret == 0)
6291 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006292 if (ret < 0) {
6293 msg->err_pos = chn->buf->i + ret;
6294 if (msg->err_pos < 0)
6295 msg->err_pos += chn->buf->size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006296 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006297 }
6298
6299 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01006300 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006301 msg->chunk_len = chunk;
6302 msg->body_len += chunk;
6303
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006304 if (msg->chunk_len) {
6305 msg->msg_state = HTTP_MSG_DATA;
6306 goto switch_states;
6307 }
6308 msg->msg_state = HTTP_MSG_TRAILERS;
6309 /* fall through for HTTP_MSG_TRAILERS */
6310
6311 case HTTP_MSG_TRAILERS:
6312 ret = http_forward_trailers(msg);
6313 if (ret < 0)
6314 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01006315 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
6316 /* default_ret */ 1,
6317 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006318 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006319 if (!ret)
6320 goto missing_data_or_waiting;
6321 break;
6322
6323 case HTTP_MSG_ENDING:
6324 goto ending;
6325
6326 default:
6327 /* This should no happen in this function */
6328 goto error;
6329 }
6330
6331 msg->msg_state = HTTP_MSG_ENDING;
6332 ending:
6333 /* we may have some pending data starting at res->buf->p such as a last
6334 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006335 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006336 /* default_ret */ msg->next,
6337 /* on_error */ goto error);
6338 b_adv(chn->buf, ret);
6339 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006340 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6341 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006342 if (msg->next)
6343 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006344
Christopher Fauletda02e172015-12-04 09:25:05 +01006345 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006346 /* default_ret */ 1,
6347 /* on_error */ goto error,
6348 /* on_wait */ goto waiting);
6349 msg->msg_state = HTTP_MSG_DONE;
6350 return 1;
6351
6352 missing_data_or_waiting:
6353 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006354 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006355 /* default_ret */ msg->next,
6356 /* on_error */ goto error);
6357 b_adv(chn->buf, ret);
6358 msg->next -= ret;
6359 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6360 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006361 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006362 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006363 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006364 return 0;
6365
6366 chunk_parsing_error:
6367 if (msg->err_pos >= 0) {
6368 if (chn->flags & CF_ISRESP)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006369 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006370 msg->msg_state, strm_fe(s));
6371 else
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006372 http_capture_bad_message(strm_fe(s), &strm_fe(s)->invalid_req, s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006373 msg, msg->msg_state, s->be);
6374 }
6375 error:
6376 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006377}
6378
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006379
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006380/* Iterate the same filter through all request headers.
6381 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006382 * Since it can manage the switch to another backend, it updates the per-proxy
6383 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006384 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006385int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006386{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006387 char *cur_ptr, *cur_end, *cur_next;
6388 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006389 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006390 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006391 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006392
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006393 last_hdr = 0;
6394
Willy Tarreau9b28e032012-10-12 23:49:43 +02006395 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006396 old_idx = 0;
6397
6398 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006399 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006400 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006401 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006402 (exp->action == ACT_ALLOW ||
6403 exp->action == ACT_DENY ||
6404 exp->action == ACT_TARPIT))
6405 return 0;
6406
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006407 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006408 if (!cur_idx)
6409 break;
6410
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006411 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006412 cur_ptr = cur_next;
6413 cur_end = cur_ptr + cur_hdr->len;
6414 cur_next = cur_end + cur_hdr->cr + 1;
6415
6416 /* Now we have one header between cur_ptr and cur_end,
6417 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006418 */
6419
Willy Tarreau15a53a42015-01-21 13:39:42 +01006420 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006421 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006422 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006423 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006424 last_hdr = 1;
6425 break;
6426
6427 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006428 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006429 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006430 break;
6431
6432 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006433 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006434 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006435 break;
6436
6437 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006438 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6439 if (trash.len < 0)
6440 return -1;
6441
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006442 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006443 /* FIXME: if the user adds a newline in the replacement, the
6444 * index will not be recalculated for now, and the new line
6445 * will not be counted as a new header.
6446 */
6447
6448 cur_end += delta;
6449 cur_next += delta;
6450 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006451 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006452 break;
6453
6454 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006455 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006456 cur_next += delta;
6457
Willy Tarreaufa355d42009-11-29 18:12:29 +01006458 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006459 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6460 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006461 cur_hdr->len = 0;
6462 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006463 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006464 break;
6465
6466 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006467 }
6468
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006469 /* keep the link from this header to next one in case of later
6470 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006471 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006472 old_idx = cur_idx;
6473 }
6474 return 0;
6475}
6476
6477
6478/* Apply the filter to the request line.
6479 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6480 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006481 * Since it can manage the switch to another backend, it updates the per-proxy
6482 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006483 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006484int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006485{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006486 char *cur_ptr, *cur_end;
6487 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006488 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006489 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006490
Willy Tarreau3d300592007-03-18 18:34:41 +01006491 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006492 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006493 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006494 (exp->action == ACT_ALLOW ||
6495 exp->action == ACT_DENY ||
6496 exp->action == ACT_TARPIT))
6497 return 0;
6498 else if (exp->action == ACT_REMOVE)
6499 return 0;
6500
6501 done = 0;
6502
Willy Tarreau9b28e032012-10-12 23:49:43 +02006503 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006504 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006505
6506 /* Now we have the request line between cur_ptr and cur_end */
6507
Willy Tarreau15a53a42015-01-21 13:39:42 +01006508 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006509 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006510 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006511 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006512 done = 1;
6513 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006514
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006515 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006516 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006517 done = 1;
6518 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006519
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006520 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006521 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006522 done = 1;
6523 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006524
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006525 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006526 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6527 if (trash.len < 0)
6528 return -1;
6529
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006530 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006531 /* FIXME: if the user adds a newline in the replacement, the
6532 * index will not be recalculated for now, and the new line
6533 * will not be counted as a new header.
6534 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006535
Willy Tarreaufa355d42009-11-29 18:12:29 +01006536 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006537 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006538 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006539 HTTP_MSG_RQMETH,
6540 cur_ptr, cur_end + 1,
6541 NULL, NULL);
6542 if (unlikely(!cur_end))
6543 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006544
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006545 /* we have a full request and we know that we have either a CR
6546 * or an LF at <ptr>.
6547 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006548 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6549 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006550 /* there is no point trying this regex on headers */
6551 return 1;
6552 }
6553 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006554 return done;
6555}
Willy Tarreau97de6242006-12-27 17:18:38 +01006556
Willy Tarreau58f10d72006-12-04 02:26:12 +01006557
Willy Tarreau58f10d72006-12-04 02:26:12 +01006558
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006559/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006560 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006561 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006562 * unparsable request. Since it can manage the switch to another backend, it
6563 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006564 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006565int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006566{
Willy Tarreau192252e2015-04-04 01:47:55 +02006567 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006568 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006569 struct hdr_exp *exp;
6570
6571 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006572 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006573
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006574 /*
6575 * The interleaving of transformations and verdicts
6576 * makes it difficult to decide to continue or stop
6577 * the evaluation.
6578 */
6579
Willy Tarreau6c123b12010-01-28 20:22:06 +01006580 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6581 break;
6582
Willy Tarreau3d300592007-03-18 18:34:41 +01006583 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006584 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006585 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006586 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006587
6588 /* if this filter had a condition, evaluate it now and skip to
6589 * next filter if the condition does not match.
6590 */
6591 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006592 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006593 ret = acl_pass(ret);
6594 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6595 ret = !ret;
6596
6597 if (!ret)
6598 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006599 }
6600
6601 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006602 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006603 if (unlikely(ret < 0))
6604 return -1;
6605
6606 if (likely(ret == 0)) {
6607 /* The filter did not match the request, it can be
6608 * iterated through all headers.
6609 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006610 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6611 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006612 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006613 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006614 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006615}
6616
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006617
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006618/* Find the end of a cookie value contained between <s> and <e>. It works the
6619 * same way as with headers above except that the semi-colon also ends a token.
6620 * See RFC2965 for more information. Note that it requires a valid header to
6621 * return a valid result.
6622 */
6623char *find_cookie_value_end(char *s, const char *e)
6624{
6625 int quoted, qdpair;
6626
6627 quoted = qdpair = 0;
6628 for (; s < e; s++) {
6629 if (qdpair) qdpair = 0;
6630 else if (quoted) {
6631 if (*s == '\\') qdpair = 1;
6632 else if (*s == '"') quoted = 0;
6633 }
6634 else if (*s == '"') quoted = 1;
6635 else if (*s == ',' || *s == ';') return s;
6636 }
6637 return s;
6638}
6639
6640/* Delete a value in a header between delimiters <from> and <next> in buffer
6641 * <buf>. The number of characters displaced is returned, and the pointer to
6642 * the first delimiter is updated if required. The function tries as much as
6643 * possible to respect the following principles :
6644 * - replace <from> delimiter by the <next> one unless <from> points to a
6645 * colon, in which case <next> is simply removed
6646 * - set exactly one space character after the new first delimiter, unless
6647 * there are not enough characters in the block being moved to do so.
6648 * - remove unneeded spaces before the previous delimiter and after the new
6649 * one.
6650 *
6651 * It is the caller's responsibility to ensure that :
6652 * - <from> points to a valid delimiter or the colon ;
6653 * - <next> points to a valid delimiter or the final CR/LF ;
6654 * - there are non-space chars before <from> ;
6655 * - there is a CR/LF at or after <next>.
6656 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006657int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006658{
6659 char *prev = *from;
6660
6661 if (*prev == ':') {
6662 /* We're removing the first value, preserve the colon and add a
6663 * space if possible.
6664 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006665 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006666 next++;
6667 prev++;
6668 if (prev < next)
6669 *prev++ = ' ';
6670
Willy Tarreau2235b262016-11-05 15:50:20 +01006671 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006672 next++;
6673 } else {
6674 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006675 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006676 prev--;
6677 *from = prev;
6678
6679 /* copy the delimiter and if possible a space if we're
6680 * not at the end of the line.
6681 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006682 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006683 *prev++ = *next++;
6684 if (prev + 1 < next)
6685 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006686 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006687 next++;
6688 }
6689 }
6690 return buffer_replace2(buf, prev, next, NULL, 0);
6691}
6692
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006693/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006694 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006695 * desirable to call it only when needed. This code is quite complex because
6696 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6697 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006698 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006699void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006700{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006701 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006702 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006703 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006704 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006705 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6706 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006707
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006708 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006709 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006710 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006711
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006712 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006713 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006714 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006715
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006716 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006717 hdr_beg = hdr_next;
6718 hdr_end = hdr_beg + cur_hdr->len;
6719 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006720
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006721 /* We have one full header between hdr_beg and hdr_end, and the
6722 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006723 * "Cookie:" headers.
6724 */
6725
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006726 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006727 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006728 old_idx = cur_idx;
6729 continue;
6730 }
6731
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006732 del_from = NULL; /* nothing to be deleted */
6733 preserve_hdr = 0; /* assume we may kill the whole header */
6734
Willy Tarreau58f10d72006-12-04 02:26:12 +01006735 /* Now look for cookies. Conforming to RFC2109, we have to support
6736 * attributes whose name begin with a '$', and associate them with
6737 * the right cookie, if we want to delete this cookie.
6738 * So there are 3 cases for each cookie read :
6739 * 1) it's a special attribute, beginning with a '$' : ignore it.
6740 * 2) it's a server id cookie that we *MAY* want to delete : save
6741 * some pointers on it (last semi-colon, beginning of cookie...)
6742 * 3) it's an application cookie : we *MAY* have to delete a previous
6743 * "special" cookie.
6744 * At the end of loop, if a "special" cookie remains, we may have to
6745 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006746 * *MUST* delete it.
6747 *
6748 * Note: RFC2965 is unclear about the processing of spaces around
6749 * the equal sign in the ATTR=VALUE form. A careful inspection of
6750 * the RFC explicitly allows spaces before it, and not within the
6751 * tokens (attrs or values). An inspection of RFC2109 allows that
6752 * too but section 10.1.3 lets one think that spaces may be allowed
6753 * after the equal sign too, resulting in some (rare) buggy
6754 * implementations trying to do that. So let's do what servers do.
6755 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6756 * allowed quoted strings in values, with any possible character
6757 * after a backslash, including control chars and delimitors, which
6758 * causes parsing to become ambiguous. Browsers also allow spaces
6759 * within values even without quotes.
6760 *
6761 * We have to keep multiple pointers in order to support cookie
6762 * removal at the beginning, middle or end of header without
6763 * corrupting the header. All of these headers are valid :
6764 *
6765 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6766 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6767 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6768 * | | | | | | | | |
6769 * | | | | | | | | hdr_end <--+
6770 * | | | | | | | +--> next
6771 * | | | | | | +----> val_end
6772 * | | | | | +-----------> val_beg
6773 * | | | | +--------------> equal
6774 * | | | +----------------> att_end
6775 * | | +---------------------> att_beg
6776 * | +--------------------------> prev
6777 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006778 */
6779
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006780 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6781 /* Iterate through all cookies on this line */
6782
6783 /* find att_beg */
6784 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006785 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006786 att_beg++;
6787
6788 /* find att_end : this is the first character after the last non
6789 * space before the equal. It may be equal to hdr_end.
6790 */
6791 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006792
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006793 while (equal < hdr_end) {
6794 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006795 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006796 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006797 continue;
6798 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006799 }
6800
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006801 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6802 * is between <att_beg> and <equal>, both may be identical.
6803 */
6804
6805 /* look for end of cookie if there is an equal sign */
6806 if (equal < hdr_end && *equal == '=') {
6807 /* look for the beginning of the value */
6808 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006809 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006810 val_beg++;
6811
6812 /* find the end of the value, respecting quotes */
6813 next = find_cookie_value_end(val_beg, hdr_end);
6814
6815 /* make val_end point to the first white space or delimitor after the value */
6816 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006817 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006818 val_end--;
6819 } else {
6820 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006821 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006822
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006823 /* We have nothing to do with attributes beginning with '$'. However,
6824 * they will automatically be removed if a header before them is removed,
6825 * since they're supposed to be linked together.
6826 */
6827 if (*att_beg == '$')
6828 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006829
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006830 /* Ignore cookies with no equal sign */
6831 if (equal == next) {
6832 /* This is not our cookie, so we must preserve it. But if we already
6833 * scheduled another cookie for removal, we cannot remove the
6834 * complete header, but we can remove the previous block itself.
6835 */
6836 preserve_hdr = 1;
6837 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006838 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006839 val_end += delta;
6840 next += delta;
6841 hdr_end += delta;
6842 hdr_next += delta;
6843 cur_hdr->len += delta;
6844 http_msg_move_end(&txn->req, delta);
6845 prev = del_from;
6846 del_from = NULL;
6847 }
6848 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006849 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006850
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006851 /* if there are spaces around the equal sign, we need to
6852 * strip them otherwise we'll get trouble for cookie captures,
6853 * or even for rewrites. Since this happens extremely rarely,
6854 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006855 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006856 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6857 int stripped_before = 0;
6858 int stripped_after = 0;
6859
6860 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006861 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006862 equal += stripped_before;
6863 val_beg += stripped_before;
6864 }
6865
6866 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006867 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006868 val_beg += stripped_after;
6869 stripped_before += stripped_after;
6870 }
6871
6872 val_end += stripped_before;
6873 next += stripped_before;
6874 hdr_end += stripped_before;
6875 hdr_next += stripped_before;
6876 cur_hdr->len += stripped_before;
6877 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006878 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006879 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006880
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006881 /* First, let's see if we want to capture this cookie. We check
6882 * that we don't already have a client side cookie, because we
6883 * can only capture one. Also as an optimisation, we ignore
6884 * cookies shorter than the declared name.
6885 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006886 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6887 (val_end - att_beg >= sess->fe->capture_namelen) &&
6888 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006889 int log_len = val_end - att_beg;
6890
6891 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6892 Alert("HTTP logging : out of memory.\n");
6893 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006894 if (log_len > sess->fe->capture_len)
6895 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006896 memcpy(txn->cli_cookie, att_beg, log_len);
6897 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006898 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006899 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006900
Willy Tarreaubca99692010-10-06 19:25:55 +02006901 /* Persistence cookies in passive, rewrite or insert mode have the
6902 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006903 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006904 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006905 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006906 * For cookies in prefix mode, the form is :
6907 *
6908 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006909 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006910 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6911 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6912 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006913 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006914
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006915 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6916 * have the server ID between val_beg and delim, and the original cookie between
6917 * delim+1 and val_end. Otherwise, delim==val_end :
6918 *
6919 * Cookie: NAME=SRV; # in all but prefix modes
6920 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6921 * | || || | |+-> next
6922 * | || || | +--> val_end
6923 * | || || +---------> delim
6924 * | || |+------------> val_beg
6925 * | || +-------------> att_end = equal
6926 * | |+-----------------> att_beg
6927 * | +------------------> prev
6928 * +-------------------------> hdr_beg
6929 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006930
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006931 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006932 for (delim = val_beg; delim < val_end; delim++)
6933 if (*delim == COOKIE_DELIM)
6934 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006935 } else {
6936 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006937 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006938 /* Now check if the cookie contains a date field, which would
6939 * appear after a vertical bar ('|') just after the server name
6940 * and before the delimiter.
6941 */
6942 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6943 if (vbar1) {
6944 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006945 * right is the last seen date. It is a base64 encoded
6946 * 30-bit value representing the UNIX date since the
6947 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006948 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006949 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006950 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006951 if (val_end - vbar1 >= 5) {
6952 val = b64tos30(vbar1);
6953 if (val > 0)
6954 txn->cookie_last_date = val << 2;
6955 }
6956 /* look for a second vertical bar */
6957 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6958 if (vbar1 && (val_end - vbar1 > 5)) {
6959 val = b64tos30(vbar1 + 1);
6960 if (val > 0)
6961 txn->cookie_first_date = val << 2;
6962 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006963 }
6964 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006965
Willy Tarreauf64d1412010-10-07 20:06:11 +02006966 /* if the cookie has an expiration date and the proxy wants to check
6967 * it, then we do that now. We first check if the cookie is too old,
6968 * then only if it has expired. We detect strict overflow because the
6969 * time resolution here is not great (4 seconds). Cookies with dates
6970 * in the future are ignored if their offset is beyond one day. This
6971 * allows an admin to fix timezone issues without expiring everyone
6972 * and at the same time avoids keeping unwanted side effects for too
6973 * long.
6974 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006975 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6976 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006977 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006978 txn->flags &= ~TX_CK_MASK;
6979 txn->flags |= TX_CK_OLD;
6980 delim = val_beg; // let's pretend we have not found the cookie
6981 txn->cookie_first_date = 0;
6982 txn->cookie_last_date = 0;
6983 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006984 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6985 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006986 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006987 txn->flags &= ~TX_CK_MASK;
6988 txn->flags |= TX_CK_EXPIRED;
6989 delim = val_beg; // let's pretend we have not found the cookie
6990 txn->cookie_first_date = 0;
6991 txn->cookie_last_date = 0;
6992 }
6993
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006994 /* Here, we'll look for the first running server which supports the cookie.
6995 * This allows to share a same cookie between several servers, for example
6996 * to dedicate backup servers to specific servers only.
6997 * However, to prevent clients from sticking to cookie-less backup server
6998 * when they have incidentely learned an empty cookie, we simply ignore
6999 * empty cookies and mark them as invalid.
7000 * The same behaviour is applied when persistence must be ignored.
7001 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007002 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007003 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007004
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007005 while (srv) {
7006 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7007 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02007008 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007009 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007010 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007011 /* we found the server and we can use it */
7012 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02007013 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007014 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007015 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007016 break;
7017 } else {
7018 /* we found a server, but it's down,
7019 * mark it as such and go on in case
7020 * another one is available.
7021 */
7022 txn->flags &= ~TX_CK_MASK;
7023 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007024 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007025 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007026 srv = srv->next;
7027 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007028
Willy Tarreauf64d1412010-10-07 20:06:11 +02007029 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007030 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007031 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007032 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007033 txn->flags |= TX_CK_UNUSED;
7034 else
7035 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007036 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007037
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007038 /* depending on the cookie mode, we may have to either :
7039 * - delete the complete cookie if we're in insert+indirect mode, so that
7040 * the server never sees it ;
7041 * - remove the server id from the cookie value, and tag the cookie as an
7042 * application cookie so that it does not get accidentely removed later,
7043 * if we're in cookie prefix mode
7044 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007045 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007046 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007047
Willy Tarreau9b28e032012-10-12 23:49:43 +02007048 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007049 val_end += delta;
7050 next += delta;
7051 hdr_end += delta;
7052 hdr_next += delta;
7053 cur_hdr->len += delta;
7054 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007055
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007056 del_from = NULL;
7057 preserve_hdr = 1; /* we want to keep this cookie */
7058 }
7059 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007060 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007061 del_from = prev;
7062 }
7063 } else {
7064 /* This is not our cookie, so we must preserve it. But if we already
7065 * scheduled another cookie for removal, we cannot remove the
7066 * complete header, but we can remove the previous block itself.
7067 */
7068 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007069
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007070 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007071 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007072 if (att_beg >= del_from)
7073 att_beg += delta;
7074 if (att_end >= del_from)
7075 att_end += delta;
7076 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007077 val_end += delta;
7078 next += delta;
7079 hdr_end += delta;
7080 hdr_next += delta;
7081 cur_hdr->len += delta;
7082 http_msg_move_end(&txn->req, delta);
7083 prev = del_from;
7084 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007085 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007086 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007087
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007088 /* continue with next cookie on this header line */
7089 att_beg = next;
7090 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007091
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007092 /* There are no more cookies on this line.
7093 * We may still have one (or several) marked for deletion at the
7094 * end of the line. We must do this now in two ways :
7095 * - if some cookies must be preserved, we only delete from the
7096 * mark to the end of line ;
7097 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007098 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007099 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007100 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007101 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007102 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007103 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007104 cur_hdr->len += delta;
7105 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007106 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007107
7108 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007109 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7110 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007111 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007112 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007113 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007114 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007115 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007116 }
7117
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007118 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007119 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007120 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007121}
7122
7123
Willy Tarreaua15645d2007-03-18 16:22:39 +01007124/* Iterate the same filter through all response headers contained in <rtr>.
7125 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7126 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007127int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007128{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007129 char *cur_ptr, *cur_end, *cur_next;
7130 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007131 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007132 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007133 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007134
7135 last_hdr = 0;
7136
Willy Tarreau9b28e032012-10-12 23:49:43 +02007137 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007138 old_idx = 0;
7139
7140 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007141 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007142 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007143 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007144 (exp->action == ACT_ALLOW ||
7145 exp->action == ACT_DENY))
7146 return 0;
7147
7148 cur_idx = txn->hdr_idx.v[old_idx].next;
7149 if (!cur_idx)
7150 break;
7151
7152 cur_hdr = &txn->hdr_idx.v[cur_idx];
7153 cur_ptr = cur_next;
7154 cur_end = cur_ptr + cur_hdr->len;
7155 cur_next = cur_end + cur_hdr->cr + 1;
7156
7157 /* Now we have one header between cur_ptr and cur_end,
7158 * and the next header starts at cur_next.
7159 */
7160
Willy Tarreau15a53a42015-01-21 13:39:42 +01007161 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007162 switch (exp->action) {
7163 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007164 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007165 last_hdr = 1;
7166 break;
7167
7168 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007169 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007170 last_hdr = 1;
7171 break;
7172
7173 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007174 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7175 if (trash.len < 0)
7176 return -1;
7177
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007178 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007179 /* FIXME: if the user adds a newline in the replacement, the
7180 * index will not be recalculated for now, and the new line
7181 * will not be counted as a new header.
7182 */
7183
7184 cur_end += delta;
7185 cur_next += delta;
7186 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007187 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007188 break;
7189
7190 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007191 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007192 cur_next += delta;
7193
Willy Tarreaufa355d42009-11-29 18:12:29 +01007194 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007195 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7196 txn->hdr_idx.used--;
7197 cur_hdr->len = 0;
7198 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007199 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007200 break;
7201
7202 }
7203 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007204
7205 /* keep the link from this header to next one in case of later
7206 * removal of next header.
7207 */
7208 old_idx = cur_idx;
7209 }
7210 return 0;
7211}
7212
7213
7214/* Apply the filter to the status line in the response buffer <rtr>.
7215 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7216 * or -1 if a replacement resulted in an invalid status line.
7217 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007218int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007219{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007220 char *cur_ptr, *cur_end;
7221 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007222 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007223 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007224
7225
Willy Tarreau3d300592007-03-18 18:34:41 +01007226 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007227 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007228 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007229 (exp->action == ACT_ALLOW ||
7230 exp->action == ACT_DENY))
7231 return 0;
7232 else if (exp->action == ACT_REMOVE)
7233 return 0;
7234
7235 done = 0;
7236
Willy Tarreau9b28e032012-10-12 23:49:43 +02007237 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007238 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007239
7240 /* Now we have the status line between cur_ptr and cur_end */
7241
Willy Tarreau15a53a42015-01-21 13:39:42 +01007242 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007243 switch (exp->action) {
7244 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007245 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007246 done = 1;
7247 break;
7248
7249 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007250 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007251 done = 1;
7252 break;
7253
7254 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007255 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7256 if (trash.len < 0)
7257 return -1;
7258
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007259 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007260 /* FIXME: if the user adds a newline in the replacement, the
7261 * index will not be recalculated for now, and the new line
7262 * will not be counted as a new header.
7263 */
7264
Willy Tarreaufa355d42009-11-29 18:12:29 +01007265 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007266 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007267 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007268 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007269 cur_ptr, cur_end + 1,
7270 NULL, NULL);
7271 if (unlikely(!cur_end))
7272 return -1;
7273
7274 /* we have a full respnse and we know that we have either a CR
7275 * or an LF at <ptr>.
7276 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007277 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007278 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007279 /* there is no point trying this regex on headers */
7280 return 1;
7281 }
7282 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007283 return done;
7284}
7285
7286
7287
7288/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007289 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007290 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7291 * unparsable response.
7292 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007293int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007294{
Willy Tarreau192252e2015-04-04 01:47:55 +02007295 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007296 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007297 struct hdr_exp *exp;
7298
7299 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007300 int ret;
7301
7302 /*
7303 * The interleaving of transformations and verdicts
7304 * makes it difficult to decide to continue or stop
7305 * the evaluation.
7306 */
7307
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007308 if (txn->flags & TX_SVDENY)
7309 break;
7310
Willy Tarreau3d300592007-03-18 18:34:41 +01007311 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007312 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7313 exp->action == ACT_PASS)) {
7314 exp = exp->next;
7315 continue;
7316 }
7317
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007318 /* if this filter had a condition, evaluate it now and skip to
7319 * next filter if the condition does not match.
7320 */
7321 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007322 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007323 ret = acl_pass(ret);
7324 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7325 ret = !ret;
7326 if (!ret)
7327 continue;
7328 }
7329
Willy Tarreaua15645d2007-03-18 16:22:39 +01007330 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007331 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007332 if (unlikely(ret < 0))
7333 return -1;
7334
7335 if (likely(ret == 0)) {
7336 /* The filter did not match the response, it can be
7337 * iterated through all headers.
7338 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007339 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7340 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007341 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007342 }
7343 return 0;
7344}
7345
7346
Willy Tarreaua15645d2007-03-18 16:22:39 +01007347/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007348 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007349 * desirable to call it only when needed. This function is also used when we
7350 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007351 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007352void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007353{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007354 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007355 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01007356 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007357 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007358 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007359 char *hdr_beg, *hdr_end, *hdr_next;
7360 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007361
Willy Tarreaua15645d2007-03-18 16:22:39 +01007362 /* Iterate through the headers.
7363 * we start with the start line.
7364 */
7365 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007366 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007367
7368 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7369 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007370 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007371
7372 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007373 hdr_beg = hdr_next;
7374 hdr_end = hdr_beg + cur_hdr->len;
7375 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007376
Willy Tarreau24581ba2010-08-31 22:39:35 +02007377 /* We have one full header between hdr_beg and hdr_end, and the
7378 * next header starts at hdr_next. We're only interested in
7379 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007380 */
7381
Willy Tarreau24581ba2010-08-31 22:39:35 +02007382 is_cookie2 = 0;
7383 prev = hdr_beg + 10;
7384 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007385 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007386 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7387 if (!val) {
7388 old_idx = cur_idx;
7389 continue;
7390 }
7391 is_cookie2 = 1;
7392 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007393 }
7394
Willy Tarreau24581ba2010-08-31 22:39:35 +02007395 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7396 * <prev> points to the colon.
7397 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007398 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007399
Willy Tarreau24581ba2010-08-31 22:39:35 +02007400 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7401 * check-cache is enabled) and we are not interested in checking
7402 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007403 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02007404 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007405 return;
7406
Willy Tarreau24581ba2010-08-31 22:39:35 +02007407 /* OK so now we know we have to process this response cookie.
7408 * The format of the Set-Cookie header is slightly different
7409 * from the format of the Cookie header in that it does not
7410 * support the comma as a cookie delimiter (thus the header
7411 * cannot be folded) because the Expires attribute described in
7412 * the original Netscape's spec may contain an unquoted date
7413 * with a comma inside. We have to live with this because
7414 * many browsers don't support Max-Age and some browsers don't
7415 * support quoted strings. However the Set-Cookie2 header is
7416 * clean.
7417 *
7418 * We have to keep multiple pointers in order to support cookie
7419 * removal at the beginning, middle or end of header without
7420 * corrupting the header (in case of set-cookie2). A special
7421 * pointer, <scav> points to the beginning of the set-cookie-av
7422 * fields after the first semi-colon. The <next> pointer points
7423 * either to the end of line (set-cookie) or next unquoted comma
7424 * (set-cookie2). All of these headers are valid :
7425 *
7426 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7427 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7428 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7429 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7430 * | | | | | | | | | |
7431 * | | | | | | | | +-> next hdr_end <--+
7432 * | | | | | | | +------------> scav
7433 * | | | | | | +--------------> val_end
7434 * | | | | | +--------------------> val_beg
7435 * | | | | +----------------------> equal
7436 * | | | +------------------------> att_end
7437 * | | +----------------------------> att_beg
7438 * | +------------------------------> prev
7439 * +-----------------------------------------> hdr_beg
7440 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007441
Willy Tarreau24581ba2010-08-31 22:39:35 +02007442 for (; prev < hdr_end; prev = next) {
7443 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007444
Willy Tarreau24581ba2010-08-31 22:39:35 +02007445 /* find att_beg */
7446 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007447 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007448 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007449
Willy Tarreau24581ba2010-08-31 22:39:35 +02007450 /* find att_end : this is the first character after the last non
7451 * space before the equal. It may be equal to hdr_end.
7452 */
7453 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007454
Willy Tarreau24581ba2010-08-31 22:39:35 +02007455 while (equal < hdr_end) {
7456 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7457 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007458 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007459 continue;
7460 att_end = equal;
7461 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007462
Willy Tarreau24581ba2010-08-31 22:39:35 +02007463 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7464 * is between <att_beg> and <equal>, both may be identical.
7465 */
7466
7467 /* look for end of cookie if there is an equal sign */
7468 if (equal < hdr_end && *equal == '=') {
7469 /* look for the beginning of the value */
7470 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007471 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007472 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007473
Willy Tarreau24581ba2010-08-31 22:39:35 +02007474 /* find the end of the value, respecting quotes */
7475 next = find_cookie_value_end(val_beg, hdr_end);
7476
7477 /* make val_end point to the first white space or delimitor after the value */
7478 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007479 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007480 val_end--;
7481 } else {
7482 /* <equal> points to next comma, semi-colon or EOL */
7483 val_beg = val_end = next = equal;
7484 }
7485
7486 if (next < hdr_end) {
7487 /* Set-Cookie2 supports multiple cookies, and <next> points to
7488 * a colon or semi-colon before the end. So skip all attr-value
7489 * pairs and look for the next comma. For Set-Cookie, since
7490 * commas are permitted in values, skip to the end.
7491 */
7492 if (is_cookie2)
7493 next = find_hdr_value_end(next, hdr_end);
7494 else
7495 next = hdr_end;
7496 }
7497
7498 /* Now everything is as on the diagram above */
7499
7500 /* Ignore cookies with no equal sign */
7501 if (equal == val_end)
7502 continue;
7503
7504 /* If there are spaces around the equal sign, we need to
7505 * strip them otherwise we'll get trouble for cookie captures,
7506 * or even for rewrites. Since this happens extremely rarely,
7507 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007508 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007509 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7510 int stripped_before = 0;
7511 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007512
Willy Tarreau24581ba2010-08-31 22:39:35 +02007513 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007514 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007515 equal += stripped_before;
7516 val_beg += stripped_before;
7517 }
7518
7519 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007520 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007521 val_beg += stripped_after;
7522 stripped_before += stripped_after;
7523 }
7524
7525 val_end += stripped_before;
7526 next += stripped_before;
7527 hdr_end += stripped_before;
7528 hdr_next += stripped_before;
7529 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007530 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007531 }
7532
7533 /* First, let's see if we want to capture this cookie. We check
7534 * that we don't already have a server side cookie, because we
7535 * can only capture one. Also as an optimisation, we ignore
7536 * cookies shorter than the declared name.
7537 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007538 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007539 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007540 (val_end - att_beg >= sess->fe->capture_namelen) &&
7541 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007542 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007543 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007544 Alert("HTTP logging : out of memory.\n");
7545 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007546 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007547 if (log_len > sess->fe->capture_len)
7548 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007549 memcpy(txn->srv_cookie, att_beg, log_len);
7550 txn->srv_cookie[log_len] = 0;
7551 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007552 }
7553
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007554 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007555 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007556 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007557 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7558 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007559 /* assume passive cookie by default */
7560 txn->flags &= ~TX_SCK_MASK;
7561 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007562
7563 /* If the cookie is in insert mode on a known server, we'll delete
7564 * this occurrence because we'll insert another one later.
7565 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007566 * a direct access.
7567 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007568 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007569 /* The "preserve" flag was set, we don't want to touch the
7570 * server's cookie.
7571 */
7572 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007573 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007574 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007575 /* this cookie must be deleted */
7576 if (*prev == ':' && next == hdr_end) {
7577 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007578 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007579 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7580 txn->hdr_idx.used--;
7581 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007582 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007583 hdr_next += delta;
7584 http_msg_move_end(&txn->rsp, delta);
7585 /* note: while both invalid now, <next> and <hdr_end>
7586 * are still equal, so the for() will stop as expected.
7587 */
7588 } else {
7589 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007590 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007591 next = prev;
7592 hdr_end += delta;
7593 hdr_next += delta;
7594 cur_hdr->len += delta;
7595 http_msg_move_end(&txn->rsp, delta);
7596 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007597 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007598 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007599 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007600 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007601 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007602 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007603 * with this server since we know it.
7604 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007605 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007606 next += delta;
7607 hdr_end += delta;
7608 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007609 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007610 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007611
Willy Tarreauf1348312010-10-07 15:54:11 +02007612 txn->flags &= ~TX_SCK_MASK;
7613 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007614 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007615 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007616 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007617 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007618 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007619 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007620 next += delta;
7621 hdr_end += delta;
7622 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007623 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007624 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007625
Willy Tarreau827aee92011-03-10 16:55:02 +01007626 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007627 txn->flags &= ~TX_SCK_MASK;
7628 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007629 }
7630 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007631 /* that's done for this cookie, check the next one on the same
7632 * line when next != hdr_end (only if is_cookie2).
7633 */
7634 }
7635 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007636 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007637 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007638}
7639
7640
Willy Tarreaua15645d2007-03-18 16:22:39 +01007641/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007642 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007643 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007644void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007645{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007646 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007647 char *p1, *p2;
7648
7649 char *cur_ptr, *cur_end, *cur_next;
7650 int cur_idx;
7651
Willy Tarreau5df51872007-11-25 16:20:08 +01007652 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007653 return;
7654
7655 /* Iterate through the headers.
7656 * we start with the start line.
7657 */
7658 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007659 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007660
7661 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7662 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007663 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007664
7665 cur_hdr = &txn->hdr_idx.v[cur_idx];
7666 cur_ptr = cur_next;
7667 cur_end = cur_ptr + cur_hdr->len;
7668 cur_next = cur_end + cur_hdr->cr + 1;
7669
7670 /* We have one full header between cur_ptr and cur_end, and the
7671 * next header starts at cur_next. We're only interested in
7672 * "Cookie:" headers.
7673 */
7674
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007675 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7676 if (val) {
7677 if ((cur_end - (cur_ptr + val) >= 8) &&
7678 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7679 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7680 return;
7681 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007682 }
7683
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007684 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7685 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007686 continue;
7687
7688 /* OK, right now we know we have a cache-control header at cur_ptr */
7689
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007690 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007691
7692 if (p1 >= cur_end) /* no more info */
7693 continue;
7694
7695 /* p1 is at the beginning of the value */
7696 p2 = p1;
7697
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007698 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007699 p2++;
7700
7701 /* we have a complete value between p1 and p2 */
7702 if (p2 < cur_end && *p2 == '=') {
7703 /* we have something of the form no-cache="set-cookie" */
7704 if ((cur_end - p1 >= 21) &&
7705 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7706 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007707 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007708 continue;
7709 }
7710
7711 /* OK, so we know that either p2 points to the end of string or to a comma */
7712 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007713 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01007714 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7715 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7716 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007717 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007718 return;
7719 }
7720
7721 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007722 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007723 continue;
7724 }
7725 }
7726}
7727
Willy Tarreau58f10d72006-12-04 02:26:12 +01007728
Willy Tarreaub2513902006-12-17 14:52:38 +01007729/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007730 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007731 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007732 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007733 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007734 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007735 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007736 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007737 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007738int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007739{
7740 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007741 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007742 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007743
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007744 if (!uri_auth)
7745 return 0;
7746
Cyril Bonté70be45d2010-10-12 00:14:35 +02007747 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007748 return 0;
7749
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007750 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007751 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007752 return 0;
7753
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007754 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007755 return 0;
7756
Willy Tarreaub2513902006-12-17 14:52:38 +01007757 return 1;
7758}
7759
Willy Tarreau4076a152009-04-02 15:18:36 +02007760/*
7761 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007762 * By default it tries to report the error position as msg->err_pos. However if
7763 * this one is not set, it will then report msg->next, which is the last known
7764 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007765 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007766 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02007767void http_capture_bad_message(struct proxy *proxy, struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007768 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007769 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007770{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007771 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007772 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007773 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007774
Christopher Faulet2a944ee2017-11-07 10:42:54 +01007775 HA_SPIN_LOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreauf3764b72016-03-31 13:45:10 +02007776 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02007777 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007778 len1 = MIN(len1, es->len);
7779 len2 = es->len - len1; /* remaining data if buffer wraps */
7780
Willy Tarreauf3764b72016-03-31 13:45:10 +02007781 if (!es->buf)
7782 es->buf = malloc(global.tune.bufsize);
7783
7784 if (es->buf) {
7785 memcpy(es->buf, chn->buf->p, len1);
7786 if (len2)
7787 memcpy(es->buf + len1, chn->buf->data, len2);
7788 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007789
Willy Tarreau4076a152009-04-02 15:18:36 +02007790 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007791 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007792 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007793 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007794
Willy Tarreau4076a152009-04-02 15:18:36 +02007795 es->when = date; // user-visible date
7796 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007797 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007798 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007799 if (objt_conn(sess->origin))
7800 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007801 else
7802 memset(&es->src, 0, sizeof(es->src));
7803
Willy Tarreau078272e2010-12-12 12:46:33 +01007804 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007805 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007806 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007807 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007808 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007809 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007810 es->b_out = chn->buf->o;
7811 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007812 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007813 es->m_clen = msg->chunk_len;
7814 es->m_blen = msg->body_len;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01007815 HA_SPIN_UNLOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreau4076a152009-04-02 15:18:36 +02007816}
Willy Tarreaub2513902006-12-17 14:52:38 +01007817
Willy Tarreau294c4732011-12-16 21:35:50 +01007818/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7819 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7820 * performed over the whole headers. Otherwise it must contain a valid header
7821 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7822 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7823 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7824 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007825 * -1. The value fetch stops at commas, so this function is suited for use with
7826 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007827 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007828 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007829unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007830 struct hdr_idx *idx, int occ,
7831 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007832{
Willy Tarreau294c4732011-12-16 21:35:50 +01007833 struct hdr_ctx local_ctx;
7834 char *ptr_hist[MAX_HDR_HISTORY];
7835 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007836 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007837 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007838
Willy Tarreau294c4732011-12-16 21:35:50 +01007839 if (!ctx) {
7840 local_ctx.idx = 0;
7841 ctx = &local_ctx;
7842 }
7843
Willy Tarreaubce70882009-09-07 11:51:47 +02007844 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007845 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007846 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007847 occ--;
7848 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007849 *vptr = ctx->line + ctx->val;
7850 *vlen = ctx->vlen;
7851 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007852 }
7853 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007854 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007855 }
7856
7857 /* negative occurrence, we scan all the list then walk back */
7858 if (-occ > MAX_HDR_HISTORY)
7859 return 0;
7860
Willy Tarreau294c4732011-12-16 21:35:50 +01007861 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007862 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007863 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7864 len_hist[hist_ptr] = ctx->vlen;
7865 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007866 hist_ptr = 0;
7867 found++;
7868 }
7869 if (-occ > found)
7870 return 0;
7871 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007872 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7873 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7874 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007875 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007876 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007877 if (hist_ptr >= MAX_HDR_HISTORY)
7878 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007879 *vptr = ptr_hist[hist_ptr];
7880 *vlen = len_hist[hist_ptr];
7881 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007882}
7883
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007884/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7885 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7886 * performed over the whole headers. Otherwise it must contain a valid header
7887 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7888 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7889 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7890 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7891 * -1. This function differs from http_get_hdr() in that it only returns full
7892 * line header values and does not stop at commas.
7893 * The return value is 0 if nothing was found, or non-zero otherwise.
7894 */
7895unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7896 struct hdr_idx *idx, int occ,
7897 struct hdr_ctx *ctx, char **vptr, int *vlen)
7898{
7899 struct hdr_ctx local_ctx;
7900 char *ptr_hist[MAX_HDR_HISTORY];
7901 int len_hist[MAX_HDR_HISTORY];
7902 unsigned int hist_ptr;
7903 int found;
7904
7905 if (!ctx) {
7906 local_ctx.idx = 0;
7907 ctx = &local_ctx;
7908 }
7909
7910 if (occ >= 0) {
7911 /* search from the beginning */
7912 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
7913 occ--;
7914 if (occ <= 0) {
7915 *vptr = ctx->line + ctx->val;
7916 *vlen = ctx->vlen;
7917 return 1;
7918 }
7919 }
7920 return 0;
7921 }
7922
7923 /* negative occurrence, we scan all the list then walk back */
7924 if (-occ > MAX_HDR_HISTORY)
7925 return 0;
7926
7927 found = hist_ptr = 0;
7928 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
7929 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7930 len_hist[hist_ptr] = ctx->vlen;
7931 if (++hist_ptr >= MAX_HDR_HISTORY)
7932 hist_ptr = 0;
7933 found++;
7934 }
7935 if (-occ > found)
7936 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007937
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007938 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007939 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7940 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7941 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007942 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007943 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007944 if (hist_ptr >= MAX_HDR_HISTORY)
7945 hist_ptr -= MAX_HDR_HISTORY;
7946 *vptr = ptr_hist[hist_ptr];
7947 *vlen = len_hist[hist_ptr];
7948 return 1;
7949}
7950
Willy Tarreaubaaee002006-06-26 02:48:02 +02007951/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007952 * Print a debug line with a header. Always stop at the first CR or LF char,
7953 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7954 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007955 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007956void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007957{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007958 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007959 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007960
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007961 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007962 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007963 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02007964 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 +02007965
7966 for (max = 0; start + max < end; max++)
7967 if (start[max] == '\r' || start[max] == '\n')
7968 break;
7969
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007970 UBOUND(max, trash.size - trash.len - 3);
7971 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
7972 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01007973 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007974}
7975
Willy Tarreaueee5b512015-04-03 23:46:31 +02007976
7977/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7978 * The hdr_idx is allocated as well. In case of allocation failure, everything
7979 * allocated is freed and NULL is returned. Otherwise the new transaction is
7980 * assigned to the stream and returned.
7981 */
7982struct http_txn *http_alloc_txn(struct stream *s)
7983{
7984 struct http_txn *txn = s->txn;
7985
7986 if (txn)
7987 return txn;
7988
7989 txn = pool_alloc2(pool2_http_txn);
7990 if (!txn)
7991 return txn;
7992
7993 txn->hdr_idx.size = global.tune.max_http_hdr;
7994 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
7995 if (!txn->hdr_idx.v) {
7996 pool_free2(pool2_http_txn, txn);
7997 return NULL;
7998 }
7999
8000 s->txn = txn;
8001 return txn;
8002}
8003
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008004void http_txn_reset_req(struct http_txn *txn)
8005{
8006 txn->req.flags = 0;
8007 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8008 txn->req.next = 0;
8009 txn->req.chunk_len = 0LL;
8010 txn->req.body_len = 0LL;
8011 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8012}
8013
8014void http_txn_reset_res(struct http_txn *txn)
8015{
8016 txn->rsp.flags = 0;
8017 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8018 txn->rsp.next = 0;
8019 txn->rsp.chunk_len = 0LL;
8020 txn->rsp.body_len = 0LL;
8021 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8022}
8023
Willy Tarreau0937bc42009-12-22 15:03:09 +01008024/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008025 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008026 * the required fields are properly allocated and that we only need to (re)init
8027 * them. This should be used before processing any new request.
8028 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008029void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008030{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008031 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008032 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008033
8034 txn->flags = 0;
8035 txn->status = -1;
8036
Willy Tarreauf64d1412010-10-07 20:06:11 +02008037 txn->cookie_first_date = 0;
8038 txn->cookie_last_date = 0;
8039
Willy Tarreaueee5b512015-04-03 23:46:31 +02008040 txn->srv_cookie = NULL;
8041 txn->cli_cookie = NULL;
8042 txn->uri = NULL;
8043
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008044 http_txn_reset_req(txn);
8045 http_txn_reset_res(txn);
8046
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008047 txn->req.chn = &s->req;
8048 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008049
8050 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008051
8052 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8053 if (fe->options2 & PR_O2_REQBUG_OK)
8054 txn->req.err_pos = -1; /* let buggy requests pass */
8055
Willy Tarreau0937bc42009-12-22 15:03:09 +01008056 if (txn->hdr_idx.v)
8057 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008058
8059 vars_init(&s->vars_txn, SCOPE_TXN);
8060 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008061}
8062
8063/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008064void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008065{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008066 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008067 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008068
8069 /* these ones will have been dynamically allocated */
8070 pool_free2(pool2_requri, txn->uri);
8071 pool_free2(pool2_capture, txn->cli_cookie);
8072 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008073 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008074
William Lallemanda73203e2012-03-12 12:48:57 +01008075 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008076 txn->uri = NULL;
8077 txn->srv_cookie = NULL;
8078 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008079
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008080 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008081 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008082 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008083 pool_free2(h->pool, s->req_cap[h->index]);
8084 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008085 }
8086
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008087 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008088 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008089 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008090 pool_free2(h->pool, s->res_cap[h->index]);
8091 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008092 }
8093
Willy Tarreau6204cd92016-03-10 16:33:04 +01008094 vars_prune(&s->vars_txn, s->sess, s);
8095 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008096}
8097
8098/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008099void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008100{
8101 http_end_txn(s);
8102 http_init_txn(s);
8103
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008104 /* reinitialise the current rule list pointer to NULL. We are sure that
8105 * any rulelist match the NULL pointer.
8106 */
8107 s->current_rule_list = NULL;
8108
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008109 s->be = strm_fe(s);
8110 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008111 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008112 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008113 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008114 /* re-init store persistence */
8115 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008116 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008117
Willy Tarreau0937bc42009-12-22 15:03:09 +01008118 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008119
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008120 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008121
Willy Tarreau739cfba2010-01-25 23:11:14 +01008122 /* We must trim any excess data from the response buffer, because we
8123 * may have blocked an invalid response from a server that we don't
8124 * want to accidentely forward once we disable the analysers, nor do
8125 * we want those data to come along with next response. A typical
8126 * example of such data would be from a buggy server responding to
8127 * a HEAD with some data, or sending more than the advertised
8128 * content-length.
8129 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008130 if (unlikely(s->res.buf->i))
8131 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008132
Christopher Fauletc0c672a2017-03-28 11:51:33 +02008133 /* Now we can realign the response buffer */
8134 buffer_realign(s->res.buf);
8135
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008136 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008137 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008138
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008139 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008140 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008141
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008142 s->req.rex = TICK_ETERNITY;
8143 s->req.wex = TICK_ETERNITY;
8144 s->req.analyse_exp = TICK_ETERNITY;
8145 s->res.rex = TICK_ETERNITY;
8146 s->res.wex = TICK_ETERNITY;
8147 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01008148 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008149}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008150
Sasha Pachev218f0642014-06-16 12:05:59 -06008151void free_http_res_rules(struct list *r)
8152{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008153 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008154
8155 list_for_each_entry_safe(pr, tr, r, list) {
8156 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008157 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008158 free(pr);
8159 }
8160}
8161
8162void free_http_req_rules(struct list *r)
8163{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008164 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01008165
8166 list_for_each_entry_safe(pr, tr, r, list) {
8167 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008168 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008169 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008170
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008171 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008172 free(pr);
8173 }
8174}
8175
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008176/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008177struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01008178{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008179 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008180 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008181 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008182 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008183
Vincent Bernat02779b62016-04-03 13:48:43 +02008184 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01008185 if (!rule) {
8186 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008187 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008188 }
8189
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008190 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008191 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01008192 cur_arg = 1;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008193 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block") || !strcmp(args[0], "tarpit")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04008194 int code;
8195 int hc;
8196
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008197 if (!strcmp(args[0], "tarpit")) {
8198 rule->action = ACT_HTTP_REQ_TARPIT;
8199 rule->deny_status = HTTP_ERR_500;
8200 }
8201 else {
8202 rule->action = ACT_ACTION_DENY;
8203 rule->deny_status = HTTP_ERR_403;
8204 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008205 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04008206 if (strcmp(args[cur_arg], "deny_status") == 0) {
8207 cur_arg++;
8208 if (!args[cur_arg]) {
8209 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
8210 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
8211 goto out_err;
8212 }
8213
8214 code = atol(args[cur_arg]);
8215 cur_arg++;
8216 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
8217 if (http_err_codes[hc] == code) {
8218 rule->deny_status = hc;
8219 break;
8220 }
8221 }
8222
8223 if (hc >= HTTP_ERR_SIZE) {
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008224 Warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n",
8225 file, linenum, code, http_err_codes[rule->deny_status]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008226 }
8227 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008228 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008229 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008230 cur_arg = 1;
8231
8232 while(*args[cur_arg]) {
8233 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008234 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008235 cur_arg+=2;
8236 continue;
8237 } else
8238 break;
8239 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008240 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008241 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008242 cur_arg = 1;
8243
8244 if (!*args[cur_arg] ||
8245 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8246 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8247 file, linenum, args[0]);
8248 goto out_err;
8249 }
8250 rule->arg.nice = atoi(args[cur_arg]);
8251 if (rule->arg.nice < -1024)
8252 rule->arg.nice = -1024;
8253 else if (rule->arg.nice > 1024)
8254 rule->arg.nice = 1024;
8255 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008256 } else if (!strcmp(args[0], "set-tos")) {
8257#ifdef IP_TOS
8258 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008259 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008260 cur_arg = 1;
8261
8262 if (!*args[cur_arg] ||
8263 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8264 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8265 file, linenum, args[0]);
8266 goto out_err;
8267 }
8268
8269 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8270 if (err && *err != '\0') {
8271 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8272 file, linenum, err, args[0]);
8273 goto out_err;
8274 }
8275 cur_arg++;
8276#else
8277 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8278 goto out_err;
8279#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008280 } else if (!strcmp(args[0], "set-mark")) {
8281#ifdef SO_MARK
8282 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008283 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008284 cur_arg = 1;
8285
8286 if (!*args[cur_arg] ||
8287 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8288 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8289 file, linenum, args[0]);
8290 goto out_err;
8291 }
8292
8293 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8294 if (err && *err != '\0') {
8295 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8296 file, linenum, err, args[0]);
8297 goto out_err;
8298 }
8299 cur_arg++;
8300 global.last_checks |= LSTCHK_NETADM;
8301#else
8302 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8303 goto out_err;
8304#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008305 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008306 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008307 cur_arg = 1;
8308
8309 if (!*args[cur_arg] ||
8310 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8311 bad_log_level:
8312 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8313 file, linenum, args[0]);
8314 goto out_err;
8315 }
8316 if (strcmp(args[cur_arg], "silent") == 0)
8317 rule->arg.loglevel = -1;
8318 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8319 goto bad_log_level;
8320 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008321 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008322 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008323 cur_arg = 1;
8324
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008325 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8326 (*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 +01008327 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8328 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008329 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008330 }
8331
8332 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8333 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8334 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008335
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008336 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008337 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008338 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 +01008339 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8340 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8341 file, linenum, args[0], error);
8342 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008343 goto out_err;
8344 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008345 free(proxy->conf.lfs_file);
8346 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8347 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008348 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02008349 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008350 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008351 cur_arg = 1;
8352
8353 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02008354 (*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 -06008355 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
8356 file, linenum, args[0]);
8357 goto out_err;
8358 }
8359
8360 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8361 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8362 LIST_INIT(&rule->arg.hdr_add.fmt);
8363
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008364 error = NULL;
8365 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
8366 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8367 args[cur_arg + 1], error);
8368 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008369 goto out_err;
8370 }
8371
8372 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008373 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008374 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 +01008375 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8376 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8377 file, linenum, args[0], error);
8378 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008379 goto out_err;
8380 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008381
8382 free(proxy->conf.lfs_file);
8383 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8384 proxy->conf.lfs_line = proxy->conf.args.line;
8385 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008386 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008387 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008388 cur_arg = 1;
8389
8390 if (!*args[cur_arg] ||
8391 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8392 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8393 file, linenum, args[0]);
8394 goto out_err;
8395 }
8396
8397 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8398 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8399
8400 proxy->conf.args.ctx = ARGC_HRQ;
8401 free(proxy->conf.lfs_file);
8402 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8403 proxy->conf.lfs_line = proxy->conf.args.line;
8404 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02008405 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
8406 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02008407 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02008408 struct sample_expr *expr;
8409 unsigned int where;
8410 char *err = NULL;
8411
8412 cur_arg = 1;
8413 proxy->conf.args.ctx = ARGC_TRK;
8414
8415 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
8416 if (!expr) {
8417 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8418 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
8419 free(err);
8420 goto out_err;
8421 }
8422
8423 where = 0;
8424 if (proxy->cap & PR_CAP_FE)
8425 where |= SMP_VAL_FE_HRQ_HDR;
8426 if (proxy->cap & PR_CAP_BE)
8427 where |= SMP_VAL_BE_HRQ_HDR;
8428
8429 if (!(expr->fetch->val & where)) {
8430 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
8431 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
8432 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
8433 args[cur_arg-1], sample_src_names(expr->fetch->use));
8434 free(expr);
8435 goto out_err;
8436 }
8437
8438 if (strcmp(args[cur_arg], "table") == 0) {
8439 cur_arg++;
8440 if (!args[cur_arg]) {
8441 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
8442 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
8443 free(expr);
8444 goto out_err;
8445 }
8446 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008447 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008448 cur_arg++;
8449 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008450 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008451 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Christopher Faulet78880fb2017-09-18 14:43:55 +02008452 rule->check_ptr = check_trk_action;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008453 } else if (strcmp(args[0], "redirect") == 0) {
8454 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008455 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008456
Willy Tarreaube4653b2015-05-28 15:26:58 +02008457 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008458 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8459 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8460 goto out_err;
8461 }
8462
8463 /* this redirect rule might already contain a parsed condition which
8464 * we'll pass to the http-request rule.
8465 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008466 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008467 rule->arg.redir = redir;
8468 rule->cond = redir->cond;
8469 redir->cond = NULL;
8470 cur_arg = 2;
8471 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008472 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8473 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008474 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008475 /*
8476 * '+ 8' for 'add-acl('
8477 * '- 9' for 'add-acl(' + trailing ')'
8478 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008479 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008480
8481 cur_arg = 1;
8482
8483 if (!*args[cur_arg] ||
8484 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8485 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8486 file, linenum, args[0]);
8487 goto out_err;
8488 }
8489
8490 LIST_INIT(&rule->arg.map.key);
8491 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008492 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008493 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008494 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8495 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8496 file, linenum, args[0], error);
8497 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008498 goto out_err;
8499 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008500 free(proxy->conf.lfs_file);
8501 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8502 proxy->conf.lfs_line = proxy->conf.args.line;
8503 cur_arg += 1;
8504 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8505 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008506 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008507 /*
8508 * '+ 8' for 'del-acl('
8509 * '- 9' for 'del-acl(' + trailing ')'
8510 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008511 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008512
8513 cur_arg = 1;
8514
8515 if (!*args[cur_arg] ||
8516 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8517 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8518 file, linenum, args[0]);
8519 goto out_err;
8520 }
8521
8522 LIST_INIT(&rule->arg.map.key);
8523 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008524 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008525 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008526 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8527 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8528 file, linenum, args[0], error);
8529 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008530 goto out_err;
8531 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008532 free(proxy->conf.lfs_file);
8533 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8534 proxy->conf.lfs_line = proxy->conf.args.line;
8535 cur_arg += 1;
8536 } else if (strncmp(args[0], "del-map", 7) == 0) {
8537 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008538 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008539 /*
8540 * '+ 8' for 'del-map('
8541 * '- 9' for 'del-map(' + trailing ')'
8542 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008543 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008544
8545 cur_arg = 1;
8546
8547 if (!*args[cur_arg] ||
8548 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8549 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8550 file, linenum, args[0]);
8551 goto out_err;
8552 }
8553
8554 LIST_INIT(&rule->arg.map.key);
8555 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008556 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008557 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008558 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8559 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8560 file, linenum, args[0], error);
8561 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008562 goto out_err;
8563 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008564 free(proxy->conf.lfs_file);
8565 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8566 proxy->conf.lfs_line = proxy->conf.args.line;
8567 cur_arg += 1;
8568 } else if (strncmp(args[0], "set-map", 7) == 0) {
8569 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008570 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008571 /*
8572 * '+ 8' for 'set-map('
8573 * '- 9' for 'set-map(' + trailing ')'
8574 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008575 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008576
8577 cur_arg = 1;
8578
8579 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8580 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8581 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8582 file, linenum, args[0]);
8583 goto out_err;
8584 }
8585
8586 LIST_INIT(&rule->arg.map.key);
8587 LIST_INIT(&rule->arg.map.value);
8588 proxy->conf.args.ctx = ARGC_HRQ;
8589
8590 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008591 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008592 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008593 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8594 Alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
8595 file, linenum, args[0], error);
8596 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008597 goto out_err;
8598 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008599
8600 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008601 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008602 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 +01008603 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8604 Alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
8605 file, linenum, args[0], error);
8606 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008607 goto out_err;
8608 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008609 free(proxy->conf.lfs_file);
8610 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8611 proxy->conf.lfs_line = proxy->conf.args.line;
8612
8613 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02008614 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
8615 char *errmsg = NULL;
8616 cur_arg = 1;
8617 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02008618 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02008619 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02008620 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02008621 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8622 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8623 free(errmsg);
8624 goto out_err;
8625 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008626 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008627 action_build_list(&http_req_keywords.list, &trash);
8628 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
8629 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08008630 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02008631 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008632 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008633 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008634 }
8635
8636 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8637 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008638 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008639
Christopher Faulet1b421ea2017-09-22 14:38:56 +02008640 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008641 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8642 file, linenum, args[0], errmsg);
8643 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008644 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008645 }
8646 rule->cond = cond;
8647 }
8648 else if (*args[cur_arg]) {
8649 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8650 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8651 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008652 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008653 }
8654
8655 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008656 out_err:
8657 free(rule);
8658 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008659}
8660
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008661/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008662struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008663{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008664 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008665 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008666 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008667 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008668
8669 rule = calloc(1, sizeof(*rule));
8670 if (!rule) {
8671 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
8672 goto out_err;
8673 }
8674
8675 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008676 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008677 cur_arg = 1;
8678 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008679 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008680 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008681 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008682 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008683 cur_arg = 1;
8684
8685 if (!*args[cur_arg] ||
8686 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8687 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8688 file, linenum, args[0]);
8689 goto out_err;
8690 }
8691 rule->arg.nice = atoi(args[cur_arg]);
8692 if (rule->arg.nice < -1024)
8693 rule->arg.nice = -1024;
8694 else if (rule->arg.nice > 1024)
8695 rule->arg.nice = 1024;
8696 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008697 } else if (!strcmp(args[0], "set-tos")) {
8698#ifdef IP_TOS
8699 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008700 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008701 cur_arg = 1;
8702
8703 if (!*args[cur_arg] ||
8704 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8705 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8706 file, linenum, args[0]);
8707 goto out_err;
8708 }
8709
8710 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8711 if (err && *err != '\0') {
8712 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8713 file, linenum, err, args[0]);
8714 goto out_err;
8715 }
8716 cur_arg++;
8717#else
8718 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8719 goto out_err;
8720#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008721 } else if (!strcmp(args[0], "set-mark")) {
8722#ifdef SO_MARK
8723 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008724 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008725 cur_arg = 1;
8726
8727 if (!*args[cur_arg] ||
8728 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8729 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8730 file, linenum, args[0]);
8731 goto out_err;
8732 }
8733
8734 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8735 if (err && *err != '\0') {
8736 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8737 file, linenum, err, args[0]);
8738 goto out_err;
8739 }
8740 cur_arg++;
8741 global.last_checks |= LSTCHK_NETADM;
8742#else
8743 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8744 goto out_err;
8745#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008746 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008747 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008748 cur_arg = 1;
8749
8750 if (!*args[cur_arg] ||
8751 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8752 bad_log_level:
8753 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8754 file, linenum, args[0]);
8755 goto out_err;
8756 }
8757 if (strcmp(args[cur_arg], "silent") == 0)
8758 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08008759 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008760 goto bad_log_level;
8761 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008762 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008763 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008764 cur_arg = 1;
8765
8766 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8767 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8768 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8769 file, linenum, args[0]);
8770 goto out_err;
8771 }
8772
8773 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8774 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8775 LIST_INIT(&rule->arg.hdr_add.fmt);
8776
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008777 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008778 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008779 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 +01008780 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8781 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8782 file, linenum, args[0], error);
8783 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008784 goto out_err;
8785 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008786 free(proxy->conf.lfs_file);
8787 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8788 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008789 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06008790 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008791 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008792 cur_arg = 1;
8793
8794 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02008795 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
8796 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06008797 file, linenum, args[0]);
8798 goto out_err;
8799 }
8800
8801 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8802 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8803 LIST_INIT(&rule->arg.hdr_add.fmt);
8804
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008805 error = NULL;
8806 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
8807 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8808 args[cur_arg + 1], error);
8809 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008810 goto out_err;
8811 }
8812
8813 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008814 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008815 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 +01008816 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8817 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8818 file, linenum, args[0], error);
8819 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008820 goto out_err;
8821 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008822
8823 free(proxy->conf.lfs_file);
8824 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8825 proxy->conf.lfs_line = proxy->conf.args.line;
8826 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008827 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008828 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008829 cur_arg = 1;
8830
8831 if (!*args[cur_arg] ||
8832 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8833 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8834 file, linenum, args[0]);
8835 goto out_err;
8836 }
8837
8838 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8839 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8840
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008841 proxy->conf.args.ctx = ARGC_HRS;
8842 free(proxy->conf.lfs_file);
8843 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8844 proxy->conf.lfs_line = proxy->conf.args.line;
8845 cur_arg += 1;
8846 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8847 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008848 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008849 /*
8850 * '+ 8' for 'add-acl('
8851 * '- 9' for 'add-acl(' + trailing ')'
8852 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008853 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008854
8855 cur_arg = 1;
8856
8857 if (!*args[cur_arg] ||
8858 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8859 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8860 file, linenum, args[0]);
8861 goto out_err;
8862 }
8863
8864 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008865 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008866 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008867 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008868 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8869 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8870 file, linenum, args[0], error);
8871 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008872 goto out_err;
8873 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008874 free(proxy->conf.lfs_file);
8875 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8876 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008877
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008878 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008879 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8880 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008881 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008882 /*
8883 * '+ 8' for 'del-acl('
8884 * '- 9' for 'del-acl(' + trailing ')'
8885 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008886 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008887
8888 cur_arg = 1;
8889
8890 if (!*args[cur_arg] ||
8891 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8892 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8893 file, linenum, args[0]);
8894 goto out_err;
8895 }
8896
8897 LIST_INIT(&rule->arg.map.key);
8898 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008899 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008900 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008901 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8902 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8903 file, linenum, args[0], error);
8904 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008905 goto out_err;
8906 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008907 free(proxy->conf.lfs_file);
8908 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8909 proxy->conf.lfs_line = proxy->conf.args.line;
8910 cur_arg += 1;
8911 } else if (strncmp(args[0], "del-map", 7) == 0) {
8912 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008913 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008914 /*
8915 * '+ 8' for 'del-map('
8916 * '- 9' for 'del-map(' + trailing ')'
8917 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008918 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008919
8920 cur_arg = 1;
8921
8922 if (!*args[cur_arg] ||
8923 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8924 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8925 file, linenum, args[0]);
8926 goto out_err;
8927 }
8928
8929 LIST_INIT(&rule->arg.map.key);
8930 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008931 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008932 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008933 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8934 Alert("parsing [%s:%d]: 'http-response %s' %s.\n",
8935 file, linenum, args[0], error);
8936 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008937 goto out_err;
8938 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008939 free(proxy->conf.lfs_file);
8940 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8941 proxy->conf.lfs_line = proxy->conf.args.line;
8942 cur_arg += 1;
8943 } else if (strncmp(args[0], "set-map", 7) == 0) {
8944 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008945 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008946 /*
8947 * '+ 8' for 'set-map('
8948 * '- 9' for 'set-map(' + trailing ')'
8949 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008950 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008951
8952 cur_arg = 1;
8953
8954 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8955 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8956 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8957 file, linenum, args[0]);
8958 goto out_err;
8959 }
8960
8961 LIST_INIT(&rule->arg.map.key);
8962 LIST_INIT(&rule->arg.map.value);
8963
8964 proxy->conf.args.ctx = ARGC_HRS;
8965
8966 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008967 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008968 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008969 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8970 Alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
8971 file, linenum, args[0], error);
8972 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008973 goto out_err;
8974 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008975
8976 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008977 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008978 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 +01008979 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8980 Alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
8981 file, linenum, args[0], error);
8982 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008983 goto out_err;
8984 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008985
8986 free(proxy->conf.lfs_file);
8987 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8988 proxy->conf.lfs_line = proxy->conf.args.line;
8989
8990 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02008991 } else if (strcmp(args[0], "redirect") == 0) {
8992 struct redirect_rule *redir;
8993 char *errmsg = NULL;
8994
8995 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
8996 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
8997 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8998 goto out_err;
8999 }
9000
9001 /* this redirect rule might already contain a parsed condition which
9002 * we'll pass to the http-request rule.
9003 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009004 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009005 rule->arg.redir = redir;
9006 rule->cond = redir->cond;
9007 redir->cond = NULL;
9008 cur_arg = 2;
9009 return rule;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009010 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9011 args[0][9] == '\0' && args[0][8] >= '0' &&
9012 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
9013 struct sample_expr *expr;
9014 unsigned int where;
9015 char *err = NULL;
9016
9017 cur_arg = 1;
9018 proxy->conf.args.ctx = ARGC_TRK;
9019
9020 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9021 if (!expr) {
9022 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9023 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9024 free(err);
9025 goto out_err;
9026 }
9027
9028 where = 0;
9029 if (proxy->cap & PR_CAP_FE)
9030 where |= SMP_VAL_FE_HRS_HDR;
9031 if (proxy->cap & PR_CAP_BE)
9032 where |= SMP_VAL_BE_HRS_HDR;
9033
9034 if (!(expr->fetch->val & where)) {
9035 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
9036 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9037 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9038 args[cur_arg-1], sample_src_names(expr->fetch->use));
9039 free(expr);
9040 goto out_err;
9041 }
9042
9043 if (strcmp(args[cur_arg], "table") == 0) {
9044 cur_arg++;
9045 if (!args[cur_arg]) {
9046 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9047 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9048 free(expr);
9049 goto out_err;
9050 }
9051 /* we copy the table name for now, it will be resolved later */
9052 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9053 cur_arg++;
9054 }
9055 rule->arg.trk_ctr.expr = expr;
9056 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Christopher Faulet78880fb2017-09-18 14:43:55 +02009057 rule->check_ptr = check_trk_action;
William Lallemand73025dd2014-04-24 14:38:37 +02009058 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9059 char *errmsg = NULL;
9060 cur_arg = 1;
9061 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009062 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009063 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009064 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009065 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9066 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9067 free(errmsg);
9068 goto out_err;
9069 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009070 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009071 action_build_list(&http_res_keywords.list, &trash);
9072 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9073 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009074 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009075 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009076 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009077 goto out_err;
9078 }
9079
9080 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9081 struct acl_cond *cond;
9082 char *errmsg = NULL;
9083
Christopher Faulet1b421ea2017-09-22 14:38:56 +02009084 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009085 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9086 file, linenum, args[0], errmsg);
9087 free(errmsg);
9088 goto out_err;
9089 }
9090 rule->cond = cond;
9091 }
9092 else if (*args[cur_arg]) {
9093 Alert("parsing [%s:%d]: 'http-response %s' expects"
9094 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9095 file, linenum, args[0], args[cur_arg]);
9096 goto out_err;
9097 }
9098
9099 return rule;
9100 out_err:
9101 free(rule);
9102 return NULL;
9103}
9104
Willy Tarreau4baae242012-12-27 12:00:31 +01009105/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009106 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009107 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9108 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009109 */
9110struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009111 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009112{
9113 struct redirect_rule *rule;
9114 int cur_arg;
9115 int type = REDIRECT_TYPE_NONE;
9116 int code = 302;
9117 const char *destination = NULL;
9118 const char *cookie = NULL;
9119 int cookie_set = 0;
9120 unsigned int flags = REDIRECT_FLAG_NONE;
9121 struct acl_cond *cond = NULL;
9122
9123 cur_arg = 0;
9124 while (*(args[cur_arg])) {
9125 if (strcmp(args[cur_arg], "location") == 0) {
9126 if (!*args[cur_arg + 1])
9127 goto missing_arg;
9128
9129 type = REDIRECT_TYPE_LOCATION;
9130 cur_arg++;
9131 destination = args[cur_arg];
9132 }
9133 else if (strcmp(args[cur_arg], "prefix") == 0) {
9134 if (!*args[cur_arg + 1])
9135 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009136 type = REDIRECT_TYPE_PREFIX;
9137 cur_arg++;
9138 destination = args[cur_arg];
9139 }
9140 else if (strcmp(args[cur_arg], "scheme") == 0) {
9141 if (!*args[cur_arg + 1])
9142 goto missing_arg;
9143
9144 type = REDIRECT_TYPE_SCHEME;
9145 cur_arg++;
9146 destination = args[cur_arg];
9147 }
9148 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9149 if (!*args[cur_arg + 1])
9150 goto missing_arg;
9151
9152 cur_arg++;
9153 cookie = args[cur_arg];
9154 cookie_set = 1;
9155 }
9156 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9157 if (!*args[cur_arg + 1])
9158 goto missing_arg;
9159
9160 cur_arg++;
9161 cookie = args[cur_arg];
9162 cookie_set = 0;
9163 }
9164 else if (strcmp(args[cur_arg], "code") == 0) {
9165 if (!*args[cur_arg + 1])
9166 goto missing_arg;
9167
9168 cur_arg++;
9169 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009170 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009171 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009172 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009173 args[cur_arg - 1], args[cur_arg]);
9174 return NULL;
9175 }
9176 }
9177 else if (!strcmp(args[cur_arg],"drop-query")) {
9178 flags |= REDIRECT_FLAG_DROP_QS;
9179 }
9180 else if (!strcmp(args[cur_arg],"append-slash")) {
9181 flags |= REDIRECT_FLAG_APPEND_SLASH;
9182 }
9183 else if (strcmp(args[cur_arg], "if") == 0 ||
9184 strcmp(args[cur_arg], "unless") == 0) {
Christopher Faulet1b421ea2017-09-22 14:38:56 +02009185 cond = build_acl_cond(file, linenum, &proxy->acl, curproxy, (const char **)args + cur_arg, errmsg);
Willy Tarreau4baae242012-12-27 12:00:31 +01009186 if (!cond) {
9187 memprintf(errmsg, "error in condition: %s", *errmsg);
9188 return NULL;
9189 }
9190 break;
9191 }
9192 else {
9193 memprintf(errmsg,
9194 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9195 args[cur_arg]);
9196 return NULL;
9197 }
9198 cur_arg++;
9199 }
9200
9201 if (type == REDIRECT_TYPE_NONE) {
9202 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9203 return NULL;
9204 }
9205
Willy Tarreaube4653b2015-05-28 15:26:58 +02009206 if (dir && type != REDIRECT_TYPE_LOCATION) {
9207 memprintf(errmsg, "response only supports redirect type 'location'");
9208 return NULL;
9209 }
9210
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02009211 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +01009212 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009213 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009214
9215 if (!use_fmt) {
9216 /* old-style static redirect rule */
9217 rule->rdr_str = strdup(destination);
9218 rule->rdr_len = strlen(destination);
9219 }
9220 else {
9221 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009222
9223 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9224 * if prefix == "/", we don't want to add anything, otherwise it
9225 * makes it hard for the user to configure a self-redirection.
9226 */
Godbachd9722032014-12-18 15:44:58 +08009227 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009228 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009229 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
9230 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 +01009231 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9232 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009233 return NULL;
9234 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009235 free(curproxy->conf.lfs_file);
9236 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9237 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009238 }
9239 }
9240
Willy Tarreau4baae242012-12-27 12:00:31 +01009241 if (cookie) {
9242 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9243 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9244 */
9245 rule->cookie_len = strlen(cookie);
9246 if (cookie_set) {
9247 rule->cookie_str = malloc(rule->cookie_len + 10);
9248 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9249 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9250 rule->cookie_len += 9;
9251 } else {
9252 rule->cookie_str = malloc(rule->cookie_len + 21);
9253 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9254 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9255 rule->cookie_len += 20;
9256 }
9257 }
9258 rule->type = type;
9259 rule->code = code;
9260 rule->flags = flags;
9261 LIST_INIT(&rule->list);
9262 return rule;
9263
9264 missing_arg:
9265 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9266 return NULL;
9267}
9268
Willy Tarreau8797c062007-05-07 00:55:35 +02009269/************************************************************************/
9270/* The code below is dedicated to ACL parsing and matching */
9271/************************************************************************/
9272
9273
Willy Tarreau14174bc2012-04-16 14:34:04 +02009274/* This function ensures that the prerequisites for an L7 fetch are ready,
9275 * which means that a request or response is ready. If some data is missing,
9276 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009277 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9278 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009279 *
9280 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009281 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9282 * decide whether or not an HTTP message is present ;
9283 * 0 if the requested data cannot be fetched or if it is certain that
9284 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009285 * 1 if an HTTP message is ready
9286 */
James Rosewell91a41cb2015-09-18 17:11:16 +01009287int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009288 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009289{
Willy Tarreau192252e2015-04-04 01:47:55 +02009290 struct http_txn *txn;
9291 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009292
Willy Tarreaube508f12016-03-10 11:47:01 +01009293 /* Note: it is possible that <s> is NULL when called before stream
9294 * initialization (eg: tcp-request connection), so this function is the
9295 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009296 */
Willy Tarreau192252e2015-04-04 01:47:55 +02009297 if (!s)
9298 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01009299
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +02009300 if (!s->txn) {
9301 if (unlikely(!http_alloc_txn(s)))
9302 return 0; /* not enough memory */
9303 http_init_txn(s);
9304 }
Willy Tarreau192252e2015-04-04 01:47:55 +02009305 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +02009306 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009307
9308 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009309 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009310
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009311 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009312 /* If the buffer does not leave enough free space at the end,
9313 * we must first realign it.
9314 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009315 if (s->req.buf->p > s->req.buf->data &&
9316 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
9317 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009318
Willy Tarreau14174bc2012-04-16 14:34:04 +02009319 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009320 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009321 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009322
9323 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009324 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009325 http_msg_analyzer(msg, &txn->hdr_idx);
9326
9327 /* Still no valid request ? */
9328 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009329 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009330 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009331 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009332 }
9333 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009334 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009335 return 0;
9336 }
9337
9338 /* OK we just got a valid HTTP request. We have some minor
9339 * preparation to perform so that further checks can rely
9340 * on HTTP tests.
9341 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009342
9343 /* If the request was parsed but was too large, we must absolutely
9344 * return an error so that it is not processed. At the moment this
9345 * cannot happen, but if the parsers are to change in the future,
9346 * we want this check to be maintained.
9347 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009348 if (unlikely(s->req.buf->i + s->req.buf->p >
9349 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01009350 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009351 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009352 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009353 return 1;
9354 }
9355
Willy Tarreau9b28e032012-10-12 23:49:43 +02009356 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009357 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02009358 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009359
Willy Tarreau506d0502013-07-06 13:29:24 +02009360 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9361 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009362 }
9363
Willy Tarreau506d0502013-07-06 13:29:24 +02009364 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009365 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009366 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009367
9368 /* otherwise everything's ready for the request */
9369 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009370 else {
9371 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009372 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9373 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009374 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009375 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009376 }
9377
9378 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009379 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009380 return 1;
9381}
Willy Tarreau8797c062007-05-07 00:55:35 +02009382
Willy Tarreau8797c062007-05-07 00:55:35 +02009383/* 1. Check on METHOD
9384 * We use the pre-parsed method if it is known, and store its number as an
9385 * integer. If it is unknown, we use the pointer and the length.
9386 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009387static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009388{
9389 int len, meth;
9390
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009391 len = strlen(text);
9392 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009393
9394 pattern->val.i = meth;
9395 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +02009396 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009397 pattern->len = len;
9398 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009399 else {
9400 pattern->ptr.str = NULL;
9401 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009402 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009403 return 1;
9404}
9405
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009406/* This function fetches the method of current HTTP request and stores
9407 * it in the global pattern struct as a chunk. There are two possibilities :
9408 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9409 * in <len> and <ptr> is NULL ;
9410 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9411 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009412 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009413 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009414static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009415smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009416{
9417 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +01009418 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009419
Willy Tarreau24e32d82012-04-23 23:55:44 +02009420 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009421
Willy Tarreaube508f12016-03-10 11:47:01 +01009422 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009423 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009424 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009425 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009426 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009427 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9428 /* ensure the indexes are not affected */
9429 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009430 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009431 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
9432 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009433 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009434 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009435 return 1;
9436}
9437
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009438/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009439static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009440{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009441 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009442 struct pattern_list *lst;
9443 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009444
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009445 list_for_each_entry(lst, &expr->patterns, list) {
9446 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009447
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009448 /* well-known method */
9449 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009450 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009451 return pattern;
9452 else
9453 continue;
9454 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009455
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009456 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009457 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009458 continue;
9459
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009460 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009461 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
9462 (!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 +01009463 return pattern;
9464 }
9465 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009466}
9467
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009468static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009469smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009470{
Willy Tarreaube508f12016-03-10 11:47:01 +01009471 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009472 char *ptr;
9473 int len;
9474
Willy Tarreauc0239e02012-04-16 14:42:55 +02009475 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009476
Willy Tarreaube508f12016-03-10 11:47:01 +01009477 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009478 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009479 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009480
9481 while ((len-- > 0) && (*ptr++ != '/'));
9482 if (len <= 0)
9483 return 0;
9484
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009485 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009486 smp->data.u.str.str = ptr;
9487 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009488
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009489 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009490 return 1;
9491}
9492
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009493static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009494smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009495{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009496 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009497 char *ptr;
9498 int len;
9499
Willy Tarreauc0239e02012-04-16 14:42:55 +02009500 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009501
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009502 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009503 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9504 return 0;
9505
Willy Tarreau8797c062007-05-07 00:55:35 +02009506 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009507 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009508
9509 while ((len-- > 0) && (*ptr++ != '/'));
9510 if (len <= 0)
9511 return 0;
9512
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009513 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009514 smp->data.u.str.str = ptr;
9515 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009516
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009517 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009518 return 1;
9519}
9520
9521/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009522static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009523smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009524{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009525 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009526 char *ptr;
9527 int len;
9528
Willy Tarreauc0239e02012-04-16 14:42:55 +02009529 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009530
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009531 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009532 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9533 return 0;
9534
Willy Tarreau8797c062007-05-07 00:55:35 +02009535 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009536 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009537
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009538 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009539 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009540 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009541 return 1;
9542}
9543
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009544static int
9545smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
9546{
9547 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
9548 return 0;
9549
9550 if (!smp->strm->unique_id) {
9551 if ((smp->strm->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
9552 return 0;
9553 smp->strm->unique_id[0] = '\0';
9554 }
9555 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
9556 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
9557
9558 smp->data.type = SMP_T_STR;
9559 smp->data.u.str.str = smp->strm->unique_id;
9560 smp->flags = SMP_F_CONST;
9561 return 1;
9562}
9563
Thierry FOURNIERd7d88812017-04-19 15:15:14 +02009564/* Returns a string block containing all headers including the
9565 * empty line wich separes headers from the body. This is useful
9566 * form some headers analysis.
9567 */
9568static int
9569smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
9570{
9571 struct http_msg *msg;
9572 struct hdr_idx *idx;
9573 struct http_txn *txn;
9574
9575 CHECK_HTTP_MESSAGE_FIRST();
9576
9577 txn = smp->strm->txn;
9578 idx = &txn->hdr_idx;
9579 msg = &txn->req;
9580
9581 smp->data.type = SMP_T_STR;
9582 smp->data.u.str.str = msg->chn->buf->p + hdr_idx_first_pos(idx);
9583 smp->data.u.str.len = msg->eoh - hdr_idx_first_pos(idx) + 1 +
9584 (msg->chn->buf->p[msg->eoh] == '\r');
9585
9586 return 1;
9587}
9588
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009589/* Returns the header request in a length/value encoded format.
9590 * This is useful for exchanges with the SPOE.
9591 *
9592 * A "length value" is a multibyte code encoding numbers. It uses the
9593 * SPOE format. The encoding is the following:
9594 *
9595 * Each couple "header name" / "header value" is composed
9596 * like this:
9597 * "length value" "header name bytes"
9598 * "length value" "header value bytes"
9599 * When the last header is reached, the header name and the header
9600 * value are empty. Their length are 0
9601 */
9602static int
9603smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
9604{
9605 struct http_msg *msg;
9606 struct chunk *temp;
9607 struct hdr_idx *idx;
9608 const char *cur_ptr, *cur_next, *p;
9609 int old_idx, cur_idx;
9610 struct hdr_idx_elem *cur_hdr;
9611 const char *hn, *hv;
9612 int hnl, hvl;
9613 int ret;
9614 struct http_txn *txn;
9615 char *buf;
9616 char *end;
9617
9618 CHECK_HTTP_MESSAGE_FIRST();
9619
9620 temp = get_trash_chunk();
9621 buf = temp->str;
9622 end = temp->str + temp->size;
9623
9624 txn = smp->strm->txn;
9625 idx = &txn->hdr_idx;
9626 msg = &txn->req;
9627
9628 /* Build array of headers. */
9629 old_idx = 0;
9630 cur_next = msg->chn->buf->p + hdr_idx_first_pos(idx);
9631 while (1) {
9632 cur_idx = idx->v[old_idx].next;
9633 if (!cur_idx)
9634 break;
9635 old_idx = cur_idx;
9636
9637 cur_hdr = &idx->v[cur_idx];
9638 cur_ptr = cur_next;
9639 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
9640
9641 /* Now we have one full header at cur_ptr of len cur_hdr->len,
9642 * and the next header starts at cur_next. We'll check
9643 * this header in the list as well as against the default
9644 * rule.
9645 */
9646
9647 /* look for ': *'. */
9648 hn = cur_ptr;
9649 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
9650 if (p >= cur_ptr+cur_hdr->len)
9651 continue;
9652 hnl = p - hn;
9653 p++;
9654 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
9655 p++;
9656 if (p >= cur_ptr + cur_hdr->len)
9657 continue;
9658 hv = p;
9659 hvl = cur_ptr + cur_hdr->len-p;
9660
9661 /* encode the header name. */
9662 ret = encode_varint(hnl, &buf, end);
9663 if (ret == -1)
9664 return 0;
9665 if (buf + hnl > end)
9666 return 0;
9667 memcpy(buf, hn, hnl);
9668 buf += hnl;
9669
9670 /* encode and copy the value. */
9671 ret = encode_varint(hvl, &buf, end);
9672 if (ret == -1)
9673 return 0;
9674 if (buf + hvl > end)
9675 return 0;
9676 memcpy(buf, hv, hvl);
9677 buf += hvl;
9678 }
9679
9680 /* encode the end of the header list with empty
9681 * header name and header value.
9682 */
9683 ret = encode_varint(0, &buf, end);
9684 if (ret == -1)
9685 return 0;
9686 ret = encode_varint(0, &buf, end);
9687 if (ret == -1)
9688 return 0;
9689
9690 /* Initialise sample data which will be filled. */
9691 smp->data.type = SMP_T_BIN;
9692 smp->data.u.str.str = temp->str;
9693 smp->data.u.str.len = buf - temp->str;
9694 smp->data.u.str.size = temp->size;
9695
9696 return 1;
9697}
9698
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009699/* returns the longest available part of the body. This requires that the body
9700 * has been waited for using http-buffer-request.
9701 */
9702static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009703smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009704{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009705 struct http_msg *msg;
9706 unsigned long len;
9707 unsigned long block1;
9708 char *body;
9709 struct chunk *temp;
9710
9711 CHECK_HTTP_MESSAGE_FIRST();
9712
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009713 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009714 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009715 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009716 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009717
9718 len = http_body_bytes(msg);
9719 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
9720
9721 block1 = len;
9722 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
9723 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
9724
9725 if (block1 == len) {
9726 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009727 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009728 smp->data.u.str.str = body;
9729 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009730 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
9731 }
9732 else {
9733 /* buffer is wrapped, we need to defragment it */
9734 temp = get_trash_chunk();
9735 memcpy(temp->str, body, block1);
9736 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009737 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009738 smp->data.u.str.str = temp->str;
9739 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009740 smp->flags = SMP_F_VOL_TEST;
9741 }
9742 return 1;
9743}
9744
9745
9746/* returns the available length of the body. This requires that the body
9747 * has been waited for using http-buffer-request.
9748 */
9749static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009750smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009751{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009752 struct http_msg *msg;
9753
9754 CHECK_HTTP_MESSAGE_FIRST();
9755
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009756 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009757 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009758 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009759 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009760
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009761 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009762 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009763
9764 smp->flags = SMP_F_VOL_TEST;
9765 return 1;
9766}
9767
9768
9769/* returns the advertised length of the body, or the advertised size of the
9770 * chunks available in the buffer. This requires that the body has been waited
9771 * for using http-buffer-request.
9772 */
9773static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009774smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009775{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009776 struct http_msg *msg;
9777
9778 CHECK_HTTP_MESSAGE_FIRST();
9779
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009780 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009781 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009782 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009783 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009784
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009785 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009786 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009787
9788 smp->flags = SMP_F_VOL_TEST;
9789 return 1;
9790}
9791
9792
Willy Tarreau8797c062007-05-07 00:55:35 +02009793/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009794static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009795smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009796{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009797 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009798
Willy Tarreauc0239e02012-04-16 14:42:55 +02009799 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009800
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009801 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009802 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009803 smp->data.u.str.len = txn->req.sl.rq.u_l;
9804 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009805 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009806 return 1;
9807}
9808
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009809static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009810smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009811{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009812 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009813 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009814
Willy Tarreauc0239e02012-04-16 14:42:55 +02009815 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009816
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009817 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009818 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 +02009819 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009820 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009821
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009822 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009823 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009824 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009825 return 1;
9826}
9827
9828static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009829smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009830{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009831 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009832 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009833
Willy Tarreauc0239e02012-04-16 14:42:55 +02009834 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009835
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009836 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009837 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 +02009838 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9839 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009840
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009841 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009842 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009843 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009844 return 1;
9845}
9846
Willy Tarreau185b5c42012-04-26 15:11:51 +02009847/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9848 * Accepts an optional argument of type string containing the header field name,
9849 * and an optional argument of type signed or unsigned integer to request an
9850 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009851 * headers are considered from the first one. It does not stop on commas and
9852 * returns full lines instead (useful for User-Agent or Date for example).
9853 */
9854static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009855smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009856{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009857 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009858 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +02009859 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009860 int occ = 0;
9861 const char *name_str = NULL;
9862 int name_len = 0;
9863
9864 if (!ctx) {
9865 /* first call */
9866 ctx = &static_hdr_ctx;
9867 ctx->idx = 0;
9868 smp->ctx.a[0] = ctx;
9869 }
9870
9871 if (args) {
9872 if (args[0].type != ARGT_STR)
9873 return 0;
9874 name_str = args[0].data.str.str;
9875 name_len = args[0].data.str.len;
9876
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02009877 if (args[1].type == ARGT_SINT)
9878 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009879 }
9880
9881 CHECK_HTTP_MESSAGE_FIRST();
9882
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009883 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009884 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 +02009885
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009886 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9887 /* search for header from the beginning */
9888 ctx->idx = 0;
9889
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009890 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009891 /* no explicit occurrence and single fetch => last header by default */
9892 occ = -1;
9893
9894 if (!occ)
9895 /* prepare to report multiple occurrences for ACL fetches */
9896 smp->flags |= SMP_F_NOT_LAST;
9897
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009898 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009899 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009900 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 +02009901 return 1;
9902
9903 smp->flags &= ~SMP_F_NOT_LAST;
9904 return 0;
9905}
9906
9907/* 6. Check on HTTP header count. The number of occurrences is returned.
9908 * Accepts exactly 1 argument of type string. It does not stop on commas and
9909 * returns full lines instead (useful for User-Agent or Date for example).
9910 */
9911static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009912smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009913{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009914 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009915 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009916 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009917 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009918 const char *name = NULL;
9919 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009920
Willy Tarreau601a4d12015-04-01 19:16:09 +02009921 if (args && args->type == ARGT_STR) {
9922 name = args->data.str.str;
9923 len = args->data.str.len;
9924 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009925
9926 CHECK_HTTP_MESSAGE_FIRST();
9927
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009928 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009929 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 +02009930
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009931 ctx.idx = 0;
9932 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009933 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009934 cnt++;
9935
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009936 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009937 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009938 smp->flags = SMP_F_VOL_HDR;
9939 return 1;
9940}
9941
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009942static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009943smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009944{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009945 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009946 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009947 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009948 struct chunk *temp;
9949 char del = ',';
9950
9951 if (args && args->type == ARGT_STR)
9952 del = *args[0].data.str.str;
9953
9954 CHECK_HTTP_MESSAGE_FIRST();
9955
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009956 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009957 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 +02009958
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009959 temp = get_trash_chunk();
9960
9961 ctx.idx = 0;
9962 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
9963 if (temp->len)
9964 temp->str[temp->len++] = del;
9965 memcpy(temp->str + temp->len, ctx.line, ctx.del);
9966 temp->len += ctx.del;
9967 }
9968
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009969 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009970 smp->data.u.str.str = temp->str;
9971 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009972 smp->flags = SMP_F_VOL_HDR;
9973 return 1;
9974}
9975
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009976/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9977 * Accepts an optional argument of type string containing the header field name,
9978 * and an optional argument of type signed or unsigned integer to request an
9979 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009980 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009981 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009982static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009983smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009984{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009985 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009986 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +02009987 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009988 int occ = 0;
9989 const char *name_str = NULL;
9990 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009991
Willy Tarreaua890d072013-04-02 12:01:06 +02009992 if (!ctx) {
9993 /* first call */
9994 ctx = &static_hdr_ctx;
9995 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009996 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009997 }
9998
Willy Tarreau185b5c42012-04-26 15:11:51 +02009999 if (args) {
10000 if (args[0].type != ARGT_STR)
10001 return 0;
10002 name_str = args[0].data.str.str;
10003 name_len = args[0].data.str.len;
10004
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010005 if (args[1].type == ARGT_SINT)
10006 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010007 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010008
Willy Tarreaue333ec92012-04-16 16:26:40 +020010009 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010010
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010011 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010012 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 +020010013
Willy Tarreau185b5c42012-04-26 15:11:51 +020010014 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010015 /* search for header from the beginning */
10016 ctx->idx = 0;
10017
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010018 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010019 /* no explicit occurrence and single fetch => last header by default */
10020 occ = -1;
10021
10022 if (!occ)
10023 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010024 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010025
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010026 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010027 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010028 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 +020010029 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010030
Willy Tarreau37406352012-04-23 16:16:37 +020010031 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010032 return 0;
10033}
10034
Willy Tarreauc11416f2007-06-17 16:58:38 +020010035/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010036 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010037 */
10038static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010039smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010040{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010041 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010042 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010043 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010044 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010045 const char *name = NULL;
10046 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010047
Willy Tarreau601a4d12015-04-01 19:16:09 +020010048 if (args && args->type == ARGT_STR) {
10049 name = args->data.str.str;
10050 len = args->data.str.len;
10051 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010052
Willy Tarreaue333ec92012-04-16 16:26:40 +020010053 CHECK_HTTP_MESSAGE_FIRST();
10054
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010055 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010056 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 +020010057
Willy Tarreau33a7e692007-06-10 19:45:56 +020010058 ctx.idx = 0;
10059 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010060 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010061 cnt++;
10062
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010063 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010064 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010065 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010066 return 1;
10067}
10068
Willy Tarreau185b5c42012-04-26 15:11:51 +020010069/* Fetch an HTTP header's integer value. The integer value is returned. It
10070 * takes a mandatory argument of type string and an optional one of type int
10071 * to designate a specific occurrence. It returns an unsigned integer, which
10072 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010073 */
10074static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010075smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010076{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010077 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010078
Willy Tarreauf853c462012-04-23 18:53:56 +020010079 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010080 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010081 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010082 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010083
Willy Tarreaud53e2422012-04-16 17:21:11 +020010084 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010085}
10086
Cyril Bonté69fa9922012-10-25 00:01:06 +020010087/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10088 * and an optional one of type int to designate a specific occurrence.
10089 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010090 */
10091static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010092smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010093{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010094 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010095
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010096 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010097 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010098 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010099 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010100 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010101 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010102 if (smp->data.u.str.len < temp->size - 1) {
10103 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10104 temp->str[smp->data.u.str.len] = '\0';
10105 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010106 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010107 break;
10108 }
10109 }
10110 }
10111
Willy Tarreaud53e2422012-04-16 17:21:11 +020010112 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010113 if (!(smp->flags & SMP_F_NOT_LAST))
10114 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010115 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010116 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010117}
10118
Willy Tarreau737b0c12007-06-10 21:28:46 +020010119/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10120 * the first '/' after the possible hostname, and ends before the possible '?'.
10121 */
10122static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010123smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010124{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010125 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010126 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010127
Willy Tarreauc0239e02012-04-16 14:42:55 +020010128 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010129
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010130 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010131 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010132 ptr = http_get_path(txn);
10133 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010134 return 0;
10135
10136 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010137 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010138 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010139
10140 while (ptr < end && *ptr != '?')
10141 ptr++;
10142
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010143 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010144 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010145 return 1;
10146}
10147
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010148/* This produces a concatenation of the first occurrence of the Host header
10149 * followed by the path component if it begins with a slash ('/'). This means
10150 * that '*' will not be added, resulting in exactly the first Host entry.
10151 * If no Host header is found, then the path is returned as-is. The returned
10152 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010153 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010154 */
10155static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010156smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010157{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010158 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010159 char *ptr, *end, *beg;
10160 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010161 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010162
10163 CHECK_HTTP_MESSAGE_FIRST();
10164
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010165 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010166 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010167 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010168 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010169
10170 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010171 temp = get_trash_chunk();
10172 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010173 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010174 smp->data.u.str.str = temp->str;
10175 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010176
10177 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010178 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010179 beg = http_get_path(txn);
10180 if (!beg)
10181 beg = end;
10182
10183 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10184
10185 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010186 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10187 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010188 }
10189
10190 smp->flags = SMP_F_VOL_1ST;
10191 return 1;
10192}
10193
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010194/* This produces a 32-bit hash of the concatenation of the first occurrence of
10195 * the Host header followed by the path component if it begins with a slash ('/').
10196 * This means that '*' will not be added, resulting in exactly the first Host
10197 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010198 * is hashed using the path hash followed by a full avalanche hash and provides a
10199 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010200 * high-traffic sites without having to store whole paths.
10201 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010202int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010203smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010204{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010205 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010206 struct hdr_ctx ctx;
10207 unsigned int hash = 0;
10208 char *ptr, *beg, *end;
10209 int len;
10210
10211 CHECK_HTTP_MESSAGE_FIRST();
10212
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010213 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010214 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010215 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010216 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10217 ptr = ctx.line + ctx.val;
10218 len = ctx.vlen;
10219 while (len--)
10220 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10221 }
10222
10223 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010224 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010225 beg = http_get_path(txn);
10226 if (!beg)
10227 beg = end;
10228
10229 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10230
10231 if (beg < ptr && *beg == '/') {
10232 while (beg < ptr)
10233 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10234 }
10235 hash = full_hash(hash);
10236
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010237 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010238 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010239 smp->flags = SMP_F_VOL_1ST;
10240 return 1;
10241}
10242
Willy Tarreau4a550602012-12-09 14:53:32 +010010243/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010244 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10245 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10246 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010247 * that in environments where IPv6 is insignificant, truncating the output to
10248 * 8 bytes would still work.
10249 */
10250static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010251smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010252{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010253 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010254 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010255
10256 if (!cli_conn)
10257 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010258
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010259 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010260 return 0;
10261
Willy Tarreau47ca5452012-12-23 20:22:19 +010010262 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010263 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010264 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010265
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010266 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010267 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010268 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010269 temp->len += 4;
10270 break;
10271 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010272 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010273 temp->len += 16;
10274 break;
10275 default:
10276 return 0;
10277 }
10278
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010279 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010280 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010281 return 1;
10282}
10283
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010284/* Extracts the query string, which comes after the question mark '?'. If no
10285 * question mark is found, nothing is returned. Otherwise it returns a sample
10286 * of type string carrying the whole query string.
10287 */
10288static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010289smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010290{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010291 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010292 char *ptr, *end;
10293
10294 CHECK_HTTP_MESSAGE_FIRST();
10295
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010296 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010297 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10298 end = ptr + txn->req.sl.rq.u_l;
10299
10300 /* look up the '?' */
10301 do {
10302 if (ptr == end)
10303 return 0;
10304 } while (*ptr++ != '?');
10305
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010306 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010307 smp->data.u.str.str = ptr;
10308 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010309 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10310 return 1;
10311}
10312
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010313static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010314smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010315{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010316 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10317 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10318 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010319
Willy Tarreau24e32d82012-04-23 23:55:44 +020010320 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010321
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010322 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010323 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010324 return 1;
10325}
10326
Willy Tarreau7f18e522010-10-22 20:04:13 +020010327/* return a valid test if the current request is the first one on the connection */
10328static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010329smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010330{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010331 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010332 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010333 return 1;
10334}
10335
Willy Tarreau34db1082012-04-19 17:16:54 +020010336/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010337static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010338smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010339{
10340
Willy Tarreau24e32d82012-04-23 23:55:44 +020010341 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010342 return 0;
10343
Willy Tarreauc0239e02012-04-16 14:42:55 +020010344 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010345
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010346 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010347 return 0;
10348
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010349 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010350 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010351 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010352 return 1;
10353}
Willy Tarreau8797c062007-05-07 00:55:35 +020010354
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010355/* Accepts exactly 1 argument of type userlist */
10356static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010357smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010358{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010359 if (!args || args->type != ARGT_USR)
10360 return 0;
10361
10362 CHECK_HTTP_MESSAGE_FIRST();
10363
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010364 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010365 return 0;
10366
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010367 /* if the user does not belong to the userlist or has a wrong password,
10368 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010369 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010370 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010371 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10372 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010373 return 0;
10374
10375 /* pat_match_auth() will need the user list */
10376 smp->ctx.a[0] = args->data.usr;
10377
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010378 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010379 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010380 smp->data.u.str.str = smp->strm->txn->auth.user;
10381 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010382
10383 return 1;
10384}
10385
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010386/* Try to find the next occurrence of a cookie name in a cookie header value.
10387 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10388 * the cookie value is returned into *value and *value_l, and the function
10389 * returns a pointer to the next pointer to search from if the value was found.
10390 * Otherwise if the cookie was not found, NULL is returned and neither value
10391 * nor value_l are touched. The input <hdr> string should first point to the
10392 * header's value, and the <hdr_end> pointer must point to the first character
10393 * not part of the value. <list> must be non-zero if value may represent a list
10394 * of values (cookie headers). This makes it faster to abort parsing when no
10395 * list is expected.
10396 */
David Carlier4686f792015-09-25 14:10:50 +010010397char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010398extract_cookie_value(char *hdr, const char *hdr_end,
10399 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010400 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010401{
10402 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10403 char *next;
10404
10405 /* we search at least a cookie name followed by an equal, and more
10406 * generally something like this :
10407 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10408 */
10409 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10410 /* Iterate through all cookies on this line */
10411
Willy Tarreau2235b262016-11-05 15:50:20 +010010412 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010413 att_beg++;
10414
10415 /* find att_end : this is the first character after the last non
10416 * space before the equal. It may be equal to hdr_end.
10417 */
10418 equal = att_end = att_beg;
10419
10420 while (equal < hdr_end) {
10421 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10422 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010010423 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010424 continue;
10425 att_end = equal;
10426 }
10427
10428 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10429 * is between <att_beg> and <equal>, both may be identical.
10430 */
10431
10432 /* look for end of cookie if there is an equal sign */
10433 if (equal < hdr_end && *equal == '=') {
10434 /* look for the beginning of the value */
10435 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010010436 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010437 val_beg++;
10438
10439 /* find the end of the value, respecting quotes */
10440 next = find_cookie_value_end(val_beg, hdr_end);
10441
10442 /* make val_end point to the first white space or delimitor after the value */
10443 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010010444 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010445 val_end--;
10446 } else {
10447 val_beg = val_end = next = equal;
10448 }
10449
10450 /* We have nothing to do with attributes beginning with '$'. However,
10451 * they will automatically be removed if a header before them is removed,
10452 * since they're supposed to be linked together.
10453 */
10454 if (*att_beg == '$')
10455 continue;
10456
10457 /* Ignore cookies with no equal sign */
10458 if (equal == next)
10459 continue;
10460
10461 /* Now we have the cookie name between att_beg and att_end, and
10462 * its value between val_beg and val_end.
10463 */
10464
10465 if (att_end - att_beg == cookie_name_l &&
10466 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10467 /* let's return this value and indicate where to go on from */
10468 *value = val_beg;
10469 *value_l = val_end - val_beg;
10470 return next + 1;
10471 }
10472
10473 /* Set-Cookie headers only have the name in the first attr=value part */
10474 if (!list)
10475 break;
10476 }
10477
10478 return NULL;
10479}
10480
William Lallemanda43ba4e2014-01-28 18:14:25 +010010481/* Fetch a captured HTTP request header. The index is the position of
10482 * the "capture" option in the configuration file
10483 */
10484static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010485smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010486{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010487 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010488 int idx;
10489
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010490 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010491 return 0;
10492
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010493 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010494
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010495 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 +010010496 return 0;
10497
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010498 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010499 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010500 smp->data.u.str.str = smp->strm->req_cap[idx];
10501 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010502
10503 return 1;
10504}
10505
10506/* Fetch a captured HTTP response header. The index is the position of
10507 * the "capture" option in the configuration file
10508 */
10509static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010510smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010511{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010512 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010513 int idx;
10514
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010515 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010516 return 0;
10517
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010518 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010519
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010520 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 +010010521 return 0;
10522
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010523 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010524 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010525 smp->data.u.str.str = smp->strm->res_cap[idx];
10526 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010527
10528 return 1;
10529}
10530
William Lallemand65ad6e12014-01-31 15:08:02 +010010531/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10532static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010533smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010534{
10535 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010536 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010010537 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010538
Willy Tarreau15e91e12015-04-04 00:52:09 +020010539 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010540 return 0;
10541
William Lallemand96a77852014-02-05 00:30:02 +010010542 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010543
William Lallemand96a77852014-02-05 00:30:02 +010010544 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10545 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010546
William Lallemand96a77852014-02-05 00:30:02 +010010547 temp = get_trash_chunk();
10548 temp->str = txn->uri;
10549 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010550 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010551 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010552 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010553
10554 return 1;
10555
10556}
10557
10558/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10559static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010560smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010561{
10562 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010563 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010010564 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010565
Willy Tarreau15e91e12015-04-04 00:52:09 +020010566 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010567 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010568
William Lallemand65ad6e12014-01-31 15:08:02 +010010569 ptr = txn->uri;
10570
10571 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10572 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010573
William Lallemand65ad6e12014-01-31 15:08:02 +010010574 if (!*ptr)
10575 return 0;
10576
10577 ptr++; /* skip the space */
10578
10579 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010580 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010581 if (!ptr)
10582 return 0;
10583 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10584 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010585
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010586 smp->data.u.str = *temp;
10587 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010588 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010589 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010590
10591 return 1;
10592}
10593
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010594/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10595 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10596 */
10597static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010598smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010599{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010600 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010601
Willy Tarreau15e91e12015-04-04 00:52:09 +020010602 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010603 return 0;
10604
10605 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010606 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010607 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010608 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010609
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010610 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010611 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010612 smp->flags = SMP_F_CONST;
10613 return 1;
10614
10615}
10616
10617/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10618 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10619 */
10620static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010621smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010622{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010623 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010624
Willy Tarreau15e91e12015-04-04 00:52:09 +020010625 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010626 return 0;
10627
10628 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010629 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010630 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010631 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010632
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010633 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010634 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010635 smp->flags = SMP_F_CONST;
10636 return 1;
10637
10638}
10639
William Lallemand65ad6e12014-01-31 15:08:02 +010010640
Willy Tarreaue333ec92012-04-16 16:26:40 +020010641/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010642 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010643 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010644 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010645 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010646 * Accepts exactly 1 argument of type string. If the input options indicate
10647 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010648 * The returned sample is of type CSTR. Can be used to parse cookies in other
10649 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010650 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010651int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010652{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010653 struct http_txn *txn;
10654 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010655 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010656 const struct http_msg *msg;
10657 const char *hdr_name;
10658 int hdr_name_len;
10659 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010660 int occ = 0;
10661 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010662
Willy Tarreau24e32d82012-04-23 23:55:44 +020010663 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010664 return 0;
10665
Willy Tarreaua890d072013-04-02 12:01:06 +020010666 if (!ctx) {
10667 /* first call */
10668 ctx = &static_hdr_ctx;
10669 ctx->idx = 0;
10670 smp->ctx.a[2] = ctx;
10671 }
10672
Willy Tarreaue333ec92012-04-16 16:26:40 +020010673 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010674
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010675 txn = smp->strm->txn;
10676 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010677
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010678 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010679 msg = &txn->req;
10680 hdr_name = "Cookie";
10681 hdr_name_len = 6;
10682 } else {
10683 msg = &txn->rsp;
10684 hdr_name = "Set-Cookie";
10685 hdr_name_len = 10;
10686 }
10687
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010688 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020010689 /* no explicit occurrence and single fetch => last cookie by default */
10690 occ = -1;
10691
10692 /* OK so basically here, either we want only one value and it's the
10693 * last one, or we want to iterate over all of them and we fetch the
10694 * next one.
10695 */
10696
Willy Tarreau9b28e032012-10-12 23:49:43 +020010697 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010698 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010699 /* search for the header from the beginning, we must first initialize
10700 * the search parameters.
10701 */
Willy Tarreau37406352012-04-23 16:16:37 +020010702 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010703 ctx->idx = 0;
10704 }
10705
Willy Tarreau28376d62012-04-26 21:26:10 +020010706 smp->flags |= SMP_F_VOL_HDR;
10707
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010708 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010709 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10710 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010711 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10712 goto out;
10713
Willy Tarreau24e32d82012-04-23 23:55:44 +020010714 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010715 continue;
10716
Willy Tarreau37406352012-04-23 16:16:37 +020010717 smp->ctx.a[0] = ctx->line + ctx->val;
10718 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010719 }
10720
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010721 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010722 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010723 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010724 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010725 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010726 &smp->data.u.str.str,
10727 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010728 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010729 found = 1;
10730 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010731 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020010732 smp->flags |= SMP_F_NOT_LAST;
10733 return 1;
10734 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010735 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010736 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010737 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010738 /* all cookie headers and values were scanned. If we're looking for the
10739 * last occurrence, we may return it now.
10740 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010741 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010742 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010743 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010744}
10745
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010746/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010747 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010748 * multiple cookies may be parsed on the same line. The returned sample is of
10749 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010750 */
10751static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010752smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010753{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010754 struct http_txn *txn;
10755 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010756 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010757 const struct http_msg *msg;
10758 const char *hdr_name;
10759 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010760 int cnt;
10761 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010762 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010763
Willy Tarreau24e32d82012-04-23 23:55:44 +020010764 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010765 return 0;
10766
Willy Tarreaue333ec92012-04-16 16:26:40 +020010767 CHECK_HTTP_MESSAGE_FIRST();
10768
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010769 txn = smp->strm->txn;
10770 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010771
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010772 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010773 msg = &txn->req;
10774 hdr_name = "Cookie";
10775 hdr_name_len = 6;
10776 } else {
10777 msg = &txn->rsp;
10778 hdr_name = "Set-Cookie";
10779 hdr_name_len = 10;
10780 }
10781
Willy Tarreau9b28e032012-10-12 23:49:43 +020010782 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010783 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010784 ctx.idx = 0;
10785 cnt = 0;
10786
10787 while (1) {
10788 /* Note: val_beg == NULL every time we need to fetch a new header */
10789 if (!val_beg) {
10790 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10791 break;
10792
Willy Tarreau24e32d82012-04-23 23:55:44 +020010793 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010794 continue;
10795
10796 val_beg = ctx.line + ctx.val;
10797 val_end = val_beg + ctx.vlen;
10798 }
10799
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010800 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010801 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010802 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010803 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010804 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010805 &smp->data.u.str.str,
10806 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010807 cnt++;
10808 }
10809 }
10810
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010811 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010812 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010813 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010814 return 1;
10815}
10816
Willy Tarreau51539362012-05-08 12:46:28 +020010817/* Fetch an cookie's integer value. The integer value is returned. It
10818 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10819 */
10820static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010821smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020010822{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010823 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020010824
10825 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010826 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010827 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020010828 }
10829
10830 return ret;
10831}
10832
Willy Tarreau8797c062007-05-07 00:55:35 +020010833/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010834/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010835/************************************************************************/
10836
David Cournapeau16023ee2010-12-23 20:55:41 +090010837/*
10838 * Given a path string and its length, find the position of beginning of the
10839 * query string. Returns NULL if no query string is found in the path.
10840 *
10841 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10842 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010843 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090010844 */
bedis4c75cca2012-10-05 08:38:24 +020010845static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010846{
10847 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020010848
bedis4c75cca2012-10-05 08:38:24 +020010849 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090010850 return p ? p + 1 : NULL;
10851}
10852
bedis4c75cca2012-10-05 08:38:24 +020010853static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010854{
bedis4c75cca2012-10-05 08:38:24 +020010855 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090010856}
10857
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010858/* after increasing a pointer value, it can exceed the first buffer
10859 * size. This function transform the value of <ptr> according with
10860 * the expected position. <chunks> is an array of the one or two
10861 * avalaible chunks. The first value is the start of the first chunk,
10862 * the second value if the end+1 of the first chunks. The third value
10863 * is NULL or the start of the second chunk and the fourth value is
10864 * the end+1 of the second chunk. The function returns 1 if does a
10865 * wrap, else returns 0.
10866 */
10867static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
10868{
10869 if (*ptr < chunks[1])
10870 return 0;
10871 if (!chunks[2])
10872 return 0;
10873 *ptr = chunks[2] + ( *ptr - chunks[1] );
10874 return 1;
10875}
10876
David Cournapeau16023ee2010-12-23 20:55:41 +090010877/*
10878 * Given a url parameter, find the starting position of the first occurence,
10879 * or NULL if the parameter is not found.
10880 *
10881 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
10882 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010883 *
Willy Tarreauf6625822015-12-27 14:51:01 +010010884 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010885 * or the second chunk. The caller must be check the position before using the
10886 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090010887 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010888static const char *
10889find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010890 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010891 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010892{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010893 const char *pos, *last, *equal;
10894 const char **bufs = chunks;
10895 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090010896
David Cournapeau16023ee2010-12-23 20:55:41 +090010897
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010898 pos = bufs[0];
10899 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010010900 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010901 /* Check the equal. */
10902 equal = pos + url_param_name_l;
10903 if (fix_pointer_if_wrap(chunks, &equal)) {
10904 if (equal >= chunks[3])
10905 return NULL;
10906 } else {
10907 if (equal >= chunks[1])
10908 return NULL;
10909 }
10910 if (*equal == '=') {
10911 if (pos + url_param_name_l > last) {
10912 /* process wrap case, we detect a wrap. In this case, the
10913 * comparison is performed in two parts.
10914 */
10915
10916 /* This is the end, we dont have any other chunk. */
10917 if (bufs != chunks || !bufs[2])
10918 return NULL;
10919
10920 /* Compute the length of each part of the comparison. */
10921 l1 = last - pos;
10922 l2 = url_param_name_l - l1;
10923
10924 /* The second buffer is too short to contain the compared string. */
10925 if (bufs[2] + l2 > bufs[3])
10926 return NULL;
10927
10928 if (memcmp(pos, url_param_name, l1) == 0 &&
10929 memcmp(bufs[2], url_param_name+l1, l2) == 0)
10930 return pos;
10931
10932 /* Perform wrapping and jump the string who fail the comparison. */
10933 bufs += 2;
10934 pos = bufs[0] + l2;
10935 last = bufs[1];
10936
10937 } else {
10938 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010939 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
10940 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010941 pos += url_param_name_l + 1;
10942 if (fix_pointer_if_wrap(chunks, &pos))
10943 last = bufs[2];
10944 }
10945 }
10946
10947 while (1) {
10948 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010949 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010950 pos++;
10951 if (pos < last)
10952 break;
10953 /* process buffer wrapping. */
10954 if (bufs != chunks || !bufs[2])
10955 return NULL;
10956 bufs += 2;
10957 pos = bufs[0];
10958 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090010959 }
David Cournapeau16023ee2010-12-23 20:55:41 +090010960 pos++;
10961 }
10962 return NULL;
10963}
10964
10965/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010010966 * Given a url parameter name and a query string, find the next value.
10967 * An empty url_param_name matches the first available parameter.
10968 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
10969 * respectively provide a pointer to the value and its end.
10970 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090010971 */
10972static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010973find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010974 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010975 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010976{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010977 const char *arg_start, *qs_end;
10978 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090010979
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010980 arg_start = chunks[0];
10981 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010982 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010983 /* Looks for an argument name. */
10984 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010985 url_param_name, url_param_name_l,
10986 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010987 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010988 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010989 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010990 }
David Cournapeau16023ee2010-12-23 20:55:41 +090010991 if (!arg_start)
10992 return 0;
10993
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010994 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010995 while (1) {
10996 /* looks for the first argument. */
10997 value_start = memchr(arg_start, '=', qs_end - arg_start);
10998 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010999 /* Check for wrapping. */
11000 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011001 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011002 chunks[2]) {
11003 arg_start = chunks[2];
11004 qs_end = chunks[3];
11005 continue;
11006 }
11007 return 0;
11008 }
11009 break;
11010 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011011 value_start++;
11012 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011013 else {
11014 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011015 value_start = arg_start + url_param_name_l + 1;
11016
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011017 /* Check for pointer wrapping. */
11018 if (fix_pointer_if_wrap(chunks, &value_start)) {
11019 /* Update the end pointer. */
11020 qs_end = chunks[3];
11021
11022 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011023 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011024 return 0;
11025 }
11026 }
11027
David Cournapeau16023ee2010-12-23 20:55:41 +090011028 value_end = value_start;
11029
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011030 while (1) {
11031 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11032 value_end++;
11033 if (value_end < qs_end)
11034 break;
11035 /* process buffer wrapping. */
11036 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011037 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011038 chunks[2]) {
11039 value_end = chunks[2];
11040 qs_end = chunks[3];
11041 continue;
11042 }
11043 break;
11044 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011045
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011046 *vstart = value_start;
11047 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011048 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011049}
11050
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011051/* This scans a URL-encoded query string. It takes an optionally wrapping
11052 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11053 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11054 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011055 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011056static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011057smp_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 +090011058{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011059 const char *vstart, *vend;
11060 struct chunk *temp;
11061 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011062
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011063 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011064 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011065 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011066 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011067 return 0;
11068
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011069 /* Create sample. If the value is contiguous, return the pointer as CONST,
11070 * if the value is wrapped, copy-it in a buffer.
11071 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011072 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011073 if (chunks[2] &&
11074 vstart >= chunks[0] && vstart <= chunks[1] &&
11075 vend >= chunks[2] && vend <= chunks[3]) {
11076 /* Wrapped case. */
11077 temp = get_trash_chunk();
11078 memcpy(temp->str, vstart, chunks[1] - vstart);
11079 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011080 smp->data.u.str.str = temp->str;
11081 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011082 } else {
11083 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011084 smp->data.u.str.str = (char *)vstart;
11085 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011086 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11087 }
11088
11089 /* Update context, check wrapping. */
11090 chunks[0] = vend;
11091 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11092 chunks[1] = chunks[3];
11093 chunks[2] = NULL;
11094 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011095
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011096 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011097 smp->flags |= SMP_F_NOT_LAST;
11098
David Cournapeau16023ee2010-12-23 20:55:41 +090011099 return 1;
11100}
11101
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011102/* This function iterates over each parameter of the query string. It uses
11103 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011104 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11105 * An optional parameter name is passed in args[0], otherwise any parameter is
11106 * considered. It supports an optional delimiter argument for the beginning of
11107 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011108 */
11109static int
11110smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11111{
11112 struct http_msg *msg;
11113 char delim = '?';
11114 const char *name;
11115 int name_len;
11116
Dragan Dosen26f77e52015-05-25 10:02:11 +020011117 if (!args ||
11118 (args[0].type && args[0].type != ARGT_STR) ||
11119 (args[1].type && args[1].type != ARGT_STR))
11120 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011121
Dragan Dosen26f77e52015-05-25 10:02:11 +020011122 name = "";
11123 name_len = 0;
11124 if (args->type == ARGT_STR) {
11125 name = args->data.str.str;
11126 name_len = args->data.str.len;
11127 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011128
Dragan Dosen26f77e52015-05-25 10:02:11 +020011129 if (args[1].type)
11130 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011131
Dragan Dosen26f77e52015-05-25 10:02:11 +020011132 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011133 CHECK_HTTP_MESSAGE_FIRST();
11134
11135 msg = &smp->strm->txn->req;
11136
11137 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11138 msg->sl.rq.u_l, delim);
11139 if (!smp->ctx.a[0])
11140 return 0;
11141
11142 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011143
11144 /* Assume that the context is filled with NULL pointer
11145 * before the first call.
11146 * smp->ctx.a[2] = NULL;
11147 * smp->ctx.a[3] = NULL;
11148 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011149 }
11150
11151 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11152}
11153
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011154/* This function iterates over each parameter of the body. This requires
11155 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011156 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11157 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11158 * optional second part if the body wraps at the end of the buffer. An optional
11159 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011160 */
11161static int
11162smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11163{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011164 struct http_msg *msg;
11165 unsigned long len;
11166 unsigned long block1;
11167 char *body;
11168 const char *name;
11169 int name_len;
11170
11171 if (!args || (args[0].type && args[0].type != ARGT_STR))
11172 return 0;
11173
11174 name = "";
11175 name_len = 0;
11176 if (args[0].type == ARGT_STR) {
11177 name = args[0].data.str.str;
11178 name_len = args[0].data.str.len;
11179 }
11180
11181 if (!smp->ctx.a[0]) { // first call, find the query string
11182 CHECK_HTTP_MESSAGE_FIRST();
11183
11184 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011185 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011186 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011187 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011188
11189 len = http_body_bytes(msg);
11190 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11191
11192 block1 = len;
11193 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11194 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11195
11196 if (block1 == len) {
11197 /* buffer is not wrapped (or empty) */
11198 smp->ctx.a[0] = body;
11199 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011200
11201 /* Assume that the context is filled with NULL pointer
11202 * before the first call.
11203 * smp->ctx.a[2] = NULL;
11204 * smp->ctx.a[3] = NULL;
11205 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011206 }
11207 else {
11208 /* buffer is wrapped, we need to defragment it */
11209 smp->ctx.a[0] = body;
11210 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011211 smp->ctx.a[2] = msg->chn->buf->data;
11212 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011213 }
11214 }
11215 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11216}
11217
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011218/* Return the signed integer value for the specified url parameter (see url_param
11219 * above).
11220 */
11221static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011222smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011223{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011224 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011225
11226 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011227 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011228 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011229 }
11230
11231 return ret;
11232}
11233
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011234/* This produces a 32-bit hash of the concatenation of the first occurrence of
11235 * the Host header followed by the path component if it begins with a slash ('/').
11236 * This means that '*' will not be added, resulting in exactly the first Host
11237 * entry. If no Host header is found, then the path is used. The resulting value
11238 * is hashed using the url hash followed by a full avalanche hash and provides a
11239 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11240 * high-traffic sites without having to store whole paths.
11241 * this differs from the base32 functions in that it includes the url parameters
11242 * as well as the path
11243 */
11244static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011245smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011246{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011247 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011248 struct hdr_ctx ctx;
11249 unsigned int hash = 0;
11250 char *ptr, *beg, *end;
11251 int len;
11252
11253 CHECK_HTTP_MESSAGE_FIRST();
11254
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011255 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011256 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011257 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011258 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11259 ptr = ctx.line + ctx.val;
11260 len = ctx.vlen;
11261 while (len--)
11262 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11263 }
11264
11265 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011266 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 +000011267 beg = http_get_path(txn);
11268 if (!beg)
11269 beg = end;
11270
11271 for (ptr = beg; ptr < end ; ptr++);
11272
11273 if (beg < ptr && *beg == '/') {
11274 while (beg < ptr)
11275 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11276 }
11277 hash = full_hash(hash);
11278
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011279 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011280 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011281 smp->flags = SMP_F_VOL_1ST;
11282 return 1;
11283}
11284
11285/* This concatenates the source address with the 32-bit hash of the Host and
11286 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11287 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11288 * on the source address length. The URL hash is stored before the address so
11289 * that in environments where IPv6 is insignificant, truncating the output to
11290 * 8 bytes would still work.
11291 */
11292static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011293smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011294{
11295 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011296 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011297
Dragan Dosendb5af612016-06-16 11:23:01 +020011298 if (!cli_conn)
11299 return 0;
11300
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011301 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011302 return 0;
11303
11304 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020011305 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
11306 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011307
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011308 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011309 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011310 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011311 temp->len += 4;
11312 break;
11313 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011314 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011315 temp->len += 16;
11316 break;
11317 default:
11318 return 0;
11319 }
11320
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011321 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011322 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011323 return 1;
11324}
11325
Willy Tarreau185b5c42012-04-26 15:11:51 +020011326/* This function is used to validate the arguments passed to any "hdr" fetch
11327 * keyword. These keywords support an optional positive or negative occurrence
11328 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11329 * is assumed that the types are already the correct ones. Returns 0 on error,
11330 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11331 * error message in case of error, that the caller is responsible for freeing.
11332 * The initial location must either be freeable or NULL.
11333 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011334int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011335{
11336 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011337 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011338 return 0;
11339 }
11340 return 1;
11341}
11342
Willy Tarreau276fae92013-07-25 14:36:01 +020011343/* takes an UINT value on input supposed to represent the time since EPOCH,
11344 * adds an optional offset found in args[0] and emits a string representing
11345 * the date in RFC-1123/5322 format.
11346 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011347static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011348{
Cyril Bontéf78d8962016-01-22 19:40:28 +010011349 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020011350 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11351 struct chunk *temp;
11352 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011353 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011354 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011355
11356 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011357 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011358 curr_date += args[0].data.sint;
11359
11360 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011361 if (!tm)
11362 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011363
11364 temp = get_trash_chunk();
11365 temp->len = snprintf(temp->str, temp->size - temp->len,
11366 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11367 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11368 tm->tm_hour, tm->tm_min, tm->tm_sec);
11369
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011370 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011371 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011372 return 1;
11373}
11374
Thierry FOURNIERad903512014-04-11 17:51:01 +020011375/* Match language range with language tag. RFC2616 14.4:
11376 *
11377 * A language-range matches a language-tag if it exactly equals
11378 * the tag, or if it exactly equals a prefix of the tag such
11379 * that the first tag character following the prefix is "-".
11380 *
11381 * Return 1 if the strings match, else return 0.
11382 */
11383static inline int language_range_match(const char *range, int range_len,
11384 const char *tag, int tag_len)
11385{
11386 const char *end = range + range_len;
11387 const char *tend = tag + tag_len;
11388 while (range < end) {
11389 if (*range == '-' && tag == tend)
11390 return 1;
11391 if (*range != *tag || tag == tend)
11392 return 0;
11393 range++;
11394 tag++;
11395 }
11396 /* Return true only if the last char of the tag is matched. */
11397 return tag == tend;
11398}
11399
11400/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011401static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011402{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011403 const char *al = smp->data.u.str.str;
11404 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011405 const char *token;
11406 int toklen;
11407 int qvalue;
11408 const char *str;
11409 const char *w;
11410 int best_q = 0;
11411
11412 /* Set the constant to the sample, because the output of the
11413 * function will be peek in the constant configuration string.
11414 */
11415 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011416 smp->data.u.str.size = 0;
11417 smp->data.u.str.str = "";
11418 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011419
11420 /* Parse the accept language */
11421 while (1) {
11422
11423 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011424 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011425 al++;
11426 if (al >= end)
11427 break;
11428
11429 /* Start of the fisrt word. */
11430 token = al;
11431
11432 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011433 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011434 al++;
11435 if (al == token)
11436 goto expect_comma;
11437
11438 /* Length of the token. */
11439 toklen = al - token;
11440 qvalue = 1000;
11441
11442 /* Check if the token exists in the list. If the token not exists,
11443 * jump to the next token.
11444 */
11445 str = args[0].data.str.str;
11446 w = str;
11447 while (1) {
11448 if (*str == ';' || *str == '\0') {
11449 if (language_range_match(token, toklen, w, str-w))
11450 goto look_for_q;
11451 if (*str == '\0')
11452 goto expect_comma;
11453 w = str + 1;
11454 }
11455 str++;
11456 }
11457 goto expect_comma;
11458
11459look_for_q:
11460
11461 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011462 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011463 al++;
11464 if (al >= end)
11465 goto process_value;
11466
11467 /* If ',' is found, process the result */
11468 if (*al == ',')
11469 goto process_value;
11470
11471 /* If the character is different from ';', look
11472 * for the end of the header part in best effort.
11473 */
11474 if (*al != ';')
11475 goto expect_comma;
11476
11477 /* Assumes that the char is ';', now expect "q=". */
11478 al++;
11479
11480 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011481 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011482 al++;
11483 if (al >= end)
11484 goto process_value;
11485
11486 /* Expect 'q'. If no 'q', continue in best effort */
11487 if (*al != 'q')
11488 goto process_value;
11489 al++;
11490
11491 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011492 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011493 al++;
11494 if (al >= end)
11495 goto process_value;
11496
11497 /* Expect '='. If no '=', continue in best effort */
11498 if (*al != '=')
11499 goto process_value;
11500 al++;
11501
11502 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011503 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011504 al++;
11505 if (al >= end)
11506 goto process_value;
11507
11508 /* Parse the q value. */
11509 qvalue = parse_qvalue(al, &al);
11510
11511process_value:
11512
11513 /* If the new q value is the best q value, then store the associated
11514 * language in the response. If qvalue is the biggest value (1000),
11515 * break the process.
11516 */
11517 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011518 smp->data.u.str.str = (char *)w;
11519 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011520 if (qvalue >= 1000)
11521 break;
11522 best_q = qvalue;
11523 }
11524
11525expect_comma:
11526
11527 /* Expect comma or end. If the end is detected, quit the loop. */
11528 while (al < end && *al != ',')
11529 al++;
11530 if (al >= end)
11531 break;
11532
11533 /* Comma is found, jump it and restart the analyzer. */
11534 al++;
11535 }
11536
11537 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011538 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
11539 smp->data.u.str.str = args[1].data.str.str;
11540 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011541 }
11542
11543 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011544 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011545}
11546
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011547/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011548static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011549{
11550 /* If the constant flag is set or if not size is avalaible at
11551 * the end of the buffer, copy the string in other buffer
11552 * before decoding.
11553 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011554 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011555 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011556 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
11557 smp->data.u.str.str = str->str;
11558 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011559 smp->flags &= ~SMP_F_CONST;
11560 }
11561
11562 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011563 smp->data.u.str.str[smp->data.u.str.len] = '\0';
11564 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Christopher Fauleta2584792017-10-05 10:03:12 +020011565 return (smp->data.u.str.len >= 0);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011566}
11567
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011568static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
11569{
11570 struct proxy *fe = strm_fe(smp->strm);
11571 int idx, i;
11572 struct cap_hdr *hdr;
11573 int len;
11574
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011575 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011576 return 0;
11577
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011578 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011579
11580 /* Check the availibity of the capture id. */
11581 if (idx > fe->nb_req_cap - 1)
11582 return 0;
11583
11584 /* Look for the original configuration. */
11585 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
11586 hdr != NULL && i != idx ;
11587 i--, hdr = hdr->next);
11588 if (!hdr)
11589 return 0;
11590
11591 /* check for the memory allocation */
11592 if (smp->strm->req_cap[hdr->index] == NULL)
11593 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
11594 if (smp->strm->req_cap[hdr->index] == NULL)
11595 return 0;
11596
11597 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011598 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011599 if (len > hdr->len)
11600 len = hdr->len;
11601
11602 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011603 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011604 smp->strm->req_cap[idx][len] = '\0';
11605
11606 return 1;
11607}
11608
11609static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
11610{
11611 struct proxy *fe = strm_fe(smp->strm);
11612 int idx, i;
11613 struct cap_hdr *hdr;
11614 int len;
11615
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011616 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011617 return 0;
11618
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011619 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011620
11621 /* Check the availibity of the capture id. */
11622 if (idx > fe->nb_rsp_cap - 1)
11623 return 0;
11624
11625 /* Look for the original configuration. */
11626 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
11627 hdr != NULL && i != idx ;
11628 i--, hdr = hdr->next);
11629 if (!hdr)
11630 return 0;
11631
11632 /* check for the memory allocation */
11633 if (smp->strm->res_cap[hdr->index] == NULL)
11634 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
11635 if (smp->strm->res_cap[hdr->index] == NULL)
11636 return 0;
11637
11638 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011639 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011640 if (len > hdr->len)
11641 len = hdr->len;
11642
11643 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011644 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011645 smp->strm->res_cap[idx][len] = '\0';
11646
11647 return 1;
11648}
11649
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011650/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011651 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011652 * the relevant part of the request line accordingly. Then it updates various
11653 * pointers to the next elements which were moved, and the total buffer length.
11654 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011655 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11656 * error, though this can be revisited when this code is finally exploited.
11657 *
11658 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
11659 * query string and 3 to replace uri.
11660 *
11661 * In query string case, the mark question '?' must be set at the start of the
11662 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011663 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011664int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011665 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011666{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011667 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011668 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011669 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011670 int delta;
11671
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011672 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011673 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011674 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011675 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11676
11677 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011678 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011679 txn->req.sl.rq.m_l += delta;
11680 txn->req.sl.rq.u += delta;
11681 txn->req.sl.rq.v += delta;
11682 break;
11683
11684 case 1: // path
11685 cur_ptr = http_get_path(txn);
11686 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011687 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011688
11689 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011690 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 +010011691 cur_end++;
11692
11693 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011694 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011695 txn->req.sl.rq.u_l += delta;
11696 txn->req.sl.rq.v += delta;
11697 break;
11698
11699 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011700 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011701 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011702 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11703 while (cur_ptr < cur_end && *cur_ptr != '?')
11704 cur_ptr++;
11705
11706 /* skip the question mark or indicate that we must insert it
11707 * (but only if the format string is not empty then).
11708 */
11709 if (cur_ptr < cur_end)
11710 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011711 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011712 offset = 0;
11713
11714 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011715 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011716 txn->req.sl.rq.u_l += delta;
11717 txn->req.sl.rq.v += delta;
11718 break;
11719
11720 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011721 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011722 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11723
11724 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011725 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011726 txn->req.sl.rq.u_l += delta;
11727 txn->req.sl.rq.v += delta;
11728 break;
11729
11730 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011731 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011732 }
11733
11734 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011735 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020011736 txn->req.sl.rq.l += delta;
11737 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011738 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011739 return 0;
11740}
11741
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011742/* This function replace the HTTP status code and the associated message. The
11743 * variable <status> contains the new status code. This function never fails.
11744 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011745void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011746{
11747 struct http_txn *txn = s->txn;
11748 char *cur_ptr, *cur_end;
11749 int delta;
11750 char *res;
11751 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011752 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011753 int msg_len;
11754
11755 chunk_reset(&trash);
11756
11757 res = ultoa_o(status, trash.str, trash.size);
11758 c_l = res - trash.str;
11759
11760 trash.str[c_l] = ' ';
11761 trash.len = c_l + 1;
11762
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011763 /* Do we have a custom reason format string? */
11764 if (msg == NULL)
11765 msg = get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011766 msg_len = strlen(msg);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011767 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
11768 trash.len += msg_len;
11769
11770 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
11771 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
11772
11773 /* commit changes and adjust message */
11774 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
11775
11776 /* adjust res line offsets and lengths */
11777 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
11778 txn->rsp.sl.st.c_l = c_l;
11779 txn->rsp.sl.st.r_l = msg_len;
11780
11781 delta = trash.len - (cur_end - cur_ptr);
11782 txn->rsp.sl.st.l += delta;
11783 txn->hdr_idx.v[0].len += delta;
11784 http_msg_move_end(&txn->rsp, delta);
11785}
11786
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011787/* This function executes one of the set-{method,path,query,uri} actions. It
11788 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011789 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011790 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011791 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
11792 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011793 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011794enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011795 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011796{
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011797 struct chunk *replace;
11798 enum act_return ret = ACT_RET_ERR;
11799
11800 replace = alloc_trash_chunk();
11801 if (!replace)
11802 goto leave;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011803
11804 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011805 if (rule->arg.http.action == 2)
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011806 replace->str[replace->len++] = '?';
11807 replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len,
11808 &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011809
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011810 http_replace_req_line(rule->arg.http.action, replace->str, replace->len, px, s);
11811
11812 ret = ACT_RET_CONT;
11813
11814leave:
11815 free_trash_chunk(replace);
11816 return ret;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011817}
11818
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011819/* This function is just a compliant action wrapper for "set-status". */
11820enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011821 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011822{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011823 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011824 return ACT_RET_CONT;
11825}
11826
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011827/* parse an http-request action among :
11828 * set-method
11829 * set-path
11830 * set-query
11831 * set-uri
11832 *
11833 * All of them accept a single argument of type string representing a log-format.
11834 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
11835 * 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 +020011836 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011837 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011838enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
11839 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011840{
11841 int cur_arg = *orig_arg;
11842
Thierry FOURNIER42148732015-09-02 17:17:33 +020011843 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011844
11845 switch (args[0][4]) {
11846 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011847 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011848 rule->action_ptr = http_action_set_req_line;
11849 break;
11850 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011851 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011852 rule->action_ptr = http_action_set_req_line;
11853 break;
11854 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011855 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011856 rule->action_ptr = http_action_set_req_line;
11857 break;
11858 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011859 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011860 rule->action_ptr = http_action_set_req_line;
11861 break;
11862 default:
11863 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011864 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011865 }
11866
11867 if (!*args[cur_arg] ||
11868 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
11869 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011870 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011871 }
11872
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011873 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011874 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010011875 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +010011876 (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 +010011877 return ACT_RET_PRS_ERR;
11878 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011879
11880 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011881 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011882}
11883
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011884/* parse set-status action:
11885 * This action accepts a single argument of type int representing
11886 * an http status code. It returns ACT_RET_PRS_OK on success,
11887 * ACT_RET_PRS_ERR on error.
11888 */
11889enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
11890 struct act_rule *rule, char **err)
11891{
11892 char *error;
11893
Thierry FOURNIER42148732015-09-02 17:17:33 +020011894 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011895 rule->action_ptr = action_http_set_status;
11896
11897 /* Check if an argument is available */
11898 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011899 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011900 return ACT_RET_PRS_ERR;
11901 }
11902
11903 /* convert status code as integer */
11904 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
11905 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
11906 memprintf(err, "expects an integer status code between 100 and 999");
11907 return ACT_RET_PRS_ERR;
11908 }
11909
11910 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011911
11912 /* set custom reason string */
11913 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
11914 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
11915 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
11916 (*orig_arg)++;
11917 rule->arg.status.reason = strdup(args[*orig_arg]);
11918 (*orig_arg)++;
11919 }
11920
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011921 return ACT_RET_PRS_OK;
11922}
11923
Willy Tarreaua9083d02015-05-08 15:27:59 +020011924/* This function executes the "capture" action. It executes a fetch expression,
11925 * turns the result into a string and puts it in a capture slot. It always
11926 * returns 1. If an error occurs the action is cancelled, but the rule
11927 * processing continues.
11928 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011929enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011930 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020011931{
Willy Tarreaua9083d02015-05-08 15:27:59 +020011932 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020011933 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011934 char **cap = s->req_cap;
11935 int len;
11936
Thierry FOURNIER32b15002015-07-31 08:56:16 +020011937 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 +020011938 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011939 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011940
11941 if (cap[h->index] == NULL)
11942 cap[h->index] = pool_alloc2(h->pool);
11943
11944 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011945 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011946
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011947 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011948 if (len > h->len)
11949 len = h->len;
11950
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011951 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020011952 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011953 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011954}
11955
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011956/* This function executes the "capture" action and store the result in a
11957 * capture slot if exists. It executes a fetch expression, turns the result
11958 * into a string and puts it in a capture slot. It always returns 1. If an
11959 * error occurs the action is cancelled, but the rule processing continues.
11960 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011961enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011962 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011963{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011964 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011965 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011966 char **cap = s->req_cap;
11967 struct proxy *fe = strm_fe(s);
11968 int len;
11969 int i;
11970
11971 /* Look for the original configuration. */
11972 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020011973 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011974 i--, h = h->next);
11975 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011976 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011977
Thierry FOURNIERe2097972015-07-31 08:56:35 +020011978 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 +020011979 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011980 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011981
11982 if (cap[h->index] == NULL)
11983 cap[h->index] = pool_alloc2(h->pool);
11984
11985 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011986 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011987
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011988 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011989 if (len > h->len)
11990 len = h->len;
11991
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011992 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011993 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011994 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011995}
11996
Christopher Faulet29730ba2017-09-18 15:26:32 +020011997/* Check an "http-request capture" action.
11998 *
11999 * The function returns 1 in success case, otherwise, it returns 0 and err is
12000 * filled.
12001 */
12002int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
12003{
12004 if (rule->arg.capid.idx >= px->nb_req_cap) {
12005 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
12006 rule->arg.capid.idx);
12007 return 0;
12008 }
12009
12010 return 1;
12011}
12012
Willy Tarreaua9083d02015-05-08 15:27:59 +020012013/* parse an "http-request capture" action. It takes a single argument which is
12014 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012015 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012016 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012017 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012018enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12019 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012020{
12021 struct sample_expr *expr;
12022 struct cap_hdr *hdr;
12023 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012024 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012025
12026 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12027 if (strcmp(args[cur_arg], "if") == 0 ||
12028 strcmp(args[cur_arg], "unless") == 0)
12029 break;
12030
12031 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012032 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012033 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012034 }
12035
Willy Tarreaua9083d02015-05-08 15:27:59 +020012036 cur_arg = *orig_arg;
12037 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12038 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012039 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012040
12041 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12042 memprintf(err,
12043 "fetch method '%s' extracts information from '%s', none of which is available here",
12044 args[cur_arg-1], sample_src_names(expr->fetch->use));
12045 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012046 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012047 }
12048
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012049 if (!args[cur_arg] || !*args[cur_arg]) {
12050 memprintf(err, "expects 'len or 'id'");
12051 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012052 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012053 }
12054
Willy Tarreaua9083d02015-05-08 15:27:59 +020012055 if (strcmp(args[cur_arg], "len") == 0) {
12056 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012057
12058 if (!(px->cap & PR_CAP_FE)) {
12059 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012060 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012061 }
12062
12063 proxy->conf.args.ctx = ARGC_CAP;
12064
Willy Tarreaua9083d02015-05-08 15:27:59 +020012065 if (!args[cur_arg]) {
12066 memprintf(err, "missing length value");
12067 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012068 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012069 }
12070 /* we copy the table name for now, it will be resolved later */
12071 len = atoi(args[cur_arg]);
12072 if (len <= 0) {
12073 memprintf(err, "length must be > 0");
12074 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012075 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012076 }
12077 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012078
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012079 if (!len) {
12080 memprintf(err, "a positive 'len' argument is mandatory");
12081 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012082 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012083 }
12084
Vincent Bernat02779b62016-04-03 13:48:43 +020012085 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012086 hdr->next = px->req_cap;
12087 hdr->name = NULL; /* not a header capture */
12088 hdr->namelen = 0;
12089 hdr->len = len;
12090 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12091 hdr->index = px->nb_req_cap++;
12092
12093 px->req_cap = hdr;
12094 px->to_log |= LW_REQHDR;
12095
Thierry FOURNIER42148732015-09-02 17:17:33 +020012096 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012097 rule->action_ptr = http_action_req_capture;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012098 rule->check_ptr = check_http_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012099 rule->arg.cap.expr = expr;
12100 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012101 }
12102
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012103 else if (strcmp(args[cur_arg], "id") == 0) {
12104 int id;
12105 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012106
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012107 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012108
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012109 if (!args[cur_arg]) {
12110 memprintf(err, "missing id value");
12111 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012112 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012113 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012114
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012115 id = strtol(args[cur_arg], &error, 10);
12116 if (*error != '\0') {
12117 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12118 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012119 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012120 }
12121 cur_arg++;
12122
12123 proxy->conf.args.ctx = ARGC_CAP;
12124
Thierry FOURNIER42148732015-09-02 17:17:33 +020012125 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012126 rule->action_ptr = http_action_req_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012127 rule->check_ptr = check_http_req_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012128 rule->arg.capid.expr = expr;
12129 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012130 }
12131
12132 else {
12133 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12134 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012135 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012136 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012137
12138 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012139 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012140}
12141
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012142/* This function executes the "capture" action and store the result in a
12143 * capture slot if exists. It executes a fetch expression, turns the result
12144 * into a string and puts it in a capture slot. It always returns 1. If an
12145 * error occurs the action is cancelled, but the rule processing continues.
12146 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012147enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012148 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012149{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012150 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012151 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012152 char **cap = s->res_cap;
12153 struct proxy *fe = strm_fe(s);
12154 int len;
12155 int i;
12156
12157 /* Look for the original configuration. */
12158 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012159 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012160 i--, h = h->next);
12161 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012162 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012163
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012164 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 +020012165 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012166 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012167
12168 if (cap[h->index] == NULL)
12169 cap[h->index] = pool_alloc2(h->pool);
12170
12171 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012172 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012173
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012174 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012175 if (len > h->len)
12176 len = h->len;
12177
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012178 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012179 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012180 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012181}
12182
Christopher Faulet29730ba2017-09-18 15:26:32 +020012183/* Check an "http-response capture" action.
12184 *
12185 * The function returns 1 in success case, otherwise, it returns 0 and err is
12186 * filled.
12187 */
12188int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
12189{
12190 if (rule->arg.capid.idx >= px->nb_rsp_cap) {
12191 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
12192 rule->arg.capid.idx);
12193 return 0;
12194 }
12195
12196 return 1;
12197}
12198
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012199/* parse an "http-response capture" action. It takes a single argument which is
12200 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12201 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012202 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012203 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012204enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12205 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012206{
12207 struct sample_expr *expr;
12208 int cur_arg;
12209 int id;
12210 char *error;
12211
12212 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12213 if (strcmp(args[cur_arg], "if") == 0 ||
12214 strcmp(args[cur_arg], "unless") == 0)
12215 break;
12216
12217 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012218 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012219 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012220 }
12221
12222 cur_arg = *orig_arg;
12223 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12224 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012225 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012226
12227 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12228 memprintf(err,
12229 "fetch method '%s' extracts information from '%s', none of which is available here",
12230 args[cur_arg-1], sample_src_names(expr->fetch->use));
12231 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 (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012236 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012237 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 if (strcmp(args[cur_arg], "id") != 0) {
12242 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12243 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012244 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012245 }
12246
12247 cur_arg++;
12248
12249 if (!args[cur_arg]) {
12250 memprintf(err, "missing id value");
12251 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012252 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012253 }
12254
12255 id = strtol(args[cur_arg], &error, 10);
12256 if (*error != '\0') {
12257 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12258 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012259 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012260 }
12261 cur_arg++;
12262
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012263 proxy->conf.args.ctx = ARGC_CAP;
12264
Thierry FOURNIER42148732015-09-02 17:17:33 +020012265 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012266 rule->action_ptr = http_action_res_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012267 rule->check_ptr = check_http_res_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012268 rule->arg.capid.expr = expr;
12269 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012270
12271 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012272 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012273}
12274
William Lallemand73025dd2014-04-24 14:38:37 +020012275/*
12276 * Return the struct http_req_action_kw associated to a keyword.
12277 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012278struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012279{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012280 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012281}
12282
12283/*
12284 * Return the struct http_res_action_kw associated to a keyword.
12285 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012286struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012287{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012288 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012289}
12290
Willy Tarreau12207b32016-11-22 19:48:51 +010012291
12292/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
12293 * now.
12294 */
12295static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private)
12296{
12297 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
12298 return 1;
12299
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012300 if (*args[2]) {
12301 struct proxy *px;
12302
12303 px = proxy_find_by_name(args[2], 0, 0);
12304 if (px)
12305 appctx->ctx.errors.iid = px->uuid;
12306 else
12307 appctx->ctx.errors.iid = atoi(args[2]);
12308
12309 if (!appctx->ctx.errors.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +020012310 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012311 appctx->ctx.cli.msg = "No such proxy.\n";
12312 appctx->st0 = CLI_ST_PRINT;
12313 return 1;
12314 }
12315 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012316 else
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012317 appctx->ctx.errors.iid = -1; // dump all proxies
12318
Willy Tarreau35069f82016-11-25 09:16:37 +010012319 appctx->ctx.errors.flag = 0;
12320 if (strcmp(args[3], "request") == 0)
12321 appctx->ctx.errors.flag |= 4; // ignore response
12322 else if (strcmp(args[3], "response") == 0)
12323 appctx->ctx.errors.flag |= 2; // ignore request
Willy Tarreau12207b32016-11-22 19:48:51 +010012324 appctx->ctx.errors.px = NULL;
Willy Tarreau12207b32016-11-22 19:48:51 +010012325 return 0;
12326}
12327
12328/* This function dumps all captured errors onto the stream interface's
12329 * read buffer. It returns 0 if the output buffer is full and it needs
12330 * to be called again, otherwise non-zero.
12331 */
12332static int cli_io_handler_show_errors(struct appctx *appctx)
12333{
12334 struct stream_interface *si = appctx->owner;
12335 extern const char *monthname[12];
12336
12337 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
12338 return 1;
12339
12340 chunk_reset(&trash);
12341
12342 if (!appctx->ctx.errors.px) {
12343 /* the function had not been called yet, let's prepare the
12344 * buffer for a response.
12345 */
12346 struct tm tm;
12347
12348 get_localtime(date.tv_sec, &tm);
12349 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
12350 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12351 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
12352 error_snapshot_id);
12353
Willy Tarreau06d80a92017-10-19 14:32:15 +020012354 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012355 /* Socket buffer full. Let's try again later from the same point */
12356 si_applet_cant_put(si);
12357 return 0;
12358 }
12359
12360 appctx->ctx.errors.px = proxy;
Willy Tarreau12207b32016-11-22 19:48:51 +010012361 appctx->ctx.errors.bol = 0;
12362 appctx->ctx.errors.ptr = -1;
12363 }
12364
12365 /* we have two inner loops here, one for the proxy, the other one for
12366 * the buffer.
12367 */
12368 while (appctx->ctx.errors.px) {
12369 struct error_snapshot *es;
12370
Willy Tarreau35069f82016-11-25 09:16:37 +010012371 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012372 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau35069f82016-11-25 09:16:37 +010012373 if (appctx->ctx.errors.flag & 2) // skip req
12374 goto next;
12375 }
12376 else {
Willy Tarreau12207b32016-11-22 19:48:51 +010012377 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau35069f82016-11-25 09:16:37 +010012378 if (appctx->ctx.errors.flag & 4) // skip resp
12379 goto next;
12380 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012381
12382 if (!es->when.tv_sec)
12383 goto next;
12384
12385 if (appctx->ctx.errors.iid >= 0 &&
12386 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
12387 es->oe->uuid != appctx->ctx.errors.iid)
12388 goto next;
12389
12390 if (appctx->ctx.errors.ptr < 0) {
12391 /* just print headers now */
12392
12393 char pn[INET6_ADDRSTRLEN];
12394 struct tm tm;
12395 int port;
12396
12397 get_localtime(es->when.tv_sec, &tm);
12398 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
12399 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12400 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
12401
12402 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
12403 case AF_INET:
12404 case AF_INET6:
12405 port = get_host_port(&es->src);
12406 break;
12407 default:
12408 port = 0;
12409 }
12410
Willy Tarreau35069f82016-11-25 09:16:37 +010012411 switch (appctx->ctx.errors.flag & 1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012412 case 0:
12413 chunk_appendf(&trash,
12414 " frontend %s (#%d): invalid request\n"
12415 " backend %s (#%d)",
12416 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12417 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
12418 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
12419 break;
12420 case 1:
12421 chunk_appendf(&trash,
12422 " backend %s (#%d): invalid response\n"
12423 " frontend %s (#%d)",
12424 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12425 es->oe->id, es->oe->uuid);
12426 break;
12427 }
12428
12429 chunk_appendf(&trash,
12430 ", server %s (#%d), event #%u\n"
12431 " src %s:%d, session #%d, session flags 0x%08x\n"
Willy Tarreau10e61cb2017-01-04 14:51:22 +010012432 " HTTP msg state %s(%d), msg flags 0x%08x, tx flags 0x%08x\n"
Willy Tarreau12207b32016-11-22 19:48:51 +010012433 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
12434 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
12435 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
12436 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
12437 es->ev_id,
12438 pn, port, es->sid, es->s_flags,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020012439 h1_msg_state_str(es->state), es->state, es->m_flags, es->t_flags,
Willy Tarreau12207b32016-11-22 19:48:51 +010012440 es->m_clen, es->m_blen,
12441 es->b_flags, es->b_out, es->b_tot,
12442 es->len, es->b_wrap, es->pos);
12443
Willy Tarreau06d80a92017-10-19 14:32:15 +020012444 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012445 /* Socket buffer full. Let's try again later from the same point */
12446 si_applet_cant_put(si);
12447 return 0;
12448 }
12449 appctx->ctx.errors.ptr = 0;
12450 appctx->ctx.errors.sid = es->sid;
12451 }
12452
12453 if (appctx->ctx.errors.sid != es->sid) {
12454 /* the snapshot changed while we were dumping it */
12455 chunk_appendf(&trash,
12456 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +020012457 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012458 si_applet_cant_put(si);
12459 return 0;
12460 }
12461 goto next;
12462 }
12463
12464 /* OK, ptr >= 0, so we have to dump the current line */
12465 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
12466 int newptr;
12467 int newline;
12468
12469 newline = appctx->ctx.errors.bol;
12470 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
12471 if (newptr == appctx->ctx.errors.ptr)
12472 return 0;
12473
Willy Tarreau06d80a92017-10-19 14:32:15 +020012474 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012475 /* Socket buffer full. Let's try again later from the same point */
12476 si_applet_cant_put(si);
12477 return 0;
12478 }
12479 appctx->ctx.errors.ptr = newptr;
12480 appctx->ctx.errors.bol = newline;
12481 };
12482 next:
12483 appctx->ctx.errors.bol = 0;
12484 appctx->ctx.errors.ptr = -1;
Willy Tarreau35069f82016-11-25 09:16:37 +010012485 appctx->ctx.errors.flag ^= 1;
12486 if (!(appctx->ctx.errors.flag & 1))
Willy Tarreau12207b32016-11-22 19:48:51 +010012487 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau12207b32016-11-22 19:48:51 +010012488 }
12489
12490 /* dump complete */
12491 return 1;
12492}
12493
12494/* register cli keywords */
12495static struct cli_kw_list cli_kws = {{ },{
12496 { { "show", "errors", NULL },
12497 "show errors : report last request and response errors for each proxy",
12498 cli_parse_show_errors, cli_io_handler_show_errors, NULL,
12499 },
12500 {{},}
12501}};
12502
Willy Tarreau4a568972010-05-12 08:08:50 +020012503/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012504/* All supported ACL keywords must be declared here. */
12505/************************************************************************/
12506
12507/* Note: must not be declared <const> as its list will be overwritten.
12508 * Please take care of keeping this list alphabetically sorted.
12509 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012510static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012511 { "base", "base", PAT_MATCH_STR },
12512 { "base_beg", "base", PAT_MATCH_BEG },
12513 { "base_dir", "base", PAT_MATCH_DIR },
12514 { "base_dom", "base", PAT_MATCH_DOM },
12515 { "base_end", "base", PAT_MATCH_END },
12516 { "base_len", "base", PAT_MATCH_LEN },
12517 { "base_reg", "base", PAT_MATCH_REG },
12518 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012519
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012520 { "cook", "req.cook", PAT_MATCH_STR },
12521 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12522 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12523 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12524 { "cook_end", "req.cook", PAT_MATCH_END },
12525 { "cook_len", "req.cook", PAT_MATCH_LEN },
12526 { "cook_reg", "req.cook", PAT_MATCH_REG },
12527 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012528
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012529 { "hdr", "req.hdr", PAT_MATCH_STR },
12530 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12531 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12532 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12533 { "hdr_end", "req.hdr", PAT_MATCH_END },
12534 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12535 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12536 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012537
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012538 /* these two declarations uses strings with list storage (in place
12539 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12540 * and delete functions are relative to the list management. The parse
12541 * and match method are related to the corresponding fetch methods. This
12542 * is very particular ACL declaration mode.
12543 */
12544 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12545 { "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 +020012546
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012547 { "path", "path", PAT_MATCH_STR },
12548 { "path_beg", "path", PAT_MATCH_BEG },
12549 { "path_dir", "path", PAT_MATCH_DIR },
12550 { "path_dom", "path", PAT_MATCH_DOM },
12551 { "path_end", "path", PAT_MATCH_END },
12552 { "path_len", "path", PAT_MATCH_LEN },
12553 { "path_reg", "path", PAT_MATCH_REG },
12554 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012555
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012556 { "req_ver", "req.ver", PAT_MATCH_STR },
12557 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012558
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012559 { "scook", "res.cook", PAT_MATCH_STR },
12560 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12561 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12562 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12563 { "scook_end", "res.cook", PAT_MATCH_END },
12564 { "scook_len", "res.cook", PAT_MATCH_LEN },
12565 { "scook_reg", "res.cook", PAT_MATCH_REG },
12566 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012567
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012568 { "shdr", "res.hdr", PAT_MATCH_STR },
12569 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12570 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12571 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12572 { "shdr_end", "res.hdr", PAT_MATCH_END },
12573 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12574 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12575 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012576
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012577 { "url", "url", PAT_MATCH_STR },
12578 { "url_beg", "url", PAT_MATCH_BEG },
12579 { "url_dir", "url", PAT_MATCH_DIR },
12580 { "url_dom", "url", PAT_MATCH_DOM },
12581 { "url_end", "url", PAT_MATCH_END },
12582 { "url_len", "url", PAT_MATCH_LEN },
12583 { "url_reg", "url", PAT_MATCH_REG },
12584 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012585
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012586 { "urlp", "urlp", PAT_MATCH_STR },
12587 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12588 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12589 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12590 { "urlp_end", "urlp", PAT_MATCH_END },
12591 { "urlp_len", "urlp", PAT_MATCH_LEN },
12592 { "urlp_reg", "urlp", PAT_MATCH_REG },
12593 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012594
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012595 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012596}};
12597
12598/************************************************************************/
12599/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012600/************************************************************************/
12601/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012602static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012603 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012604 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012605 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12606
Willy Tarreau87b09662015-04-03 00:22:06 +020012607 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012608 { "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 +020012609
12610 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012611 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12612 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12613 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012614
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012615 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12616 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012617
Willy Tarreau409bcde2013-01-08 00:31:00 +010012618 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12619 * are only here to match the ACL's name, are request-only and are used
12620 * for ACL compatibility only.
12621 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012622 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12623 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012624 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12625 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012626
12627 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12628 * only here to match the ACL's name, are request-only and are used for
12629 * ACL compatibility only.
12630 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012631 { "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 +020012632 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012633 { "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 +020012634 { "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 +010012635
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012636 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012637 { "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 +010012638 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012639 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012640 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012641 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012642
12643 /* HTTP protocol on the request path */
12644 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012645 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012646
12647 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012648 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12649 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012650
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012651 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012652 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12653 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012654 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012655
Thierry FOURNIERd7d88812017-04-19 15:15:14 +020012656 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER5617dce2017-04-09 05:38:19 +020012657 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12658
Willy Tarreau18ed2562013-01-14 15:56:36 +010012659 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012660 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12661 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012662
Willy Tarreau18ed2562013-01-14 15:56:36 +010012663 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012664 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012665 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12666 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012667
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012668 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012669 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012670 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012671 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012672 { "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 +010012673 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012674 { "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 +010012675
12676 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012677 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012678 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12679 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012680
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012681 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012682 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012683 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012684 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012685 { "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 +010012686 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012687 { "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 +010012688
Willy Tarreau409bcde2013-01-08 00:31:00 +010012689 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012690 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012691 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12692 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012693 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012694
12695 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012696 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012697 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012698 { "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 +020012699 { "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 +010012700
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012701 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020012702 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012703 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012704 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012705 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012706 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012707 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020012708 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12709 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012710 { "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 +010012711 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012712}};
12713
Willy Tarreau8797c062007-05-07 00:55:35 +020012714
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012715/************************************************************************/
12716/* All supported converter keywords must be declared here. */
12717/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012718/* Note: must not be declared <const> as its list will be overwritten */
12719static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012720 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020012721 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012722 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
12723 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012724 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012725 { NULL, NULL, 0, 0, 0 },
12726}};
12727
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012728
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012729/************************************************************************/
12730/* All supported http-request action keywords must be declared here. */
12731/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012732struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012733 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020012734 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012735 { "set-method", parse_set_req_line },
12736 { "set-path", parse_set_req_line },
12737 { "set-query", parse_set_req_line },
12738 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012739 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012740 }
12741};
12742
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012743struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012744 .kw = {
12745 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012746 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012747 { NULL, NULL }
12748 }
12749};
12750
Willy Tarreau8797c062007-05-07 00:55:35 +020012751__attribute__((constructor))
12752static void __http_protocol_init(void)
12753{
12754 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012755 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012756 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012757 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012758 http_res_keywords_register(&http_res_actions);
Willy Tarreau12207b32016-11-22 19:48:51 +010012759 cli_register_kw(&cli_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020012760}
12761
12762
Willy Tarreau58f10d72006-12-04 02:26:12 +010012763/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012764 * Local variables:
12765 * c-indent-level: 8
12766 * c-basic-offset: 8
12767 * End:
12768 */