blob: 07c53ee2551dd7368a5479bbb52ab1fe4fec5116 [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>
Christopher Fauletd7c91962015-04-30 11:48:27 +020042#include <types/filters.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreau8797c062007-05-07 00:55:35 +020045#include <proto/acl.h>
Thierry FOURNIER322a1242015-08-19 09:07:47 +020046#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020047#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010048#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020049#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020050#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010051#include <proto/checks.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020052#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020053#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020054#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020055#include <proto/filters.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020056#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020057#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010058#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010059#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020060#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010062#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020063#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020064#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010065#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020066#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010067#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020068#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020069#include <proto/pattern.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020070#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020071
Willy Tarreau522d6c02009-12-06 18:49:18 +010072const char HTTP_100[] =
73 "HTTP/1.1 100 Continue\r\n\r\n";
74
75const struct chunk http_100_chunk = {
76 .str = (char *)&HTTP_100,
77 .len = sizeof(HTTP_100)-1
78};
79
Willy Tarreaua9679ac2010-01-03 17:32:57 +010080/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020081const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010082 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010083 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020084 "Location: "; /* not terminated since it will be concatenated with the URL */
85
Willy Tarreau0f772532006-12-23 20:51:41 +010086const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010087 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010088 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010089 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010090 "Location: "; /* not terminated since it will be concatenated with the URL */
91
92/* same as 302 except that the browser MUST retry with the GET method */
93const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010094 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010095 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010096 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010097 "Location: "; /* not terminated since it will be concatenated with the URL */
98
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040099
100/* same as 302 except that the browser MUST retry with the same method */
101const char *HTTP_307 =
102 "HTTP/1.1 307 Temporary Redirect\r\n"
103 "Cache-Control: no-cache\r\n"
104 "Content-length: 0\r\n"
105 "Location: "; /* not terminated since it will be concatenated with the URL */
106
107/* same as 301 except that the browser MUST retry with the same method */
108const char *HTTP_308 =
109 "HTTP/1.1 308 Permanent Redirect\r\n"
110 "Content-length: 0\r\n"
111 "Location: "; /* not terminated since it will be concatenated with the URL */
112
Willy Tarreaubaaee002006-06-26 02:48:02 +0200113/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
114const char *HTTP_401_fmt =
115 "HTTP/1.0 401 Unauthorized\r\n"
116 "Cache-Control: no-cache\r\n"
117 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200118 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200119 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
120 "\r\n"
121 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
122
Willy Tarreau844a7e72010-01-31 21:46:18 +0100123const char *HTTP_407_fmt =
124 "HTTP/1.0 407 Unauthorized\r\n"
125 "Cache-Control: no-cache\r\n"
126 "Connection: close\r\n"
127 "Content-Type: text/html\r\n"
128 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
129 "\r\n"
Godbach1f1fae62014-12-17 16:32:05 +0800130 "<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 +0100131
Willy Tarreau0f772532006-12-23 20:51:41 +0100132
133const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200134 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100135 [HTTP_ERR_400] = 400,
136 [HTTP_ERR_403] = 403,
CJ Ess108b1dd2015-04-07 12:03:37 -0400137 [HTTP_ERR_405] = 405,
Willy Tarreau0f772532006-12-23 20:51:41 +0100138 [HTTP_ERR_408] = 408,
CJ Ess108b1dd2015-04-07 12:03:37 -0400139 [HTTP_ERR_429] = 429,
Willy Tarreau0f772532006-12-23 20:51:41 +0100140 [HTTP_ERR_500] = 500,
141 [HTTP_ERR_502] = 502,
142 [HTTP_ERR_503] = 503,
143 [HTTP_ERR_504] = 504,
144};
145
Willy Tarreau80587432006-12-24 17:47:20 +0100146static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200147 [HTTP_ERR_200] =
148 "HTTP/1.0 200 OK\r\n"
149 "Cache-Control: no-cache\r\n"
150 "Connection: close\r\n"
151 "Content-Type: text/html\r\n"
152 "\r\n"
153 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
154
Willy Tarreau0f772532006-12-23 20:51:41 +0100155 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100156 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100157 "Cache-Control: no-cache\r\n"
158 "Connection: close\r\n"
159 "Content-Type: text/html\r\n"
160 "\r\n"
161 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
162
163 [HTTP_ERR_403] =
164 "HTTP/1.0 403 Forbidden\r\n"
165 "Cache-Control: no-cache\r\n"
166 "Connection: close\r\n"
167 "Content-Type: text/html\r\n"
168 "\r\n"
169 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
170
CJ Ess108b1dd2015-04-07 12:03:37 -0400171 [HTTP_ERR_405] =
172 "HTTP/1.0 405 Method Not Allowed\r\n"
173 "Cache-Control: no-cache\r\n"
174 "Connection: close\r\n"
175 "Content-Type: text/html\r\n"
176 "\r\n"
177 "<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",
178
Willy Tarreau0f772532006-12-23 20:51:41 +0100179 [HTTP_ERR_408] =
180 "HTTP/1.0 408 Request Time-out\r\n"
181 "Cache-Control: no-cache\r\n"
182 "Connection: close\r\n"
183 "Content-Type: text/html\r\n"
184 "\r\n"
185 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
186
CJ Ess108b1dd2015-04-07 12:03:37 -0400187 [HTTP_ERR_429] =
188 "HTTP/1.0 429 Too Many Requests\r\n"
189 "Cache-Control: no-cache\r\n"
190 "Connection: close\r\n"
191 "Content-Type: text/html\r\n"
192 "\r\n"
193 "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n",
194
Willy Tarreau0f772532006-12-23 20:51:41 +0100195 [HTTP_ERR_500] =
196 "HTTP/1.0 500 Server Error\r\n"
197 "Cache-Control: no-cache\r\n"
198 "Connection: close\r\n"
199 "Content-Type: text/html\r\n"
200 "\r\n"
201 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
202
203 [HTTP_ERR_502] =
204 "HTTP/1.0 502 Bad Gateway\r\n"
205 "Cache-Control: no-cache\r\n"
206 "Connection: close\r\n"
207 "Content-Type: text/html\r\n"
208 "\r\n"
209 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
210
211 [HTTP_ERR_503] =
212 "HTTP/1.0 503 Service Unavailable\r\n"
213 "Cache-Control: no-cache\r\n"
214 "Connection: close\r\n"
215 "Content-Type: text/html\r\n"
216 "\r\n"
217 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
218
219 [HTTP_ERR_504] =
220 "HTTP/1.0 504 Gateway Time-out\r\n"
221 "Cache-Control: no-cache\r\n"
222 "Connection: close\r\n"
223 "Content-Type: text/html\r\n"
224 "\r\n"
225 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
226
227};
228
Cyril Bonté19979e12012-04-04 12:57:21 +0200229/* status codes available for the stats admin page (strictly 4 chars length) */
230const char *stat_status_codes[STAT_STATUS_SIZE] = {
231 [STAT_STATUS_DENY] = "DENY",
232 [STAT_STATUS_DONE] = "DONE",
233 [STAT_STATUS_ERRP] = "ERRP",
234 [STAT_STATUS_EXCD] = "EXCD",
235 [STAT_STATUS_NONE] = "NONE",
236 [STAT_STATUS_PART] = "PART",
237 [STAT_STATUS_UNKN] = "UNKN",
238};
239
240
William Lallemand73025dd2014-04-24 14:38:37 +0200241/* List head of all known action keywords for "http-request" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200242struct action_kw_list http_req_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200243 .list = LIST_HEAD_INIT(http_req_keywords.list)
244};
245
246/* List head of all known action keywords for "http-response" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200247struct action_kw_list http_res_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200248 .list = LIST_HEAD_INIT(http_res_keywords.list)
249};
250
Willy Tarreau80587432006-12-24 17:47:20 +0100251/* We must put the messages here since GCC cannot initialize consts depending
252 * on strlen().
253 */
254struct chunk http_err_chunks[HTTP_ERR_SIZE];
255
Willy Tarreaua890d072013-04-02 12:01:06 +0200256/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
257static struct hdr_ctx static_hdr_ctx;
258
Willy Tarreau42250582007-04-01 01:30:43 +0200259#define FD_SETS_ARE_BITFIELDS
260#ifdef FD_SETS_ARE_BITFIELDS
261/*
262 * This map is used with all the FD_* macros to check whether a particular bit
263 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
264 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
265 * byte should be encoded. Be careful to always pass bytes from 0 to 255
266 * exclusively to the macros.
267 */
268fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
269fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100270fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200271
272#else
273#error "Check if your OS uses bitfields for fd_sets"
274#endif
275
Willy Tarreau87b09662015-04-03 00:22:06 +0200276static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn);
Willy Tarreau0b748332014-04-29 00:13:29 +0200277
David Carlier7365f7d2016-04-04 11:54:42 +0100278static inline int http_msg_forward_body(struct stream *s, struct http_msg *msg);
279static inline int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +0100280
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200281/* This function returns a reason associated with the HTTP status.
282 * This function never fails, a message is always returned.
283 */
284const char *get_reason(unsigned int status)
285{
286 switch (status) {
287 case 100: return "Continue";
288 case 101: return "Switching Protocols";
289 case 102: return "Processing";
290 case 200: return "OK";
291 case 201: return "Created";
292 case 202: return "Accepted";
293 case 203: return "Non-Authoritative Information";
294 case 204: return "No Content";
295 case 205: return "Reset Content";
296 case 206: return "Partial Content";
297 case 207: return "Multi-Status";
298 case 210: return "Content Different";
299 case 226: return "IM Used";
300 case 300: return "Multiple Choices";
301 case 301: return "Moved Permanently";
302 case 302: return "Moved Temporarily";
303 case 303: return "See Other";
304 case 304: return "Not Modified";
305 case 305: return "Use Proxy";
306 case 307: return "Temporary Redirect";
307 case 308: return "Permanent Redirect";
308 case 310: return "Too many Redirects";
309 case 400: return "Bad Request";
310 case 401: return "Unauthorized";
311 case 402: return "Payment Required";
312 case 403: return "Forbidden";
313 case 404: return "Not Found";
314 case 405: return "Method Not Allowed";
315 case 406: return "Not Acceptable";
316 case 407: return "Proxy Authentication Required";
317 case 408: return "Request Time-out";
318 case 409: return "Conflict";
319 case 410: return "Gone";
320 case 411: return "Length Required";
321 case 412: return "Precondition Failed";
322 case 413: return "Request Entity Too Large";
323 case 414: return "Request-URI Too Long";
324 case 415: return "Unsupported Media Type";
325 case 416: return "Requested range unsatisfiable";
326 case 417: return "Expectation failed";
327 case 418: return "I'm a teapot";
328 case 422: return "Unprocessable entity";
329 case 423: return "Locked";
330 case 424: return "Method failure";
331 case 425: return "Unordered Collection";
332 case 426: return "Upgrade Required";
333 case 428: return "Precondition Required";
334 case 429: return "Too Many Requests";
335 case 431: return "Request Header Fields Too Large";
336 case 449: return "Retry With";
337 case 450: return "Blocked by Windows Parental Controls";
338 case 451: return "Unavailable For Legal Reasons";
339 case 456: return "Unrecoverable Error";
340 case 499: return "client has closed connection";
341 case 500: return "Internal Server Error";
342 case 501: return "Not Implemented";
343 case 502: return "Bad Gateway ou Proxy Error";
344 case 503: return "Service Unavailable";
345 case 504: return "Gateway Time-out";
346 case 505: return "HTTP Version not supported";
347 case 506: return "Variant also negociate";
348 case 507: return "Insufficient storage";
349 case 508: return "Loop detected";
350 case 509: return "Bandwidth Limit Exceeded";
351 case 510: return "Not extended";
352 case 511: return "Network authentication required";
353 case 520: return "Web server is returning an unknown error";
354 default:
355 switch (status) {
356 case 100 ... 199: return "Informational";
357 case 200 ... 299: return "Success";
358 case 300 ... 399: return "Redirection";
359 case 400 ... 499: return "Client Error";
360 case 500 ... 599: return "Server Error";
361 default: return "Other";
362 }
363 }
364}
365
Willy Tarreau80587432006-12-24 17:47:20 +0100366void init_proto_http()
367{
Willy Tarreau42250582007-04-01 01:30:43 +0200368 int i;
369 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100370 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200371
Willy Tarreau80587432006-12-24 17:47:20 +0100372 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
373 if (!http_err_msgs[msg]) {
374 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
375 abort();
376 }
377
378 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
379 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
380 }
Willy Tarreau42250582007-04-01 01:30:43 +0200381
382 /* initialize the log header encoding map : '{|}"#' should be encoded with
383 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
384 * URL encoding only requires '"', '#' to be encoded as well as non-
385 * printable characters above.
386 */
387 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
388 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100389 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200390 for (i = 0; i < 32; i++) {
391 FD_SET(i, hdr_encode_map);
392 FD_SET(i, url_encode_map);
393 }
394 for (i = 127; i < 256; i++) {
395 FD_SET(i, hdr_encode_map);
396 FD_SET(i, url_encode_map);
397 }
398
399 tmp = "\"#{|}";
400 while (*tmp) {
401 FD_SET(*tmp, hdr_encode_map);
402 tmp++;
403 }
404
405 tmp = "\"#";
406 while (*tmp) {
407 FD_SET(*tmp, url_encode_map);
408 tmp++;
409 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200410
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100411 /* initialize the http header encoding map. The draft httpbis define the
412 * header content as:
413 *
414 * HTTP-message = start-line
415 * *( header-field CRLF )
416 * CRLF
417 * [ message-body ]
418 * header-field = field-name ":" OWS field-value OWS
419 * field-value = *( field-content / obs-fold )
420 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
421 * obs-fold = CRLF 1*( SP / HTAB )
422 * field-vchar = VCHAR / obs-text
423 * VCHAR = %x21-7E
424 * obs-text = %x80-FF
425 *
426 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
427 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
428 * "obs-fold" is volontary forgotten because haproxy remove this.
429 */
430 memset(http_encode_map, 0, sizeof(http_encode_map));
431 for (i = 0x00; i <= 0x08; i++)
432 FD_SET(i, http_encode_map);
433 for (i = 0x0a; i <= 0x1f; i++)
434 FD_SET(i, http_encode_map);
435 FD_SET(0x7f, http_encode_map);
436
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200437 /* memory allocations */
Willy Tarreau63986c72015-04-03 22:55:33 +0200438 pool2_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200439 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100440 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100441}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200442
Willy Tarreau53b6c742006-12-17 13:37:46 +0100443/*
444 * We have 26 list of methods (1 per first letter), each of which can have
445 * up to 3 entries (2 valid, 1 null).
446 */
447struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100448 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100449 int len;
450 const char text[8];
451};
452
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100453const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100454 ['C' - 'A'] = {
455 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
456 },
457 ['D' - 'A'] = {
458 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
459 },
460 ['G' - 'A'] = {
461 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
462 },
463 ['H' - 'A'] = {
464 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
465 },
Christopher Fauletd57ad642015-07-31 14:26:57 +0200466 ['O' - 'A'] = {
467 [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" },
468 },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100469 ['P' - 'A'] = {
470 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
471 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
472 },
473 ['T' - 'A'] = {
474 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
475 },
476 /* rest is empty like this :
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200477 * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100478 */
479};
480
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100481const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100482 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
483 [HTTP_METH_GET] = { "GET", 3 },
484 [HTTP_METH_HEAD] = { "HEAD", 4 },
485 [HTTP_METH_POST] = { "POST", 4 },
486 [HTTP_METH_PUT] = { "PUT", 3 },
487 [HTTP_METH_DELETE] = { "DELETE", 6 },
488 [HTTP_METH_TRACE] = { "TRACE", 5 },
489 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
490};
491
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100492/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200493 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau2235b262016-11-05 15:50:20 +0100494 * RFC2616/RFC5234/RFC7230 for those chars. A token is any ASCII char that is
495 * neither a separator nor a CTL char. An http ver_token is any ASCII which can
496 * be found in an HTTP version, which includes 'H', 'T', 'P', '/', '.' and any
497 * digit. Note: please do not overwrite values in assignment since gcc-2.95
498 * will not handle them correctly. It's worth noting that chars 128..255 are
499 * nothing, not even control chars.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100500 */
Willy Tarreau2235b262016-11-05 15:50:20 +0100501const unsigned char http_char_classes[256] = {
502 [ 0] = HTTP_FLG_CTL,
503 [ 1] = HTTP_FLG_CTL,
504 [ 2] = HTTP_FLG_CTL,
505 [ 3] = HTTP_FLG_CTL,
506 [ 4] = HTTP_FLG_CTL,
507 [ 5] = HTTP_FLG_CTL,
508 [ 6] = HTTP_FLG_CTL,
509 [ 7] = HTTP_FLG_CTL,
510 [ 8] = HTTP_FLG_CTL,
511 [ 9] = HTTP_FLG_SPHT | HTTP_FLG_LWS | HTTP_FLG_SEP | HTTP_FLG_CTL,
512 [ 10] = HTTP_FLG_CRLF | HTTP_FLG_LWS | HTTP_FLG_CTL,
513 [ 11] = HTTP_FLG_CTL,
514 [ 12] = HTTP_FLG_CTL,
515 [ 13] = HTTP_FLG_CRLF | HTTP_FLG_LWS | HTTP_FLG_CTL,
516 [ 14] = HTTP_FLG_CTL,
517 [ 15] = HTTP_FLG_CTL,
518 [ 16] = HTTP_FLG_CTL,
519 [ 17] = HTTP_FLG_CTL,
520 [ 18] = HTTP_FLG_CTL,
521 [ 19] = HTTP_FLG_CTL,
522 [ 20] = HTTP_FLG_CTL,
523 [ 21] = HTTP_FLG_CTL,
524 [ 22] = HTTP_FLG_CTL,
525 [ 23] = HTTP_FLG_CTL,
526 [ 24] = HTTP_FLG_CTL,
527 [ 25] = HTTP_FLG_CTL,
528 [ 26] = HTTP_FLG_CTL,
529 [ 27] = HTTP_FLG_CTL,
530 [ 28] = HTTP_FLG_CTL,
531 [ 29] = HTTP_FLG_CTL,
532 [ 30] = HTTP_FLG_CTL,
533 [ 31] = HTTP_FLG_CTL,
534 [' '] = HTTP_FLG_SPHT | HTTP_FLG_LWS | HTTP_FLG_SEP,
535 ['!'] = HTTP_FLG_TOK,
536 ['"'] = HTTP_FLG_SEP,
537 ['#'] = HTTP_FLG_TOK,
538 ['$'] = HTTP_FLG_TOK,
539 ['%'] = HTTP_FLG_TOK,
540 ['&'] = HTTP_FLG_TOK,
541 [ 39] = HTTP_FLG_TOK,
542 ['('] = HTTP_FLG_SEP,
543 [')'] = HTTP_FLG_SEP,
544 ['*'] = HTTP_FLG_TOK,
545 ['+'] = HTTP_FLG_TOK,
546 [','] = HTTP_FLG_SEP,
547 ['-'] = HTTP_FLG_TOK,
548 ['.'] = HTTP_FLG_TOK | HTTP_FLG_VER,
549 ['/'] = HTTP_FLG_SEP | HTTP_FLG_VER,
550 ['0'] = HTTP_FLG_TOK | HTTP_FLG_VER,
551 ['1'] = HTTP_FLG_TOK | HTTP_FLG_VER,
552 ['2'] = HTTP_FLG_TOK | HTTP_FLG_VER,
553 ['3'] = HTTP_FLG_TOK | HTTP_FLG_VER,
554 ['4'] = HTTP_FLG_TOK | HTTP_FLG_VER,
555 ['5'] = HTTP_FLG_TOK | HTTP_FLG_VER,
556 ['6'] = HTTP_FLG_TOK | HTTP_FLG_VER,
557 ['7'] = HTTP_FLG_TOK | HTTP_FLG_VER,
558 ['8'] = HTTP_FLG_TOK | HTTP_FLG_VER,
559 ['9'] = HTTP_FLG_TOK | HTTP_FLG_VER,
560 [':'] = HTTP_FLG_SEP,
561 [';'] = HTTP_FLG_SEP,
562 ['<'] = HTTP_FLG_SEP,
563 ['='] = HTTP_FLG_SEP,
564 ['>'] = HTTP_FLG_SEP,
565 ['?'] = HTTP_FLG_SEP,
566 ['@'] = HTTP_FLG_SEP,
567 ['A'] = HTTP_FLG_TOK,
568 ['B'] = HTTP_FLG_TOK,
569 ['C'] = HTTP_FLG_TOK,
570 ['D'] = HTTP_FLG_TOK,
571 ['E'] = HTTP_FLG_TOK,
572 ['F'] = HTTP_FLG_TOK,
573 ['G'] = HTTP_FLG_TOK,
574 ['H'] = HTTP_FLG_TOK | HTTP_FLG_VER,
575 ['I'] = HTTP_FLG_TOK,
576 ['J'] = HTTP_FLG_TOK,
577 ['K'] = HTTP_FLG_TOK,
578 ['L'] = HTTP_FLG_TOK,
579 ['M'] = HTTP_FLG_TOK,
580 ['N'] = HTTP_FLG_TOK,
581 ['O'] = HTTP_FLG_TOK,
582 ['P'] = HTTP_FLG_TOK | HTTP_FLG_VER,
583 ['Q'] = HTTP_FLG_TOK,
584 ['R'] = HTTP_FLG_TOK | HTTP_FLG_VER,
585 ['S'] = HTTP_FLG_TOK | HTTP_FLG_VER,
586 ['T'] = HTTP_FLG_TOK | HTTP_FLG_VER,
587 ['U'] = HTTP_FLG_TOK,
588 ['V'] = HTTP_FLG_TOK,
589 ['W'] = HTTP_FLG_TOK,
590 ['X'] = HTTP_FLG_TOK,
591 ['Y'] = HTTP_FLG_TOK,
592 ['Z'] = HTTP_FLG_TOK,
593 ['['] = HTTP_FLG_SEP,
594 [ 92] = HTTP_FLG_SEP,
595 [']'] = HTTP_FLG_SEP,
596 ['^'] = HTTP_FLG_TOK,
597 ['_'] = HTTP_FLG_TOK,
598 ['`'] = HTTP_FLG_TOK,
599 ['a'] = HTTP_FLG_TOK,
600 ['b'] = HTTP_FLG_TOK,
601 ['c'] = HTTP_FLG_TOK,
602 ['d'] = HTTP_FLG_TOK,
603 ['e'] = HTTP_FLG_TOK,
604 ['f'] = HTTP_FLG_TOK,
605 ['g'] = HTTP_FLG_TOK,
606 ['h'] = HTTP_FLG_TOK,
607 ['i'] = HTTP_FLG_TOK,
608 ['j'] = HTTP_FLG_TOK,
609 ['k'] = HTTP_FLG_TOK,
610 ['l'] = HTTP_FLG_TOK,
611 ['m'] = HTTP_FLG_TOK,
612 ['n'] = HTTP_FLG_TOK,
613 ['o'] = HTTP_FLG_TOK,
614 ['p'] = HTTP_FLG_TOK,
615 ['q'] = HTTP_FLG_TOK,
616 ['r'] = HTTP_FLG_TOK,
617 ['s'] = HTTP_FLG_TOK,
618 ['t'] = HTTP_FLG_TOK,
619 ['u'] = HTTP_FLG_TOK,
620 ['v'] = HTTP_FLG_TOK,
621 ['w'] = HTTP_FLG_TOK,
622 ['x'] = HTTP_FLG_TOK,
623 ['y'] = HTTP_FLG_TOK,
624 ['z'] = HTTP_FLG_TOK,
625 ['{'] = HTTP_FLG_SEP,
626 ['|'] = HTTP_FLG_TOK,
627 ['}'] = HTTP_FLG_SEP,
628 ['~'] = HTTP_FLG_TOK,
629 [127] = HTTP_FLG_CTL,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100630};
631
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100632/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100633 * Adds a header and its CRLF at the tail of the message's buffer, just before
634 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100635 * The header is also automatically added to the index <hdr_idx>, and the end
636 * of headers is automatically adjusted. The number of bytes added is returned
637 * on success, otherwise <0 is returned indicating an error.
638 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100639int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100640{
641 int bytes, len;
642
643 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200644 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100645 if (!bytes)
646 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100647 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100648 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
649}
650
651/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100652 * Adds a header and its CRLF at the tail of the message's buffer, just before
653 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100654 * the buffer is only opened and the space reserved, but nothing is copied.
655 * The header is also automatically added to the index <hdr_idx>, and the end
656 * of headers is automatically adjusted. The number of bytes added is returned
657 * on success, otherwise <0 is returned indicating an error.
658 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100659int http_header_add_tail2(struct http_msg *msg,
660 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100661{
662 int bytes;
663
Willy Tarreau9b28e032012-10-12 23:49:43 +0200664 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100665 if (!bytes)
666 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100667 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100668 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
669}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200670
671/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100672 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
673 * If so, returns the position of the first non-space character relative to
674 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
675 * to return a pointer to the place after the first space. Returns 0 if the
676 * header name does not match. Checks are case-insensitive.
677 */
678int http_header_match2(const char *hdr, const char *end,
679 const char *name, int len)
680{
681 const char *val;
682
683 if (hdr + len >= end)
684 return 0;
685 if (hdr[len] != ':')
686 return 0;
687 if (strncasecmp(hdr, name, len) != 0)
688 return 0;
689 val = hdr + len + 1;
690 while (val < end && HTTP_IS_SPHT(*val))
691 val++;
692 if ((val >= end) && (len + 2 <= end - hdr))
693 return len + 2; /* we may replace starting from second space */
694 return val - hdr;
695}
696
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200697/* Find the first or next occurrence of header <name> in message buffer <sol>
698 * using headers index <idx>, and return it in the <ctx> structure. This
699 * structure holds everything necessary to use the header and find next
700 * occurrence. If its <idx> member is 0, the header is searched from the
701 * beginning. Otherwise, the next occurrence is returned. The function returns
702 * 1 when it finds a value, and 0 when there is no more. It is very similar to
703 * http_find_header2() except that it is designed to work with full-line headers
704 * whose comma is not a delimiter but is part of the syntax. As a special case,
705 * if ctx->val is NULL when searching for a new values of a header, the current
706 * header is rescanned. This allows rescanning after a header deletion.
707 */
708int http_find_full_header2(const char *name, int len,
709 char *sol, struct hdr_idx *idx,
710 struct hdr_ctx *ctx)
711{
712 char *eol, *sov;
713 int cur_idx, old_idx;
714
715 cur_idx = ctx->idx;
716 if (cur_idx) {
717 /* We have previously returned a header, let's search another one */
718 sol = ctx->line;
719 eol = sol + idx->v[cur_idx].len;
720 goto next_hdr;
721 }
722
723 /* first request for this header */
724 sol += hdr_idx_first_pos(idx);
725 old_idx = 0;
726 cur_idx = hdr_idx_first_idx(idx);
727 while (cur_idx) {
728 eol = sol + idx->v[cur_idx].len;
729
730 if (len == 0) {
731 /* No argument was passed, we want any header.
732 * To achieve this, we simply build a fake request. */
733 while (sol + len < eol && sol[len] != ':')
734 len++;
735 name = sol;
736 }
737
738 if ((len < eol - sol) &&
739 (sol[len] == ':') &&
740 (strncasecmp(sol, name, len) == 0)) {
741 ctx->del = len;
742 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100743 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200744 sov++;
745
746 ctx->line = sol;
747 ctx->prev = old_idx;
748 ctx->idx = cur_idx;
749 ctx->val = sov - sol;
750 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100751 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200752 eol--;
753 ctx->tws++;
754 }
755 ctx->vlen = eol - sov;
756 return 1;
757 }
758 next_hdr:
759 sol = eol + idx->v[cur_idx].cr + 1;
760 old_idx = cur_idx;
761 cur_idx = idx->v[cur_idx].next;
762 }
763 return 0;
764}
765
Willy Tarreauc90dc232015-02-20 13:51:36 +0100766/* Find the first or next header field in message buffer <sol> using headers
767 * index <idx>, and return it in the <ctx> structure. This structure holds
768 * everything necessary to use the header and find next occurrence. If its
769 * <idx> member is 0, the first header is retrieved. Otherwise, the next
770 * occurrence is returned. The function returns 1 when it finds a value, and
771 * 0 when there is no more. It is equivalent to http_find_full_header2() with
772 * no header name.
773 */
774int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
775{
776 char *eol, *sov;
777 int cur_idx, old_idx;
778 int len;
779
780 cur_idx = ctx->idx;
781 if (cur_idx) {
782 /* We have previously returned a header, let's search another one */
783 sol = ctx->line;
784 eol = sol + idx->v[cur_idx].len;
785 goto next_hdr;
786 }
787
788 /* first request for this header */
789 sol += hdr_idx_first_pos(idx);
790 old_idx = 0;
791 cur_idx = hdr_idx_first_idx(idx);
792 while (cur_idx) {
793 eol = sol + idx->v[cur_idx].len;
794
795 len = 0;
796 while (1) {
797 if (len >= eol - sol)
798 goto next_hdr;
799 if (sol[len] == ':')
800 break;
801 len++;
802 }
803
804 ctx->del = len;
805 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100806 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreauc90dc232015-02-20 13:51:36 +0100807 sov++;
808
809 ctx->line = sol;
810 ctx->prev = old_idx;
811 ctx->idx = cur_idx;
812 ctx->val = sov - sol;
813 ctx->tws = 0;
814
Willy Tarreau2235b262016-11-05 15:50:20 +0100815 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreauc90dc232015-02-20 13:51:36 +0100816 eol--;
817 ctx->tws++;
818 }
819 ctx->vlen = eol - sov;
820 return 1;
821
822 next_hdr:
823 sol = eol + idx->v[cur_idx].cr + 1;
824 old_idx = cur_idx;
825 cur_idx = idx->v[cur_idx].next;
826 }
827 return 0;
828}
829
Willy Tarreau68085d82010-01-18 14:54:04 +0100830/* Find the end of the header value contained between <s> and <e>. See RFC2616,
831 * par 2.2 for more information. Note that it requires a valid header to return
832 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200833 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100834char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200835{
836 int quoted, qdpair;
837
838 quoted = qdpair = 0;
839 for (; s < e; s++) {
840 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200841 else if (quoted) {
842 if (*s == '\\') qdpair = 1;
843 else if (*s == '"') quoted = 0;
844 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200845 else if (*s == '"') quoted = 1;
846 else if (*s == ',') return s;
847 }
848 return s;
849}
850
851/* Find the first or next occurrence of header <name> in message buffer <sol>
852 * using headers index <idx>, and return it in the <ctx> structure. This
853 * structure holds everything necessary to use the header and find next
854 * occurrence. If its <idx> member is 0, the header is searched from the
855 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100856 * 1 when it finds a value, and 0 when there is no more. It is designed to work
857 * with headers defined as comma-separated lists. As a special case, if ctx->val
858 * is NULL when searching for a new values of a header, the current header is
859 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200860 */
861int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100862 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200863 struct hdr_ctx *ctx)
864{
Willy Tarreau68085d82010-01-18 14:54:04 +0100865 char *eol, *sov;
866 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200867
Willy Tarreau68085d82010-01-18 14:54:04 +0100868 cur_idx = ctx->idx;
869 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200870 /* We have previously returned a value, let's search
871 * another one on the same line.
872 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200873 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200874 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100875 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200876 eol = sol + idx->v[cur_idx].len;
877
878 if (sov >= eol)
879 /* no more values in this header */
880 goto next_hdr;
881
Willy Tarreau68085d82010-01-18 14:54:04 +0100882 /* values remaining for this header, skip the comma but save it
883 * for later use (eg: for header deletion).
884 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200885 sov++;
Willy Tarreau2235b262016-11-05 15:50:20 +0100886 while (sov < eol && HTTP_IS_LWS((*sov)))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200887 sov++;
888
889 goto return_hdr;
890 }
891
892 /* first request for this header */
893 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100894 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200895 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200896 while (cur_idx) {
897 eol = sol + idx->v[cur_idx].len;
898
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200899 if (len == 0) {
900 /* No argument was passed, we want any header.
901 * To achieve this, we simply build a fake request. */
902 while (sol + len < eol && sol[len] != ':')
903 len++;
904 name = sol;
905 }
906
Willy Tarreau33a7e692007-06-10 19:45:56 +0200907 if ((len < eol - sol) &&
908 (sol[len] == ':') &&
909 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100910 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200911 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100912 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200913 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100914
Willy Tarreau33a7e692007-06-10 19:45:56 +0200915 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100916 ctx->prev = old_idx;
917 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200918 ctx->idx = cur_idx;
919 ctx->val = sov - sol;
920
921 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200922 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100923 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200924 eol--;
925 ctx->tws++;
926 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200927 ctx->vlen = eol - sov;
928 return 1;
929 }
930 next_hdr:
931 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100932 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200933 cur_idx = idx->v[cur_idx].next;
934 }
935 return 0;
936}
937
938int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100939 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200940 struct hdr_ctx *ctx)
941{
942 return http_find_header2(name, strlen(name), sol, idx, ctx);
943}
944
Willy Tarreau68085d82010-01-18 14:54:04 +0100945/* Remove one value of a header. This only works on a <ctx> returned by one of
946 * the http_find_header functions. The value is removed, as well as surrounding
947 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100948 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100949 * message <msg>. The new index is returned. If it is zero, it means there is
950 * no more header, so any processing may stop. The ctx is always left in a form
951 * that can be handled by http_find_header2() to find next occurrence.
952 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100953int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100954{
955 int cur_idx = ctx->idx;
956 char *sol = ctx->line;
957 struct hdr_idx_elem *hdr;
958 int delta, skip_comma;
959
960 if (!cur_idx)
961 return 0;
962
963 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200964 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100965 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200966 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100967 http_msg_move_end(msg, delta);
968 idx->used--;
969 hdr->len = 0; /* unused entry */
970 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100971 if (idx->tail == ctx->idx)
972 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100973 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100974 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100975 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200976 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100977 return ctx->idx;
978 }
979
980 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200981 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
982 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100983 */
984
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200985 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200986 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200987 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100988 NULL, 0);
989 hdr->len += delta;
990 http_msg_move_end(msg, delta);
991 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200992 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100993 return ctx->idx;
994}
995
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100996/* This function handles a server error at the stream interface level. The
997 * stream interface is assumed to be already in a closed state. An optional
998 * message is copied into the input buffer, and an HTTP status code stored.
999 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +01001000 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001001 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001002static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001003 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001004{
Christopher Faulet3e344292015-11-24 16:24:13 +01001005 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001006 channel_auto_read(si_oc(si));
1007 channel_abort(si_oc(si));
1008 channel_auto_close(si_oc(si));
1009 channel_erase(si_oc(si));
1010 channel_auto_close(si_ic(si));
1011 channel_auto_read(si_ic(si));
Willy Tarreau0f772532006-12-23 20:51:41 +01001012 if (status > 0 && msg) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02001013 s->txn->status = status;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001014 bo_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001015 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02001016 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001017 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001018 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001019 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001020}
1021
Willy Tarreau87b09662015-04-03 00:22:06 +02001022/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +01001023 * and message.
1024 */
1025
Willy Tarreau87b09662015-04-03 00:22:06 +02001026struct chunk *http_error_message(struct stream *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +01001027{
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001028 if (s->be->errmsg[msgnum].str)
1029 return &s->be->errmsg[msgnum];
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001030 else if (strm_fe(s)->errmsg[msgnum].str)
1031 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +01001032 else
1033 return &http_err_chunks[msgnum];
1034}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001035
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001036void
1037http_reply_and_close(struct stream *s, short status, struct chunk *msg)
1038{
Christopher Fauletd7c91962015-04-30 11:48:27 +02001039 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +01001040 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001041 stream_int_retnclose(&s->si[0], msg);
1042}
1043
Willy Tarreau53b6c742006-12-17 13:37:46 +01001044/*
Willy Tarreaub7ce4242015-09-03 17:15:21 +02001045 * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown
1046 * ones.
Willy Tarreau53b6c742006-12-17 13:37:46 +01001047 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +01001048enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +01001049{
1050 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001051 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001052
1053 m = ((unsigned)*str - 'A');
1054
1055 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001056 for (h = http_methods[m]; h->len > 0; h++) {
1057 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +01001058 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001059 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +01001060 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001061 };
Willy Tarreau53b6c742006-12-17 13:37:46 +01001062 }
Willy Tarreaub7ce4242015-09-03 17:15:21 +02001063 return HTTP_METH_OTHER;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001064}
1065
Willy Tarreau21d2af32008-02-14 20:25:24 +01001066/* Parse the URI from the given transaction (which is assumed to be in request
1067 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
1068 * It is returned otherwise.
1069 */
Thierry FOURNIER3c331782015-09-17 19:33:35 +02001070char *http_get_path(struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +01001071{
1072 char *ptr, *end;
1073
Willy Tarreau9b28e032012-10-12 23:49:43 +02001074 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +01001075 end = ptr + txn->req.sl.rq.u_l;
1076
1077 if (ptr >= end)
1078 return NULL;
1079
1080 /* RFC2616, par. 5.1.2 :
1081 * Request-URI = "*" | absuri | abspath | authority
1082 */
1083
1084 if (*ptr == '*')
1085 return NULL;
1086
1087 if (isalpha((unsigned char)*ptr)) {
1088 /* this is a scheme as described by RFC3986, par. 3.1 */
1089 ptr++;
1090 while (ptr < end &&
1091 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
1092 ptr++;
1093 /* skip '://' */
1094 if (ptr == end || *ptr++ != ':')
1095 return NULL;
1096 if (ptr == end || *ptr++ != '/')
1097 return NULL;
1098 if (ptr == end || *ptr++ != '/')
1099 return NULL;
1100 }
1101 /* skip [user[:passwd]@]host[:[port]] */
1102
1103 while (ptr < end && *ptr != '/')
1104 ptr++;
1105
1106 if (ptr == end)
1107 return NULL;
1108
1109 /* OK, we got the '/' ! */
1110 return ptr;
1111}
1112
William Lallemand65ad6e12014-01-31 15:08:02 +01001113/* Parse the URI from the given string and look for the "/" beginning the PATH.
1114 * If not found, return NULL. It is returned otherwise.
1115 */
1116static char *
1117http_get_path_from_string(char *str)
1118{
1119 char *ptr = str;
1120
1121 /* RFC2616, par. 5.1.2 :
1122 * Request-URI = "*" | absuri | abspath | authority
1123 */
1124
1125 if (*ptr == '*')
1126 return NULL;
1127
1128 if (isalpha((unsigned char)*ptr)) {
1129 /* this is a scheme as described by RFC3986, par. 3.1 */
1130 ptr++;
1131 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
1132 ptr++;
1133 /* skip '://' */
1134 if (*ptr == '\0' || *ptr++ != ':')
1135 return NULL;
1136 if (*ptr == '\0' || *ptr++ != '/')
1137 return NULL;
1138 if (*ptr == '\0' || *ptr++ != '/')
1139 return NULL;
1140 }
1141 /* skip [user[:passwd]@]host[:[port]] */
1142
1143 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
1144 ptr++;
1145
1146 if (*ptr == '\0' || *ptr == ' ')
1147 return NULL;
1148
1149 /* OK, we got the '/' ! */
1150 return ptr;
1151}
1152
Willy Tarreau71241ab2012-12-27 11:30:54 +01001153/* Returns a 302 for a redirectable request that reaches a server working in
1154 * in redirect mode. This may only be called just after the stream interface
1155 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
1156 * follow normal proxy processing. NOTE: this function is designed to support
1157 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001158 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001159void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001160{
1161 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001162 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001163 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001164 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001165
1166 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001167 trash.len = strlen(HTTP_302);
1168 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001169
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001170 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001171
Willy Tarreauefb453c2008-10-26 20:49:47 +01001172 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001173 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001174 return;
1175
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001176 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001177 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001178 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
1179 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001180 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001181
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001182 /* 3: add the request URI. Since it was already forwarded, we need
1183 * to temporarily rewind the buffer.
1184 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001185 txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001186 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001187
Willy Tarreauefb453c2008-10-26 20:49:47 +01001188 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001189 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 +02001190
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001191 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001192
Willy Tarreauefb453c2008-10-26 20:49:47 +01001193 if (!path)
1194 return;
1195
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001196 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001197 return;
1198
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001199 memcpy(trash.str + trash.len, path, len);
1200 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001201
1202 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001203 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1204 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001205 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001206 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1207 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001208 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001209
1210 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001211 si_shutr(si);
1212 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001213 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001214 si->state = SI_ST_CLO;
1215
1216 /* send the message */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001217 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001218
1219 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001220 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001221 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001222}
1223
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001224/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001225 * that the server side is closed. Note that err_type is actually a
1226 * bitmask, where almost only aborts may be cumulated with other
1227 * values. We consider that aborted operations are more important
1228 * than timeouts or errors due to the fact that nobody else in the
1229 * logs might explain incomplete retries. All others should avoid
1230 * being cumulated. It should normally not be possible to have multiple
1231 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001232 * Note that connection errors appearing on the second request of a keep-alive
1233 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001234 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001235void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001236{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001237 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001238
1239 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001240 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001241 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001242 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001243 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001244 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001245 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001246 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001247 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001248 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001249 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001250 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001251 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001252 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001253 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001254 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001255 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001256 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001257 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
1258 503, (s->flags & SF_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001259 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001260 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001261 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001262 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001263 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001264 else /* SI_ET_CONN_OTHER and others */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001265 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001266 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001267}
1268
Willy Tarreau42250582007-04-01 01:30:43 +02001269extern const char sess_term_cond[8];
1270extern const char sess_fin_state[8];
1271extern const char *monthname[12];
Willy Tarreau63986c72015-04-03 22:55:33 +02001272struct pool_head *pool2_http_txn;
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001273struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001274struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001275struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001276
Willy Tarreau117f59e2007-03-04 18:17:17 +01001277/*
1278 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001279 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001280 */
1281void capture_headers(char *som, struct hdr_idx *idx,
1282 char **cap, struct cap_hdr *cap_hdr)
1283{
1284 char *eol, *sol, *col, *sov;
1285 int cur_idx;
1286 struct cap_hdr *h;
1287 int len;
1288
1289 sol = som + hdr_idx_first_pos(idx);
1290 cur_idx = hdr_idx_first_idx(idx);
1291
1292 while (cur_idx) {
1293 eol = sol + idx->v[cur_idx].len;
1294
1295 col = sol;
1296 while (col < eol && *col != ':')
1297 col++;
1298
1299 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01001300 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001301 sov++;
1302
1303 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001304 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001305 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1306 if (cap[h->index] == NULL)
1307 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001308 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001309
1310 if (cap[h->index] == NULL) {
1311 Alert("HTTP capture : out of memory.\n");
1312 continue;
1313 }
1314
1315 len = eol - sov;
1316 if (len > h->len)
1317 len = h->len;
1318
1319 memcpy(cap[h->index], sov, len);
1320 cap[h->index][len]=0;
1321 }
1322 }
1323 sol = eol + idx->v[cur_idx].cr + 1;
1324 cur_idx = idx->v[cur_idx].next;
1325 }
1326}
1327
1328
Willy Tarreau42250582007-04-01 01:30:43 +02001329/* either we find an LF at <ptr> or we jump to <bad>.
1330 */
1331#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1332
1333/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1334 * otherwise to <http_msg_ood> with <state> set to <st>.
1335 */
1336#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1337 ptr++; \
1338 if (likely(ptr < end)) \
1339 goto good; \
1340 else { \
1341 state = (st); \
1342 goto http_msg_ood; \
1343 } \
1344 } while (0)
1345
1346
Willy Tarreaubaaee002006-06-26 02:48:02 +02001347/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001348 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001349 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1350 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1351 * will give undefined results.
1352 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1353 * and that msg->sol points to the beginning of the response.
1354 * If a complete line is found (which implies that at least one CR or LF is
1355 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1356 * returned indicating an incomplete line (which does not mean that parts have
1357 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1358 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1359 * upon next call.
1360 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001361 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001362 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1363 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001364 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001365 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001366const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001367 enum ht_state state, const char *ptr, const char *end,
1368 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001369{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001370 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001371
Willy Tarreau8973c702007-01-21 23:58:29 +01001372 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001373 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001374 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001375 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001376 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1377
1378 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001379 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001380 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1381 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001382 state = HTTP_MSG_ERROR;
1383 break;
1384
Willy Tarreau8973c702007-01-21 23:58:29 +01001385 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001386 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001387 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001388 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001389 goto http_msg_rpcode;
1390 }
1391 if (likely(HTTP_IS_SPHT(*ptr)))
1392 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1393 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001394 state = HTTP_MSG_ERROR;
1395 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001396
Willy Tarreau8973c702007-01-21 23:58:29 +01001397 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001398 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001399 if (likely(!HTTP_IS_LWS(*ptr)))
1400 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1401
1402 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001403 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001404 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1405 }
1406
1407 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001408 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001409 http_msg_rsp_reason:
1410 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001411 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001412 msg->sl.st.r_l = 0;
1413 goto http_msg_rpline_eol;
1414
Willy Tarreau8973c702007-01-21 23:58:29 +01001415 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001416 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001417 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001418 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001419 goto http_msg_rpreason;
1420 }
1421 if (likely(HTTP_IS_SPHT(*ptr)))
1422 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1423 /* so it's a CR/LF, so there is no reason phrase */
1424 goto http_msg_rsp_reason;
1425
Willy Tarreau8973c702007-01-21 23:58:29 +01001426 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001427 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001428 if (likely(!HTTP_IS_CRLF(*ptr)))
1429 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001430 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001431 http_msg_rpline_eol:
1432 /* We have seen the end of line. Note that we do not
1433 * necessarily have the \n yet, but at least we know that we
1434 * have EITHER \r OR \n, otherwise the response would not be
1435 * complete. We can then record the response length and return
1436 * to the caller which will be able to register it.
1437 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001438 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001439 return ptr;
1440
Willy Tarreau8973c702007-01-21 23:58:29 +01001441 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001442#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001443 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1444 exit(1);
1445#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001446 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001447 }
1448
1449 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001450 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001451 if (ret_state)
1452 *ret_state = state;
1453 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001454 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001455 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001456}
1457
Willy Tarreau8973c702007-01-21 23:58:29 +01001458/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001459 * This function parses a request line between <ptr> and <end>, starting with
1460 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1461 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1462 * will give undefined results.
1463 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1464 * and that msg->sol points to the beginning of the request.
1465 * If a complete line is found (which implies that at least one CR or LF is
1466 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1467 * returned indicating an incomplete line (which does not mean that parts have
1468 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1469 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1470 * upon next call.
1471 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001472 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001473 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1474 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001475 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001476 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001477const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001478 enum ht_state state, const char *ptr, const char *end,
1479 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001480{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001481 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001482
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001483 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001484 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001485 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001486 if (likely(HTTP_IS_TOKEN(*ptr)))
1487 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001488
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001489 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001490 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1492 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001493
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001494 if (likely(HTTP_IS_CRLF(*ptr))) {
1495 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001496 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001497 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001498 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001499 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001500 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001501 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001502 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001503 msg->sl.rq.v_l = 0;
1504 goto http_msg_rqline_eol;
1505 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001506 state = HTTP_MSG_ERROR;
1507 break;
1508
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001509 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001510 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001511 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001512 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001513 goto http_msg_rquri;
1514 }
1515 if (likely(HTTP_IS_SPHT(*ptr)))
1516 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1517 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1518 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001519
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001520 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001521 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001522 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001523 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001524
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001525 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001526 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001527 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1528 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001529
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001530 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001531 /* non-ASCII chars are forbidden unless option
1532 * accept-invalid-http-request is enabled in the frontend.
1533 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001534 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001535 if (msg->err_pos < -1)
1536 goto invalid_char;
1537 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001538 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001539 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1540 }
1541
1542 if (likely(HTTP_IS_CRLF(*ptr))) {
1543 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1544 goto http_msg_req09_uri_e;
1545 }
1546
1547 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001548 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001549 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001550 state = HTTP_MSG_ERROR;
1551 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001552
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001553 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001554 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001555 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001556 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001557 goto http_msg_rqver;
1558 }
1559 if (likely(HTTP_IS_SPHT(*ptr)))
1560 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1561 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1562 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001563
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001564 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001565 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001566 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001567 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001568
1569 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001570 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001571 http_msg_rqline_eol:
1572 /* We have seen the end of line. Note that we do not
1573 * necessarily have the \n yet, but at least we know that we
1574 * have EITHER \r OR \n, otherwise the request would not be
1575 * complete. We can then record the request length and return
1576 * to the caller which will be able to register it.
1577 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001578 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001579 return ptr;
1580 }
1581
1582 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001583 state = HTTP_MSG_ERROR;
1584 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001585
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001586 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001587#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001588 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1589 exit(1);
1590#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001591 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001592 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001593
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001594 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001595 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001596 if (ret_state)
1597 *ret_state = state;
1598 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001599 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001600 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001601}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001602
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001603/*
1604 * Returns the data from Authorization header. Function may be called more
1605 * than once so data is stored in txn->auth_data. When no header is found
1606 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001607 * searching again for something we are unable to find anyway. However, if
1608 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001609 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001610 */
1611
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001612/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1613 * set according to global.tune.bufsize.
1614 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001615char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001616
1617int
Willy Tarreau87b09662015-04-03 00:22:06 +02001618get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001619{
1620
Willy Tarreaueee5b512015-04-03 23:46:31 +02001621 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001622 struct chunk auth_method;
1623 struct hdr_ctx ctx;
1624 char *h, *p;
1625 int len;
1626
1627#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001628 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001629#endif
1630
1631 if (txn->auth.method == HTTP_AUTH_WRONG)
1632 return 0;
1633
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001634 txn->auth.method = HTTP_AUTH_WRONG;
1635
1636 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001637
1638 if (txn->flags & TX_USE_PX_CONN) {
1639 h = "Proxy-Authorization";
1640 len = strlen(h);
1641 } else {
1642 h = "Authorization";
1643 len = strlen(h);
1644 }
1645
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001646 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001647 return 0;
1648
1649 h = ctx.line + ctx.val;
1650
1651 p = memchr(h, ' ', ctx.vlen);
Willy Tarreau5c557d12016-03-13 08:17:02 +01001652 len = p - h;
1653 if (!p || len <= 0)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001654 return 0;
1655
David Carlier7365f7d2016-04-04 11:54:42 +01001656 if (chunk_initlen(&auth_method, h, 0, len) != 1)
1657 return 0;
1658
Willy Tarreau5c557d12016-03-13 08:17:02 +01001659 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001660
1661 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1662
1663 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001664 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001665
1666 if (len < 0)
1667 return 0;
1668
1669
1670 get_http_auth_buff[len] = '\0';
1671
1672 p = strchr(get_http_auth_buff, ':');
1673
1674 if (!p)
1675 return 0;
1676
1677 txn->auth.user = get_http_auth_buff;
1678 *p = '\0';
1679 txn->auth.pass = p+1;
1680
1681 txn->auth.method = HTTP_AUTH_BASIC;
1682 return 1;
1683 }
1684
1685 return 0;
1686}
1687
Willy Tarreau58f10d72006-12-04 02:26:12 +01001688
Willy Tarreau8973c702007-01-21 23:58:29 +01001689/*
1690 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001691 * depending on the initial msg->msg_state. The caller is responsible for
1692 * ensuring that the message does not wrap. The function can be preempted
1693 * everywhere when data are missing and recalled at the exact same location
1694 * with no information loss. The message may even be realigned between two
1695 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001696 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001697 * fields. Note that msg->sol will be initialized after completing the first
1698 * state, so that none of the msg pointers has to be initialized prior to the
1699 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001700 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001701void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001702{
Willy Tarreau3770f232013-12-07 00:01:53 +01001703 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001704 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001705 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001706
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001707 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001708 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001709 ptr = buf->p + msg->next;
1710 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001711
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001712 if (unlikely(ptr >= end))
1713 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001714
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001715 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001716 /*
1717 * First, states that are specific to the response only.
1718 * We check them first so that request and headers are
1719 * closer to each other (accessed more often).
1720 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001721 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001722 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001723 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001724 /* we have a start of message, but we have to check
1725 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001726 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001727 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001728 if (unlikely(ptr != buf->p)) {
1729 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001730 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001731 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001732 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001733 }
Willy Tarreau26927362012-05-18 23:22:52 +02001734 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001735 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001736 hdr_idx_init(idx);
1737 state = HTTP_MSG_RPVER;
1738 goto http_msg_rpver;
1739 }
1740
1741 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1742 goto http_msg_invalid;
1743
1744 if (unlikely(*ptr == '\n'))
1745 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1746 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1747 /* stop here */
1748
Willy Tarreau8973c702007-01-21 23:58:29 +01001749 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001750 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001751 EXPECT_LF_HERE(ptr, http_msg_invalid);
1752 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1753 /* stop here */
1754
Willy Tarreau8973c702007-01-21 23:58:29 +01001755 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001756 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001757 case HTTP_MSG_RPVER_SP:
1758 case HTTP_MSG_RPCODE:
1759 case HTTP_MSG_RPCODE_SP:
1760 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001761 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001762 state, ptr, end,
1763 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001764 if (unlikely(!ptr))
1765 return;
1766
1767 /* we have a full response and we know that we have either a CR
1768 * or an LF at <ptr>.
1769 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001770 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1771
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001772 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001773 if (likely(*ptr == '\r'))
1774 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1775 goto http_msg_rpline_end;
1776
Willy Tarreau8973c702007-01-21 23:58:29 +01001777 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001778 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001779 /* msg->sol must point to the first of CR or LF. */
1780 EXPECT_LF_HERE(ptr, http_msg_invalid);
1781 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1782 /* stop here */
1783
1784 /*
1785 * Second, states that are specific to the request only
1786 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001787 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001788 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001789 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001790 /* we have a start of message, but we have to check
1791 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001792 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001793 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001794 if (likely(ptr != buf->p)) {
1795 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001796 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001797 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001798 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001799 }
Willy Tarreau26927362012-05-18 23:22:52 +02001800 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001801 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001802 state = HTTP_MSG_RQMETH;
1803 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001804 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001805
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001806 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1807 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001808
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001809 if (unlikely(*ptr == '\n'))
1810 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1811 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001812 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001813
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001814 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001815 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001816 EXPECT_LF_HERE(ptr, http_msg_invalid);
1817 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001818 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001819
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001820 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001821 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001822 case HTTP_MSG_RQMETH_SP:
1823 case HTTP_MSG_RQURI:
1824 case HTTP_MSG_RQURI_SP:
1825 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001826 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001827 state, ptr, end,
1828 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001829 if (unlikely(!ptr))
1830 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001831
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001832 /* we have a full request and we know that we have either a CR
1833 * or an LF at <ptr>.
1834 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001835 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001836
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001837 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001838 if (likely(*ptr == '\r'))
1839 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001840 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001841
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001842 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001843 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001844 /* check for HTTP/0.9 request : no version information available.
1845 * msg->sol must point to the first of CR or LF.
1846 */
1847 if (unlikely(msg->sl.rq.v_l == 0))
1848 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001849
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001850 EXPECT_LF_HERE(ptr, http_msg_invalid);
1851 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001852 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001853
Willy Tarreau8973c702007-01-21 23:58:29 +01001854 /*
1855 * Common states below
1856 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001857 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001858 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001859 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001860 if (likely(!HTTP_IS_CRLF(*ptr))) {
1861 goto http_msg_hdr_name;
1862 }
1863
1864 if (likely(*ptr == '\r'))
1865 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1866 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001867
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001868 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001869 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001870 /* assumes msg->sol points to the first char */
1871 if (likely(HTTP_IS_TOKEN(*ptr)))
1872 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001873
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001874 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001875 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001876
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001877 if (likely(msg->err_pos < -1) || *ptr == '\n')
1878 goto http_msg_invalid;
1879
1880 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001881 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001882
1883 /* and we still accept this non-token character */
1884 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001885
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001886 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001887 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001888 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001889 if (likely(HTTP_IS_SPHT(*ptr)))
1890 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001891
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001892 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001893 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001894
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001895 if (likely(!HTTP_IS_CRLF(*ptr))) {
1896 goto http_msg_hdr_val;
1897 }
1898
1899 if (likely(*ptr == '\r'))
1900 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1901 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001902
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001903 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001904 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001905 EXPECT_LF_HERE(ptr, http_msg_invalid);
1906 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001907
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001908 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001909 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001910 if (likely(HTTP_IS_SPHT(*ptr))) {
1911 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001912 for (; buf->p + msg->sov < ptr; msg->sov++)
1913 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001914 goto http_msg_hdr_l1_sp;
1915 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001916 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001917 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001918 goto http_msg_complete_header;
1919
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001920 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001921 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001922 /* assumes msg->sol points to the first char, and msg->sov
1923 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001924 */
1925 if (likely(!HTTP_IS_CRLF(*ptr)))
1926 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001927
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001928 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001929 /* Note: we could also copy eol into ->eoh so that we have the
1930 * real header end in case it ends with lots of LWS, but is this
1931 * really needed ?
1932 */
1933 if (likely(*ptr == '\r'))
1934 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1935 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001936
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001937 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001938 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001939 EXPECT_LF_HERE(ptr, http_msg_invalid);
1940 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001941
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001942 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001943 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001944 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1945 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001946 for (; buf->p + msg->eol < ptr; msg->eol++)
1947 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001948 goto http_msg_hdr_val;
1949 }
1950 http_msg_complete_header:
1951 /*
1952 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001953 * Assumes msg->sol points to the first char, msg->sov points
1954 * to the first character of the value and msg->eol to the
1955 * first CR or LF so we know how the line ends. We insert last
1956 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001957 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001958 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001959 idx, idx->tail) < 0))
1960 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001961
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001962 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001963 if (likely(!HTTP_IS_CRLF(*ptr))) {
1964 goto http_msg_hdr_name;
1965 }
1966
1967 if (likely(*ptr == '\r'))
1968 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1969 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001970
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001971 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001972 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001973 /* Assumes msg->sol points to the first of either CR or LF.
1974 * Sets ->sov and ->next to the total header length, ->eoh to
1975 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1976 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001977 EXPECT_LF_HERE(ptr, http_msg_invalid);
1978 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001979 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001980 msg->eoh = msg->sol;
1981 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001982 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001983 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001984 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001985
1986 case HTTP_MSG_ERROR:
1987 /* this may only happen if we call http_msg_analyser() twice with an error */
1988 break;
1989
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001990 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001991#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001992 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1993 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001994#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001995 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001996 }
1997 http_msg_ood:
1998 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001999 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002000 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002001 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002002
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002003 http_msg_invalid:
2004 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002005 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002006 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002007 return;
2008}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002009
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002010/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
2011 * conversion succeeded, 0 in case of error. If the request was already 1.X,
2012 * nothing is done and 1 is returned.
2013 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002014static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002015{
2016 int delta;
2017 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002018 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002019
2020 if (msg->sl.rq.v_l != 0)
2021 return 1;
2022
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03002023 /* RFC 1945 allows only GET for HTTP/0.9 requests */
2024 if (txn->meth != HTTP_METH_GET)
2025 return 0;
2026
Willy Tarreau9b28e032012-10-12 23:49:43 +02002027 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002028
2029 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03002030 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
2031 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002032 }
2033 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002034 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01002035 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002036 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02002037 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002038 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002039 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002040 NULL, NULL);
2041 if (unlikely(!cur_end))
2042 return 0;
2043
2044 /* we have a full HTTP/1.0 request now and we know that
2045 * we have either a CR or an LF at <ptr>.
2046 */
2047 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
2048 return 1;
2049}
2050
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002051/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002052 * and "keep-alive" values. If we already know that some headers may safely
2053 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002054 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
2055 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01002056 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002057 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
2058 * found, and TX_CON_*_SET is adjusted depending on what is left so only
2059 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002060 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01002061 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002062void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01002063{
Willy Tarreau5b154472009-12-21 20:11:07 +01002064 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002065 const char *hdr_val = "Connection";
2066 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01002067
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002068 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01002069 return;
2070
Willy Tarreau88d349d2010-01-25 12:15:43 +01002071 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2072 hdr_val = "Proxy-Connection";
2073 hdr_len = 16;
2074 }
2075
Willy Tarreau5b154472009-12-21 20:11:07 +01002076 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002077 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002078 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002079 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2080 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002081 if (to_del & 2)
2082 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002083 else
2084 txn->flags |= TX_CON_KAL_SET;
2085 }
2086 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2087 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002088 if (to_del & 1)
2089 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002090 else
2091 txn->flags |= TX_CON_CLO_SET;
2092 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01002093 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
2094 txn->flags |= TX_HDR_CONN_UPG;
2095 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002096 }
2097
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002098 txn->flags |= TX_HDR_CONN_PRS;
2099 return;
2100}
Willy Tarreau5b154472009-12-21 20:11:07 +01002101
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002102/* Apply desired changes on the Connection: header. Values may be removed and/or
2103 * added depending on the <wanted> flags, which are exclusively composed of
2104 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2105 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2106 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002107void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002108{
2109 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002110 const char *hdr_val = "Connection";
2111 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002112
2113 ctx.idx = 0;
2114
Willy Tarreau88d349d2010-01-25 12:15:43 +01002115
2116 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2117 hdr_val = "Proxy-Connection";
2118 hdr_len = 16;
2119 }
2120
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002121 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002122 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002123 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2124 if (wanted & TX_CON_KAL_SET)
2125 txn->flags |= TX_CON_KAL_SET;
2126 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002127 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002128 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002129 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2130 if (wanted & TX_CON_CLO_SET)
2131 txn->flags |= TX_CON_CLO_SET;
2132 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002133 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002134 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002135 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002136
2137 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2138 return;
2139
2140 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2141 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002142 hdr_val = "Connection: close";
2143 hdr_len = 17;
2144 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2145 hdr_val = "Proxy-Connection: close";
2146 hdr_len = 23;
2147 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002148 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002149 }
2150
2151 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2152 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002153 hdr_val = "Connection: keep-alive";
2154 hdr_len = 22;
2155 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2156 hdr_val = "Proxy-Connection: keep-alive";
2157 hdr_len = 28;
2158 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002159 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002160 }
2161 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002162}
2163
Christopher Faulet113f7de2015-12-14 14:52:13 +01002164/* Parse the chunk size at msg->next. Once done, caller should adjust ->next to
2165 * point to the first byte of data after the chunk size, so that we know we can
2166 * forward exactly msg->next bytes. msg->sol contains the exact number of bytes
2167 * forming the chunk size. That way it is always possible to differentiate
2168 * between the start of the body and the start of the data. Return the number
2169 * of byte parsed on success, 0 when some data is missing, <0 on error. Note:
2170 * this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002171 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002172static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002173{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002174 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002175 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002176 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002177 const char *end = buf->data + buf->size;
2178 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01002179 unsigned int chunk = 0;
2180
2181 /* The chunk size is in the following form, though we are only
2182 * interested in the size and CRLF :
2183 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2184 */
2185 while (1) {
2186 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002187 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002188 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002189 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002190 if (c < 0) /* not a hex digit anymore */
2191 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002192 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002193 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002194 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002195 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002196 chunk = (chunk << 4) + c;
2197 }
2198
Willy Tarreaud98cf932009-12-27 22:54:55 +01002199 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002200 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002201 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002202
Willy Tarreau2235b262016-11-05 15:50:20 +01002203 while (HTTP_IS_SPHT(*ptr)) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01002204 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002205 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002206 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002207 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002208 }
2209
Willy Tarreaud98cf932009-12-27 22:54:55 +01002210 /* Up to there, we know that at least one byte is present at *ptr. Check
2211 * for the end of chunk size.
2212 */
2213 while (1) {
2214 if (likely(HTTP_IS_CRLF(*ptr))) {
2215 /* we now have a CR or an LF at ptr */
2216 if (likely(*ptr == '\r')) {
2217 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002218 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002219 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002220 return 0;
2221 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002222
Willy Tarreaud98cf932009-12-27 22:54:55 +01002223 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002224 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002225 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002226 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002227 /* done */
2228 break;
2229 }
2230 else if (*ptr == ';') {
2231 /* chunk extension, ends at next CRLF */
2232 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002233 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002234 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002235 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002236
2237 while (!HTTP_IS_CRLF(*ptr)) {
2238 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002239 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002240 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002241 return 0;
2242 }
2243 /* we have a CRLF now, loop above */
2244 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002245 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002246 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002247 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002248 }
2249
Christopher Faulet113f7de2015-12-14 14:52:13 +01002250 /* OK we found our CRLF and now <ptr> points to the next byte, which may
2251 * or may not be present. We save the number of bytes parsed into
2252 * msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002253 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002254 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002255 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002256 msg->sol += buf->size;
Willy Tarreau124d9912011-03-01 20:30:48 +01002257 msg->chunk_len = chunk;
2258 msg->body_len += chunk;
Christopher Faulet113f7de2015-12-14 14:52:13 +01002259 return msg->sol;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002260 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002261 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002262 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002263}
2264
Christopher Faulet113f7de2015-12-14 14:52:13 +01002265/* This function skips trailers in the buffer associated with HTTP message
2266 * <msg>. The first visited position is msg->next. If the end of the trailers is
2267 * found, the function returns >0. So, the caller can automatically schedul it
2268 * to be forwarded, and switch msg->msg_state to HTTP_MSG_DONE. If not enough
2269 * data are available, the function does not change anything except maybe
2270 * msg->sol if it could parse some lines, and returns zero. If a parse error
2271 * is encountered, the function returns < 0 and does not change anything except
2272 * maybe msg->sol. Note that the message must already be in HTTP_MSG_TRAILERS
2273 * state before calling this function, which implies that all non-trailers data
2274 * have already been scheduled for forwarding, and that msg->next exactly
2275 * matches the length of trailers already parsed and not forwarded. It is also
2276 * important to note that this function is designed to be able to parse wrapped
2277 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002278 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002279static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002280{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002281 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002282
Christopher Faulet113f7de2015-12-14 14:52:13 +01002283 /* we have msg->next which points to next line. Look for CRLF. But
2284 * first, we reset msg->sol */
2285 msg->sol = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002286 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002287 const char *p1 = NULL, *p2 = NULL;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002288 const char *start = b_ptr(buf, msg->next + msg->sol);
2289 const char *stop = bi_end(buf);
2290 const char *ptr = start;
2291 int bytes = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002292
2293 /* scan current line and stop at LF or CRLF */
2294 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002295 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002296 return 0;
2297
2298 if (*ptr == '\n') {
2299 if (!p1)
2300 p1 = ptr;
2301 p2 = ptr;
2302 break;
2303 }
2304
2305 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002306 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002307 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002308 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002309 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002310 p1 = ptr;
2311 }
2312
2313 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002314 if (ptr >= buf->data + buf->size)
2315 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002316 }
2317
2318 /* after LF; point to beginning of next line */
2319 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002320 if (p2 >= buf->data + buf->size)
2321 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002322
Christopher Faulet2fb28802015-12-01 10:40:57 +01002323 bytes = p2 - start;
Willy Tarreau638cd022010-01-03 07:42:04 +01002324 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002325 bytes += buf->size;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002326 msg->sol += bytes;
Willy Tarreau638cd022010-01-03 07:42:04 +01002327
Christopher Fauletd7c91962015-04-30 11:48:27 +02002328 /* LF/CRLF at beginning of line => end of trailers at p2.
2329 * Everything was scheduled for forwarding, there's nothing left
Christopher Faulet2fb28802015-12-01 10:40:57 +01002330 * from this message. */
2331 if (p1 == start)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002332 return 1;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002333
Willy Tarreaud98cf932009-12-27 22:54:55 +01002334 /* OK, next line then */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002335 }
2336}
2337
Christopher Faulet113f7de2015-12-14 14:52:13 +01002338/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
2339 * a possible LF alone at the end of a chunk. The caller should adjust msg->next
2340 * in order to include this part into the next forwarding phase. Note that the
2341 * caller must ensure that ->p points to the first byte to parse. It returns
2342 * the number of bytes parsed on success, so the caller can set msg_state to
2343 * HTTP_MSG_CHUNK_SIZE. If not enough data are available, the function does not
2344 * change anything and returns zero. If a parse error is encountered, the
2345 * function returns < 0. Note: this function is designed to parse wrapped CRLF
2346 * at the end of the buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002347 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002348static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002349{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002350 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002351 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002352 int bytes;
2353
2354 /* NB: we'll check data availabilty at the end. It's not a
2355 * problem because whatever we match first will be checked
2356 * against the correct length.
2357 */
2358 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002359 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002360 if (*ptr == '\r') {
2361 bytes++;
2362 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002363 if (ptr >= buf->data + buf->size)
2364 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002365 }
2366
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002367 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002368 return 0;
2369
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002370 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002371 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002372 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002373 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01002374 return bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002375}
Willy Tarreau5b154472009-12-21 20:11:07 +01002376
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002377/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2378 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2379 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2380 * Unparsable qvalues return 1000 as "q=1.000".
2381 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002382int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002383{
2384 int q = 1000;
2385
Willy Tarreau506c69a2014-07-08 00:59:48 +02002386 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002387 goto out;
2388 q = (*qvalue++ - '0') * 1000;
2389
2390 if (*qvalue++ != '.')
2391 goto out;
2392
Willy Tarreau506c69a2014-07-08 00:59:48 +02002393 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002394 goto out;
2395 q += (*qvalue++ - '0') * 100;
2396
Willy Tarreau506c69a2014-07-08 00:59:48 +02002397 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002398 goto out;
2399 q += (*qvalue++ - '0') * 10;
2400
Willy Tarreau506c69a2014-07-08 00:59:48 +02002401 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002402 goto out;
2403 q += (*qvalue++ - '0') * 1;
2404 out:
2405 if (q > 1000)
2406 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002407 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002408 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002409 return q;
2410}
William Lallemand82fe75c2012-10-23 10:25:10 +02002411
Willy Tarreau87b09662015-04-03 00:22:06 +02002412void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002413{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002414 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002415 int tmp = TX_CON_WANT_KAL;
2416
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002417 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2418 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002419 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2420 tmp = TX_CON_WANT_TUN;
2421
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002422 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002423 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2424 tmp = TX_CON_WANT_TUN;
2425 }
2426
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002427 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002428 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2429 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002430 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002431 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2432 tmp = TX_CON_WANT_CLO;
2433 else
2434 tmp = TX_CON_WANT_SCL;
2435 }
2436
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002437 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002438 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2439 tmp = TX_CON_WANT_CLO;
2440
2441 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2442 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2443
2444 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2445 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2446 /* parse the Connection header and possibly clean it */
2447 int to_del = 0;
2448 if ((msg->flags & HTTP_MSGF_VER_11) ||
2449 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002450 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002451 to_del |= 2; /* remove "keep-alive" */
2452 if (!(msg->flags & HTTP_MSGF_VER_11))
2453 to_del |= 1; /* remove "close" */
2454 http_parse_connection_header(txn, msg, to_del);
2455 }
2456
2457 /* check if client or config asks for explicit close in KAL/SCL */
2458 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2459 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2460 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2461 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2462 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002463 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002464 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2465}
William Lallemand82fe75c2012-10-23 10:25:10 +02002466
Willy Tarreaud787e662009-07-07 10:14:51 +02002467/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2468 * processing can continue on next analysers, or zero if it either needs more
2469 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002470 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002471 * when it has nothing left to do, and may remove any analyser when it wants to
2472 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002473 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002474int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002475{
Willy Tarreau59234e92008-11-30 23:51:27 +01002476 /*
2477 * We will parse the partial (or complete) lines.
2478 * We will check the request syntax, and also join multi-line
2479 * headers. An index of all the lines will be elaborated while
2480 * parsing.
2481 *
2482 * For the parsing, we use a 28 states FSM.
2483 *
2484 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002485 * req->buf->p = beginning of request
2486 * req->buf->p + msg->eoh = end of processed headers / start of current one
2487 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002488 * msg->eol = end of current header or line (LF or CRLF)
2489 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002490 *
2491 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02002492 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002493 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2494 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002495 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002496
Willy Tarreau59234e92008-11-30 23:51:27 +01002497 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002498 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002499 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01002500 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002501 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002502
Willy Tarreau87b09662015-04-03 00:22:06 +02002503 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 +01002504 now_ms, __FUNCTION__,
2505 s,
2506 req,
2507 req->rex, req->wex,
2508 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002509 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002510 req->analysers);
2511
Willy Tarreau52a0c602009-08-16 22:45:38 +02002512 /* we're speaking HTTP here, so let's speak HTTP to the client */
2513 s->srv_error = http_return_srv_error;
2514
Willy Tarreau83e3af02009-12-28 17:39:57 +01002515 /* There's a protected area at the end of the buffer for rewriting
2516 * purposes. We don't want to start to parse the request if the
2517 * protected area is affected, because we may have to move processed
2518 * data later, which is much more complicated.
2519 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002520 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002521
2522 /* This point is executed when some data is avalaible for analysis,
2523 * so we log the end of the idle time. */
2524 if (s->logs.t_idle == -1)
2525 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
2526
Willy Tarreau379357a2013-06-08 12:55:46 +02002527 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002528 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002529 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002530 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002531 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002532 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002533 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002534 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002535 return 0;
2536 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002537 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2538 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2539 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002540 }
2541
Willy Tarreau065e8332010-01-08 00:30:20 +01002542 /* Note that we have the same problem with the response ; we
2543 * may want to send a redirect, error or anything which requires
2544 * some spare space. So we'll ensure that we have at least
2545 * maxrewrite bytes available in the response buffer before
2546 * processing that one. This will only affect pipelined
2547 * keep-alive requests.
2548 */
2549 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002550 unlikely(!channel_is_rewritable(&s->res) ||
2551 bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) ||
2552 bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) {
2553 if (s->res.buf->o) {
2554 if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002555 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002556 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002557 channel_dont_connect(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002558 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
2559 s->res.flags |= CF_WAKE_WRITE;
2560 s->res.analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002561 return 0;
2562 }
2563 }
2564
Willy Tarreau9b28e032012-10-12 23:49:43 +02002565 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002566 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002567 }
2568
Willy Tarreau59234e92008-11-30 23:51:27 +01002569 /* 1: we might have to print this header in debug mode */
2570 if (unlikely((global.mode & MODE_DEBUG) &&
2571 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002572 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002573 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002574
Willy Tarreau9b28e032012-10-12 23:49:43 +02002575 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002576 /* this is a bit complex : in case of error on the request line,
2577 * we know that rq.l is still zero, so we display only the part
2578 * up to the end of the line (truncated by debug_hdr).
2579 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002580 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002581 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002582
Willy Tarreau59234e92008-11-30 23:51:27 +01002583 sol += hdr_idx_first_pos(&txn->hdr_idx);
2584 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002585
Willy Tarreau59234e92008-11-30 23:51:27 +01002586 while (cur_idx) {
2587 eol = sol + txn->hdr_idx.v[cur_idx].len;
2588 debug_hdr("clihdr", s, sol, eol);
2589 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2590 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002591 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002592 }
2593
Willy Tarreau58f10d72006-12-04 02:26:12 +01002594
Willy Tarreau59234e92008-11-30 23:51:27 +01002595 /*
2596 * Now we quickly check if we have found a full valid request.
2597 * If not so, we check the FD and buffer states before leaving.
2598 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002599 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002600 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02002601 * on a keep-alive stream, if we encounter and error, close, t/o,
2602 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002603 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02002604 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002605 * Last, we may increase some tracked counters' http request errors on
2606 * the cases that are deliberately the client's fault. For instance,
2607 * a timeout or connection reset is not counted as an error. However
2608 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002609 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002610
Willy Tarreau655dce92009-11-08 13:10:58 +01002611 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002612 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002613 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002614 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002615 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002616 stream_inc_http_req_ctr(s);
2617 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002618 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002619 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002620 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002621
Willy Tarreau59234e92008-11-30 23:51:27 +01002622 /* 1: Since we are in header mode, if there's no space
2623 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02002624 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01002625 * must terminate it now.
2626 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002627 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002628 /* FIXME: check if URI is set and return Status
2629 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002630 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002631 stream_inc_http_req_ctr(s);
2632 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002633 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002634 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002635 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002636 goto return_bad_req;
2637 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002638
Willy Tarreau59234e92008-11-30 23:51:27 +01002639 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002640 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002641 if (!(s->flags & SF_ERR_MASK))
2642 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002643
Willy Tarreaufcffa692010-01-10 14:21:19 +01002644 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002645 goto failed_keep_alive;
2646
Willy Tarreau0f228a02015-05-01 15:37:53 +02002647 if (sess->fe->options & PR_O_IGNORE_PRB)
2648 goto failed_keep_alive;
2649
Willy Tarreau59234e92008-11-30 23:51:27 +01002650 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002651 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002652 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002653 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002654 }
2655
Willy Tarreaudc979f22012-12-04 10:39:01 +01002656 txn->status = 400;
Willy Tarreau59234e92008-11-30 23:51:27 +01002657 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002658 http_reply_and_close(s, txn->status, NULL);
Christopher Fauletd7c91962015-04-30 11:48:27 +02002659 req->analysers &= AN_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02002660 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002661 proxy_inc_fe_req_ctr(sess->fe);
2662 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002663 if (sess->listener->counters)
2664 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002665
Willy Tarreaue7dff022015-04-03 01:14:29 +02002666 if (!(s->flags & SF_FINST_MASK))
2667 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002668 return 0;
2669 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002670
Willy Tarreau59234e92008-11-30 23:51:27 +01002671 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002672 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002673 if (!(s->flags & SF_ERR_MASK))
2674 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002675
Willy Tarreaufcffa692010-01-10 14:21:19 +01002676 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002677 goto failed_keep_alive;
2678
Willy Tarreau0f228a02015-05-01 15:37:53 +02002679 if (sess->fe->options & PR_O_IGNORE_PRB)
2680 goto failed_keep_alive;
2681
Willy Tarreau59234e92008-11-30 23:51:27 +01002682 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002683 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002684 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002685 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002686 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002687 txn->status = 408;
Willy Tarreau59234e92008-11-30 23:51:27 +01002688 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002689 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_408));
Christopher Fauletd7c91962015-04-30 11:48:27 +02002690 req->analysers &= AN_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002691
Willy Tarreau87b09662015-04-03 00:22:06 +02002692 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002693 proxy_inc_fe_req_ctr(sess->fe);
2694 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002695 if (sess->listener->counters)
2696 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002697
Willy Tarreaue7dff022015-04-03 01:14:29 +02002698 if (!(s->flags & SF_FINST_MASK))
2699 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002700 return 0;
2701 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002702
Willy Tarreau59234e92008-11-30 23:51:27 +01002703 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002704 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002705 if (!(s->flags & SF_ERR_MASK))
2706 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002707
Willy Tarreaufcffa692010-01-10 14:21:19 +01002708 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002709 goto failed_keep_alive;
2710
Willy Tarreau0f228a02015-05-01 15:37:53 +02002711 if (sess->fe->options & PR_O_IGNORE_PRB)
2712 goto failed_keep_alive;
2713
Willy Tarreau4076a152009-04-02 15:18:36 +02002714 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002715 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002716 txn->status = 400;
Willy Tarreau59234e92008-11-30 23:51:27 +01002717 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002718 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Christopher Fauletd7c91962015-04-30 11:48:27 +02002719 req->analysers &= AN_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02002720 stream_inc_http_err_ctr(s);
2721 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002722 proxy_inc_fe_req_ctr(sess->fe);
2723 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002724 if (sess->listener->counters)
2725 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002726
Willy Tarreaue7dff022015-04-03 01:14:29 +02002727 if (!(s->flags & SF_FINST_MASK))
2728 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002729 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002730 }
2731
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002732 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002733 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002734 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002735#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002736 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
2737 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002738 /* We need more data, we have to re-enable quick-ack in case we
2739 * previously disabled it, otherwise we might cause the client
2740 * to delay next data.
2741 */
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002742 setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002743 }
2744#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002745
Willy Tarreaufcffa692010-01-10 14:21:19 +01002746 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2747 /* If the client starts to talk, let's fall back to
2748 * request timeout processing.
2749 */
2750 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002751 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002752 }
2753
Willy Tarreau59234e92008-11-30 23:51:27 +01002754 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002755 if (!tick_isset(req->analyse_exp)) {
2756 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2757 (txn->flags & TX_WAIT_NEXT_RQ) &&
2758 tick_isset(s->be->timeout.httpka))
2759 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2760 else
2761 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2762 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002763
Willy Tarreau59234e92008-11-30 23:51:27 +01002764 /* we're not ready yet */
2765 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002766
2767 failed_keep_alive:
2768 /* Here we process low-level errors for keep-alive requests. In
2769 * short, if the request is not the first one and it experiences
2770 * a timeout, read error or shutdown, we just silently close so
2771 * that the client can try again.
2772 */
2773 txn->status = 0;
2774 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Fauletd7c91962015-04-30 11:48:27 +02002775 req->analysers &= AN_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002776 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002777 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002778 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002779 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002780 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002781 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002782
Willy Tarreaud787e662009-07-07 10:14:51 +02002783 /* OK now we have a complete HTTP request with indexed headers. Let's
2784 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002785 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002786 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002787 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002788 * byte after the last LF. msg->sov points to the first byte of data.
2789 * msg->eol cannot be trusted because it may have been left uninitialized
2790 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002791 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002792
Willy Tarreau87b09662015-04-03 00:22:06 +02002793 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002794 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002795
Willy Tarreaub16a5742010-01-10 14:46:16 +01002796 if (txn->flags & TX_WAIT_NEXT_RQ) {
2797 /* kill the pending keep-alive timeout */
2798 txn->flags &= ~TX_WAIT_NEXT_RQ;
2799 req->analyse_exp = TICK_ETERNITY;
2800 }
2801
2802
Willy Tarreaud787e662009-07-07 10:14:51 +02002803 /* Maybe we found in invalid header name while we were configured not
2804 * to block on that, so we have to capture it now.
2805 */
2806 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002807 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002808
Willy Tarreau59234e92008-11-30 23:51:27 +01002809 /*
2810 * 1: identify the method
2811 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002812 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002813
2814 /* we can make use of server redirect on GET and HEAD */
2815 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002816 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002817
Willy Tarreau59234e92008-11-30 23:51:27 +01002818 /*
2819 * 2: check if the URI matches the monitor_uri.
2820 * We have to do this for every request which gets in, because
2821 * the monitor-uri is defined by the frontend.
2822 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002823 if (unlikely((sess->fe->monitor_uri_len != 0) &&
2824 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002825 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002826 sess->fe->monitor_uri,
2827 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002828 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002829 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002830 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002831 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002832
Willy Tarreaue7dff022015-04-03 01:14:29 +02002833 s->flags |= SF_MONITOR;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002834 sess->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002835
Willy Tarreau59234e92008-11-30 23:51:27 +01002836 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002837 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002838 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002839
Willy Tarreau59234e92008-11-30 23:51:27 +01002840 ret = acl_pass(ret);
2841 if (cond->pol == ACL_COND_UNLESS)
2842 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002843
Willy Tarreau59234e92008-11-30 23:51:27 +01002844 if (ret) {
2845 /* we fail this request, let's return 503 service unavail */
2846 txn->status = 503;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002847 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_503));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002848 if (!(s->flags & SF_ERR_MASK))
2849 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002850 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002851 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002852 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002853
Willy Tarreau59234e92008-11-30 23:51:27 +01002854 /* nothing to fail, let's reply normaly */
2855 txn->status = 200;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002856 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_200));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002857 if (!(s->flags & SF_ERR_MASK))
2858 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002859 goto return_prx_cond;
2860 }
2861
2862 /*
2863 * 3: Maybe we have to copy the original REQURI for the logs ?
2864 * Note: we cannot log anymore if the request has been
2865 * classified as invalid.
2866 */
2867 if (unlikely(s->logs.logwait & LW_REQ)) {
2868 /* we have a complete HTTP request that we must log */
2869 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2870 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002871
Willy Tarreau59234e92008-11-30 23:51:27 +01002872 if (urilen >= REQURI_LEN)
2873 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002874 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002875 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002876
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002877 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002878 s->do_log(s);
2879 } else {
2880 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002881 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002882 }
Willy Tarreau06619262006-12-17 08:37:22 +01002883
Willy Tarreau91852eb2015-05-01 13:26:00 +02002884 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
2885 * exactly one digit "." one digit. This check may be disabled using
2886 * option accept-invalid-http-request.
2887 */
2888 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
2889 if (msg->sl.rq.v_l != 8) {
2890 msg->err_pos = msg->sl.rq.v;
2891 goto return_bad_req;
2892 }
2893
2894 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
2895 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
2896 req->buf->p[msg->sl.rq.v + 6] != '.' ||
2897 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
2898 msg->err_pos = msg->sl.rq.v + 4;
2899 goto return_bad_req;
2900 }
2901 }
Willy Tarreau13317662015-05-01 13:47:08 +02002902 else {
2903 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
2904 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
2905 goto return_bad_req;
2906 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02002907
Willy Tarreau5b154472009-12-21 20:11:07 +01002908 /* ... and check if the request is HTTP/1.1 or above */
2909 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002910 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2911 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2912 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002913 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002914
2915 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002916 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 +01002917
Willy Tarreau88d349d2010-01-25 12:15:43 +01002918 /* if the frontend has "option http-use-proxy-header", we'll check if
2919 * we have what looks like a proxied connection instead of a connection,
2920 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2921 * Note that this is *not* RFC-compliant, however browsers and proxies
2922 * happen to do that despite being non-standard :-(
2923 * We consider that a request not beginning with either '/' or '*' is
2924 * a proxied connection, which covers both "scheme://location" and
2925 * CONNECT ip:port.
2926 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002927 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002928 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002929 txn->flags |= TX_USE_PX_CONN;
2930
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002931 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002932 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002933
Willy Tarreau59234e92008-11-30 23:51:27 +01002934 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002935 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002936 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002937 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002938
Willy Tarreau557f1992015-05-01 10:05:17 +02002939 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
2940 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002941 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002942 * The length of a message body is determined by one of the following
2943 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02002944 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002945 * 1. Any response to a HEAD request and any response with a 1xx
2946 * (Informational), 204 (No Content), or 304 (Not Modified) status
2947 * code is always terminated by the first empty line after the
2948 * header fields, regardless of the header fields present in the
2949 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002950 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002951 * 2. Any 2xx (Successful) response to a CONNECT request implies that
2952 * the connection will become a tunnel immediately after the empty
2953 * line that concludes the header fields. A client MUST ignore any
2954 * Content-Length or Transfer-Encoding header fields received in
2955 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002956 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002957 * 3. If a Transfer-Encoding header field is present and the chunked
2958 * transfer coding (Section 4.1) is the final encoding, the message
2959 * body length is determined by reading and decoding the chunked
2960 * data until the transfer coding indicates the data is complete.
2961 *
2962 * If a Transfer-Encoding header field is present in a response and
2963 * the chunked transfer coding is not the final encoding, the
2964 * message body length is determined by reading the connection until
2965 * it is closed by the server. If a Transfer-Encoding header field
2966 * is present in a request and the chunked transfer coding is not
2967 * the final encoding, the message body length cannot be determined
2968 * reliably; the server MUST respond with the 400 (Bad Request)
2969 * status code and then close the connection.
2970 *
2971 * If a message is received with both a Transfer-Encoding and a
2972 * Content-Length header field, the Transfer-Encoding overrides the
2973 * Content-Length. Such a message might indicate an attempt to
2974 * perform request smuggling (Section 9.5) or response splitting
2975 * (Section 9.4) and ought to be handled as an error. A sender MUST
2976 * remove the received Content-Length field prior to forwarding such
2977 * a message downstream.
2978 *
2979 * 4. If a message is received without Transfer-Encoding and with
2980 * either multiple Content-Length header fields having differing
2981 * field-values or a single Content-Length header field having an
2982 * invalid value, then the message framing is invalid and the
2983 * recipient MUST treat it as an unrecoverable error. If this is a
2984 * request message, the server MUST respond with a 400 (Bad Request)
2985 * status code and then close the connection. If this is a response
2986 * message received by a proxy, the proxy MUST close the connection
2987 * to the server, discard the received response, and send a 502 (Bad
2988 * Gateway) response to the client. If this is a response message
2989 * received by a user agent, the user agent MUST close the
2990 * connection to the server and discard the received response.
2991 *
2992 * 5. If a valid Content-Length header field is present without
2993 * Transfer-Encoding, its decimal value defines the expected message
2994 * body length in octets. If the sender closes the connection or
2995 * the recipient times out before the indicated number of octets are
2996 * received, the recipient MUST consider the message to be
2997 * incomplete and close the connection.
2998 *
2999 * 6. If this is a request message and none of the above are true, then
3000 * the message body length is zero (no message body is present).
3001 *
3002 * 7. Otherwise, this is a response message without a declared message
3003 * body length, so the message body length is determined by the
3004 * number of octets received prior to the server closing the
3005 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003006 */
3007
Willy Tarreau32b47f42009-10-18 20:55:02 +02003008 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003009 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02003010 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003011 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003012 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
3013 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02003014 /* chunked not last, return badreq */
3015 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003016 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003017 }
3018
Willy Tarreau1c913912015-04-30 10:57:51 +02003019 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02003020 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02003021 if (msg->flags & HTTP_MSGF_TE_CHNK) {
3022 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
3023 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3024 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02003025 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003026 signed long long cl;
3027
Willy Tarreauad14f752011-09-02 20:33:27 +02003028 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003029 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003030 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003031 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003032
Willy Tarreauad14f752011-09-02 20:33:27 +02003033 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003034 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003035 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003036 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003037
Willy Tarreauad14f752011-09-02 20:33:27 +02003038 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003039 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003040 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003041 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003042
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003043 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003044 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003045 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003046 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003047
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003048 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003049 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003050 }
3051
Willy Tarreau34dfc602015-05-01 10:09:49 +02003052 /* even bodyless requests have a known length */
3053 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003054
Willy Tarreau179085c2014-04-28 16:48:56 +02003055 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3056 * only change if both the request and the config reference something else.
3057 * Option httpclose by itself sets tunnel mode where headers are mangled.
3058 * However, if another mode is set, it will affect it (eg: server-close/
3059 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3060 * if FE and BE have the same settings (common). The method consists in
3061 * checking if options changed between the two calls (implying that either
3062 * one is non-null, or one of them is non-null and we are there for the first
3063 * time.
3064 */
3065 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003066 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003067 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003068
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02003069 /* we may have to wait for the request's body */
3070 if ((s->be->options & PR_O_WREQ_BODY) &&
3071 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
3072 req->analysers |= AN_REQ_HTTP_BODY;
3073
Willy Tarreaud787e662009-07-07 10:14:51 +02003074 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003075 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003076 req->analyse_exp = TICK_ETERNITY;
3077 return 1;
3078
3079 return_bad_req:
3080 /* We centralize bad requests processing here */
3081 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3082 /* we detected a parsing error. We want to archive this request
3083 * in the dedicated proxy area for later troubleshooting.
3084 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003085 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003086 }
3087
3088 txn->req.msg_state = HTTP_MSG_ERROR;
3089 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003090 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003091
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003092 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003093 if (sess->listener->counters)
3094 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003095
3096 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003097 if (!(s->flags & SF_ERR_MASK))
3098 s->flags |= SF_ERR_PRXCOND;
3099 if (!(s->flags & SF_FINST_MASK))
3100 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02003101
Christopher Fauletd7c91962015-04-30 11:48:27 +02003102 req->analysers &= AN_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02003103 req->analyse_exp = TICK_ETERNITY;
3104 return 0;
3105}
3106
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003107
Willy Tarreau347a35d2013-11-22 17:51:09 +01003108/* This function prepares an applet to handle the stats. It can deal with the
3109 * "100-continue" expectation, check that admin rules are met for POST requests,
3110 * and program a response message if something was unexpected. It cannot fail
3111 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003112 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003113 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02003114 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003115 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003116int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003117{
3118 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003119 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02003120 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003121 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003122 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003123 struct uri_auth *uri_auth = s->be->uri_auth;
3124 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003125 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003126
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003127 appctx = si_appctx(si);
3128 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3129 appctx->st1 = appctx->st2 = 0;
3130 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3131 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003132 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003133 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003134
3135 uri = msg->chn->buf->p + msg->sl.rq.u;
3136 lookup = uri + uri_auth->uri_len;
3137
3138 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3139 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003140 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003141 break;
3142 }
3143 }
3144
3145 if (uri_auth->refresh) {
3146 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3147 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003148 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003149 break;
3150 }
3151 }
3152 }
3153
3154 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3155 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003156 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003157 break;
3158 }
3159 }
3160
Willy Tarreau1e62df92016-01-11 18:57:53 +01003161 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
3162 if (memcmp(h, ";typed", 6) == 0) {
3163 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
3164 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3165 break;
3166 }
3167 }
3168
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003169 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3170 if (memcmp(h, ";st=", 4) == 0) {
3171 int i;
3172 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003173 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003174 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3175 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003176 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003177 break;
3178 }
3179 }
3180 break;
3181 }
3182 }
3183
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003184 appctx->ctx.stats.scope_str = 0;
3185 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003186 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3187 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3188 int itx = 0;
3189 const char *h2;
3190 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3191 const char *err;
3192
3193 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3194 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003195 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003196 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3197 itx++;
3198 h++;
3199 }
3200
3201 if (itx > STAT_SCOPE_TXT_MAXLEN)
3202 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003203 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003204
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003205 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003206 memcpy(scope_txt, h2, itx);
3207 scope_txt[itx] = '\0';
3208 err = invalid_char(scope_txt);
3209 if (err) {
3210 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003211 appctx->ctx.stats.scope_str = 0;
3212 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003213 }
3214 break;
3215 }
3216 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003217
3218 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003219 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003220 int ret = 1;
3221
3222 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003223 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 +01003224 ret = acl_pass(ret);
3225 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3226 ret = !ret;
3227 }
3228
3229 if (ret) {
3230 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003231 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003232 break;
3233 }
3234 }
3235
3236 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003237 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003238 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003239 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003240 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
3241 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003242 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003243 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003244 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003245 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3246 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003247 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003248 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003249 else {
3250 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003251 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003252 }
3253
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003254 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003255 return 1;
3256}
3257
Lukas Tribus67db8df2013-06-23 17:37:13 +02003258/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3259 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3260 */
Vincent Bernat6e615892016-05-18 16:17:44 +02003261void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003262{
3263#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02003264 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003265 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3266#endif
3267#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02003268 if (from->ss_family == AF_INET6) {
3269 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02003270 /* v4-mapped addresses need IP_TOS */
3271 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3272 else
3273 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3274 }
3275#endif
3276}
3277
Willy Tarreau87b09662015-04-03 00:22:06 +02003278int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003279 const char* name, unsigned int name_len,
3280 const char *str, struct my_regex *re,
3281 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003282{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003283 struct hdr_ctx ctx;
3284 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003285 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003286 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3287 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003288 struct chunk *output = get_trash_chunk();
3289
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003290 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003291
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003292 /* Choose the header browsing function. */
3293 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003294 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003295 http_find_hdr_func = http_find_header2;
3296 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003297 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003298 http_find_hdr_func = http_find_full_header2;
3299 break;
3300 default: /* impossible */
3301 return -1;
3302 }
3303
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003304 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3305 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003306 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003307 char *val = ctx.line + ctx.val;
3308 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003309
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003310 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3311 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003312
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003313 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003314 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003315 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003316
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003317 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003318
3319 hdr->len += delta;
3320 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003321
3322 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003323 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003324 }
3325
3326 return 0;
3327}
3328
Willy Tarreau87b09662015-04-03 00:22:06 +02003329static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003330 const char* name, unsigned int name_len,
3331 struct list *fmt, struct my_regex *re,
3332 int action)
3333{
3334 struct chunk *replace = get_trash_chunk();
3335
3336 replace->len = build_logline(s, replace->str, replace->size, fmt);
3337 if (replace->len >= replace->size - 1)
3338 return -1;
3339
3340 return http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
3341}
3342
Willy Tarreau87b09662015-04-03 00:22:06 +02003343/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003344 * transaction <txn>. Returns the verdict of the first rule that prevents
3345 * further processing of the request (auth, deny, ...), and defaults to
3346 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3347 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02003348 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
3349 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
3350 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003351 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003352enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02003353http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003354{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003355 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003356 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003357 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003358 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003359 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003360 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02003361 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02003362 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003363
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003364 /* If "the current_rule_list" match the executed rule list, we are in
3365 * resume condition. If a resume is needed it is always in the action
3366 * and never in the ACL or converters. In this case, we initialise the
3367 * current rule, and go to the action execution point.
3368 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003369 if (s->current_rule) {
3370 rule = s->current_rule;
3371 s->current_rule = NULL;
3372 if (s->current_rule_list == rules)
3373 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003374 }
3375 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003376
Willy Tarreauff011f22011-01-06 17:51:27 +01003377 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003378
Willy Tarreau96257ec2012-12-27 10:46:37 +01003379 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003380 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003381 int ret;
3382
Willy Tarreau192252e2015-04-04 01:47:55 +02003383 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003384 ret = acl_pass(ret);
3385
Willy Tarreauff011f22011-01-06 17:51:27 +01003386 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003387 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003388
3389 if (!ret) /* condition not matched */
3390 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003391 }
3392
Willy Tarreauacc98002015-09-27 23:34:39 +02003393 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003394resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003395 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003396 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003397 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003398
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003399 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02003400 if (deny_status)
3401 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003402 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003403
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003404 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01003405 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003406 if (deny_status)
3407 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003408 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003409
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003410 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003411 /* Auth might be performed on regular http-req rules as well as on stats */
3412 auth_realm = rule->arg.auth.realm;
3413 if (!auth_realm) {
3414 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3415 auth_realm = STATS_DEFAULT_REALM;
3416 else
3417 auth_realm = px->id;
3418 }
3419 /* send 401/407 depending on whether we use a proxy or not. We still
3420 * count one error, because normal browsing won't significantly
3421 * increase the counter but brute force attempts will.
3422 */
3423 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3424 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003425 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003426 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003427 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003428
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003429 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003430 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3431 return HTTP_RULE_RES_BADREQ;
3432 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003433
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003434 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003435 s->task->nice = rule->arg.nice;
3436 break;
3437
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003438 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003439 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Vincent Bernat6e615892016-05-18 16:17:44 +02003440 inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003441 break;
3442
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003443 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003444#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003445 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003446 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003447#endif
3448 break;
3449
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003450 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003451 s->logs.level = rule->arg.loglevel;
3452 break;
3453
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003454 case ACT_HTTP_REPLACE_HDR:
3455 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003456 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3457 rule->arg.hdr_add.name_len,
3458 &rule->arg.hdr_add.fmt,
3459 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003460 return HTTP_RULE_RES_BADREQ;
3461 break;
3462
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003463 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003464 ctx.idx = 0;
3465 /* remove all occurrences of the header */
3466 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3467 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3468 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003469 }
Willy Tarreau85603282015-01-21 20:39:27 +01003470 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003471
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003472 case ACT_HTTP_SET_HDR:
3473 case ACT_HTTP_ADD_HDR:
Thierry Fournier4b788f72016-06-01 13:35:36 +02003474 /* The scope of the trash buffer must be limited to this function. The
3475 * build_logline() function can execute a lot of other function which
3476 * can use the trash buffer. So for limiting the scope of this global
3477 * buffer, we build first the header value using build_logline, and
3478 * after we store the header name.
3479 */
3480 len = rule->arg.hdr_add.name_len + 2,
3481 len += build_logline(s, trash.str + len, trash.size - len, &rule->arg.hdr_add.fmt);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003482 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
Thierry Fournier4b788f72016-06-01 13:35:36 +02003483 trash.str[rule->arg.hdr_add.name_len] = ':';
3484 trash.str[rule->arg.hdr_add.name_len + 1] = ' ';
3485 trash.len = len;
Willy Tarreau85603282015-01-21 20:39:27 +01003486
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003487 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003488 /* remove all occurrences of the header */
3489 ctx.idx = 0;
3490 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3491 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3492 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3493 }
3494 }
3495
Willy Tarreau96257ec2012-12-27 10:46:37 +01003496 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3497 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003498
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003499 case ACT_HTTP_DEL_ACL:
3500 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003501 struct pat_ref *ref;
3502 char *key;
3503 int len;
3504
3505 /* collect reference */
3506 ref = pat_ref_lookup(rule->arg.map.ref);
3507 if (!ref)
3508 continue;
3509
3510 /* collect key */
3511 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3512 key = trash.str;
3513 key[len] = '\0';
3514
3515 /* perform update */
3516 /* returned code: 1=ok, 0=ko */
3517 pat_ref_delete(ref, key);
3518
3519 break;
3520 }
3521
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003522 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003523 struct pat_ref *ref;
3524 char *key;
3525 struct chunk *trash_key;
3526 int len;
3527
3528 trash_key = get_trash_chunk();
3529
3530 /* collect reference */
3531 ref = pat_ref_lookup(rule->arg.map.ref);
3532 if (!ref)
3533 continue;
3534
3535 /* collect key */
3536 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3537 key = trash_key->str;
3538 key[len] = '\0';
3539
3540 /* perform update */
3541 /* add entry only if it does not already exist */
3542 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003543 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003544
3545 break;
3546 }
3547
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003548 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003549 struct pat_ref *ref;
3550 char *key, *value;
3551 struct chunk *trash_key, *trash_value;
3552 int len;
3553
3554 trash_key = get_trash_chunk();
3555 trash_value = get_trash_chunk();
3556
3557 /* collect reference */
3558 ref = pat_ref_lookup(rule->arg.map.ref);
3559 if (!ref)
3560 continue;
3561
3562 /* collect key */
3563 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3564 key = trash_key->str;
3565 key[len] = '\0';
3566
3567 /* collect value */
3568 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3569 value = trash_value->str;
3570 value[len] = '\0';
3571
3572 /* perform update */
3573 if (pat_ref_find_elt(ref, key) != NULL)
3574 /* update entry if it exists */
3575 pat_ref_set(ref, key, value, NULL);
3576 else
3577 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003578 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003579
3580 break;
3581 }
William Lallemand73025dd2014-04-24 14:38:37 +02003582
Thierry FOURNIER42148732015-09-02 17:17:33 +02003583 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003584 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3585 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003586
Willy Tarreauacc98002015-09-27 23:34:39 +02003587 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003588 case ACT_RET_ERR:
3589 case ACT_RET_CONT:
3590 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003591 case ACT_RET_STOP:
3592 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003593 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003594 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003595 return HTTP_RULE_RES_YIELD;
3596 }
William Lallemand73025dd2014-04-24 14:38:37 +02003597 break;
3598
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003599 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02003600 /* Note: only the first valid tracking parameter of each
3601 * applies.
3602 */
3603
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003604 if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02003605 struct stktable *t;
3606 struct stksess *ts;
3607 struct stktable_key *key;
3608 void *ptr;
3609
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02003610 t = rule->arg.trk_ctr.table.t;
3611 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 +02003612
3613 if (key && (ts = stktable_get_entry(t, key))) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003614 stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003615
3616 /* let's count a new HTTP request as it's the first time we do it */
3617 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3618 if (ptr)
3619 stktable_data_cast(ptr, http_req_cnt)++;
3620
3621 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3622 if (ptr)
3623 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3624 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3625
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003626 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003627 if (sess->fe != s->be)
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003628 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02003629 }
3630 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003631 break;
3632
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003633 /* other flags exists, but normaly, they never be matched. */
3634 default:
3635 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003636 }
3637 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003638
3639 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003640 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003641}
3642
Willy Tarreau71241ab2012-12-27 11:30:54 +01003643
Willy Tarreau51d861a2015-05-22 17:30:48 +02003644/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3645 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3646 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3647 * is returned, the process can continue the evaluation of next rule list. If
3648 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3649 * is returned, it means the operation could not be processed and a server error
3650 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3651 * deny rule. If *YIELD is returned, the caller must call again the function
3652 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003653 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003654static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003655http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003656{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003657 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003658 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003659 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003660 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003661 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02003662 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003663
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003664 /* If "the current_rule_list" match the executed rule list, we are in
3665 * resume condition. If a resume is needed it is always in the action
3666 * and never in the ACL or converters. In this case, we initialise the
3667 * current rule, and go to the action execution point.
3668 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003669 if (s->current_rule) {
3670 rule = s->current_rule;
3671 s->current_rule = NULL;
3672 if (s->current_rule_list == rules)
3673 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003674 }
3675 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003676
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003677 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003678
3679 /* check optional condition */
3680 if (rule->cond) {
3681 int ret;
3682
Willy Tarreau192252e2015-04-04 01:47:55 +02003683 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003684 ret = acl_pass(ret);
3685
3686 if (rule->cond->pol == ACL_COND_UNLESS)
3687 ret = !ret;
3688
3689 if (!ret) /* condition not matched */
3690 continue;
3691 }
3692
Willy Tarreauacc98002015-09-27 23:34:39 +02003693 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003694resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003695 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003696 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003697 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003698
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003699 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003700 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003701 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003702
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003703 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003704 s->task->nice = rule->arg.nice;
3705 break;
3706
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003707 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003708 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Vincent Bernat6e615892016-05-18 16:17:44 +02003709 inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003710 break;
3711
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003712 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003713#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003714 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003715 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003716#endif
3717 break;
3718
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003719 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003720 s->logs.level = rule->arg.loglevel;
3721 break;
3722
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003723 case ACT_HTTP_REPLACE_HDR:
3724 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003725 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3726 rule->arg.hdr_add.name_len,
3727 &rule->arg.hdr_add.fmt,
3728 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003729 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003730 break;
3731
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003732 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003733 ctx.idx = 0;
3734 /* remove all occurrences of the header */
3735 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3736 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3737 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3738 }
Willy Tarreau85603282015-01-21 20:39:27 +01003739 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003740
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003741 case ACT_HTTP_SET_HDR:
3742 case ACT_HTTP_ADD_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003743 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3744 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3745 trash.len = rule->arg.hdr_add.name_len;
3746 trash.str[trash.len++] = ':';
3747 trash.str[trash.len++] = ' ';
3748 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01003749
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003750 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003751 /* remove all occurrences of the header */
3752 ctx.idx = 0;
3753 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3754 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3755 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3756 }
3757 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003758 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3759 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003760
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003761 case ACT_HTTP_DEL_ACL:
3762 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003763 struct pat_ref *ref;
3764 char *key;
3765 int len;
3766
3767 /* collect reference */
3768 ref = pat_ref_lookup(rule->arg.map.ref);
3769 if (!ref)
3770 continue;
3771
3772 /* collect key */
3773 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3774 key = trash.str;
3775 key[len] = '\0';
3776
3777 /* perform update */
3778 /* returned code: 1=ok, 0=ko */
3779 pat_ref_delete(ref, key);
3780
3781 break;
3782 }
3783
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003784 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003785 struct pat_ref *ref;
3786 char *key;
3787 struct chunk *trash_key;
3788 int len;
3789
3790 trash_key = get_trash_chunk();
3791
3792 /* collect reference */
3793 ref = pat_ref_lookup(rule->arg.map.ref);
3794 if (!ref)
3795 continue;
3796
3797 /* collect key */
3798 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3799 key = trash_key->str;
3800 key[len] = '\0';
3801
3802 /* perform update */
3803 /* check if the entry already exists */
3804 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003805 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003806
3807 break;
3808 }
3809
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003810 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003811 struct pat_ref *ref;
3812 char *key, *value;
3813 struct chunk *trash_key, *trash_value;
3814 int len;
3815
3816 trash_key = get_trash_chunk();
3817 trash_value = get_trash_chunk();
3818
3819 /* collect reference */
3820 ref = pat_ref_lookup(rule->arg.map.ref);
3821 if (!ref)
3822 continue;
3823
3824 /* collect key */
3825 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3826 key = trash_key->str;
3827 key[len] = '\0';
3828
3829 /* collect value */
3830 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3831 value = trash_value->str;
3832 value[len] = '\0';
3833
3834 /* perform update */
3835 if (pat_ref_find_elt(ref, key) != NULL)
3836 /* update entry if it exists */
3837 pat_ref_set(ref, key, value, NULL);
3838 else
3839 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003840 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003841
3842 break;
3843 }
William Lallemand73025dd2014-04-24 14:38:37 +02003844
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003845 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003846 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3847 return HTTP_RULE_RES_BADREQ;
3848 return HTTP_RULE_RES_DONE;
3849
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003850 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3851 /* Note: only the first valid tracking parameter of each
3852 * applies.
3853 */
3854
3855 if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) {
3856 struct stktable *t;
3857 struct stksess *ts;
3858 struct stktable_key *key;
3859 void *ptr;
3860
3861 t = rule->arg.trk_ctr.table.t;
3862 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
3863
3864 if (key && (ts = stktable_get_entry(t, key))) {
3865 stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts);
3866
3867 /* let's count a new HTTP request as it's the first time we do it */
3868 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3869 if (ptr)
3870 stktable_data_cast(ptr, http_req_cnt)++;
3871
3872 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3873 if (ptr)
3874 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3875 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3876
3877 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3878 if (sess->fe != s->be)
3879 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3880
3881 /* When the client triggers a 4xx from the server, it's most often due
3882 * to a missing object or permission. These events should be tracked
3883 * because if they happen often, it may indicate a brute force or a
3884 * vulnerability scan. Normally this is done when receiving the response
3885 * but here we're tracking after this ought to have been done so we have
3886 * to do it on purpose.
3887 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02003888 if ((unsigned)(txn->status - 400) < 100) {
3889 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3890 if (ptr)
3891 stktable_data_cast(ptr, http_err_cnt)++;
3892
3893 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3894 if (ptr)
3895 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3896 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3897 }
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003898 }
3899 }
3900 break;
3901
Thierry FOURNIER42148732015-09-02 17:17:33 +02003902 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003903 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3904 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003905
Willy Tarreauacc98002015-09-27 23:34:39 +02003906 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003907 case ACT_RET_ERR:
3908 case ACT_RET_CONT:
3909 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003910 case ACT_RET_STOP:
3911 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003912 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003913 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003914 return HTTP_RULE_RES_YIELD;
3915 }
William Lallemand73025dd2014-04-24 14:38:37 +02003916 break;
3917
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003918 /* other flags exists, but normaly, they never be matched. */
3919 default:
3920 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003921 }
3922 }
3923
3924 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003925 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003926}
3927
3928
Willy Tarreau71241ab2012-12-27 11:30:54 +01003929/* Perform an HTTP redirect based on the information in <rule>. The function
3930 * returns non-zero on success, or zero in case of a, irrecoverable error such
3931 * as too large a request to build a valid response.
3932 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003933static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003934{
Willy Tarreaub329a312015-05-22 16:27:37 +02003935 struct http_msg *req = &txn->req;
3936 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003937 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003938 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003939
3940 /* build redirect message */
3941 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003942 case 308:
3943 msg_fmt = HTTP_308;
3944 break;
3945 case 307:
3946 msg_fmt = HTTP_307;
3947 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003948 case 303:
3949 msg_fmt = HTTP_303;
3950 break;
3951 case 301:
3952 msg_fmt = HTTP_301;
3953 break;
3954 case 302:
3955 default:
3956 msg_fmt = HTTP_302;
3957 break;
3958 }
3959
3960 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3961 return 0;
3962
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003963 location = trash.str + trash.len;
3964
Willy Tarreau71241ab2012-12-27 11:30:54 +01003965 switch(rule->type) {
3966 case REDIRECT_TYPE_SCHEME: {
3967 const char *path;
3968 const char *host;
3969 struct hdr_ctx ctx;
3970 int pathlen;
3971 int hostlen;
3972
3973 host = "";
3974 hostlen = 0;
3975 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02003976 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003977 host = ctx.line + ctx.val;
3978 hostlen = ctx.vlen;
3979 }
3980
3981 path = http_get_path(txn);
3982 /* build message using path */
3983 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003984 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003985 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3986 int qs = 0;
3987 while (qs < pathlen) {
3988 if (path[qs] == '?') {
3989 pathlen = qs;
3990 break;
3991 }
3992 qs++;
3993 }
3994 }
3995 } else {
3996 path = "/";
3997 pathlen = 1;
3998 }
3999
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004000 if (rule->rdr_str) { /* this is an old "redirect" rule */
4001 /* check if we can add scheme + "://" + host + path */
4002 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
4003 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004004
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004005 /* add scheme */
4006 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4007 trash.len += rule->rdr_len;
4008 }
4009 else {
4010 /* add scheme with executing log format */
4011 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004012
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004013 /* check if we can add scheme + "://" + host + path */
4014 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
4015 return 0;
4016 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004017 /* add "://" */
4018 memcpy(trash.str + trash.len, "://", 3);
4019 trash.len += 3;
4020
4021 /* add host */
4022 memcpy(trash.str + trash.len, host, hostlen);
4023 trash.len += hostlen;
4024
4025 /* add path */
4026 memcpy(trash.str + trash.len, path, pathlen);
4027 trash.len += pathlen;
4028
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004029 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004030 if (trash.len && trash.str[trash.len - 1] != '/' &&
4031 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4032 if (trash.len > trash.size - 5)
4033 return 0;
4034 trash.str[trash.len] = '/';
4035 trash.len++;
4036 }
4037
4038 break;
4039 }
4040 case REDIRECT_TYPE_PREFIX: {
4041 const char *path;
4042 int pathlen;
4043
4044 path = http_get_path(txn);
4045 /* build message using path */
4046 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004047 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004048 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4049 int qs = 0;
4050 while (qs < pathlen) {
4051 if (path[qs] == '?') {
4052 pathlen = qs;
4053 break;
4054 }
4055 qs++;
4056 }
4057 }
4058 } else {
4059 path = "/";
4060 pathlen = 1;
4061 }
4062
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004063 if (rule->rdr_str) { /* this is an old "redirect" rule */
4064 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
4065 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004066
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004067 /* add prefix. Note that if prefix == "/", we don't want to
4068 * add anything, otherwise it makes it hard for the user to
4069 * configure a self-redirection.
4070 */
4071 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
4072 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4073 trash.len += rule->rdr_len;
4074 }
4075 }
4076 else {
4077 /* add prefix with executing log format */
4078 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
4079
4080 /* Check length */
4081 if (trash.len + pathlen > trash.size - 4)
4082 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004083 }
4084
4085 /* add path */
4086 memcpy(trash.str + trash.len, path, pathlen);
4087 trash.len += pathlen;
4088
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004089 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004090 if (trash.len && trash.str[trash.len - 1] != '/' &&
4091 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4092 if (trash.len > trash.size - 5)
4093 return 0;
4094 trash.str[trash.len] = '/';
4095 trash.len++;
4096 }
4097
4098 break;
4099 }
4100 case REDIRECT_TYPE_LOCATION:
4101 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004102 if (rule->rdr_str) { /* this is an old "redirect" rule */
4103 if (trash.len + rule->rdr_len > trash.size - 4)
4104 return 0;
4105
4106 /* add location */
4107 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4108 trash.len += rule->rdr_len;
4109 }
4110 else {
4111 /* add location with executing log format */
4112 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004113
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004114 /* Check left length */
4115 if (trash.len > trash.size - 4)
4116 return 0;
4117 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004118 break;
4119 }
4120
4121 if (rule->cookie_len) {
4122 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
4123 trash.len += 14;
4124 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4125 trash.len += rule->cookie_len;
4126 memcpy(trash.str + trash.len, "\r\n", 2);
4127 trash.len += 2;
4128 }
4129
4130 /* add end of headers and the keep-alive/close status.
4131 * We may choose to set keep-alive if the Location begins
4132 * with a slash, because the client will come back to the
4133 * same server.
4134 */
4135 txn->status = rule->code;
4136 /* let's log the request time */
4137 s->logs.tv_request = now;
4138
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004139 if (*location == '/' &&
Willy Tarreaub329a312015-05-22 16:27:37 +02004140 (req->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau2de8a502015-05-28 17:23:54 +02004141 ((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004142 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4143 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4144 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02004145 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004146 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4147 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4148 trash.len += 30;
4149 } else {
4150 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4151 trash.len += 24;
4152 }
4153 }
4154 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4155 trash.len += 4;
Christopher Faulet3e344292015-11-24 16:24:13 +01004156 FLT_STRM_CB(s, flt_http_reply(s, txn->status, &trash));
Willy Tarreaub329a312015-05-22 16:27:37 +02004157 bo_inject(res->chn, trash.str, trash.len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004158 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02004159 bi_fast_delete(req->chn->buf, req->sov);
4160 req->next -= req->sov;
4161 req->sov = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004162 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_FLT_END);
4163 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->req.analysers & AN_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02004164 req->msg_state = HTTP_MSG_CLOSED;
4165 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02004166 /* Trim any possible response */
4167 res->chn->buf->i = 0;
4168 res->next = res->sov = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004169 } else {
4170 /* keep-alive not possible */
4171 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4172 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4173 trash.len += 29;
4174 } else {
4175 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4176 trash.len += 23;
4177 }
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004178 http_reply_and_close(s, txn->status, &trash);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004179 req->chn->analysers &= AN_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004180 }
4181
Willy Tarreaue7dff022015-04-03 01:14:29 +02004182 if (!(s->flags & SF_ERR_MASK))
4183 s->flags |= SF_ERR_LOCAL;
4184 if (!(s->flags & SF_FINST_MASK))
4185 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004186
4187 return 1;
4188}
4189
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004190/* This stream analyser runs all HTTP request processing which is common to
4191 * frontends and backends, which means blocking ACLs, filters, connection-close,
4192 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004193 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004194 * either needs more data or wants to immediately abort the request (eg: deny,
4195 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004196 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004197int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004198{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004199 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004200 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004201 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004202 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004203 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004204 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004205 int deny_status = HTTP_ERR_403;
Willy Tarreaud787e662009-07-07 10:14:51 +02004206
Willy Tarreau655dce92009-11-08 13:10:58 +01004207 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004208 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004209 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004210 }
4211
Willy Tarreau87b09662015-04-03 00:22:06 +02004212 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 +02004213 now_ms, __FUNCTION__,
4214 s,
4215 req,
4216 req->rex, req->wex,
4217 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004218 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004219 req->analysers);
4220
Willy Tarreau65410832014-04-28 21:25:43 +02004221 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004222 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004223
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004224 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004225 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02004226 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01004227
Willy Tarreau0b748332014-04-29 00:13:29 +02004228 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004229 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4230 goto return_prx_yield;
4231
Willy Tarreau0b748332014-04-29 00:13:29 +02004232 case HTTP_RULE_RES_CONT:
4233 case HTTP_RULE_RES_STOP: /* nothing to do */
4234 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004235
Willy Tarreau0b748332014-04-29 00:13:29 +02004236 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4237 if (txn->flags & TX_CLTARPIT)
4238 goto tarpit;
4239 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004240
Willy Tarreau0b748332014-04-29 00:13:29 +02004241 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4242 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004243
Willy Tarreau0b748332014-04-29 00:13:29 +02004244 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004245 goto done;
4246
Willy Tarreau0b748332014-04-29 00:13:29 +02004247 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4248 goto return_bad_req;
4249 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004250 }
4251
Willy Tarreau52542592014-04-28 18:33:26 +02004252 /* OK at this stage, we know that the request was accepted according to
4253 * the http-request rules, we can check for the stats. Note that the
4254 * URI is detected *before* the req* rules in order not to be affected
4255 * by a possible reqrep, while they are processed *after* so that a
4256 * reqdeny can still block them. This clearly needs to change in 1.6!
4257 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004258 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004259 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004260 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004261 txn->status = 500;
4262 s->logs.tv_request = now;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004263 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004264
Willy Tarreaue7dff022015-04-03 01:14:29 +02004265 if (!(s->flags & SF_ERR_MASK))
4266 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004267 goto return_prx_cond;
4268 }
4269
4270 /* parse the whole stats request and extract the relevant information */
4271 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02004272 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02004273 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004274
Willy Tarreau0b748332014-04-29 00:13:29 +02004275 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4276 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004277
Willy Tarreau0b748332014-04-29 00:13:29 +02004278 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4279 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004280 }
4281
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004282 /* evaluate the req* rules except reqadd */
4283 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004284 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004285 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004286
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004287 if (txn->flags & TX_CLDENY)
4288 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004289
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004290 if (txn->flags & TX_CLTARPIT)
4291 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004292 }
Willy Tarreau06619262006-12-17 08:37:22 +01004293
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004294 /* add request headers from the rule sets in the same order */
4295 list_for_each_entry(wl, &px->req_add, list) {
4296 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004297 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004298 ret = acl_pass(ret);
4299 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4300 ret = !ret;
4301 if (!ret)
4302 continue;
4303 }
4304
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004305 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004306 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004307 }
4308
Willy Tarreau52542592014-04-28 18:33:26 +02004309
4310 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004311 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004312 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004313 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4314 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004315
Willy Tarreaue7dff022015-04-03 01:14:29 +02004316 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4317 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4318 if (!(s->flags & SF_FINST_MASK))
4319 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004320
Willy Tarreau70730dd2014-04-24 18:06:27 +02004321 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet309c6412015-12-02 09:57:32 +01004322 req->analysers &= (AN_REQ_HTTP_BODY | AN_FLT_HTTP_HDRS | AN_FLT_END);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004323 req->analysers &= ~AN_FLT_XFER_DATA;
4324 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004325 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004326 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004327
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004328 /* check whether we have some ACLs set to redirect this request */
4329 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004330 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004331 int ret;
4332
Willy Tarreau192252e2015-04-04 01:47:55 +02004333 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004334 ret = acl_pass(ret);
4335 if (rule->cond->pol == ACL_COND_UNLESS)
4336 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004337 if (!ret)
4338 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004339 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004340 if (!http_apply_redirect_rule(rule, s, txn))
4341 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004342 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004343 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004344
Willy Tarreau2be39392010-01-03 17:24:51 +01004345 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4346 * If this happens, then the data will not come immediately, so we must
4347 * send all what we have without waiting. Note that due to the small gain
4348 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004349 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004350 * itself once used.
4351 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004352 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004353
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004354 done: /* done with this analyser, continue with next ones that the calling
4355 * points will have set, if any.
4356 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004357 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004358 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4359 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004360 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004361
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004362 tarpit:
4363 /* When a connection is tarpitted, we use the tarpit timeout,
4364 * which may be the same as the connect timeout if unspecified.
4365 * If unset, then set it to zero because we really want it to
4366 * eventually expire. We build the tarpit as an analyser.
4367 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004368 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004369
4370 /* wipe the request out so that we can drop the connection early
4371 * if the client closes first.
4372 */
4373 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004374
4375 /* Allow cookie logging
4376 */
4377 if (s->be->cookie_name || sess->fe->capture_name)
4378 manage_client_side_cookies(s, req);
4379
Christopher Fauletd7c91962015-04-30 11:48:27 +02004380 req->analysers &= AN_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004381 req->analysers |= AN_REQ_HTTP_TARPIT;
4382 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4383 if (!req->analyse_exp)
4384 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004385 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004386 sess->fe->fe_counters.denied_req++;
4387 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004388 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004389 if (sess->listener->counters)
4390 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004391 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004392
4393 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004394
4395 /* Allow cookie logging
4396 */
4397 if (s->be->cookie_name || sess->fe->capture_name)
4398 manage_client_side_cookies(s, req);
4399
Willy Tarreau0b748332014-04-29 00:13:29 +02004400 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004401 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004402 s->logs.tv_request = now;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004403 http_reply_and_close(s, txn->status, http_error_message(s, deny_status));
Willy Tarreau87b09662015-04-03 00:22:06 +02004404 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004405 sess->fe->fe_counters.denied_req++;
4406 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004407 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004408 if (sess->listener->counters)
4409 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004410 goto return_prx_cond;
4411
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004412 return_bad_req:
4413 /* We centralize bad requests processing here */
4414 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4415 /* we detected a parsing error. We want to archive this request
4416 * in the dedicated proxy area for later troubleshooting.
4417 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004418 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004419 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004420
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004421 txn->req.msg_state = HTTP_MSG_ERROR;
4422 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004423 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004424
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004425 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004426 if (sess->listener->counters)
4427 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004428
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004429 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004430 if (!(s->flags & SF_ERR_MASK))
4431 s->flags |= SF_ERR_PRXCOND;
4432 if (!(s->flags & SF_FINST_MASK))
4433 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004434
Christopher Fauletd7c91962015-04-30 11:48:27 +02004435 req->analysers &= AN_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004436 req->analyse_exp = TICK_ETERNITY;
4437 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004438
4439 return_prx_yield:
4440 channel_dont_connect(req);
4441 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004442}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004443
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004444/* This function performs all the processing enabled for the current request.
4445 * It returns 1 if the processing can continue on next analysers, or zero if it
4446 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004447 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004448 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004449int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004450{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004451 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004452 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004453 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004454 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004455
Willy Tarreau655dce92009-11-08 13:10:58 +01004456 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004457 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004458 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004459 return 0;
4460 }
4461
Willy Tarreau87b09662015-04-03 00:22:06 +02004462 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 +02004463 now_ms, __FUNCTION__,
4464 s,
4465 req,
4466 req->rex, req->wex,
4467 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004468 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004469 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004470
Willy Tarreau59234e92008-11-30 23:51:27 +01004471 /*
4472 * Right now, we know that we have processed the entire headers
4473 * and that unwanted requests have been filtered out. We can do
4474 * whatever we want with the remaining request. Also, now we
4475 * may have separate values for ->fe, ->be.
4476 */
Willy Tarreau06619262006-12-17 08:37:22 +01004477
Willy Tarreau59234e92008-11-30 23:51:27 +01004478 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004479 * If HTTP PROXY is set we simply get remote server address parsing
4480 * incoming request. Note that this requires that a connection is
4481 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004482 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004483 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004484 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004485 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004486
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004487 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau973a5422015-08-05 21:47:23 +02004488 if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004489 txn->req.msg_state = HTTP_MSG_ERROR;
4490 txn->status = 500;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004491 req->analysers &= AN_FLT_END;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004492 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004493
Willy Tarreaue7dff022015-04-03 01:14:29 +02004494 if (!(s->flags & SF_ERR_MASK))
4495 s->flags |= SF_ERR_RESOURCE;
4496 if (!(s->flags & SF_FINST_MASK))
4497 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004498
4499 return 0;
4500 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004501
4502 path = http_get_path(txn);
4503 url2sa(req->buf->p + msg->sl.rq.u,
4504 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004505 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004506 /* if the path was found, we have to remove everything between
4507 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4508 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4509 * u_l characters by a single "/".
4510 */
4511 if (path) {
4512 char *cur_ptr = req->buf->p;
4513 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4514 int delta;
4515
4516 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4517 http_msg_move_end(&txn->req, delta);
4518 cur_end += delta;
4519 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4520 goto return_bad_req;
4521 }
4522 else {
4523 char *cur_ptr = req->buf->p;
4524 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4525 int delta;
4526
4527 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4528 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4529 http_msg_move_end(&txn->req, delta);
4530 cur_end += delta;
4531 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4532 goto return_bad_req;
4533 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004534 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004535
Willy Tarreau59234e92008-11-30 23:51:27 +01004536 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004537 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004538 * Note that doing so might move headers in the request, but
4539 * the fields will stay coherent and the URI will not move.
4540 * This should only be performed in the backend.
4541 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004542 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004543 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004544
William Lallemanda73203e2012-03-12 12:48:57 +01004545 /* add unique-id if "header-unique-id" is specified */
4546
Thierry Fournierf4011dd2016-03-29 17:23:51 +02004547 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004548 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4549 goto return_bad_req;
4550 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004551 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004552 }
William Lallemanda73203e2012-03-12 12:48:57 +01004553
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004554 if (sess->fe->header_unique_id && s->unique_id) {
4555 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004556 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004557 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004558 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004559 goto return_bad_req;
4560 }
4561
Cyril Bontéb21570a2009-11-29 20:04:48 +01004562 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004563 * 9: add X-Forwarded-For if either the frontend or the backend
4564 * asks for it.
4565 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004566 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004567 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004568 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4569 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4570 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004571 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004572 /* The header is set to be added only if none is present
4573 * and we found it, so don't do anything.
4574 */
4575 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004576 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004577 /* Add an X-Forwarded-For header unless the source IP is
4578 * in the 'except' network range.
4579 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004580 if ((!sess->fe->except_mask.s_addr ||
4581 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4582 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004583 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004584 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004585 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004586 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004587 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004588 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004589
4590 /* Note: we rely on the backend to get the header name to be used for
4591 * x-forwarded-for, because the header is really meant for the backends.
4592 * However, if the backend did not specify any option, we have to rely
4593 * on the frontend's header name.
4594 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004595 if (s->be->fwdfor_hdr_len) {
4596 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004597 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004598 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004599 len = sess->fe->fwdfor_hdr_len;
4600 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004601 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004602 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 +01004603
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004604 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004605 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004606 }
4607 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004608 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004609 /* FIXME: for the sake of completeness, we should also support
4610 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004611 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004612 int len;
4613 char pn[INET6_ADDRSTRLEN];
4614 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004615 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004616 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004617
Willy Tarreau59234e92008-11-30 23:51:27 +01004618 /* Note: we rely on the backend to get the header name to be used for
4619 * x-forwarded-for, because the header is really meant for the backends.
4620 * However, if the backend did not specify any option, we have to rely
4621 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004622 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004623 if (s->be->fwdfor_hdr_len) {
4624 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004625 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004626 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004627 len = sess->fe->fwdfor_hdr_len;
4628 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004629 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004630 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004631
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004632 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004633 goto return_bad_req;
4634 }
4635 }
4636
4637 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004638 * 10: add X-Original-To if either the frontend or the backend
4639 * asks for it.
4640 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004641 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004642
4643 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004644 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004645 /* Add an X-Original-To header unless the destination IP is
4646 * in the 'except' network range.
4647 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004648 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004649
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004650 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004651 ((!sess->fe->except_mask_to.s_addr ||
4652 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4653 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004654 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004655 (((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 +02004656 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004657 int len;
4658 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004659 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004660
4661 /* Note: we rely on the backend to get the header name to be used for
4662 * x-original-to, because the header is really meant for the backends.
4663 * However, if the backend did not specify any option, we have to rely
4664 * on the frontend's header name.
4665 */
4666 if (s->be->orgto_hdr_len) {
4667 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004668 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004669 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004670 len = sess->fe->orgto_hdr_len;
4671 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004672 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004673 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 +02004674
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004675 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004676 goto return_bad_req;
4677 }
4678 }
4679 }
4680
Willy Tarreau50fc7772012-11-11 22:19:57 +01004681 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4682 * If an "Upgrade" token is found, the header is left untouched in order not to have
4683 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4684 * "Upgrade" is present in the Connection header.
4685 */
4686 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4687 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004688 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004689 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004690 unsigned int want_flags = 0;
4691
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004692 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004693 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004694 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004695 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004696 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004697 want_flags |= TX_CON_CLO_SET;
4698 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004699 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004700 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004701 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004702 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004703 want_flags |= TX_CON_KAL_SET;
4704 }
4705
4706 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004707 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004708 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004709
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004710
Willy Tarreau522d6c02009-12-06 18:49:18 +01004711 /* If we have no server assigned yet and we're balancing on url_param
4712 * with a POST request, we may be interested in checking the body for
4713 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004714 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004715 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004716 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004717 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004718 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004719 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004720 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004721
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004722 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02004723 req->analysers &= ~AN_FLT_XFER_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004724 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004725#ifdef TCP_QUICKACK
4726 /* We expect some data from the client. Unless we know for sure
4727 * we already have a full request, we have to re-enable quick-ack
4728 * in case we previously disabled it, otherwise we might cause
4729 * the client to delay further data.
4730 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004731 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004732 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004733 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004734 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004735 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004736#endif
4737 }
Willy Tarreau03945942009-12-22 16:50:27 +01004738
Willy Tarreau59234e92008-11-30 23:51:27 +01004739 /*************************************************************
4740 * OK, that's finished for the headers. We have done what we *
4741 * could. Let's switch to the DATA state. *
4742 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004743 req->analyse_exp = TICK_ETERNITY;
4744 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004745
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004746 /* if the server closes the connection, we want to immediately react
4747 * and close the socket to save packets and syscalls.
4748 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004749 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004750 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004751
Willy Tarreau59234e92008-11-30 23:51:27 +01004752 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004753 /* OK let's go on with the BODY now */
4754 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004755
Willy Tarreau59234e92008-11-30 23:51:27 +01004756 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004757 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004758 /* we detected a parsing error. We want to archive this request
4759 * in the dedicated proxy area for later troubleshooting.
4760 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004761 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004762 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004763
Willy Tarreau59234e92008-11-30 23:51:27 +01004764 txn->req.msg_state = HTTP_MSG_ERROR;
4765 txn->status = 400;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004766 req->analysers &= AN_FLT_END;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004767 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004768
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004769 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004770 if (sess->listener->counters)
4771 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004772
Willy Tarreaue7dff022015-04-03 01:14:29 +02004773 if (!(s->flags & SF_ERR_MASK))
4774 s->flags |= SF_ERR_PRXCOND;
4775 if (!(s->flags & SF_FINST_MASK))
4776 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004777 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004778}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004779
Willy Tarreau60b85b02008-11-30 23:28:40 +01004780/* This function is an analyser which processes the HTTP tarpit. It always
4781 * returns zero, at the beginning because it prevents any other processing
4782 * from occurring, and at the end because it terminates the request.
4783 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004784int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004785{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004786 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004787
4788 /* This connection is being tarpitted. The CLIENT side has
4789 * already set the connect expiration date to the right
4790 * timeout. We just have to check that the client is still
4791 * there and that the timeout has not expired.
4792 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004793 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004794 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004795 !tick_is_expired(req->analyse_exp, now_ms))
4796 return 0;
4797
4798 /* We will set the queue timer to the time spent, just for
4799 * logging purposes. We fake a 500 server error, so that the
4800 * attacker will not suspect his connection has been tarpitted.
4801 * It will not cause trouble to the logs because we can exclude
4802 * the tarpitted connections by filtering on the 'PT' status flags.
4803 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004804 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4805
4806 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004807 if (!(req->flags & CF_READ_ERROR))
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004808 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004809
Christopher Fauletd7c91962015-04-30 11:48:27 +02004810 req->analysers &= AN_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004811 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004812
Willy Tarreaue7dff022015-04-03 01:14:29 +02004813 if (!(s->flags & SF_ERR_MASK))
4814 s->flags |= SF_ERR_PRXCOND;
4815 if (!(s->flags & SF_FINST_MASK))
4816 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004817 return 0;
4818}
4819
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004820/* This function is an analyser which waits for the HTTP request body. It waits
4821 * for either the buffer to be full, or the full advertised contents to have
4822 * reached the buffer. It must only be called after the standard HTTP request
4823 * processing has occurred, because it expects the request to be parsed and will
4824 * look for the Expect header. It may send a 100-Continue interim response. It
4825 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4826 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4827 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004828 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004829int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004830{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004831 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004832 struct http_txn *txn = s->txn;
4833 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004834
4835 /* We have to parse the HTTP request body to find any required data.
4836 * "balance url_param check_post" should have been the only way to get
4837 * into this. We were brought here after HTTP header analysis, so all
4838 * related structures are ready.
4839 */
4840
Willy Tarreau890988f2014-04-10 11:59:33 +02004841 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4842 /* This is the first call */
4843 if (msg->msg_state < HTTP_MSG_BODY)
4844 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004845
Willy Tarreau890988f2014-04-10 11:59:33 +02004846 if (msg->msg_state < HTTP_MSG_100_SENT) {
4847 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4848 * send an HTTP/1.1 100 Continue intermediate response.
4849 */
4850 if (msg->flags & HTTP_MSGF_VER_11) {
4851 struct hdr_ctx ctx;
4852 ctx.idx = 0;
4853 /* Expect is allowed in 1.1, look for it */
4854 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4855 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004856 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004857 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004858 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004859 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004860 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004861
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004862 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004863 * req->buf->p still points to the beginning of the message. We
4864 * must save the body in msg->next because it survives buffer
4865 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004866 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004867 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004868
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004869 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004870 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4871 else
4872 msg->msg_state = HTTP_MSG_DATA;
4873 }
4874
Willy Tarreau890988f2014-04-10 11:59:33 +02004875 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4876 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004877 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004878 goto missing_data;
4879
4880 /* OK we have everything we need now */
4881 goto http_end;
4882 }
4883
4884 /* OK here we're parsing a chunked-encoded message */
4885
Willy Tarreau522d6c02009-12-06 18:49:18 +01004886 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004887 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004888 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004889 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004890 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004891 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004892
Willy Tarreau115acb92009-12-26 13:56:06 +01004893 if (!ret)
4894 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004895 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004896 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004897 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004898 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01004899 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004900 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01004901 }
4902
Willy Tarreaud98cf932009-12-27 22:54:55 +01004903 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004904 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4905 * for at least a whole chunk or the whole content length bytes after
4906 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004907 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004908 if (msg->msg_state == HTTP_MSG_TRAILERS)
4909 goto http_end;
4910
Willy Tarreaue115b492015-05-01 23:05:14 +02004911 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004912 goto http_end;
4913
4914 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004915 /* we get here if we need to wait for more data. If the buffer is full,
4916 * we have the maximum we can expect.
4917 */
4918 if (buffer_full(req->buf, global.tune.maxrewrite))
4919 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004920
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004921 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004922 txn->status = 408;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004923 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004924
Willy Tarreaue7dff022015-04-03 01:14:29 +02004925 if (!(s->flags & SF_ERR_MASK))
4926 s->flags |= SF_ERR_CLITO;
4927 if (!(s->flags & SF_FINST_MASK))
4928 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004929 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004930 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004931
4932 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004933 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004934 /* Not enough data. We'll re-use the http-request
4935 * timeout here. Ideally, we should set the timeout
4936 * relative to the accept() date. We just set the
4937 * request timeout once at the beginning of the
4938 * request.
4939 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004940 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004941 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004942 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004943 return 0;
4944 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004945
4946 http_end:
4947 /* The situation will not evolve, so let's give up on the analysis. */
4948 s->logs.tv_request = now; /* update the request timer to reflect full request */
4949 req->analysers &= ~an_bit;
4950 req->analyse_exp = TICK_ETERNITY;
4951 return 1;
4952
4953 return_bad_req: /* let's centralize all bad requests */
4954 txn->req.msg_state = HTTP_MSG_ERROR;
4955 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004956 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004957
Willy Tarreaue7dff022015-04-03 01:14:29 +02004958 if (!(s->flags & SF_ERR_MASK))
4959 s->flags |= SF_ERR_PRXCOND;
4960 if (!(s->flags & SF_FINST_MASK))
4961 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004962
Willy Tarreau522d6c02009-12-06 18:49:18 +01004963 return_err_msg:
Christopher Fauletd7c91962015-04-30 11:48:27 +02004964 req->analysers &= AN_FLT_END;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004965 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004966 if (sess->listener->counters)
4967 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004968 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004969}
4970
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004971/* send a server's name with an outgoing request over an established connection.
4972 * Note: this function is designed to be called once the request has been scheduled
4973 * for being forwarded. This is the reason why it rewinds the buffer before
4974 * proceeding.
4975 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004976int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004977
4978 struct hdr_ctx ctx;
4979
Mark Lamourinec2247f02012-01-04 13:02:01 -05004980 char *hdr_name = be->server_id_hdr_name;
4981 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004982 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004983 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004984 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004985
William Lallemandd9e90662012-01-30 17:27:17 +01004986 ctx.idx = 0;
4987
Willy Tarreau211cdec2014-04-17 20:18:08 +02004988 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004989 if (old_o) {
4990 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004991 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004992 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004993 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004994 }
4995
Willy Tarreau9b28e032012-10-12 23:49:43 +02004996 old_i = chn->buf->i;
4997 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 -05004998 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004999 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005000 }
5001
5002 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005003 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005004 memcpy(hdr_val, hdr_name, hdr_name_len);
5005 hdr_val += hdr_name_len;
5006 *hdr_val++ = ':';
5007 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005008 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
5009 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005010
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005011 if (old_o) {
5012 /* If this was a forwarded request, we must readjust the amount of
5013 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02005014 * variations. Note that the current state is >= HTTP_MSG_BODY,
5015 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005016 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02005017 old_o += chn->buf->i - old_i;
5018 b_adv(chn->buf, old_o);
5019 txn->req.next -= old_o;
5020 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005021 }
5022
Mark Lamourinec2247f02012-01-04 13:02:01 -05005023 return 0;
5024}
5025
Willy Tarreau610ecce2010-01-04 21:15:02 +01005026/* Terminate current transaction and prepare a new one. This is very tricky
5027 * right now but it works.
5028 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005029void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005030{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005031 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005032 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01005033 struct proxy *be = s->be;
Willy Tarreau323a2d92015-08-04 19:00:17 +02005034 struct connection *srv_conn;
5035 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02005036 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01005037
Willy Tarreau610ecce2010-01-04 21:15:02 +01005038 /* FIXME: We need a more portable way of releasing a backend's and a
5039 * server's connections. We need a safer way to reinitialize buffer
5040 * flags. We also need a more accurate method for computing per-request
5041 * data.
5042 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02005043 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005044
Willy Tarreau4213a112013-12-15 10:25:42 +01005045 /* unless we're doing keep-alive, we want to quickly close the connection
5046 * to the server.
5047 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005048 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005049 !si_conn_ready(&s->si[1])) {
5050 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
5051 si_shutr(&s->si[1]);
5052 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005053 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005054
Willy Tarreaue7dff022015-04-03 01:14:29 +02005055 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau858b1032015-12-07 17:04:59 +01005056 be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01005057 if (unlikely(s->srv_conn))
5058 sess_change_server(s, NULL);
5059 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005060
5061 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02005062 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005063
Willy Tarreaueee5b512015-04-03 23:46:31 +02005064 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005065 int n;
5066
Willy Tarreaueee5b512015-04-03 23:46:31 +02005067 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005068 if (n < 1 || n > 5)
5069 n = 0;
5070
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005071 if (fe->mode == PR_MODE_HTTP) {
5072 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005073 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005074 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01005075 (be->mode == PR_MODE_HTTP)) {
5076 be->be_counters.p.http.rsp[n]++;
5077 be->be_counters.p.http.cum_req++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005078 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005079 }
5080
5081 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005082 s->logs.bytes_in -= s->req.buf->i;
5083 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005084
5085 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005086 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005087 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005088 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005089 s->do_log(s);
5090 }
5091
Willy Tarreaud713bcc2014-06-25 15:36:04 +02005092 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02005093 stream_stop_content_counters(s);
5094 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02005095
Willy Tarreau610ecce2010-01-04 21:15:02 +01005096 s->logs.accept_date = date; /* user-visible date for logging */
5097 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02005098 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
5099 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005100 tv_zero(&s->logs.tv_request);
5101 s->logs.t_queue = -1;
5102 s->logs.t_connect = -1;
5103 s->logs.t_data = -1;
5104 s->logs.t_close = 0;
5105 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5106 s->logs.srv_queue_size = 0; /* we will get this number soon */
5107
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005108 s->logs.bytes_in = s->req.total = s->req.buf->i;
5109 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005110
5111 if (s->pend_pos)
5112 pendconn_free(s->pend_pos);
5113
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005114 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005115 if (s->flags & SF_CURR_SESS) {
5116 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005117 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005118 }
Willy Tarreau858b1032015-12-07 17:04:59 +01005119 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005120 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005121 }
5122
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005123 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005124
Willy Tarreau4213a112013-12-15 10:25:42 +01005125 /* only release our endpoint if we don't intend to reuse the
5126 * connection.
5127 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005128 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005129 !si_conn_ready(&s->si[1])) {
5130 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005131 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005132 }
5133
Willy Tarreau350f4872014-11-28 14:42:25 +01005134 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5135 s->si[1].err_type = SI_ET_NONE;
5136 s->si[1].conn_retries = 0; /* used for logging too */
5137 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005138 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 +01005139 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);
5140 s->res.flags &= ~(CF_SHUTR|CF_SHUTR_NOW|CF_READ_ATTACHED|CF_READ_ERROR|CF_READ_NOEXP|CF_STREAMER|CF_STREAMER_FAST|CF_WRITE_PARTIAL|CF_NEVER_WAIT|CF_WROTE_DATA);
Willy Tarreaue7dff022015-04-03 01:14:29 +02005141 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5142 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5143 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005144
Willy Tarreaueee5b512015-04-03 23:46:31 +02005145 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005146 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005147 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005148
5149 if (prev_status == 401 || prev_status == 407) {
5150 /* In HTTP keep-alive mode, if we receive a 401, we still have
5151 * a chance of being able to send the visitor again to the same
5152 * server over the same connection. This is required by some
5153 * broken protocols such as NTLM, and anyway whenever there is
5154 * an opportunity for sending the challenge to the proper place,
5155 * it's better to do it (at least it helps with debugging).
5156 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005157 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02005158 if (srv_conn)
5159 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01005160 }
5161
Willy Tarreau53f96852016-02-02 18:50:47 +01005162 /* Never ever allow to reuse a connection from a non-reuse backend */
5163 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
5164 srv_conn->flags |= CO_FL_PRIVATE;
5165
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005166 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005167 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005168
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005169 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005170 s->req.flags |= CF_NEVER_WAIT;
5171 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005172 }
5173
Willy Tarreau610ecce2010-01-04 21:15:02 +01005174 /* if the request buffer is not empty, it means we're
5175 * about to process another request, so send pending
5176 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005177 * Just don't do this if the buffer is close to be full,
5178 * because the request will wait for it to flush a little
5179 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005180 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005181 if (s->req.buf->i) {
5182 if (s->res.buf->o &&
5183 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5184 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5185 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005186 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005187
Willy Tarreau714ea782015-11-25 20:11:11 +01005188 /* we're removing the analysers, we MUST re-enable events detection.
5189 * We don't enable close on the response channel since it's either
5190 * already closed, or in keep-alive with an idle connection handler.
5191 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005192 channel_auto_read(&s->req);
5193 channel_auto_close(&s->req);
5194 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005195
Willy Tarreau1c59bd52015-11-02 20:20:11 +01005196 /* we're in keep-alive with an idle connection, monitor it if not already done */
5197 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02005198 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005199 if (!srv)
5200 si_idle_conn(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01005201 else if (srv_conn->flags & CO_FL_PRIVATE)
Willy Tarreau8dff9982015-08-04 20:45:52 +02005202 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02005203 else if (prev_flags & TX_NOT_FIRST)
5204 /* note: we check the request, not the connection, but
5205 * this is valid for strategies SAFE and AGGR, and in
5206 * case of ALWS, we don't care anyway.
5207 */
5208 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005209 else
5210 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005211 }
Willy Tarreau27375622013-12-17 00:00:28 +01005212
Christopher Faulet3e344292015-11-24 16:24:13 +01005213 if (HAS_FILTERS(s)) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02005214 s->req.analysers &= AN_FLT_END;
5215 s->res.analysers &= AN_FLT_END;
5216 }
Christopher Faulet3e344292015-11-24 16:24:13 +01005217 else {
5218 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
5219 s->res.analysers = 0;
5220 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005221}
5222
5223
5224/* This function updates the request state machine according to the response
5225 * state machine and buffer flags. It returns 1 if it changes anything (flag
5226 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5227 * it is only used to find when a request/response couple is complete. Both
5228 * this function and its equivalent should loop until both return zero. It
5229 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5230 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005231int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005232{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005233 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005234 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005235 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005236 unsigned int old_state = txn->req.msg_state;
5237
Willy Tarreau610ecce2010-01-04 21:15:02 +01005238 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5239 return 0;
5240
5241 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005242 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005243 * We can shut the read side unless we want to abort_on_close,
5244 * or we have a POST request. The issue with POST requests is
5245 * that some browsers still send a CRLF after the request, and
5246 * this CRLF must be read so that it does not remain in the kernel
5247 * buffers, otherwise a close could cause an RST on some systems
5248 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005249 * Note that if we're using keep-alive on the client side, we'd
5250 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005251 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005252 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005253 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005254 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5255 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5256 !(s->be->options & PR_O_ABRT_CLOSE) &&
5257 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005258 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005259
Willy Tarreau40f151a2012-12-20 12:10:09 +01005260 /* if the server closes the connection, we want to immediately react
5261 * and close the socket to save packets and syscalls.
5262 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005263 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005264
Willy Tarreau7f876a12015-11-18 11:59:55 +01005265 /* In any case we've finished parsing the request so we must
5266 * disable Nagle when sending data because 1) we're not going
5267 * to shut this side, and 2) the server is waiting for us to
5268 * send pending data.
5269 */
5270 chn->flags |= CF_NEVER_WAIT;
5271
Willy Tarreau610ecce2010-01-04 21:15:02 +01005272 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5273 goto wait_other_side;
5274
5275 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5276 /* The server has not finished to respond, so we
5277 * don't want to move in order not to upset it.
5278 */
5279 goto wait_other_side;
5280 }
5281
5282 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5283 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005284 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005285 txn->req.msg_state = HTTP_MSG_TUNNEL;
5286 goto wait_other_side;
5287 }
5288
5289 /* When we get here, it means that both the request and the
5290 * response have finished receiving. Depending on the connection
5291 * mode, we'll have to wait for the last bytes to leave in either
5292 * direction, and sometimes for a close to be effective.
5293 */
5294
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005295 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5296 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005297 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5298 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005299 }
5300 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5301 /* Option forceclose is set, or either side wants to close,
5302 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005303 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005304 * once both states are CLOSED.
5305 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005306 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5307 channel_shutr_now(chn);
5308 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005309 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005310 }
5311 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005312 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5313 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005314 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005315 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5316 channel_auto_read(chn);
5317 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005318 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005319 }
5320
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005321 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005322 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005323 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005324
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005325 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005326 txn->req.msg_state = HTTP_MSG_CLOSING;
5327 goto http_msg_closing;
5328 }
5329 else {
5330 txn->req.msg_state = HTTP_MSG_CLOSED;
5331 goto http_msg_closed;
5332 }
5333 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005334 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005335 }
5336
5337 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5338 http_msg_closing:
5339 /* nothing else to forward, just waiting for the output buffer
5340 * to be empty and for the shutw_now to take effect.
5341 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005342 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005343 txn->req.msg_state = HTTP_MSG_CLOSED;
5344 goto http_msg_closed;
5345 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005346 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005347 txn->req.msg_state = HTTP_MSG_ERROR;
5348 goto wait_other_side;
5349 }
5350 }
5351
5352 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5353 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005354 /* see above in MSG_DONE why we only do this in these states */
5355 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5356 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5357 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005358 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005359 goto wait_other_side;
5360 }
5361
5362 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005363 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005364}
5365
5366
5367/* This function updates the response state machine according to the request
5368 * state machine and buffer flags. It returns 1 if it changes anything (flag
5369 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5370 * it is only used to find when a request/response couple is complete. Both
5371 * this function and its equivalent should loop until both return zero. It
5372 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5373 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005374int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005375{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005376 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005377 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005378 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005379 unsigned int old_state = txn->rsp.msg_state;
5380
Willy Tarreau610ecce2010-01-04 21:15:02 +01005381 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5382 return 0;
5383
5384 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5385 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005386 * still monitor the server connection for a possible close
5387 * while the request is being uploaded, so we don't disable
5388 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005389 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005390 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005391
5392 if (txn->req.msg_state == HTTP_MSG_ERROR)
5393 goto wait_other_side;
5394
5395 if (txn->req.msg_state < HTTP_MSG_DONE) {
5396 /* The client seems to still be sending data, probably
5397 * because we got an error response during an upload.
5398 * We have the choice of either breaking the connection
5399 * or letting it pass through. Let's do the later.
5400 */
5401 goto wait_other_side;
5402 }
5403
5404 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5405 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005406 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005407 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005408 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005409 goto wait_other_side;
5410 }
5411
5412 /* When we get here, it means that both the request and the
5413 * response have finished receiving. Depending on the connection
5414 * mode, we'll have to wait for the last bytes to leave in either
5415 * direction, and sometimes for a close to be effective.
5416 */
5417
5418 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5419 /* Server-close mode : shut read and wait for the request
5420 * side to close its output buffer. The caller will detect
5421 * when we're in DONE and the other is in CLOSED and will
5422 * catch that for the final cleanup.
5423 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005424 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5425 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005426 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005427 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5428 /* Option forceclose is set, or either side wants to close,
5429 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005430 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005431 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005432 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005433 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5434 channel_shutr_now(chn);
5435 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005436 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005437 }
5438 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005439 /* The last possible modes are keep-alive and tunnel. Tunnel will
5440 * need to forward remaining data. Keep-alive will need to monitor
5441 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005442 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005443 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005444 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005445 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5446 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005447 }
5448
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005449 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005450 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005451 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005452 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5453 goto http_msg_closing;
5454 }
5455 else {
5456 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5457 goto http_msg_closed;
5458 }
5459 }
5460 goto wait_other_side;
5461 }
5462
5463 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5464 http_msg_closing:
5465 /* nothing else to forward, just waiting for the output buffer
5466 * to be empty and for the shutw_now to take effect.
5467 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005468 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005469 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5470 goto http_msg_closed;
5471 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005472 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005473 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005474 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005475 if (objt_server(s->target))
5476 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005477 goto wait_other_side;
5478 }
5479 }
5480
5481 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5482 http_msg_closed:
5483 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005484 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005485 channel_auto_close(chn);
5486 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005487 goto wait_other_side;
5488 }
5489
5490 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005491 /* We force the response to leave immediately if we're waiting for the
5492 * other side, since there is no pending shutdown to push it out.
5493 */
5494 if (!channel_is_empty(chn))
5495 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005496 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005497}
5498
5499
5500/* Resync the request and response state machines. Return 1 if either state
5501 * changes.
5502 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005503int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005504{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005505 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005506 int old_req_state = txn->req.msg_state;
5507 int old_res_state = txn->rsp.msg_state;
5508
Willy Tarreau610ecce2010-01-04 21:15:02 +01005509 http_sync_req_state(s);
5510 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005511 if (!http_sync_res_state(s))
5512 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005513 if (!http_sync_req_state(s))
5514 break;
5515 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005516
Willy Tarreau610ecce2010-01-04 21:15:02 +01005517 /* OK, both state machines agree on a compatible state.
5518 * There are a few cases we're interested in :
5519 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5520 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5521 * directions, so let's simply disable both analysers.
5522 * - HTTP_MSG_CLOSED on the response only means we must abort the
5523 * request.
5524 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5525 * with server-close mode means we've completed one request and we
5526 * must re-initialize the server connection.
5527 */
5528
5529 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5530 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5531 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5532 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02005533 s->req.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005534 channel_auto_close(&s->req);
5535 channel_auto_read(&s->req);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005536 s->res.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005537 channel_auto_close(&s->res);
5538 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005539 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005540 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005541 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005542 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005543 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02005544 s->res.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005545 channel_auto_close(&s->res);
5546 channel_auto_read(&s->res);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005547 s->req.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005548 channel_abort(&s->req);
5549 channel_auto_close(&s->req);
5550 channel_auto_read(&s->req);
5551 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005552 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005553 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5554 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005555 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005556 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5557 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5558 /* server-close/keep-alive: terminate this transaction,
5559 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005560 * a fresh-new transaction, but only once we're sure there's
5561 * enough room in the request and response buffer to process
5562 * another request. The request buffer must not hold any
5563 * pending output data and the request buffer must not have
5564 * output data occupying the reserve.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005565 */
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005566 if (s->req.buf->o)
5567 s->req.flags |= CF_WAKE_WRITE;
5568 else if (channel_congested(&s->res))
5569 s->res.flags |= CF_WAKE_WRITE;
5570 else
5571 http_end_txn_clean_session(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005572 }
5573
Willy Tarreau610ecce2010-01-04 21:15:02 +01005574 return txn->req.msg_state != old_req_state ||
5575 txn->rsp.msg_state != old_res_state;
5576}
5577
Willy Tarreaud98cf932009-12-27 22:54:55 +01005578/* This function is an analyser which forwards request body (including chunk
5579 * sizes if any). It is called as soon as we must forward, even if we forward
5580 * zero byte. The only situation where it must not be called is when we're in
5581 * tunnel mode and we want to forward till the close. It's used both to forward
5582 * remaining data and to resync after end of body. It expects the msg_state to
5583 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005584 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005585 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005586 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005587 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005588int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005589{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005590 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005591 struct http_txn *txn = s->txn;
5592 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01005593 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005594
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005595 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5596 return 0;
5597
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005598 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005599 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005600 /* Output closed while we were sending data. We must abort and
5601 * wake the other side up.
5602 */
5603 msg->msg_state = HTTP_MSG_ERROR;
5604 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005605 return 1;
5606 }
5607
Willy Tarreaud98cf932009-12-27 22:54:55 +01005608 /* Note that we don't have to send 100-continue back because we don't
5609 * need the data to complete our job, and it's up to the server to
5610 * decide whether to return 100, 417 or anything else in return of
5611 * an "Expect: 100-continue" header.
5612 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02005613 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005614 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5615 ? HTTP_MSG_CHUNK_SIZE
5616 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005617
5618 /* TODO/filters: when http-buffer-request option is set or if a
5619 * rule on url_param exists, the first chunk size could be
5620 * already parsed. In that case, msg->next is after the chunk
5621 * size (including the CRLF after the size). So this case should
5622 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005623 }
5624
Willy Tarreau7ba23542014-04-17 21:50:00 +02005625 /* Some post-connect processing might want us to refrain from starting to
5626 * forward data. Currently, the only reason for this is "balance url_param"
5627 * whichs need to parse/process the request after we've enabled forwarding.
5628 */
5629 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005630 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005631 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005632 req->flags |= CF_WAKE_CONNECT;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005633 goto missing_data_or_waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005634 }
5635 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5636 }
5637
Willy Tarreau80a92c02014-03-12 10:41:13 +01005638 /* in most states, we should abort in case of early close */
5639 channel_auto_close(req);
5640
Willy Tarreauefdf0942014-04-24 20:08:57 +02005641 if (req->to_forward) {
5642 /* We can't process the buffer's contents yet */
5643 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005644 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005645 }
5646
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005647 if (msg->msg_state < HTTP_MSG_DONE) {
5648 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5649 ? http_msg_forward_chunked_body(s, msg)
5650 : http_msg_forward_body(s, msg));
5651 if (!ret)
5652 goto missing_data_or_waiting;
5653 if (ret < 0)
5654 goto return_bad_req;
5655 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005656
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005657 /* other states, DONE...TUNNEL */
5658 /* we don't want to forward closes on DONE except in tunnel mode. */
5659 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5660 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005661
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005662 ret = msg->msg_state;
5663 if (http_resync_states(s)) {
5664 /* some state changes occurred, maybe the analyser
5665 * was disabled too. */
5666 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5667 if (req->flags & CF_SHUTW) {
5668 /* request errors are most likely due to the
5669 * server aborting the transfer. */
5670 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005671 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005672 if (msg->err_pos >= 0)
5673 http_capture_bad_message(&sess->fe->invalid_req, s, msg, ret, s->be);
5674 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005675 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005676 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005677 }
5678
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005679 /* If "option abortonclose" is set on the backend, we want to monitor
5680 * the client's connection and forward any shutdown notification to the
5681 * server, which will decide whether to close or to go on processing the
5682 * request. We only do that in tunnel mode, and not in other modes since
5683 * it can be abused to exhaust source ports. */
5684 if (s->be->options & PR_O_ABRT_CLOSE) {
5685 channel_auto_read(req);
5686 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5687 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5688 s->si[1].flags |= SI_FL_NOLINGER;
5689 channel_auto_close(req);
5690 }
5691 else if (s->txn->meth == HTTP_METH_POST) {
5692 /* POST requests may require to read extra CRLF sent by broken
5693 * browsers and which could cause an RST to be sent upon close
5694 * on some systems (eg: Linux). */
5695 channel_auto_read(req);
5696 }
5697 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005698
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005699 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005700 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005701 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005702 if (!(s->flags & SF_ERR_MASK))
5703 s->flags |= SF_ERR_CLICL;
5704 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005705 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005706 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005707 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005708 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005709 }
5710
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005711 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005712 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005713 if (objt_server(s->target))
5714 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005715
5716 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005717 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005718
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005719 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005720 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005721 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005722
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005723 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005724 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005725 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005726 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005727 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005728
Willy Tarreau5c620922011-05-11 19:56:11 +02005729 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005730 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005731 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005732 * modes are already handled by the stream sock layer. We must not do
5733 * this in content-length mode because it could present the MSG_MORE
5734 * flag with the last block of forwarded data, which would cause an
5735 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005736 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005737 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005738 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005739
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005740 return 0;
5741
Willy Tarreaud98cf932009-12-27 22:54:55 +01005742 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005743 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005744 if (sess->listener->counters)
5745 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005746
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005747 return_bad_req_stats_ok:
5748 txn->req.msg_state = HTTP_MSG_ERROR;
5749 if (txn->status) {
5750 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005751 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005752 } else {
5753 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005754 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005755 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005756 req->analysers &= AN_FLT_END;
5757 s->res.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005758
Willy Tarreaue7dff022015-04-03 01:14:29 +02005759 if (!(s->flags & SF_ERR_MASK))
5760 s->flags |= SF_ERR_PRXCOND;
5761 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005762 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005763 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005764 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005765 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005766 }
5767 return 0;
5768
5769 aborted_xfer:
5770 txn->req.msg_state = HTTP_MSG_ERROR;
5771 if (txn->status) {
5772 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005773 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005774 } else {
5775 txn->status = 502;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005776 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005777 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005778 req->analysers &= AN_FLT_END;
5779 s->res.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005780
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005781 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005782 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005783 if (objt_server(s->target))
5784 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005785
Willy Tarreaue7dff022015-04-03 01:14:29 +02005786 if (!(s->flags & SF_ERR_MASK))
5787 s->flags |= SF_ERR_SRVCL;
5788 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005789 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005790 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005791 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005792 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005793 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005794 return 0;
5795}
5796
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005797/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5798 * processing can continue on next analysers, or zero if it either needs more
5799 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005800 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005801 * when it has nothing left to do, and may remove any analyser when it wants to
5802 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005803 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005804int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005805{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005806 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005807 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005808 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005809 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005810 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005811 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005812 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005813
Willy Tarreau87b09662015-04-03 00:22:06 +02005814 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 +02005815 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005816 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005817 rep,
5818 rep->rex, rep->wex,
5819 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005820 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005821 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005822
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005823 /*
5824 * Now parse the partial (or complete) lines.
5825 * We will check the response syntax, and also join multi-line
5826 * headers. An index of all the lines will be elaborated while
5827 * parsing.
5828 *
5829 * For the parsing, we use a 28 states FSM.
5830 *
5831 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005832 * rep->buf->p = beginning of response
5833 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5834 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005835 * msg->eol = end of current header or line (LF or CRLF)
5836 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005837 */
5838
Willy Tarreau628c40c2014-04-24 19:11:26 +02005839 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005840 /* There's a protected area at the end of the buffer for rewriting
5841 * purposes. We don't want to start to parse the request if the
5842 * protected area is affected, because we may have to move processed
5843 * data later, which is much more complicated.
5844 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005845 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005846 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005847 /* some data has still not left the buffer, wake us once that's done */
5848 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5849 goto abort_response;
5850 channel_dont_close(rep);
5851 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005852 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005853 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005854 }
5855
Willy Tarreau379357a2013-06-08 12:55:46 +02005856 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5857 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5858 buffer_slow_realign(rep->buf);
5859
Willy Tarreau9b28e032012-10-12 23:49:43 +02005860 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005861 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005862 }
5863
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005864 /* 1: we might have to print this header in debug mode */
5865 if (unlikely((global.mode & MODE_DEBUG) &&
5866 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005867 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005868 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005869
Willy Tarreau9b28e032012-10-12 23:49:43 +02005870 sol = rep->buf->p;
5871 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005872 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005873
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005874 sol += hdr_idx_first_pos(&txn->hdr_idx);
5875 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005876
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005877 while (cur_idx) {
5878 eol = sol + txn->hdr_idx.v[cur_idx].len;
5879 debug_hdr("srvhdr", s, sol, eol);
5880 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5881 cur_idx = txn->hdr_idx.v[cur_idx].next;
5882 }
5883 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005884
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005885 /*
5886 * Now we quickly check if we have found a full valid response.
5887 * If not so, we check the FD and buffer states before leaving.
5888 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005889 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005890 * responses are checked first.
5891 *
5892 * Depending on whether the client is still there or not, we
5893 * may send an error response back or not. Note that normally
5894 * we should only check for HTTP status there, and check I/O
5895 * errors somewhere else.
5896 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005897
Willy Tarreau655dce92009-11-08 13:10:58 +01005898 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005899 /* Invalid response */
5900 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5901 /* we detected a parsing error. We want to archive this response
5902 * in the dedicated proxy area for later troubleshooting.
5903 */
5904 hdr_response_bad:
5905 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005906 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005907
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005908 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005909 if (objt_server(s->target)) {
5910 objt_server(s->target)->counters.failed_resp++;
5911 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005912 }
Willy Tarreau64648412010-03-05 10:41:54 +01005913 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005914 channel_auto_close(rep);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005915 rep->analysers &= AN_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005916 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005917 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005918 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005919 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005920
Willy Tarreaue7dff022015-04-03 01:14:29 +02005921 if (!(s->flags & SF_ERR_MASK))
5922 s->flags |= SF_ERR_PRXCOND;
5923 if (!(s->flags & SF_FINST_MASK))
5924 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005925
5926 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005927 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005928
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005929 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005930 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005931 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005932 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005933 goto hdr_response_bad;
5934 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005935
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005936 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005937 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005938 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005939 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005940 else if (txn->flags & TX_NOT_FIRST)
5941 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005942
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005943 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005944 if (objt_server(s->target)) {
5945 objt_server(s->target)->counters.failed_resp++;
5946 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005947 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005948
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005949 channel_auto_close(rep);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005950 rep->analysers &= AN_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005951 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005952 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005953 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005954 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005955
Willy Tarreaue7dff022015-04-03 01:14:29 +02005956 if (!(s->flags & SF_ERR_MASK))
5957 s->flags |= SF_ERR_SRVCL;
5958 if (!(s->flags & SF_FINST_MASK))
5959 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005960 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005961 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005962
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005963 /* read timeout : return a 504 to the client. */
5964 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005965 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005966 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005967
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005968 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005969 if (objt_server(s->target)) {
5970 objt_server(s->target)->counters.failed_resp++;
5971 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005972 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005973
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005974 channel_auto_close(rep);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005975 rep->analysers &= AN_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005976 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005977 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005978 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005979 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005980
Willy Tarreaue7dff022015-04-03 01:14:29 +02005981 if (!(s->flags & SF_ERR_MASK))
5982 s->flags |= SF_ERR_SRVTO;
5983 if (!(s->flags & SF_FINST_MASK))
5984 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005985 return 0;
5986 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005987
Willy Tarreauf003d372012-11-26 13:35:37 +01005988 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005989 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005990 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01005991 s->be->be_counters.cli_aborts++;
5992 if (objt_server(s->target))
5993 objt_server(s->target)->counters.cli_aborts++;
5994
Christopher Fauletd7c91962015-04-30 11:48:27 +02005995 rep->analysers &= AN_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01005996 channel_auto_close(rep);
5997
5998 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005999 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006000 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01006001
Willy Tarreaue7dff022015-04-03 01:14:29 +02006002 if (!(s->flags & SF_ERR_MASK))
6003 s->flags |= SF_ERR_CLICL;
6004 if (!(s->flags & SF_FINST_MASK))
6005 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01006006
Willy Tarreau87b09662015-04-03 00:22:06 +02006007 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01006008 return 0;
6009 }
6010
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006011 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006012 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006013 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006014 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006015 else if (txn->flags & TX_NOT_FIRST)
6016 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006017
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006018 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006019 if (objt_server(s->target)) {
6020 objt_server(s->target)->counters.failed_resp++;
6021 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006022 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006023
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006024 channel_auto_close(rep);
Christopher Fauletd7c91962015-04-30 11:48:27 +02006025 rep->analysers &= AN_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006026 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006027 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006028 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006029 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01006030
Willy Tarreaue7dff022015-04-03 01:14:29 +02006031 if (!(s->flags & SF_ERR_MASK))
6032 s->flags |= SF_ERR_SRVCL;
6033 if (!(s->flags & SF_FINST_MASK))
6034 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006035 return 0;
6036 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006037
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006038 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006039 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006040 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006041 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006042 else if (txn->flags & TX_NOT_FIRST)
6043 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006044
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006045 s->be->be_counters.failed_resp++;
Christopher Fauletd7c91962015-04-30 11:48:27 +02006046 rep->analysers &= AN_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006047 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006048
Willy Tarreaue7dff022015-04-03 01:14:29 +02006049 if (!(s->flags & SF_ERR_MASK))
6050 s->flags |= SF_ERR_CLICL;
6051 if (!(s->flags & SF_FINST_MASK))
6052 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006053
Willy Tarreau87b09662015-04-03 00:22:06 +02006054 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006055 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006056 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006057
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006058 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006059 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006060 return 0;
6061 }
6062
6063 /* More interesting part now : we know that we have a complete
6064 * response which at least looks like HTTP. We have an indicator
6065 * of each header's length, so we can parse them quickly.
6066 */
6067
6068 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006069 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006070
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006071 /*
6072 * 1: get the status code
6073 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006074 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006075 if (n < 1 || n > 5)
6076 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006077 /* when the client triggers a 4xx from the server, it's most often due
6078 * to a missing object or permission. These events should be tracked
6079 * because if they happen often, it may indicate a brute force or a
6080 * vulnerability scan.
6081 */
6082 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006083 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006084
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006085 if (objt_server(s->target))
6086 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006087
Willy Tarreau91852eb2015-05-01 13:26:00 +02006088 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
6089 * exactly one digit "." one digit. This check may be disabled using
6090 * option accept-invalid-http-response.
6091 */
6092 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
6093 if (msg->sl.st.v_l != 8) {
6094 msg->err_pos = 0;
6095 goto hdr_response_bad;
6096 }
6097
6098 if (rep->buf->p[4] != '/' ||
6099 !isdigit((unsigned char)rep->buf->p[5]) ||
6100 rep->buf->p[6] != '.' ||
6101 !isdigit((unsigned char)rep->buf->p[7])) {
6102 msg->err_pos = 4;
6103 goto hdr_response_bad;
6104 }
6105 }
6106
Willy Tarreau5b154472009-12-21 20:11:07 +01006107 /* check if the response is HTTP/1.1 or above */
6108 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006109 ((rep->buf->p[5] > '1') ||
6110 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006111 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006112
6113 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006114 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 +01006115
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006116 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006117 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006118
Willy Tarreau9b28e032012-10-12 23:49:43 +02006119 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006120
Willy Tarreau39650402010-03-15 19:44:39 +01006121 /* Adjust server's health based on status code. Note: status codes 501
6122 * and 505 are triggered on demand by client request, so we must not
6123 * count them as server failures.
6124 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006125 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006126 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006127 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006128 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006129 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006130 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006131
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006132 /*
6133 * 2: check for cacheability.
6134 */
6135
6136 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006137 case 100:
6138 /*
6139 * We may be facing a 100-continue response, in which case this
6140 * is not the right response, and we're waiting for the next one.
6141 * Let's allow this response to go to the client and wait for the
6142 * next one.
6143 */
6144 hdr_idx_init(&txn->hdr_idx);
6145 msg->next -= channel_forward(rep, msg->next);
6146 msg->msg_state = HTTP_MSG_RPBEFORE;
6147 txn->status = 0;
6148 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01006149 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02006150 goto next_one;
6151
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006152 case 200:
6153 case 203:
6154 case 206:
6155 case 300:
6156 case 301:
6157 case 410:
6158 /* RFC2616 @13.4:
6159 * "A response received with a status code of
6160 * 200, 203, 206, 300, 301 or 410 MAY be stored
6161 * by a cache (...) unless a cache-control
6162 * directive prohibits caching."
6163 *
6164 * RFC2616 @9.5: POST method :
6165 * "Responses to this method are not cacheable,
6166 * unless the response includes appropriate
6167 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006168 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006169 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006170 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006171 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6172 break;
6173 default:
6174 break;
6175 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006176
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006177 /*
6178 * 3: we may need to capture headers
6179 */
6180 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006181 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006182 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006183 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006184
Willy Tarreau557f1992015-05-01 10:05:17 +02006185 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6186 * by RFC7230#3.3.3 :
6187 *
6188 * The length of a message body is determined by one of the following
6189 * (in order of precedence):
6190 *
6191 * 1. Any response to a HEAD request and any response with a 1xx
6192 * (Informational), 204 (No Content), or 304 (Not Modified) status
6193 * code is always terminated by the first empty line after the
6194 * header fields, regardless of the header fields present in the
6195 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006196 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006197 * 2. Any 2xx (Successful) response to a CONNECT request implies that
6198 * the connection will become a tunnel immediately after the empty
6199 * line that concludes the header fields. A client MUST ignore any
6200 * Content-Length or Transfer-Encoding header fields received in
6201 * such a message.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006202 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006203 * 3. If a Transfer-Encoding header field is present and the chunked
6204 * transfer coding (Section 4.1) is the final encoding, the message
6205 * body length is determined by reading and decoding the chunked
6206 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006207 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006208 * If a Transfer-Encoding header field is present in a response and
6209 * the chunked transfer coding is not the final encoding, the
6210 * message body length is determined by reading the connection until
6211 * it is closed by the server. If a Transfer-Encoding header field
6212 * is present in a request and the chunked transfer coding is not
6213 * the final encoding, the message body length cannot be determined
6214 * reliably; the server MUST respond with the 400 (Bad Request)
6215 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006216 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006217 * If a message is received with both a Transfer-Encoding and a
6218 * Content-Length header field, the Transfer-Encoding overrides the
6219 * Content-Length. Such a message might indicate an attempt to
6220 * perform request smuggling (Section 9.5) or response splitting
6221 * (Section 9.4) and ought to be handled as an error. A sender MUST
6222 * remove the received Content-Length field prior to forwarding such
6223 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006224 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006225 * 4. If a message is received without Transfer-Encoding and with
6226 * either multiple Content-Length header fields having differing
6227 * field-values or a single Content-Length header field having an
6228 * invalid value, then the message framing is invalid and the
6229 * recipient MUST treat it as an unrecoverable error. If this is a
6230 * request message, the server MUST respond with a 400 (Bad Request)
6231 * status code and then close the connection. If this is a response
6232 * message received by a proxy, the proxy MUST close the connection
6233 * to the server, discard the received response, and send a 502 (Bad
6234 * Gateway) response to the client. If this is a response message
6235 * received by a user agent, the user agent MUST close the
6236 * connection to the server and discard the received response.
6237 *
6238 * 5. If a valid Content-Length header field is present without
6239 * Transfer-Encoding, its decimal value defines the expected message
6240 * body length in octets. If the sender closes the connection or
6241 * the recipient times out before the indicated number of octets are
6242 * received, the recipient MUST consider the message to be
6243 * incomplete and close the connection.
6244 *
6245 * 6. If this is a request message and none of the above are true, then
6246 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006247 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006248 * 7. Otherwise, this is a response message without a declared message
6249 * body length, so the message body length is determined by the
6250 * number of octets received prior to the server closing the
6251 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006252 */
6253
6254 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006255 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006256 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006257 * FIXME: should we parse anyway and return an error on chunked encoding ?
6258 */
6259 if (txn->meth == HTTP_METH_HEAD ||
6260 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006261 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006262 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006263 goto skip_content_length;
6264 }
6265
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006266 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006267 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006268 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006269 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006270 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6271 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006272 /* bad transfer-encoding (chunked followed by something else) */
6273 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006274 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006275 break;
6276 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006277 }
6278
Willy Tarreau1c913912015-04-30 10:57:51 +02006279 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006280 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006281 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006282 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6283 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6284 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006285 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006286 signed long long cl;
6287
Willy Tarreauad14f752011-09-02 20:33:27 +02006288 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006289 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006290 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006291 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006292
Willy Tarreauad14f752011-09-02 20:33:27 +02006293 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006294 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006295 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006296 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006297
Willy Tarreauad14f752011-09-02 20:33:27 +02006298 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006299 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006300 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006301 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006302
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006303 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006304 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006305 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006306 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006307
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006308 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006309 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006310 }
6311
6312skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006313 /* Now we have to check if we need to modify the Connection header.
6314 * This is more difficult on the response than it is on the request,
6315 * because we can have two different HTTP versions and we don't know
6316 * how the client will interprete a response. For instance, let's say
6317 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6318 * HTTP/1.1 response without any header. Maybe it will bound itself to
6319 * HTTP/1.0 because it only knows about it, and will consider the lack
6320 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6321 * the lack of header as a keep-alive. Thus we will use two flags
6322 * indicating how a request MAY be understood by the client. In case
6323 * of multiple possibilities, we'll fix the header to be explicit. If
6324 * ambiguous cases such as both close and keepalive are seen, then we
6325 * will fall back to explicit close. Note that we won't take risks with
6326 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006327 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006328 */
6329
Willy Tarreaudc008c52010-02-01 16:20:08 +01006330 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6331 txn->status == 101)) {
6332 /* Either we've established an explicit tunnel, or we're
6333 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006334 * to understand the next protocols. We have to switch to tunnel
6335 * mode, so that we transfer the request and responses then let
6336 * this protocol pass unmodified. When we later implement specific
6337 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006338 * header which contains information about that protocol for
6339 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006340 */
6341 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6342 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006343 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6344 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006345 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006346 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006347 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006348
Willy Tarreau70dffda2014-01-30 03:07:23 +01006349 /* this situation happens when combining pretend-keepalive with httpclose. */
6350 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006351 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006352 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006353 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6354
Willy Tarreau60466522010-01-18 19:08:45 +01006355 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006356 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006357 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6358 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006359
Willy Tarreau60466522010-01-18 19:08:45 +01006360 /* now adjust header transformations depending on current state */
6361 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6362 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6363 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006364 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006365 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006366 }
Willy Tarreau60466522010-01-18 19:08:45 +01006367 else { /* SCL / KAL */
6368 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006369 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006370 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006371 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006372
Willy Tarreau60466522010-01-18 19:08:45 +01006373 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006374 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006375
Willy Tarreau60466522010-01-18 19:08:45 +01006376 /* Some keep-alive responses are converted to Server-close if
6377 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006378 */
Willy Tarreau60466522010-01-18 19:08:45 +01006379 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6380 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006381 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006382 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006383 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006384 }
6385
Willy Tarreau7959a552013-09-23 16:44:27 +02006386 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006387 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006388
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006389 /* end of job, return OK */
6390 rep->analysers &= ~an_bit;
6391 rep->analyse_exp = TICK_ETERNITY;
6392 channel_auto_close(rep);
6393 return 1;
6394
6395 abort_keep_alive:
6396 /* A keep-alive request to the server failed on a network error.
6397 * The client is required to retry. We need to close without returning
6398 * any other information so that the client retries.
6399 */
6400 txn->status = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +02006401 rep->analysers &= AN_FLT_END;
6402 s->req.analysers &= AN_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006403 channel_auto_close(rep);
6404 s->logs.logwait = 0;
6405 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006406 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006407 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006408 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006409 return 0;
6410}
6411
6412/* This function performs all the processing enabled for the current response.
6413 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006414 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006415 * other functions. It works like process_request (see indications above).
6416 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006417int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006418{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006419 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006420 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006421 struct http_msg *msg = &txn->rsp;
6422 struct proxy *cur_proxy;
6423 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006424 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006425
Willy Tarreau87b09662015-04-03 00:22:06 +02006426 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 +02006427 now_ms, __FUNCTION__,
6428 s,
6429 rep,
6430 rep->rex, rep->wex,
6431 rep->flags,
6432 rep->buf->i,
6433 rep->analysers);
6434
6435 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6436 return 0;
6437
Willy Tarreau70730dd2014-04-24 18:06:27 +02006438 /* The stats applet needs to adjust the Connection header but we don't
6439 * apply any filter there.
6440 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006441 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6442 rep->analysers &= ~an_bit;
6443 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006444 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006445 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006446
Willy Tarreau58975672014-04-24 21:13:57 +02006447 /*
6448 * We will have to evaluate the filters.
6449 * As opposed to version 1.2, now they will be evaluated in the
6450 * filters order and not in the header order. This means that
6451 * each filter has to be validated among all headers.
6452 *
6453 * Filters are tried with ->be first, then with ->fe if it is
6454 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006455 *
6456 * Maybe we are in resume condiion. In this case I choose the
6457 * "struct proxy" which contains the rule list matching the resume
6458 * pointer. If none of theses "struct proxy" match, I initialise
6459 * the process with the first one.
6460 *
6461 * In fact, I check only correspondance betwwen the current list
6462 * pointer and the ->fe rule list. If it doesn't match, I initialize
6463 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006464 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006465 if (s->current_rule_list == &sess->fe->http_res_rules)
6466 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006467 else
6468 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006469 while (1) {
6470 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006471
Willy Tarreau58975672014-04-24 21:13:57 +02006472 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02006473 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006474 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006475
Willy Tarreau51d861a2015-05-22 17:30:48 +02006476 if (ret == HTTP_RULE_RES_BADREQ)
6477 goto return_srv_prx_502;
6478
6479 if (ret == HTTP_RULE_RES_DONE) {
6480 rep->analysers &= ~an_bit;
6481 rep->analyse_exp = TICK_ETERNITY;
6482 return 1;
6483 }
6484 }
6485
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006486 /* we need to be called again. */
6487 if (ret == HTTP_RULE_RES_YIELD) {
6488 channel_dont_close(rep);
6489 return 0;
6490 }
6491
Willy Tarreau58975672014-04-24 21:13:57 +02006492 /* try headers filters */
6493 if (rule_set->rsp_exp != NULL) {
6494 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6495 return_bad_resp:
6496 if (objt_server(s->target)) {
6497 objt_server(s->target)->counters.failed_resp++;
6498 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006499 }
Willy Tarreau58975672014-04-24 21:13:57 +02006500 s->be->be_counters.failed_resp++;
6501 return_srv_prx_502:
Christopher Fauletd7c91962015-04-30 11:48:27 +02006502 rep->analysers &= AN_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02006503 txn->status = 502;
6504 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006505 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006506 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006507 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006508 if (!(s->flags & SF_ERR_MASK))
6509 s->flags |= SF_ERR_PRXCOND;
6510 if (!(s->flags & SF_FINST_MASK))
6511 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006512 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006513 }
Willy Tarreau58975672014-04-24 21:13:57 +02006514 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006515
Willy Tarreau58975672014-04-24 21:13:57 +02006516 /* has the response been denied ? */
6517 if (txn->flags & TX_SVDENY) {
6518 if (objt_server(s->target))
6519 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006520
Willy Tarreau58975672014-04-24 21:13:57 +02006521 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006522 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006523 if (sess->listener->counters)
6524 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006525
Willy Tarreau58975672014-04-24 21:13:57 +02006526 goto return_srv_prx_502;
6527 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006528
Willy Tarreau58975672014-04-24 21:13:57 +02006529 /* add response headers from the rule sets in the same order */
6530 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006531 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006532 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006533 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006534 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006535 ret = acl_pass(ret);
6536 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6537 ret = !ret;
6538 if (!ret)
6539 continue;
6540 }
6541 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6542 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006543 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006544
Willy Tarreau58975672014-04-24 21:13:57 +02006545 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006546 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006547 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006548 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006549 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006550
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006551 /* After this point, this anayzer can't return yield, so we can
6552 * remove the bit corresponding to this analyzer from the list.
6553 *
6554 * Note that the intermediate returns and goto found previously
6555 * reset the analyzers.
6556 */
6557 rep->analysers &= ~an_bit;
6558 rep->analyse_exp = TICK_ETERNITY;
6559
Willy Tarreau58975672014-04-24 21:13:57 +02006560 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006561 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006562 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006563
Willy Tarreau58975672014-04-24 21:13:57 +02006564 /*
6565 * Now check for a server cookie.
6566 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006567 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02006568 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006569
Willy Tarreau58975672014-04-24 21:13:57 +02006570 /*
6571 * Check for cache-control or pragma headers if required.
6572 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006573 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 +02006574 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006575
Willy Tarreau58975672014-04-24 21:13:57 +02006576 /*
6577 * Add server cookie in the response if needed
6578 */
6579 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6580 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006581 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006582 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6583 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6584 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6585 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6586 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006587 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006588 /* the server is known, it's not the one the client requested, or the
6589 * cookie's last seen date needs to be refreshed. We have to
6590 * insert a set-cookie here, except if we want to insert only on POST
6591 * requests and this one isn't. Note that servers which don't have cookies
6592 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006593 */
Willy Tarreau58975672014-04-24 21:13:57 +02006594 if (!objt_server(s->target)->cookie) {
6595 chunk_printf(&trash,
6596 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6597 s->be->cookie_name);
6598 }
6599 else {
6600 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006601
Willy Tarreau58975672014-04-24 21:13:57 +02006602 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6603 /* emit last_date, which is mandatory */
6604 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6605 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6606 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006607
Willy Tarreau58975672014-04-24 21:13:57 +02006608 if (s->be->cookie_maxlife) {
6609 /* emit first_date, which is either the original one or
6610 * the current date.
6611 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006612 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006613 s30tob64(txn->cookie_first_date ?
6614 txn->cookie_first_date >> 2 :
6615 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006616 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006617 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006618 }
Willy Tarreau58975672014-04-24 21:13:57 +02006619 chunk_appendf(&trash, "; path=/");
6620 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006621
Willy Tarreau58975672014-04-24 21:13:57 +02006622 if (s->be->cookie_domain)
6623 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006624
Willy Tarreau58975672014-04-24 21:13:57 +02006625 if (s->be->ck_opts & PR_CK_HTTPONLY)
6626 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006627
Willy Tarreau58975672014-04-24 21:13:57 +02006628 if (s->be->ck_opts & PR_CK_SECURE)
6629 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006630
Willy Tarreau58975672014-04-24 21:13:57 +02006631 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6632 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006633
Willy Tarreau58975672014-04-24 21:13:57 +02006634 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006635 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006636 /* the server did not change, only the date was updated */
6637 txn->flags |= TX_SCK_UPDATED;
6638 else
6639 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006640
Willy Tarreau58975672014-04-24 21:13:57 +02006641 /* Here, we will tell an eventual cache on the client side that we don't
6642 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6643 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6644 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006645 */
Willy Tarreau58975672014-04-24 21:13:57 +02006646 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006647
Willy Tarreau58975672014-04-24 21:13:57 +02006648 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006649
Willy Tarreau58975672014-04-24 21:13:57 +02006650 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6651 "Cache-control: private", 22) < 0))
6652 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006653 }
Willy Tarreau58975672014-04-24 21:13:57 +02006654 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006655
Willy Tarreau58975672014-04-24 21:13:57 +02006656 /*
6657 * Check if result will be cacheable with a cookie.
6658 * We'll block the response if security checks have caught
6659 * nasty things such as a cacheable cookie.
6660 */
6661 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6662 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6663 (s->be->options & PR_O_CHK_CACHE)) {
6664 /* we're in presence of a cacheable response containing
6665 * a set-cookie header. We'll block it as requested by
6666 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006667 */
Willy Tarreau58975672014-04-24 21:13:57 +02006668 if (objt_server(s->target))
6669 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006670
Willy Tarreau58975672014-04-24 21:13:57 +02006671 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006672 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006673 if (sess->listener->counters)
6674 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006675
Willy Tarreau58975672014-04-24 21:13:57 +02006676 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6677 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6678 send_log(s->be, LOG_ALERT,
6679 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6680 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6681 goto return_srv_prx_502;
6682 }
Willy Tarreau03945942009-12-22 16:50:27 +01006683
Willy Tarreau70730dd2014-04-24 18:06:27 +02006684 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006685 /*
6686 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6687 * If an "Upgrade" token is found, the header is left untouched in order
6688 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006689 * if anything but "Upgrade" is present in the Connection header. We don't
6690 * want to touch any 101 response either since it's switching to another
6691 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006692 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006693 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006694 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006695 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006696 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6697 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006698
Willy Tarreau58975672014-04-24 21:13:57 +02006699 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6700 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6701 /* we want a keep-alive response here. Keep-alive header
6702 * required if either side is not 1.1.
6703 */
6704 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6705 want_flags |= TX_CON_KAL_SET;
6706 }
6707 else {
6708 /* we want a close response here. Close header required if
6709 * the server is 1.1, regardless of the client.
6710 */
6711 if (msg->flags & HTTP_MSGF_VER_11)
6712 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006713 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006714
Willy Tarreau58975672014-04-24 21:13:57 +02006715 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6716 http_change_connection_header(txn, msg, want_flags);
6717 }
6718
6719 skip_header_mangling:
Christopher Faulet3e344292015-11-24 16:24:13 +01006720 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_FILTERS(s) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006721 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
6722 rep->analysers &= ~AN_FLT_XFER_DATA;
Willy Tarreau58975672014-04-24 21:13:57 +02006723 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Christopher Fauletd7c91962015-04-30 11:48:27 +02006724 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006725
Willy Tarreau58975672014-04-24 21:13:57 +02006726 /* if the user wants to log as soon as possible, without counting
6727 * bytes from the server, then this is the right moment. We have
6728 * to temporarily assign bytes_out to log what we currently have.
6729 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006730 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006731 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6732 s->logs.bytes_out = txn->rsp.eoh;
6733 s->do_log(s);
6734 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006735 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006736 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006737}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006738
Willy Tarreaud98cf932009-12-27 22:54:55 +01006739/* This function is an analyser which forwards response body (including chunk
6740 * sizes if any). It is called as soon as we must forward, even if we forward
6741 * zero byte. The only situation where it must not be called is when we're in
6742 * tunnel mode and we want to forward till the close. It's used both to forward
6743 * remaining data and to resync after end of body. It expects the msg_state to
6744 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006745 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006746 *
6747 * It is capable of compressing response data both in content-length mode and
6748 * in chunked mode. The state machines follows different flows depending on
6749 * whether content-length and chunked modes are used, since there are no
6750 * trailers in content-length :
6751 *
6752 * chk-mode cl-mode
6753 * ,----- BODY -----.
6754 * / \
6755 * V size > 0 V chk-mode
6756 * .--> SIZE -------------> DATA -------------> CRLF
6757 * | | size == 0 | last byte |
6758 * | v final crlf v inspected |
6759 * | TRAILERS -----------> DONE |
6760 * | |
6761 * `----------------------------------------------'
6762 *
6763 * Compression only happens in the DATA state, and must be flushed in final
6764 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6765 * is performed at once on final states for all bytes parsed, or when leaving
6766 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006767 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006768int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006769{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006770 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006771 struct http_txn *txn = s->txn;
6772 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01006773 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006774
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006775 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6776 return 0;
6777
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006778 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006779 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006780 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006781 /* Output closed while we were sending data. We must abort and
6782 * wake the other side up.
6783 */
6784 msg->msg_state = HTTP_MSG_ERROR;
6785 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006786 return 1;
6787 }
6788
Willy Tarreau4fe41902010-06-07 22:27:41 +02006789 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006790 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006791
Christopher Fauletd7c91962015-04-30 11:48:27 +02006792 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006793 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
6794 ? HTTP_MSG_CHUNK_SIZE
6795 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006796 }
6797
Willy Tarreauefdf0942014-04-24 20:08:57 +02006798 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006799 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02006800 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006801 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006802 }
6803
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006804 if (msg->msg_state < HTTP_MSG_DONE) {
6805 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6806 ? http_msg_forward_chunked_body(s, msg)
6807 : http_msg_forward_body(s, msg));
6808 if (!ret)
6809 goto missing_data_or_waiting;
6810 if (ret < 0)
6811 goto return_bad_res;
6812 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006813
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006814 /* other states, DONE...TUNNEL */
6815 /* for keep-alive we don't want to forward closes on DONE */
6816 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6817 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6818 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006819
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006820 ret = msg->msg_state;
6821 if (http_resync_states(s)) {
6822 /* some state changes occurred, maybe the analyser was disabled
6823 * too. */
6824 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6825 if (res->flags & CF_SHUTW) {
6826 /* response errors are most likely due to the
6827 * client aborting the transfer. */
6828 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006829 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006830 if (msg->err_pos >= 0)
6831 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, strm_fe(s));
6832 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006833 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006834 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006835 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006836 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006837
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006838 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006839 if (res->flags & CF_SHUTW)
6840 goto aborted_xfer;
6841
6842 /* stop waiting for data if the input is closed before the end. If the
6843 * client side was already closed, it means that the client has aborted,
6844 * so we don't want to count this as a server abort. Otherwise it's a
6845 * server abort.
6846 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006847 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006848 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006849 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006850 /* If we have some pending data, we continue the processing */
6851 if (!buffer_pending(res->buf)) {
6852 if (!(s->flags & SF_ERR_MASK))
6853 s->flags |= SF_ERR_SRVCL;
6854 s->be->be_counters.srv_aborts++;
6855 if (objt_server(s->target))
6856 objt_server(s->target)->counters.srv_aborts++;
6857 goto return_bad_res_stats_ok;
6858 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006859 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006860
Willy Tarreau40dba092010-03-04 18:14:51 +01006861 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006862 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006863 goto return_bad_res;
6864
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006865 /* When TE: chunked is used, we need to get there again to parse
6866 * remaining chunks even if the server has closed, so we don't want to
6867 * set CF_DONTCLOSE. Similarly, if the body length is undefined, if
6868 * keep-alive is set on the client side or if there are filters
6869 * registered on the stream, we don't want to forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006870 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006871 if ((msg->flags & HTTP_MSGF_TE_CHNK) || !msg->body_len ||
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006872 HAS_FILTERS(s) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006873 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6874 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006875 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006876
Willy Tarreau5c620922011-05-11 19:56:11 +02006877 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006878 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006879 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006880 * modes are already handled by the stream sock layer. We must not do
6881 * this in content-length mode because it could present the MSG_MORE
6882 * flag with the last block of forwarded data, which would cause an
6883 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006884 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006885 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006886 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006887
Willy Tarreau87b09662015-04-03 00:22:06 +02006888 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006889 return 0;
6890
Willy Tarreau40dba092010-03-04 18:14:51 +01006891 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006892 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006893 if (objt_server(s->target))
6894 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006895
6896 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006897 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006898 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006899 http_reply_and_close(s, txn->status, NULL);
Christopher Fauletd7c91962015-04-30 11:48:27 +02006900 res->analysers &= AN_FLT_END;
6901 s->req.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006902 if (objt_server(s->target))
6903 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006904
Willy Tarreaue7dff022015-04-03 01:14:29 +02006905 if (!(s->flags & SF_ERR_MASK))
6906 s->flags |= SF_ERR_PRXCOND;
6907 if (!(s->flags & SF_FINST_MASK))
6908 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006909 return 0;
6910
6911 aborted_xfer:
6912 txn->rsp.msg_state = HTTP_MSG_ERROR;
6913 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006914 http_reply_and_close(s, txn->status, NULL);
Christopher Fauletd7c91962015-04-30 11:48:27 +02006915 res->analysers &= AN_FLT_END;
6916 s->req.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006917
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006918 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006919 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006920 if (objt_server(s->target))
6921 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006922
Willy Tarreaue7dff022015-04-03 01:14:29 +02006923 if (!(s->flags & SF_ERR_MASK))
6924 s->flags |= SF_ERR_CLICL;
6925 if (!(s->flags & SF_FINST_MASK))
6926 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006927 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006928}
6929
6930
6931static inline int
6932http_msg_forward_body(struct stream *s, struct http_msg *msg)
6933{
6934 struct channel *chn = msg->chn;
6935 int ret;
6936
6937 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
6938
6939 if (msg->msg_state == HTTP_MSG_ENDING)
6940 goto ending;
6941
6942 /* Neither content-length, nor transfer-encoding was found, so we must
6943 * read the body until the server connection is closed. In that case, we
6944 * eat data as they come. Of course, this happens for response only. */
6945 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6946 unsigned long long len = (chn->buf->i - msg->next);
6947 msg->chunk_len += len;
6948 msg->body_len += len;
6949 }
Christopher Fauletda02e172015-12-04 09:25:05 +01006950 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6951 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6952 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006953 msg->next += ret;
6954 msg->chunk_len -= ret;
6955 if (msg->chunk_len) {
6956 /* input empty or output full */
6957 if (chn->buf->i > msg->next)
6958 chn->flags |= CF_WAKE_WRITE;
6959 goto missing_data_or_waiting;
6960 }
6961
6962 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && !(chn->flags & CF_SHUTR)) {
6963 /* The server still sending data */
6964 goto missing_data_or_waiting;
6965 }
6966 msg->msg_state = HTTP_MSG_ENDING;
6967
6968 ending:
6969 /* we may have some pending data starting at res->buf->p such as a last
6970 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006971 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6972 /* default_ret */ msg->next,
6973 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006974 b_adv(chn->buf, ret);
6975 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006976 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6977 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006978 if (msg->next)
6979 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006980
Christopher Fauletda02e172015-12-04 09:25:05 +01006981 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
6982 /* default_ret */ 1,
6983 /* on_error */ goto error,
6984 /* on_wait */ goto waiting);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006985 msg->msg_state = HTTP_MSG_DONE;
6986 return 1;
6987
6988 missing_data_or_waiting:
6989 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006990 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6991 /* default_ret */ msg->next,
6992 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006993 b_adv(chn->buf, ret);
6994 msg->next -= ret;
6995 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6996 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006997 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006998 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006999 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007000 return 0;
7001 error:
7002 return -1;
7003}
7004
7005static inline int
7006http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
7007{
7008 struct channel *chn = msg->chn;
7009 int ret;
7010
7011 /* Here we have the guarantee to be in one of the following state:
7012 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
7013 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
7014
7015 switch_states:
7016 switch (msg->msg_state) {
7017 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01007018 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
7019 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
7020 /* on_error */ goto error);
7021 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007022 msg->chunk_len -= ret;
7023 if (msg->chunk_len) {
7024 /* input empty or output full */
7025 if (chn->buf->i > msg->next)
7026 chn->flags |= CF_WAKE_WRITE;
7027 goto missing_data_or_waiting;
7028 }
7029
7030 /* nothing left to forward for this chunk*/
7031 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
7032 /* fall through for HTTP_MSG_CHUNK_CRLF */
7033
7034 case HTTP_MSG_CHUNK_CRLF:
7035 /* we want the CRLF after the data */
7036 ret = http_skip_chunk_crlf(msg);
7037 if (ret == 0)
7038 goto missing_data_or_waiting;
7039 if (ret < 0)
7040 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01007041 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007042 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
7043 /* fall through for HTTP_MSG_CHUNK_SIZE */
7044
7045 case HTTP_MSG_CHUNK_SIZE:
7046 /* read the chunk size and assign it to ->chunk_len,
7047 * then set ->next to point to the body and switch to
7048 * DATA or TRAILERS state.
7049 */
7050 ret = http_parse_chunk_size(msg);
7051 if (ret == 0)
7052 goto missing_data_or_waiting;
7053 if (ret < 0)
7054 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01007055 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007056 if (msg->chunk_len) {
7057 msg->msg_state = HTTP_MSG_DATA;
7058 goto switch_states;
7059 }
7060 msg->msg_state = HTTP_MSG_TRAILERS;
7061 /* fall through for HTTP_MSG_TRAILERS */
7062
7063 case HTTP_MSG_TRAILERS:
7064 ret = http_forward_trailers(msg);
7065 if (ret < 0)
7066 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01007067 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
7068 /* default_ret */ 1,
7069 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007070 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007071 if (!ret)
7072 goto missing_data_or_waiting;
7073 break;
7074
7075 case HTTP_MSG_ENDING:
7076 goto ending;
7077
7078 default:
7079 /* This should no happen in this function */
7080 goto error;
7081 }
7082
7083 msg->msg_state = HTTP_MSG_ENDING;
7084 ending:
7085 /* we may have some pending data starting at res->buf->p such as a last
7086 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01007087 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007088 /* default_ret */ msg->next,
7089 /* on_error */ goto error);
7090 b_adv(chn->buf, ret);
7091 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007092 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
7093 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02007094 if (msg->next)
7095 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007096
Christopher Fauletda02e172015-12-04 09:25:05 +01007097 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007098 /* default_ret */ 1,
7099 /* on_error */ goto error,
7100 /* on_wait */ goto waiting);
7101 msg->msg_state = HTTP_MSG_DONE;
7102 return 1;
7103
7104 missing_data_or_waiting:
7105 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01007106 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007107 /* default_ret */ msg->next,
7108 /* on_error */ goto error);
7109 b_adv(chn->buf, ret);
7110 msg->next -= ret;
7111 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
7112 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01007113 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007114 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02007115 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007116 return 0;
7117
7118 chunk_parsing_error:
7119 if (msg->err_pos >= 0) {
7120 if (chn->flags & CF_ISRESP)
7121 http_capture_bad_message(&s->be->invalid_rep, s, msg,
7122 msg->msg_state, strm_fe(s));
7123 else
7124 http_capture_bad_message(&strm_fe(s)->invalid_req, s,
7125 msg, msg->msg_state, s->be);
7126 }
7127 error:
7128 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007129}
7130
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007131
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007132/* Iterate the same filter through all request headers.
7133 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007134 * Since it can manage the switch to another backend, it updates the per-proxy
7135 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007136 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007137int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007138{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007139 char *cur_ptr, *cur_end, *cur_next;
7140 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007141 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007142 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007143 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007144
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007145 last_hdr = 0;
7146
Willy Tarreau9b28e032012-10-12 23:49:43 +02007147 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007148 old_idx = 0;
7149
7150 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007151 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007152 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007153 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007154 (exp->action == ACT_ALLOW ||
7155 exp->action == ACT_DENY ||
7156 exp->action == ACT_TARPIT))
7157 return 0;
7158
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007159 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007160 if (!cur_idx)
7161 break;
7162
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007163 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007164 cur_ptr = cur_next;
7165 cur_end = cur_ptr + cur_hdr->len;
7166 cur_next = cur_end + cur_hdr->cr + 1;
7167
7168 /* Now we have one header between cur_ptr and cur_end,
7169 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007170 */
7171
Willy Tarreau15a53a42015-01-21 13:39:42 +01007172 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007173 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007174 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007175 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007176 last_hdr = 1;
7177 break;
7178
7179 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007180 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007181 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007182 break;
7183
7184 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007185 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007186 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007187 break;
7188
7189 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007190 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7191 if (trash.len < 0)
7192 return -1;
7193
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007194 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007195 /* FIXME: if the user adds a newline in the replacement, the
7196 * index will not be recalculated for now, and the new line
7197 * will not be counted as a new header.
7198 */
7199
7200 cur_end += delta;
7201 cur_next += delta;
7202 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007203 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007204 break;
7205
7206 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007207 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007208 cur_next += delta;
7209
Willy Tarreaufa355d42009-11-29 18:12:29 +01007210 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007211 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7212 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007213 cur_hdr->len = 0;
7214 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007215 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007216 break;
7217
7218 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007219 }
7220
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007221 /* keep the link from this header to next one in case of later
7222 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007223 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007224 old_idx = cur_idx;
7225 }
7226 return 0;
7227}
7228
7229
7230/* Apply the filter to the request line.
7231 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7232 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007233 * Since it can manage the switch to another backend, it updates the per-proxy
7234 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007235 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007236int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007237{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007238 char *cur_ptr, *cur_end;
7239 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007240 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007241 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007242
Willy Tarreau3d300592007-03-18 18:34:41 +01007243 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007244 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007245 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007246 (exp->action == ACT_ALLOW ||
7247 exp->action == ACT_DENY ||
7248 exp->action == ACT_TARPIT))
7249 return 0;
7250 else if (exp->action == ACT_REMOVE)
7251 return 0;
7252
7253 done = 0;
7254
Willy Tarreau9b28e032012-10-12 23:49:43 +02007255 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007256 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007257
7258 /* Now we have the request line between cur_ptr and cur_end */
7259
Willy Tarreau15a53a42015-01-21 13:39:42 +01007260 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007261 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007262 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007263 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007264 done = 1;
7265 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007266
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007267 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007268 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007269 done = 1;
7270 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007271
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007272 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007273 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007274 done = 1;
7275 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007276
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007277 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007278 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7279 if (trash.len < 0)
7280 return -1;
7281
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007282 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007283 /* FIXME: if the user adds a newline in the replacement, the
7284 * index will not be recalculated for now, and the new line
7285 * will not be counted as a new header.
7286 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007287
Willy Tarreaufa355d42009-11-29 18:12:29 +01007288 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007289 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007290 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007291 HTTP_MSG_RQMETH,
7292 cur_ptr, cur_end + 1,
7293 NULL, NULL);
7294 if (unlikely(!cur_end))
7295 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007296
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007297 /* we have a full request and we know that we have either a CR
7298 * or an LF at <ptr>.
7299 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007300 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7301 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007302 /* there is no point trying this regex on headers */
7303 return 1;
7304 }
7305 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007306 return done;
7307}
Willy Tarreau97de6242006-12-27 17:18:38 +01007308
Willy Tarreau58f10d72006-12-04 02:26:12 +01007309
Willy Tarreau58f10d72006-12-04 02:26:12 +01007310
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007311/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007312 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007313 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007314 * unparsable request. Since it can manage the switch to another backend, it
7315 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007316 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007317int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007318{
Willy Tarreau192252e2015-04-04 01:47:55 +02007319 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007320 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007321 struct hdr_exp *exp;
7322
7323 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007324 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007325
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007326 /*
7327 * The interleaving of transformations and verdicts
7328 * makes it difficult to decide to continue or stop
7329 * the evaluation.
7330 */
7331
Willy Tarreau6c123b12010-01-28 20:22:06 +01007332 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7333 break;
7334
Willy Tarreau3d300592007-03-18 18:34:41 +01007335 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007336 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007337 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007338 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007339
7340 /* if this filter had a condition, evaluate it now and skip to
7341 * next filter if the condition does not match.
7342 */
7343 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007344 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007345 ret = acl_pass(ret);
7346 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7347 ret = !ret;
7348
7349 if (!ret)
7350 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007351 }
7352
7353 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007354 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007355 if (unlikely(ret < 0))
7356 return -1;
7357
7358 if (likely(ret == 0)) {
7359 /* The filter did not match the request, it can be
7360 * iterated through all headers.
7361 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007362 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7363 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007364 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007365 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007366 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007367}
7368
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007369
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007370/* Find the end of a cookie value contained between <s> and <e>. It works the
7371 * same way as with headers above except that the semi-colon also ends a token.
7372 * See RFC2965 for more information. Note that it requires a valid header to
7373 * return a valid result.
7374 */
7375char *find_cookie_value_end(char *s, const char *e)
7376{
7377 int quoted, qdpair;
7378
7379 quoted = qdpair = 0;
7380 for (; s < e; s++) {
7381 if (qdpair) qdpair = 0;
7382 else if (quoted) {
7383 if (*s == '\\') qdpair = 1;
7384 else if (*s == '"') quoted = 0;
7385 }
7386 else if (*s == '"') quoted = 1;
7387 else if (*s == ',' || *s == ';') return s;
7388 }
7389 return s;
7390}
7391
7392/* Delete a value in a header between delimiters <from> and <next> in buffer
7393 * <buf>. The number of characters displaced is returned, and the pointer to
7394 * the first delimiter is updated if required. The function tries as much as
7395 * possible to respect the following principles :
7396 * - replace <from> delimiter by the <next> one unless <from> points to a
7397 * colon, in which case <next> is simply removed
7398 * - set exactly one space character after the new first delimiter, unless
7399 * there are not enough characters in the block being moved to do so.
7400 * - remove unneeded spaces before the previous delimiter and after the new
7401 * one.
7402 *
7403 * It is the caller's responsibility to ensure that :
7404 * - <from> points to a valid delimiter or the colon ;
7405 * - <next> points to a valid delimiter or the final CR/LF ;
7406 * - there are non-space chars before <from> ;
7407 * - there is a CR/LF at or after <next>.
7408 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007409int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007410{
7411 char *prev = *from;
7412
7413 if (*prev == ':') {
7414 /* We're removing the first value, preserve the colon and add a
7415 * space if possible.
7416 */
Willy Tarreau2235b262016-11-05 15:50:20 +01007417 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007418 next++;
7419 prev++;
7420 if (prev < next)
7421 *prev++ = ' ';
7422
Willy Tarreau2235b262016-11-05 15:50:20 +01007423 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007424 next++;
7425 } else {
7426 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01007427 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007428 prev--;
7429 *from = prev;
7430
7431 /* copy the delimiter and if possible a space if we're
7432 * not at the end of the line.
7433 */
Willy Tarreau2235b262016-11-05 15:50:20 +01007434 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007435 *prev++ = *next++;
7436 if (prev + 1 < next)
7437 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01007438 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007439 next++;
7440 }
7441 }
7442 return buffer_replace2(buf, prev, next, NULL, 0);
7443}
7444
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007445/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007446 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007447 * desirable to call it only when needed. This code is quite complex because
7448 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7449 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007450 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007451void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007452{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007453 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007454 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007455 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007456 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007457 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7458 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007459
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007460 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007461 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007462 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007463
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007464 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007465 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007466 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007467
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007468 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007469 hdr_beg = hdr_next;
7470 hdr_end = hdr_beg + cur_hdr->len;
7471 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007472
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007473 /* We have one full header between hdr_beg and hdr_end, and the
7474 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007475 * "Cookie:" headers.
7476 */
7477
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007478 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007479 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007480 old_idx = cur_idx;
7481 continue;
7482 }
7483
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007484 del_from = NULL; /* nothing to be deleted */
7485 preserve_hdr = 0; /* assume we may kill the whole header */
7486
Willy Tarreau58f10d72006-12-04 02:26:12 +01007487 /* Now look for cookies. Conforming to RFC2109, we have to support
7488 * attributes whose name begin with a '$', and associate them with
7489 * the right cookie, if we want to delete this cookie.
7490 * So there are 3 cases for each cookie read :
7491 * 1) it's a special attribute, beginning with a '$' : ignore it.
7492 * 2) it's a server id cookie that we *MAY* want to delete : save
7493 * some pointers on it (last semi-colon, beginning of cookie...)
7494 * 3) it's an application cookie : we *MAY* have to delete a previous
7495 * "special" cookie.
7496 * At the end of loop, if a "special" cookie remains, we may have to
7497 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007498 * *MUST* delete it.
7499 *
7500 * Note: RFC2965 is unclear about the processing of spaces around
7501 * the equal sign in the ATTR=VALUE form. A careful inspection of
7502 * the RFC explicitly allows spaces before it, and not within the
7503 * tokens (attrs or values). An inspection of RFC2109 allows that
7504 * too but section 10.1.3 lets one think that spaces may be allowed
7505 * after the equal sign too, resulting in some (rare) buggy
7506 * implementations trying to do that. So let's do what servers do.
7507 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7508 * allowed quoted strings in values, with any possible character
7509 * after a backslash, including control chars and delimitors, which
7510 * causes parsing to become ambiguous. Browsers also allow spaces
7511 * within values even without quotes.
7512 *
7513 * We have to keep multiple pointers in order to support cookie
7514 * removal at the beginning, middle or end of header without
7515 * corrupting the header. All of these headers are valid :
7516 *
7517 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7518 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7519 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7520 * | | | | | | | | |
7521 * | | | | | | | | hdr_end <--+
7522 * | | | | | | | +--> next
7523 * | | | | | | +----> val_end
7524 * | | | | | +-----------> val_beg
7525 * | | | | +--------------> equal
7526 * | | | +----------------> att_end
7527 * | | +---------------------> att_beg
7528 * | +--------------------------> prev
7529 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007530 */
7531
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007532 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7533 /* Iterate through all cookies on this line */
7534
7535 /* find att_beg */
7536 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007537 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007538 att_beg++;
7539
7540 /* find att_end : this is the first character after the last non
7541 * space before the equal. It may be equal to hdr_end.
7542 */
7543 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007544
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007545 while (equal < hdr_end) {
7546 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007547 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007548 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007549 continue;
7550 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007551 }
7552
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007553 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7554 * is between <att_beg> and <equal>, both may be identical.
7555 */
7556
7557 /* look for end of cookie if there is an equal sign */
7558 if (equal < hdr_end && *equal == '=') {
7559 /* look for the beginning of the value */
7560 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007561 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007562 val_beg++;
7563
7564 /* find the end of the value, respecting quotes */
7565 next = find_cookie_value_end(val_beg, hdr_end);
7566
7567 /* make val_end point to the first white space or delimitor after the value */
7568 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007569 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007570 val_end--;
7571 } else {
7572 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007573 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007574
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007575 /* We have nothing to do with attributes beginning with '$'. However,
7576 * they will automatically be removed if a header before them is removed,
7577 * since they're supposed to be linked together.
7578 */
7579 if (*att_beg == '$')
7580 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007581
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007582 /* Ignore cookies with no equal sign */
7583 if (equal == next) {
7584 /* This is not our cookie, so we must preserve it. But if we already
7585 * scheduled another cookie for removal, we cannot remove the
7586 * complete header, but we can remove the previous block itself.
7587 */
7588 preserve_hdr = 1;
7589 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007590 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007591 val_end += delta;
7592 next += delta;
7593 hdr_end += delta;
7594 hdr_next += delta;
7595 cur_hdr->len += delta;
7596 http_msg_move_end(&txn->req, delta);
7597 prev = del_from;
7598 del_from = NULL;
7599 }
7600 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007601 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007602
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007603 /* if there are spaces around the equal sign, we need to
7604 * strip them otherwise we'll get trouble for cookie captures,
7605 * or even for rewrites. Since this happens extremely rarely,
7606 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007607 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007608 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7609 int stripped_before = 0;
7610 int stripped_after = 0;
7611
7612 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007613 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007614 equal += stripped_before;
7615 val_beg += stripped_before;
7616 }
7617
7618 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007619 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007620 val_beg += stripped_after;
7621 stripped_before += stripped_after;
7622 }
7623
7624 val_end += stripped_before;
7625 next += stripped_before;
7626 hdr_end += stripped_before;
7627 hdr_next += stripped_before;
7628 cur_hdr->len += stripped_before;
7629 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007630 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007631 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007632
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007633 /* First, let's see if we want to capture this cookie. We check
7634 * that we don't already have a client side cookie, because we
7635 * can only capture one. Also as an optimisation, we ignore
7636 * cookies shorter than the declared name.
7637 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007638 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7639 (val_end - att_beg >= sess->fe->capture_namelen) &&
7640 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007641 int log_len = val_end - att_beg;
7642
7643 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7644 Alert("HTTP logging : out of memory.\n");
7645 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007646 if (log_len > sess->fe->capture_len)
7647 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007648 memcpy(txn->cli_cookie, att_beg, log_len);
7649 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007650 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007651 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007652
Willy Tarreaubca99692010-10-06 19:25:55 +02007653 /* Persistence cookies in passive, rewrite or insert mode have the
7654 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007655 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007656 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007657 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007658 * For cookies in prefix mode, the form is :
7659 *
7660 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007661 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007662 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7663 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7664 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007665 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007666
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007667 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7668 * have the server ID between val_beg and delim, and the original cookie between
7669 * delim+1 and val_end. Otherwise, delim==val_end :
7670 *
7671 * Cookie: NAME=SRV; # in all but prefix modes
7672 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7673 * | || || | |+-> next
7674 * | || || | +--> val_end
7675 * | || || +---------> delim
7676 * | || |+------------> val_beg
7677 * | || +-------------> att_end = equal
7678 * | |+-----------------> att_beg
7679 * | +------------------> prev
7680 * +-------------------------> hdr_beg
7681 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007682
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007683 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007684 for (delim = val_beg; delim < val_end; delim++)
7685 if (*delim == COOKIE_DELIM)
7686 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007687 } else {
7688 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007689 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007690 /* Now check if the cookie contains a date field, which would
7691 * appear after a vertical bar ('|') just after the server name
7692 * and before the delimiter.
7693 */
7694 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7695 if (vbar1) {
7696 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007697 * right is the last seen date. It is a base64 encoded
7698 * 30-bit value representing the UNIX date since the
7699 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007700 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007701 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007702 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007703 if (val_end - vbar1 >= 5) {
7704 val = b64tos30(vbar1);
7705 if (val > 0)
7706 txn->cookie_last_date = val << 2;
7707 }
7708 /* look for a second vertical bar */
7709 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7710 if (vbar1 && (val_end - vbar1 > 5)) {
7711 val = b64tos30(vbar1 + 1);
7712 if (val > 0)
7713 txn->cookie_first_date = val << 2;
7714 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007715 }
7716 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007717
Willy Tarreauf64d1412010-10-07 20:06:11 +02007718 /* if the cookie has an expiration date and the proxy wants to check
7719 * it, then we do that now. We first check if the cookie is too old,
7720 * then only if it has expired. We detect strict overflow because the
7721 * time resolution here is not great (4 seconds). Cookies with dates
7722 * in the future are ignored if their offset is beyond one day. This
7723 * allows an admin to fix timezone issues without expiring everyone
7724 * and at the same time avoids keeping unwanted side effects for too
7725 * long.
7726 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007727 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7728 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007729 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007730 txn->flags &= ~TX_CK_MASK;
7731 txn->flags |= TX_CK_OLD;
7732 delim = val_beg; // let's pretend we have not found the cookie
7733 txn->cookie_first_date = 0;
7734 txn->cookie_last_date = 0;
7735 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007736 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7737 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007738 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007739 txn->flags &= ~TX_CK_MASK;
7740 txn->flags |= TX_CK_EXPIRED;
7741 delim = val_beg; // let's pretend we have not found the cookie
7742 txn->cookie_first_date = 0;
7743 txn->cookie_last_date = 0;
7744 }
7745
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007746 /* Here, we'll look for the first running server which supports the cookie.
7747 * This allows to share a same cookie between several servers, for example
7748 * to dedicate backup servers to specific servers only.
7749 * However, to prevent clients from sticking to cookie-less backup server
7750 * when they have incidentely learned an empty cookie, we simply ignore
7751 * empty cookies and mark them as invalid.
7752 * The same behaviour is applied when persistence must be ignored.
7753 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007754 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007755 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007756
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007757 while (srv) {
7758 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7759 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007760 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007761 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007762 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007763 /* we found the server and we can use it */
7764 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007765 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007766 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007767 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007768 break;
7769 } else {
7770 /* we found a server, but it's down,
7771 * mark it as such and go on in case
7772 * another one is available.
7773 */
7774 txn->flags &= ~TX_CK_MASK;
7775 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007776 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007777 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007778 srv = srv->next;
7779 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007780
Willy Tarreauf64d1412010-10-07 20:06:11 +02007781 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007782 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007783 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007784 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007785 txn->flags |= TX_CK_UNUSED;
7786 else
7787 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007788 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007789
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007790 /* depending on the cookie mode, we may have to either :
7791 * - delete the complete cookie if we're in insert+indirect mode, so that
7792 * the server never sees it ;
7793 * - remove the server id from the cookie value, and tag the cookie as an
7794 * application cookie so that it does not get accidentely removed later,
7795 * if we're in cookie prefix mode
7796 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007797 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007798 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007799
Willy Tarreau9b28e032012-10-12 23:49:43 +02007800 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007801 val_end += delta;
7802 next += delta;
7803 hdr_end += delta;
7804 hdr_next += delta;
7805 cur_hdr->len += delta;
7806 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007807
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007808 del_from = NULL;
7809 preserve_hdr = 1; /* we want to keep this cookie */
7810 }
7811 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007812 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007813 del_from = prev;
7814 }
7815 } else {
7816 /* This is not our cookie, so we must preserve it. But if we already
7817 * scheduled another cookie for removal, we cannot remove the
7818 * complete header, but we can remove the previous block itself.
7819 */
7820 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007821
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007822 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007823 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007824 if (att_beg >= del_from)
7825 att_beg += delta;
7826 if (att_end >= del_from)
7827 att_end += delta;
7828 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007829 val_end += delta;
7830 next += delta;
7831 hdr_end += delta;
7832 hdr_next += delta;
7833 cur_hdr->len += delta;
7834 http_msg_move_end(&txn->req, delta);
7835 prev = del_from;
7836 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007837 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007838 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007839
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007840 /* continue with next cookie on this header line */
7841 att_beg = next;
7842 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007843
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007844 /* There are no more cookies on this line.
7845 * We may still have one (or several) marked for deletion at the
7846 * end of the line. We must do this now in two ways :
7847 * - if some cookies must be preserved, we only delete from the
7848 * mark to the end of line ;
7849 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007850 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007851 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007852 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007853 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007854 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007855 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007856 cur_hdr->len += delta;
7857 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007858 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007859
7860 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007861 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7862 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007863 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007864 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007865 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007866 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007867 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007868 }
7869
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007870 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007871 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007872 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007873}
7874
7875
Willy Tarreaua15645d2007-03-18 16:22:39 +01007876/* Iterate the same filter through all response headers contained in <rtr>.
7877 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7878 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007879int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007880{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007881 char *cur_ptr, *cur_end, *cur_next;
7882 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007883 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007884 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007885 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007886
7887 last_hdr = 0;
7888
Willy Tarreau9b28e032012-10-12 23:49:43 +02007889 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007890 old_idx = 0;
7891
7892 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007893 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007894 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007895 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007896 (exp->action == ACT_ALLOW ||
7897 exp->action == ACT_DENY))
7898 return 0;
7899
7900 cur_idx = txn->hdr_idx.v[old_idx].next;
7901 if (!cur_idx)
7902 break;
7903
7904 cur_hdr = &txn->hdr_idx.v[cur_idx];
7905 cur_ptr = cur_next;
7906 cur_end = cur_ptr + cur_hdr->len;
7907 cur_next = cur_end + cur_hdr->cr + 1;
7908
7909 /* Now we have one header between cur_ptr and cur_end,
7910 * and the next header starts at cur_next.
7911 */
7912
Willy Tarreau15a53a42015-01-21 13:39:42 +01007913 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007914 switch (exp->action) {
7915 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007916 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007917 last_hdr = 1;
7918 break;
7919
7920 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007921 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007922 last_hdr = 1;
7923 break;
7924
7925 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007926 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7927 if (trash.len < 0)
7928 return -1;
7929
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007930 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007931 /* FIXME: if the user adds a newline in the replacement, the
7932 * index will not be recalculated for now, and the new line
7933 * will not be counted as a new header.
7934 */
7935
7936 cur_end += delta;
7937 cur_next += delta;
7938 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007939 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007940 break;
7941
7942 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007943 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007944 cur_next += delta;
7945
Willy Tarreaufa355d42009-11-29 18:12:29 +01007946 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007947 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7948 txn->hdr_idx.used--;
7949 cur_hdr->len = 0;
7950 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007951 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007952 break;
7953
7954 }
7955 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007956
7957 /* keep the link from this header to next one in case of later
7958 * removal of next header.
7959 */
7960 old_idx = cur_idx;
7961 }
7962 return 0;
7963}
7964
7965
7966/* Apply the filter to the status line in the response buffer <rtr>.
7967 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7968 * or -1 if a replacement resulted in an invalid status line.
7969 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007970int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007971{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007972 char *cur_ptr, *cur_end;
7973 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007974 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007975 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007976
7977
Willy Tarreau3d300592007-03-18 18:34:41 +01007978 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007979 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007980 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007981 (exp->action == ACT_ALLOW ||
7982 exp->action == ACT_DENY))
7983 return 0;
7984 else if (exp->action == ACT_REMOVE)
7985 return 0;
7986
7987 done = 0;
7988
Willy Tarreau9b28e032012-10-12 23:49:43 +02007989 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007990 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007991
7992 /* Now we have the status line between cur_ptr and cur_end */
7993
Willy Tarreau15a53a42015-01-21 13:39:42 +01007994 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007995 switch (exp->action) {
7996 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007997 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007998 done = 1;
7999 break;
8000
8001 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008002 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008003 done = 1;
8004 break;
8005
8006 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008007 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8008 if (trash.len < 0)
8009 return -1;
8010
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008011 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008012 /* FIXME: if the user adds a newline in the replacement, the
8013 * index will not be recalculated for now, and the new line
8014 * will not be counted as a new header.
8015 */
8016
Willy Tarreaufa355d42009-11-29 18:12:29 +01008017 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008018 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008019 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008020 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008021 cur_ptr, cur_end + 1,
8022 NULL, NULL);
8023 if (unlikely(!cur_end))
8024 return -1;
8025
8026 /* we have a full respnse and we know that we have either a CR
8027 * or an LF at <ptr>.
8028 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008029 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008030 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008031 /* there is no point trying this regex on headers */
8032 return 1;
8033 }
8034 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008035 return done;
8036}
8037
8038
8039
8040/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008041 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008042 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8043 * unparsable response.
8044 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008045int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008046{
Willy Tarreau192252e2015-04-04 01:47:55 +02008047 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008048 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008049 struct hdr_exp *exp;
8050
8051 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008052 int ret;
8053
8054 /*
8055 * The interleaving of transformations and verdicts
8056 * makes it difficult to decide to continue or stop
8057 * the evaluation.
8058 */
8059
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008060 if (txn->flags & TX_SVDENY)
8061 break;
8062
Willy Tarreau3d300592007-03-18 18:34:41 +01008063 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008064 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8065 exp->action == ACT_PASS)) {
8066 exp = exp->next;
8067 continue;
8068 }
8069
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008070 /* if this filter had a condition, evaluate it now and skip to
8071 * next filter if the condition does not match.
8072 */
8073 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008074 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008075 ret = acl_pass(ret);
8076 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8077 ret = !ret;
8078 if (!ret)
8079 continue;
8080 }
8081
Willy Tarreaua15645d2007-03-18 16:22:39 +01008082 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008083 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008084 if (unlikely(ret < 0))
8085 return -1;
8086
8087 if (likely(ret == 0)) {
8088 /* The filter did not match the response, it can be
8089 * iterated through all headers.
8090 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008091 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8092 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008093 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008094 }
8095 return 0;
8096}
8097
8098
Willy Tarreaua15645d2007-03-18 16:22:39 +01008099/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008100 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008101 * desirable to call it only when needed. This function is also used when we
8102 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008103 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008104void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008105{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008106 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008107 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008108 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008109 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008110 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008111 char *hdr_beg, *hdr_end, *hdr_next;
8112 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008113
Willy Tarreaua15645d2007-03-18 16:22:39 +01008114 /* Iterate through the headers.
8115 * we start with the start line.
8116 */
8117 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008118 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008119
8120 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8121 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008122 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008123
8124 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008125 hdr_beg = hdr_next;
8126 hdr_end = hdr_beg + cur_hdr->len;
8127 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008128
Willy Tarreau24581ba2010-08-31 22:39:35 +02008129 /* We have one full header between hdr_beg and hdr_end, and the
8130 * next header starts at hdr_next. We're only interested in
8131 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008132 */
8133
Willy Tarreau24581ba2010-08-31 22:39:35 +02008134 is_cookie2 = 0;
8135 prev = hdr_beg + 10;
8136 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008137 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008138 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8139 if (!val) {
8140 old_idx = cur_idx;
8141 continue;
8142 }
8143 is_cookie2 = 1;
8144 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008145 }
8146
Willy Tarreau24581ba2010-08-31 22:39:35 +02008147 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8148 * <prev> points to the colon.
8149 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008150 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008151
Willy Tarreau24581ba2010-08-31 22:39:35 +02008152 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8153 * check-cache is enabled) and we are not interested in checking
8154 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008155 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02008156 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008157 return;
8158
Willy Tarreau24581ba2010-08-31 22:39:35 +02008159 /* OK so now we know we have to process this response cookie.
8160 * The format of the Set-Cookie header is slightly different
8161 * from the format of the Cookie header in that it does not
8162 * support the comma as a cookie delimiter (thus the header
8163 * cannot be folded) because the Expires attribute described in
8164 * the original Netscape's spec may contain an unquoted date
8165 * with a comma inside. We have to live with this because
8166 * many browsers don't support Max-Age and some browsers don't
8167 * support quoted strings. However the Set-Cookie2 header is
8168 * clean.
8169 *
8170 * We have to keep multiple pointers in order to support cookie
8171 * removal at the beginning, middle or end of header without
8172 * corrupting the header (in case of set-cookie2). A special
8173 * pointer, <scav> points to the beginning of the set-cookie-av
8174 * fields after the first semi-colon. The <next> pointer points
8175 * either to the end of line (set-cookie) or next unquoted comma
8176 * (set-cookie2). All of these headers are valid :
8177 *
8178 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8179 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8180 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8181 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8182 * | | | | | | | | | |
8183 * | | | | | | | | +-> next hdr_end <--+
8184 * | | | | | | | +------------> scav
8185 * | | | | | | +--------------> val_end
8186 * | | | | | +--------------------> val_beg
8187 * | | | | +----------------------> equal
8188 * | | | +------------------------> att_end
8189 * | | +----------------------------> att_beg
8190 * | +------------------------------> prev
8191 * +-----------------------------------------> hdr_beg
8192 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008193
Willy Tarreau24581ba2010-08-31 22:39:35 +02008194 for (; prev < hdr_end; prev = next) {
8195 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008196
Willy Tarreau24581ba2010-08-31 22:39:35 +02008197 /* find att_beg */
8198 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01008199 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008200 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008201
Willy Tarreau24581ba2010-08-31 22:39:35 +02008202 /* find att_end : this is the first character after the last non
8203 * space before the equal. It may be equal to hdr_end.
8204 */
8205 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008206
Willy Tarreau24581ba2010-08-31 22:39:35 +02008207 while (equal < hdr_end) {
8208 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8209 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01008210 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008211 continue;
8212 att_end = equal;
8213 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008214
Willy Tarreau24581ba2010-08-31 22:39:35 +02008215 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8216 * is between <att_beg> and <equal>, both may be identical.
8217 */
8218
8219 /* look for end of cookie if there is an equal sign */
8220 if (equal < hdr_end && *equal == '=') {
8221 /* look for the beginning of the value */
8222 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01008223 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008224 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008225
Willy Tarreau24581ba2010-08-31 22:39:35 +02008226 /* find the end of the value, respecting quotes */
8227 next = find_cookie_value_end(val_beg, hdr_end);
8228
8229 /* make val_end point to the first white space or delimitor after the value */
8230 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01008231 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008232 val_end--;
8233 } else {
8234 /* <equal> points to next comma, semi-colon or EOL */
8235 val_beg = val_end = next = equal;
8236 }
8237
8238 if (next < hdr_end) {
8239 /* Set-Cookie2 supports multiple cookies, and <next> points to
8240 * a colon or semi-colon before the end. So skip all attr-value
8241 * pairs and look for the next comma. For Set-Cookie, since
8242 * commas are permitted in values, skip to the end.
8243 */
8244 if (is_cookie2)
8245 next = find_hdr_value_end(next, hdr_end);
8246 else
8247 next = hdr_end;
8248 }
8249
8250 /* Now everything is as on the diagram above */
8251
8252 /* Ignore cookies with no equal sign */
8253 if (equal == val_end)
8254 continue;
8255
8256 /* If there are spaces around the equal sign, we need to
8257 * strip them otherwise we'll get trouble for cookie captures,
8258 * or even for rewrites. Since this happens extremely rarely,
8259 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008260 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008261 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8262 int stripped_before = 0;
8263 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008264
Willy Tarreau24581ba2010-08-31 22:39:35 +02008265 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008266 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008267 equal += stripped_before;
8268 val_beg += stripped_before;
8269 }
8270
8271 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008272 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008273 val_beg += stripped_after;
8274 stripped_before += stripped_after;
8275 }
8276
8277 val_end += stripped_before;
8278 next += stripped_before;
8279 hdr_end += stripped_before;
8280 hdr_next += stripped_before;
8281 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008282 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008283 }
8284
8285 /* First, let's see if we want to capture this cookie. We check
8286 * that we don't already have a server side cookie, because we
8287 * can only capture one. Also as an optimisation, we ignore
8288 * cookies shorter than the declared name.
8289 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008290 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008291 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008292 (val_end - att_beg >= sess->fe->capture_namelen) &&
8293 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008294 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008295 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008296 Alert("HTTP logging : out of memory.\n");
8297 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008298 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008299 if (log_len > sess->fe->capture_len)
8300 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008301 memcpy(txn->srv_cookie, att_beg, log_len);
8302 txn->srv_cookie[log_len] = 0;
8303 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008304 }
8305
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008306 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008307 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008308 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008309 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8310 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008311 /* assume passive cookie by default */
8312 txn->flags &= ~TX_SCK_MASK;
8313 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008314
8315 /* If the cookie is in insert mode on a known server, we'll delete
8316 * this occurrence because we'll insert another one later.
8317 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008318 * a direct access.
8319 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008320 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008321 /* The "preserve" flag was set, we don't want to touch the
8322 * server's cookie.
8323 */
8324 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008325 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008326 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008327 /* this cookie must be deleted */
8328 if (*prev == ':' && next == hdr_end) {
8329 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008330 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008331 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8332 txn->hdr_idx.used--;
8333 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008334 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008335 hdr_next += delta;
8336 http_msg_move_end(&txn->rsp, delta);
8337 /* note: while both invalid now, <next> and <hdr_end>
8338 * are still equal, so the for() will stop as expected.
8339 */
8340 } else {
8341 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008342 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008343 next = prev;
8344 hdr_end += delta;
8345 hdr_next += delta;
8346 cur_hdr->len += delta;
8347 http_msg_move_end(&txn->rsp, delta);
8348 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008349 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008350 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008351 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008352 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008353 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008354 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008355 * with this server since we know it.
8356 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008357 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008358 next += delta;
8359 hdr_end += delta;
8360 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008361 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008362 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008363
Willy Tarreauf1348312010-10-07 15:54:11 +02008364 txn->flags &= ~TX_SCK_MASK;
8365 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008366 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008367 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008368 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008369 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008370 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008371 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008372 next += delta;
8373 hdr_end += delta;
8374 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008375 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008376 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008377
Willy Tarreau827aee92011-03-10 16:55:02 +01008378 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008379 txn->flags &= ~TX_SCK_MASK;
8380 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008381 }
8382 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008383 /* that's done for this cookie, check the next one on the same
8384 * line when next != hdr_end (only if is_cookie2).
8385 */
8386 }
8387 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008388 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008389 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008390}
8391
8392
Willy Tarreaua15645d2007-03-18 16:22:39 +01008393/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008394 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008395 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008396void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008397{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008398 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008399 char *p1, *p2;
8400
8401 char *cur_ptr, *cur_end, *cur_next;
8402 int cur_idx;
8403
Willy Tarreau5df51872007-11-25 16:20:08 +01008404 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008405 return;
8406
8407 /* Iterate through the headers.
8408 * we start with the start line.
8409 */
8410 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008411 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008412
8413 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8414 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008415 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008416
8417 cur_hdr = &txn->hdr_idx.v[cur_idx];
8418 cur_ptr = cur_next;
8419 cur_end = cur_ptr + cur_hdr->len;
8420 cur_next = cur_end + cur_hdr->cr + 1;
8421
8422 /* We have one full header between cur_ptr and cur_end, and the
8423 * next header starts at cur_next. We're only interested in
8424 * "Cookie:" headers.
8425 */
8426
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008427 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8428 if (val) {
8429 if ((cur_end - (cur_ptr + val) >= 8) &&
8430 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8431 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8432 return;
8433 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008434 }
8435
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008436 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8437 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008438 continue;
8439
8440 /* OK, right now we know we have a cache-control header at cur_ptr */
8441
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008442 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008443
8444 if (p1 >= cur_end) /* no more info */
8445 continue;
8446
8447 /* p1 is at the beginning of the value */
8448 p2 = p1;
8449
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008450 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008451 p2++;
8452
8453 /* we have a complete value between p1 and p2 */
8454 if (p2 < cur_end && *p2 == '=') {
8455 /* we have something of the form no-cache="set-cookie" */
8456 if ((cur_end - p1 >= 21) &&
8457 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8458 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008459 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008460 continue;
8461 }
8462
8463 /* OK, so we know that either p2 points to the end of string or to a comma */
8464 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008465 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008466 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8467 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8468 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008469 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008470 return;
8471 }
8472
8473 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008474 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008475 continue;
8476 }
8477 }
8478}
8479
Willy Tarreau58f10d72006-12-04 02:26:12 +01008480
Willy Tarreaub2513902006-12-17 14:52:38 +01008481/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008482 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008483 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008484 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008485 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008486 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008487 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008488 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008489 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008490int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008491{
8492 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008493 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008494 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008495
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008496 if (!uri_auth)
8497 return 0;
8498
Cyril Bonté70be45d2010-10-12 00:14:35 +02008499 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008500 return 0;
8501
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008502 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008503 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008504 return 0;
8505
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008506 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008507 return 0;
8508
Willy Tarreaub2513902006-12-17 14:52:38 +01008509 return 1;
8510}
8511
Willy Tarreau4076a152009-04-02 15:18:36 +02008512/*
8513 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008514 * By default it tries to report the error position as msg->err_pos. However if
8515 * this one is not set, it will then report msg->next, which is the last known
8516 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008517 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008518 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008519void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008520 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008521 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008522{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008523 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008524 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008525 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008526
Willy Tarreauf3764b72016-03-31 13:45:10 +02008527 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02008528 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008529 len1 = MIN(len1, es->len);
8530 len2 = es->len - len1; /* remaining data if buffer wraps */
8531
Willy Tarreauf3764b72016-03-31 13:45:10 +02008532 if (!es->buf)
8533 es->buf = malloc(global.tune.bufsize);
8534
8535 if (es->buf) {
8536 memcpy(es->buf, chn->buf->p, len1);
8537 if (len2)
8538 memcpy(es->buf + len1, chn->buf->data, len2);
8539 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008540
Willy Tarreau4076a152009-04-02 15:18:36 +02008541 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008542 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008543 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008544 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008545
Willy Tarreau4076a152009-04-02 15:18:36 +02008546 es->when = date; // user-visible date
8547 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008548 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008549 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008550 if (objt_conn(sess->origin))
8551 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008552 else
8553 memset(&es->src, 0, sizeof(es->src));
8554
Willy Tarreau078272e2010-12-12 12:46:33 +01008555 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008556 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008557 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008558 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008559 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008560 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008561 es->b_out = chn->buf->o;
8562 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008563 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008564 es->m_clen = msg->chunk_len;
8565 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008566}
Willy Tarreaub2513902006-12-17 14:52:38 +01008567
Willy Tarreau294c4732011-12-16 21:35:50 +01008568/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8569 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8570 * performed over the whole headers. Otherwise it must contain a valid header
8571 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8572 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8573 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8574 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008575 * -1. The value fetch stops at commas, so this function is suited for use with
8576 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008577 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008578 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008579unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008580 struct hdr_idx *idx, int occ,
8581 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008582{
Willy Tarreau294c4732011-12-16 21:35:50 +01008583 struct hdr_ctx local_ctx;
8584 char *ptr_hist[MAX_HDR_HISTORY];
8585 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008586 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008587 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008588
Willy Tarreau294c4732011-12-16 21:35:50 +01008589 if (!ctx) {
8590 local_ctx.idx = 0;
8591 ctx = &local_ctx;
8592 }
8593
Willy Tarreaubce70882009-09-07 11:51:47 +02008594 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008595 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008596 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008597 occ--;
8598 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008599 *vptr = ctx->line + ctx->val;
8600 *vlen = ctx->vlen;
8601 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008602 }
8603 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008604 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008605 }
8606
8607 /* negative occurrence, we scan all the list then walk back */
8608 if (-occ > MAX_HDR_HISTORY)
8609 return 0;
8610
Willy Tarreau294c4732011-12-16 21:35:50 +01008611 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008612 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008613 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8614 len_hist[hist_ptr] = ctx->vlen;
8615 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008616 hist_ptr = 0;
8617 found++;
8618 }
8619 if (-occ > found)
8620 return 0;
8621 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008622 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8623 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8624 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008625 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008626 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008627 if (hist_ptr >= MAX_HDR_HISTORY)
8628 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008629 *vptr = ptr_hist[hist_ptr];
8630 *vlen = len_hist[hist_ptr];
8631 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008632}
8633
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008634/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8635 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8636 * performed over the whole headers. Otherwise it must contain a valid header
8637 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8638 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8639 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8640 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8641 * -1. This function differs from http_get_hdr() in that it only returns full
8642 * line header values and does not stop at commas.
8643 * The return value is 0 if nothing was found, or non-zero otherwise.
8644 */
8645unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8646 struct hdr_idx *idx, int occ,
8647 struct hdr_ctx *ctx, char **vptr, int *vlen)
8648{
8649 struct hdr_ctx local_ctx;
8650 char *ptr_hist[MAX_HDR_HISTORY];
8651 int len_hist[MAX_HDR_HISTORY];
8652 unsigned int hist_ptr;
8653 int found;
8654
8655 if (!ctx) {
8656 local_ctx.idx = 0;
8657 ctx = &local_ctx;
8658 }
8659
8660 if (occ >= 0) {
8661 /* search from the beginning */
8662 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8663 occ--;
8664 if (occ <= 0) {
8665 *vptr = ctx->line + ctx->val;
8666 *vlen = ctx->vlen;
8667 return 1;
8668 }
8669 }
8670 return 0;
8671 }
8672
8673 /* negative occurrence, we scan all the list then walk back */
8674 if (-occ > MAX_HDR_HISTORY)
8675 return 0;
8676
8677 found = hist_ptr = 0;
8678 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8679 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8680 len_hist[hist_ptr] = ctx->vlen;
8681 if (++hist_ptr >= MAX_HDR_HISTORY)
8682 hist_ptr = 0;
8683 found++;
8684 }
8685 if (-occ > found)
8686 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008687
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008688 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008689 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8690 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8691 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008692 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008693 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008694 if (hist_ptr >= MAX_HDR_HISTORY)
8695 hist_ptr -= MAX_HDR_HISTORY;
8696 *vptr = ptr_hist[hist_ptr];
8697 *vlen = len_hist[hist_ptr];
8698 return 1;
8699}
8700
Willy Tarreaubaaee002006-06-26 02:48:02 +02008701/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008702 * Print a debug line with a header. Always stop at the first CR or LF char,
8703 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8704 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008705 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008706void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008707{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008708 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008709 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008710
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008711 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008712 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008713 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008714 objt_conn(s->si[1].end) ? (unsigned short)objt_conn(s->si[1].end)->t.sock.fd : -1);
Willy Tarreaue92693a2012-09-24 21:13:39 +02008715
8716 for (max = 0; start + max < end; max++)
8717 if (start[max] == '\r' || start[max] == '\n')
8718 break;
8719
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008720 UBOUND(max, trash.size - trash.len - 3);
8721 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8722 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008723 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008724}
8725
Willy Tarreaueee5b512015-04-03 23:46:31 +02008726
8727/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8728 * The hdr_idx is allocated as well. In case of allocation failure, everything
8729 * allocated is freed and NULL is returned. Otherwise the new transaction is
8730 * assigned to the stream and returned.
8731 */
8732struct http_txn *http_alloc_txn(struct stream *s)
8733{
8734 struct http_txn *txn = s->txn;
8735
8736 if (txn)
8737 return txn;
8738
8739 txn = pool_alloc2(pool2_http_txn);
8740 if (!txn)
8741 return txn;
8742
8743 txn->hdr_idx.size = global.tune.max_http_hdr;
8744 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8745 if (!txn->hdr_idx.v) {
8746 pool_free2(pool2_http_txn, txn);
8747 return NULL;
8748 }
8749
8750 s->txn = txn;
8751 return txn;
8752}
8753
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008754void http_txn_reset_req(struct http_txn *txn)
8755{
8756 txn->req.flags = 0;
8757 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8758 txn->req.next = 0;
8759 txn->req.chunk_len = 0LL;
8760 txn->req.body_len = 0LL;
8761 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8762}
8763
8764void http_txn_reset_res(struct http_txn *txn)
8765{
8766 txn->rsp.flags = 0;
8767 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8768 txn->rsp.next = 0;
8769 txn->rsp.chunk_len = 0LL;
8770 txn->rsp.body_len = 0LL;
8771 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8772}
8773
Willy Tarreau0937bc42009-12-22 15:03:09 +01008774/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008775 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008776 * the required fields are properly allocated and that we only need to (re)init
8777 * them. This should be used before processing any new request.
8778 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008779void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008780{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008781 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008782 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008783
8784 txn->flags = 0;
8785 txn->status = -1;
8786
Willy Tarreauf64d1412010-10-07 20:06:11 +02008787 txn->cookie_first_date = 0;
8788 txn->cookie_last_date = 0;
8789
Willy Tarreaueee5b512015-04-03 23:46:31 +02008790 txn->srv_cookie = NULL;
8791 txn->cli_cookie = NULL;
8792 txn->uri = NULL;
8793
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008794 http_txn_reset_req(txn);
8795 http_txn_reset_res(txn);
8796
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008797 txn->req.chn = &s->req;
8798 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008799
8800 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008801
8802 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8803 if (fe->options2 & PR_O2_REQBUG_OK)
8804 txn->req.err_pos = -1; /* let buggy requests pass */
8805
Willy Tarreau0937bc42009-12-22 15:03:09 +01008806 if (txn->hdr_idx.v)
8807 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008808
8809 vars_init(&s->vars_txn, SCOPE_TXN);
8810 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008811}
8812
8813/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008814void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008815{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008816 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008817 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008818
8819 /* these ones will have been dynamically allocated */
8820 pool_free2(pool2_requri, txn->uri);
8821 pool_free2(pool2_capture, txn->cli_cookie);
8822 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008823 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008824
William Lallemanda73203e2012-03-12 12:48:57 +01008825 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008826 txn->uri = NULL;
8827 txn->srv_cookie = NULL;
8828 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008829
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008830 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008831 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008832 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008833 pool_free2(h->pool, s->req_cap[h->index]);
8834 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008835 }
8836
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008837 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008838 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008839 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008840 pool_free2(h->pool, s->res_cap[h->index]);
8841 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008842 }
8843
Willy Tarreau6204cd92016-03-10 16:33:04 +01008844 vars_prune(&s->vars_txn, s->sess, s);
8845 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008846}
8847
8848/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008849void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008850{
8851 http_end_txn(s);
8852 http_init_txn(s);
8853
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008854 /* reinitialise the current rule list pointer to NULL. We are sure that
8855 * any rulelist match the NULL pointer.
8856 */
8857 s->current_rule_list = NULL;
8858
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008859 s->be = strm_fe(s);
8860 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008861 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008862 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008863 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008864 /* re-init store persistence */
8865 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008866 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008867
Willy Tarreau0937bc42009-12-22 15:03:09 +01008868 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008869
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008870 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008871
Willy Tarreau739cfba2010-01-25 23:11:14 +01008872 /* We must trim any excess data from the response buffer, because we
8873 * may have blocked an invalid response from a server that we don't
8874 * want to accidentely forward once we disable the analysers, nor do
8875 * we want those data to come along with next response. A typical
8876 * example of such data would be from a buggy server responding to
8877 * a HEAD with some data, or sending more than the advertised
8878 * content-length.
8879 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008880 if (unlikely(s->res.buf->i))
8881 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008882
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008883 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008884 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008885
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008886 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008887 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008888
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008889 s->req.rex = TICK_ETERNITY;
8890 s->req.wex = TICK_ETERNITY;
8891 s->req.analyse_exp = TICK_ETERNITY;
8892 s->res.rex = TICK_ETERNITY;
8893 s->res.wex = TICK_ETERNITY;
8894 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008895}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008896
Sasha Pachev218f0642014-06-16 12:05:59 -06008897void free_http_res_rules(struct list *r)
8898{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008899 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008900
8901 list_for_each_entry_safe(pr, tr, r, list) {
8902 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008903 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008904 free(pr);
8905 }
8906}
8907
8908void free_http_req_rules(struct list *r)
8909{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008910 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01008911
8912 list_for_each_entry_safe(pr, tr, r, list) {
8913 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008914 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008915 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008916
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008917 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008918 free(pr);
8919 }
8920}
8921
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008922/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008923struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01008924{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008925 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008926 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008927 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008928 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008929
Vincent Bernat02779b62016-04-03 13:48:43 +02008930 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01008931 if (!rule) {
8932 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008933 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008934 }
8935
CJ Ess108b1dd2015-04-07 12:03:37 -04008936 rule->deny_status = HTTP_ERR_403;
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008937 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008938 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01008939 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02008940 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04008941 int code;
8942 int hc;
8943
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008944 rule->action = ACT_ACTION_DENY;
Willy Tarreauff011f22011-01-06 17:51:27 +01008945 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04008946 if (strcmp(args[cur_arg], "deny_status") == 0) {
8947 cur_arg++;
8948 if (!args[cur_arg]) {
8949 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
8950 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
8951 goto out_err;
8952 }
8953
8954 code = atol(args[cur_arg]);
8955 cur_arg++;
8956 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
8957 if (http_err_codes[hc] == code) {
8958 rule->deny_status = hc;
8959 break;
8960 }
8961 }
8962
8963 if (hc >= HTTP_ERR_SIZE) {
8964 Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n",
8965 file, linenum, code);
8966 }
8967 }
Willy Tarreauccbcc372012-12-27 12:37:57 +01008968 } else if (!strcmp(args[0], "tarpit")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008969 rule->action = ACT_HTTP_REQ_TARPIT;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008970 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008971 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008972 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008973 cur_arg = 1;
8974
8975 while(*args[cur_arg]) {
8976 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008977 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008978 cur_arg+=2;
8979 continue;
8980 } else
8981 break;
8982 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008983 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008984 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008985 cur_arg = 1;
8986
8987 if (!*args[cur_arg] ||
8988 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8989 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8990 file, linenum, args[0]);
8991 goto out_err;
8992 }
8993 rule->arg.nice = atoi(args[cur_arg]);
8994 if (rule->arg.nice < -1024)
8995 rule->arg.nice = -1024;
8996 else if (rule->arg.nice > 1024)
8997 rule->arg.nice = 1024;
8998 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008999 } else if (!strcmp(args[0], "set-tos")) {
9000#ifdef IP_TOS
9001 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009002 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009003 cur_arg = 1;
9004
9005 if (!*args[cur_arg] ||
9006 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9007 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9008 file, linenum, args[0]);
9009 goto out_err;
9010 }
9011
9012 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9013 if (err && *err != '\0') {
9014 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9015 file, linenum, err, args[0]);
9016 goto out_err;
9017 }
9018 cur_arg++;
9019#else
9020 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9021 goto out_err;
9022#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009023 } else if (!strcmp(args[0], "set-mark")) {
9024#ifdef SO_MARK
9025 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009026 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009027 cur_arg = 1;
9028
9029 if (!*args[cur_arg] ||
9030 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9031 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9032 file, linenum, args[0]);
9033 goto out_err;
9034 }
9035
9036 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9037 if (err && *err != '\0') {
9038 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9039 file, linenum, err, args[0]);
9040 goto out_err;
9041 }
9042 cur_arg++;
9043 global.last_checks |= LSTCHK_NETADM;
9044#else
9045 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9046 goto out_err;
9047#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009048 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009049 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009050 cur_arg = 1;
9051
9052 if (!*args[cur_arg] ||
9053 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9054 bad_log_level:
9055 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9056 file, linenum, args[0]);
9057 goto out_err;
9058 }
9059 if (strcmp(args[cur_arg], "silent") == 0)
9060 rule->arg.loglevel = -1;
9061 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9062 goto bad_log_level;
9063 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009064 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009065 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009066 cur_arg = 1;
9067
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009068 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9069 (*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 +01009070 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9071 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009072 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009073 }
9074
9075 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9076 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9077 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009078
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009079 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009080 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009081 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9082 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009083 free(proxy->conf.lfs_file);
9084 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9085 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009086 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009087 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009088 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009089 cur_arg = 1;
9090
9091 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009092 (*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 -06009093 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9094 file, linenum, args[0]);
9095 goto out_err;
9096 }
9097
9098 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9099 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9100 LIST_INIT(&rule->arg.hdr_add.fmt);
9101
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009102 error = NULL;
9103 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9104 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9105 args[cur_arg + 1], error);
9106 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009107 goto out_err;
9108 }
9109
9110 proxy->conf.args.ctx = ARGC_HRQ;
9111 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9112 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9113 file, linenum);
9114
9115 free(proxy->conf.lfs_file);
9116 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9117 proxy->conf.lfs_line = proxy->conf.args.line;
9118 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009119 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009120 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009121 cur_arg = 1;
9122
9123 if (!*args[cur_arg] ||
9124 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9125 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9126 file, linenum, args[0]);
9127 goto out_err;
9128 }
9129
9130 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9131 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9132
9133 proxy->conf.args.ctx = ARGC_HRQ;
9134 free(proxy->conf.lfs_file);
9135 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9136 proxy->conf.lfs_line = proxy->conf.args.line;
9137 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009138 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9139 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009140 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009141 struct sample_expr *expr;
9142 unsigned int where;
9143 char *err = NULL;
9144
9145 cur_arg = 1;
9146 proxy->conf.args.ctx = ARGC_TRK;
9147
9148 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9149 if (!expr) {
9150 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9151 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9152 free(err);
9153 goto out_err;
9154 }
9155
9156 where = 0;
9157 if (proxy->cap & PR_CAP_FE)
9158 where |= SMP_VAL_FE_HRQ_HDR;
9159 if (proxy->cap & PR_CAP_BE)
9160 where |= SMP_VAL_BE_HRQ_HDR;
9161
9162 if (!(expr->fetch->val & where)) {
9163 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9164 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9165 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9166 args[cur_arg-1], sample_src_names(expr->fetch->use));
9167 free(expr);
9168 goto out_err;
9169 }
9170
9171 if (strcmp(args[cur_arg], "table") == 0) {
9172 cur_arg++;
9173 if (!args[cur_arg]) {
9174 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9175 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9176 free(expr);
9177 goto out_err;
9178 }
9179 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009180 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02009181 cur_arg++;
9182 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009183 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009184 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009185 } else if (strcmp(args[0], "redirect") == 0) {
9186 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009187 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009188
Willy Tarreaube4653b2015-05-28 15:26:58 +02009189 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009190 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9191 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9192 goto out_err;
9193 }
9194
9195 /* this redirect rule might already contain a parsed condition which
9196 * we'll pass to the http-request rule.
9197 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009198 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009199 rule->arg.redir = redir;
9200 rule->cond = redir->cond;
9201 redir->cond = NULL;
9202 cur_arg = 2;
9203 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009204 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9205 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009206 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009207 /*
9208 * '+ 8' for 'add-acl('
9209 * '- 9' for 'add-acl(' + trailing ')'
9210 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009211 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009212
9213 cur_arg = 1;
9214
9215 if (!*args[cur_arg] ||
9216 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9217 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9218 file, linenum, args[0]);
9219 goto out_err;
9220 }
9221
9222 LIST_INIT(&rule->arg.map.key);
9223 proxy->conf.args.ctx = ARGC_HRQ;
9224 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9225 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9226 file, linenum);
9227 free(proxy->conf.lfs_file);
9228 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9229 proxy->conf.lfs_line = proxy->conf.args.line;
9230 cur_arg += 1;
9231 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9232 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009233 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009234 /*
9235 * '+ 8' for 'del-acl('
9236 * '- 9' for 'del-acl(' + trailing ')'
9237 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009238 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009239
9240 cur_arg = 1;
9241
9242 if (!*args[cur_arg] ||
9243 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9244 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9245 file, linenum, args[0]);
9246 goto out_err;
9247 }
9248
9249 LIST_INIT(&rule->arg.map.key);
9250 proxy->conf.args.ctx = ARGC_HRQ;
9251 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9252 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9253 file, linenum);
9254 free(proxy->conf.lfs_file);
9255 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9256 proxy->conf.lfs_line = proxy->conf.args.line;
9257 cur_arg += 1;
9258 } else if (strncmp(args[0], "del-map", 7) == 0) {
9259 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009260 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009261 /*
9262 * '+ 8' for 'del-map('
9263 * '- 9' for 'del-map(' + trailing ')'
9264 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009265 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009266
9267 cur_arg = 1;
9268
9269 if (!*args[cur_arg] ||
9270 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9271 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9272 file, linenum, args[0]);
9273 goto out_err;
9274 }
9275
9276 LIST_INIT(&rule->arg.map.key);
9277 proxy->conf.args.ctx = ARGC_HRQ;
9278 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9279 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9280 file, linenum);
9281 free(proxy->conf.lfs_file);
9282 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9283 proxy->conf.lfs_line = proxy->conf.args.line;
9284 cur_arg += 1;
9285 } else if (strncmp(args[0], "set-map", 7) == 0) {
9286 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009287 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009288 /*
9289 * '+ 8' for 'set-map('
9290 * '- 9' for 'set-map(' + trailing ')'
9291 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009292 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009293
9294 cur_arg = 1;
9295
9296 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9297 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9298 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9299 file, linenum, args[0]);
9300 goto out_err;
9301 }
9302
9303 LIST_INIT(&rule->arg.map.key);
9304 LIST_INIT(&rule->arg.map.value);
9305 proxy->conf.args.ctx = ARGC_HRQ;
9306
9307 /* key pattern */
9308 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9309 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9310 file, linenum);
9311
9312 /* value pattern */
9313 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9314 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9315 file, linenum);
9316 free(proxy->conf.lfs_file);
9317 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9318 proxy->conf.lfs_line = proxy->conf.args.line;
9319
9320 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009321 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9322 char *errmsg = NULL;
9323 cur_arg = 1;
9324 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009325 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009326 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009327 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009328 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9329 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9330 free(errmsg);
9331 goto out_err;
9332 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009333 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009334 action_build_list(&http_req_keywords.list, &trash);
9335 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
9336 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009337 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009338 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009339 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009340 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009341 }
9342
9343 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9344 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009345 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009346
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009347 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9348 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9349 file, linenum, args[0], errmsg);
9350 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009351 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009352 }
9353 rule->cond = cond;
9354 }
9355 else if (*args[cur_arg]) {
9356 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9357 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9358 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009359 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009360 }
9361
9362 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009363 out_err:
9364 free(rule);
9365 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009366}
9367
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009368/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009369struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009370{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009371 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009372 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009373 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009374 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009375
9376 rule = calloc(1, sizeof(*rule));
9377 if (!rule) {
9378 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9379 goto out_err;
9380 }
9381
9382 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009383 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009384 cur_arg = 1;
9385 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009386 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009387 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009388 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009389 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009390 cur_arg = 1;
9391
9392 if (!*args[cur_arg] ||
9393 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9394 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9395 file, linenum, args[0]);
9396 goto out_err;
9397 }
9398 rule->arg.nice = atoi(args[cur_arg]);
9399 if (rule->arg.nice < -1024)
9400 rule->arg.nice = -1024;
9401 else if (rule->arg.nice > 1024)
9402 rule->arg.nice = 1024;
9403 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009404 } else if (!strcmp(args[0], "set-tos")) {
9405#ifdef IP_TOS
9406 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009407 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009408 cur_arg = 1;
9409
9410 if (!*args[cur_arg] ||
9411 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9412 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9413 file, linenum, args[0]);
9414 goto out_err;
9415 }
9416
9417 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9418 if (err && *err != '\0') {
9419 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9420 file, linenum, err, args[0]);
9421 goto out_err;
9422 }
9423 cur_arg++;
9424#else
9425 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9426 goto out_err;
9427#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009428 } else if (!strcmp(args[0], "set-mark")) {
9429#ifdef SO_MARK
9430 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009431 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009432 cur_arg = 1;
9433
9434 if (!*args[cur_arg] ||
9435 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9436 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9437 file, linenum, args[0]);
9438 goto out_err;
9439 }
9440
9441 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9442 if (err && *err != '\0') {
9443 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9444 file, linenum, err, args[0]);
9445 goto out_err;
9446 }
9447 cur_arg++;
9448 global.last_checks |= LSTCHK_NETADM;
9449#else
9450 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9451 goto out_err;
9452#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009453 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009454 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009455 cur_arg = 1;
9456
9457 if (!*args[cur_arg] ||
9458 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9459 bad_log_level:
9460 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9461 file, linenum, args[0]);
9462 goto out_err;
9463 }
9464 if (strcmp(args[cur_arg], "silent") == 0)
9465 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08009466 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009467 goto bad_log_level;
9468 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009469 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009470 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009471 cur_arg = 1;
9472
9473 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9474 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9475 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9476 file, linenum, args[0]);
9477 goto out_err;
9478 }
9479
9480 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9481 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9482 LIST_INIT(&rule->arg.hdr_add.fmt);
9483
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009484 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009485 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009486 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9487 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009488 free(proxy->conf.lfs_file);
9489 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9490 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009491 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009492 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009493 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009494 cur_arg = 1;
9495
9496 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009497 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9498 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009499 file, linenum, args[0]);
9500 goto out_err;
9501 }
9502
9503 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9504 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9505 LIST_INIT(&rule->arg.hdr_add.fmt);
9506
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009507 error = NULL;
9508 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9509 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9510 args[cur_arg + 1], error);
9511 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009512 goto out_err;
9513 }
9514
9515 proxy->conf.args.ctx = ARGC_HRQ;
9516 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9517 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9518 file, linenum);
9519
9520 free(proxy->conf.lfs_file);
9521 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9522 proxy->conf.lfs_line = proxy->conf.args.line;
9523 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009524 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009525 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009526 cur_arg = 1;
9527
9528 if (!*args[cur_arg] ||
9529 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9530 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9531 file, linenum, args[0]);
9532 goto out_err;
9533 }
9534
9535 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9536 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9537
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009538 proxy->conf.args.ctx = ARGC_HRS;
9539 free(proxy->conf.lfs_file);
9540 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9541 proxy->conf.lfs_line = proxy->conf.args.line;
9542 cur_arg += 1;
9543 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9544 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009545 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009546 /*
9547 * '+ 8' for 'add-acl('
9548 * '- 9' for 'add-acl(' + trailing ')'
9549 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009550 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009551
9552 cur_arg = 1;
9553
9554 if (!*args[cur_arg] ||
9555 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9556 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9557 file, linenum, args[0]);
9558 goto out_err;
9559 }
9560
9561 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009562 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009563 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9564 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9565 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009566 free(proxy->conf.lfs_file);
9567 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9568 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009569
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009570 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009571 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9572 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009573 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009574 /*
9575 * '+ 8' for 'del-acl('
9576 * '- 9' for 'del-acl(' + trailing ')'
9577 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009578 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009579
9580 cur_arg = 1;
9581
9582 if (!*args[cur_arg] ||
9583 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9584 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9585 file, linenum, args[0]);
9586 goto out_err;
9587 }
9588
9589 LIST_INIT(&rule->arg.map.key);
9590 proxy->conf.args.ctx = ARGC_HRS;
9591 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9592 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9593 file, linenum);
9594 free(proxy->conf.lfs_file);
9595 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9596 proxy->conf.lfs_line = proxy->conf.args.line;
9597 cur_arg += 1;
9598 } else if (strncmp(args[0], "del-map", 7) == 0) {
9599 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009600 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009601 /*
9602 * '+ 8' for 'del-map('
9603 * '- 9' for 'del-map(' + trailing ')'
9604 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009605 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009606
9607 cur_arg = 1;
9608
9609 if (!*args[cur_arg] ||
9610 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9611 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9612 file, linenum, args[0]);
9613 goto out_err;
9614 }
9615
9616 LIST_INIT(&rule->arg.map.key);
9617 proxy->conf.args.ctx = ARGC_HRS;
9618 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9619 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9620 file, linenum);
9621 free(proxy->conf.lfs_file);
9622 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9623 proxy->conf.lfs_line = proxy->conf.args.line;
9624 cur_arg += 1;
9625 } else if (strncmp(args[0], "set-map", 7) == 0) {
9626 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009627 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009628 /*
9629 * '+ 8' for 'set-map('
9630 * '- 9' for 'set-map(' + trailing ')'
9631 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009632 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009633
9634 cur_arg = 1;
9635
9636 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9637 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9638 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9639 file, linenum, args[0]);
9640 goto out_err;
9641 }
9642
9643 LIST_INIT(&rule->arg.map.key);
9644 LIST_INIT(&rule->arg.map.value);
9645
9646 proxy->conf.args.ctx = ARGC_HRS;
9647
9648 /* key pattern */
9649 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9650 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9651 file, linenum);
9652
9653 /* value pattern */
9654 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9655 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9656 file, linenum);
9657
9658 free(proxy->conf.lfs_file);
9659 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9660 proxy->conf.lfs_line = proxy->conf.args.line;
9661
9662 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009663 } else if (strcmp(args[0], "redirect") == 0) {
9664 struct redirect_rule *redir;
9665 char *errmsg = NULL;
9666
9667 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9668 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9669 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9670 goto out_err;
9671 }
9672
9673 /* this redirect rule might already contain a parsed condition which
9674 * we'll pass to the http-request rule.
9675 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009676 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009677 rule->arg.redir = redir;
9678 rule->cond = redir->cond;
9679 redir->cond = NULL;
9680 cur_arg = 2;
9681 return rule;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009682 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9683 args[0][9] == '\0' && args[0][8] >= '0' &&
9684 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
9685 struct sample_expr *expr;
9686 unsigned int where;
9687 char *err = NULL;
9688
9689 cur_arg = 1;
9690 proxy->conf.args.ctx = ARGC_TRK;
9691
9692 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9693 if (!expr) {
9694 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9695 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9696 free(err);
9697 goto out_err;
9698 }
9699
9700 where = 0;
9701 if (proxy->cap & PR_CAP_FE)
9702 where |= SMP_VAL_FE_HRS_HDR;
9703 if (proxy->cap & PR_CAP_BE)
9704 where |= SMP_VAL_BE_HRS_HDR;
9705
9706 if (!(expr->fetch->val & where)) {
9707 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
9708 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9709 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9710 args[cur_arg-1], sample_src_names(expr->fetch->use));
9711 free(expr);
9712 goto out_err;
9713 }
9714
9715 if (strcmp(args[cur_arg], "table") == 0) {
9716 cur_arg++;
9717 if (!args[cur_arg]) {
9718 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9719 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9720 free(expr);
9721 goto out_err;
9722 }
9723 /* we copy the table name for now, it will be resolved later */
9724 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9725 cur_arg++;
9726 }
9727 rule->arg.trk_ctr.expr = expr;
9728 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
William Lallemand73025dd2014-04-24 14:38:37 +02009729 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9730 char *errmsg = NULL;
9731 cur_arg = 1;
9732 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009733 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009734 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009735 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009736 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9737 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9738 free(errmsg);
9739 goto out_err;
9740 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009741 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009742 action_build_list(&http_res_keywords.list, &trash);
9743 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9744 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009745 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009746 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009747 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009748 goto out_err;
9749 }
9750
9751 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9752 struct acl_cond *cond;
9753 char *errmsg = NULL;
9754
9755 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9756 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9757 file, linenum, args[0], errmsg);
9758 free(errmsg);
9759 goto out_err;
9760 }
9761 rule->cond = cond;
9762 }
9763 else if (*args[cur_arg]) {
9764 Alert("parsing [%s:%d]: 'http-response %s' expects"
9765 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9766 file, linenum, args[0], args[cur_arg]);
9767 goto out_err;
9768 }
9769
9770 return rule;
9771 out_err:
9772 free(rule);
9773 return NULL;
9774}
9775
Willy Tarreau4baae242012-12-27 12:00:31 +01009776/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009777 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009778 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9779 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009780 */
9781struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009782 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009783{
9784 struct redirect_rule *rule;
9785 int cur_arg;
9786 int type = REDIRECT_TYPE_NONE;
9787 int code = 302;
9788 const char *destination = NULL;
9789 const char *cookie = NULL;
9790 int cookie_set = 0;
9791 unsigned int flags = REDIRECT_FLAG_NONE;
9792 struct acl_cond *cond = NULL;
9793
9794 cur_arg = 0;
9795 while (*(args[cur_arg])) {
9796 if (strcmp(args[cur_arg], "location") == 0) {
9797 if (!*args[cur_arg + 1])
9798 goto missing_arg;
9799
9800 type = REDIRECT_TYPE_LOCATION;
9801 cur_arg++;
9802 destination = args[cur_arg];
9803 }
9804 else if (strcmp(args[cur_arg], "prefix") == 0) {
9805 if (!*args[cur_arg + 1])
9806 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009807 type = REDIRECT_TYPE_PREFIX;
9808 cur_arg++;
9809 destination = args[cur_arg];
9810 }
9811 else if (strcmp(args[cur_arg], "scheme") == 0) {
9812 if (!*args[cur_arg + 1])
9813 goto missing_arg;
9814
9815 type = REDIRECT_TYPE_SCHEME;
9816 cur_arg++;
9817 destination = args[cur_arg];
9818 }
9819 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9820 if (!*args[cur_arg + 1])
9821 goto missing_arg;
9822
9823 cur_arg++;
9824 cookie = args[cur_arg];
9825 cookie_set = 1;
9826 }
9827 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9828 if (!*args[cur_arg + 1])
9829 goto missing_arg;
9830
9831 cur_arg++;
9832 cookie = args[cur_arg];
9833 cookie_set = 0;
9834 }
9835 else if (strcmp(args[cur_arg], "code") == 0) {
9836 if (!*args[cur_arg + 1])
9837 goto missing_arg;
9838
9839 cur_arg++;
9840 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009841 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009842 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009843 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009844 args[cur_arg - 1], args[cur_arg]);
9845 return NULL;
9846 }
9847 }
9848 else if (!strcmp(args[cur_arg],"drop-query")) {
9849 flags |= REDIRECT_FLAG_DROP_QS;
9850 }
9851 else if (!strcmp(args[cur_arg],"append-slash")) {
9852 flags |= REDIRECT_FLAG_APPEND_SLASH;
9853 }
9854 else if (strcmp(args[cur_arg], "if") == 0 ||
9855 strcmp(args[cur_arg], "unless") == 0) {
9856 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9857 if (!cond) {
9858 memprintf(errmsg, "error in condition: %s", *errmsg);
9859 return NULL;
9860 }
9861 break;
9862 }
9863 else {
9864 memprintf(errmsg,
9865 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9866 args[cur_arg]);
9867 return NULL;
9868 }
9869 cur_arg++;
9870 }
9871
9872 if (type == REDIRECT_TYPE_NONE) {
9873 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9874 return NULL;
9875 }
9876
Willy Tarreaube4653b2015-05-28 15:26:58 +02009877 if (dir && type != REDIRECT_TYPE_LOCATION) {
9878 memprintf(errmsg, "response only supports redirect type 'location'");
9879 return NULL;
9880 }
9881
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02009882 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +01009883 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009884 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009885
9886 if (!use_fmt) {
9887 /* old-style static redirect rule */
9888 rule->rdr_str = strdup(destination);
9889 rule->rdr_len = strlen(destination);
9890 }
9891 else {
9892 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009893
9894 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9895 * if prefix == "/", we don't want to add anything, otherwise it
9896 * makes it hard for the user to configure a self-redirection.
9897 */
Godbachd9722032014-12-18 15:44:58 +08009898 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009899 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009900 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009901 dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR
9902 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009903 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009904 free(curproxy->conf.lfs_file);
9905 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9906 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009907 }
9908 }
9909
Willy Tarreau4baae242012-12-27 12:00:31 +01009910 if (cookie) {
9911 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9912 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9913 */
9914 rule->cookie_len = strlen(cookie);
9915 if (cookie_set) {
9916 rule->cookie_str = malloc(rule->cookie_len + 10);
9917 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9918 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9919 rule->cookie_len += 9;
9920 } else {
9921 rule->cookie_str = malloc(rule->cookie_len + 21);
9922 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9923 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9924 rule->cookie_len += 20;
9925 }
9926 }
9927 rule->type = type;
9928 rule->code = code;
9929 rule->flags = flags;
9930 LIST_INIT(&rule->list);
9931 return rule;
9932
9933 missing_arg:
9934 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9935 return NULL;
9936}
9937
Willy Tarreau8797c062007-05-07 00:55:35 +02009938/************************************************************************/
9939/* The code below is dedicated to ACL parsing and matching */
9940/************************************************************************/
9941
9942
Willy Tarreau14174bc2012-04-16 14:34:04 +02009943/* This function ensures that the prerequisites for an L7 fetch are ready,
9944 * which means that a request or response is ready. If some data is missing,
9945 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009946 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9947 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009948 *
9949 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009950 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9951 * decide whether or not an HTTP message is present ;
9952 * 0 if the requested data cannot be fetched or if it is certain that
9953 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009954 * 1 if an HTTP message is ready
9955 */
James Rosewell91a41cb2015-09-18 17:11:16 +01009956int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009957 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009958{
Willy Tarreau192252e2015-04-04 01:47:55 +02009959 struct http_txn *txn;
9960 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009961
Willy Tarreaube508f12016-03-10 11:47:01 +01009962 /* Note: it is possible that <s> is NULL when called before stream
9963 * initialization (eg: tcp-request connection), so this function is the
9964 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009965 */
Willy Tarreau192252e2015-04-04 01:47:55 +02009966 if (!s)
9967 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01009968
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +02009969 if (!s->txn) {
9970 if (unlikely(!http_alloc_txn(s)))
9971 return 0; /* not enough memory */
9972 http_init_txn(s);
9973 }
Willy Tarreau192252e2015-04-04 01:47:55 +02009974 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +02009975 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009976
9977 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009978 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009979
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009980 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009981 /* If the buffer does not leave enough free space at the end,
9982 * we must first realign it.
9983 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009984 if (s->req.buf->p > s->req.buf->data &&
9985 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
9986 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009987
Willy Tarreau14174bc2012-04-16 14:34:04 +02009988 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009989 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009990 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009991
9992 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009993 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009994 http_msg_analyzer(msg, &txn->hdr_idx);
9995
9996 /* Still no valid request ? */
9997 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009998 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009999 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010000 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010001 }
10002 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010003 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010004 return 0;
10005 }
10006
10007 /* OK we just got a valid HTTP request. We have some minor
10008 * preparation to perform so that further checks can rely
10009 * on HTTP tests.
10010 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010011
10012 /* If the request was parsed but was too large, we must absolutely
10013 * return an error so that it is not processed. At the moment this
10014 * cannot happen, but if the parsers are to change in the future,
10015 * we want this check to be maintained.
10016 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010017 if (unlikely(s->req.buf->i + s->req.buf->p >
10018 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010019 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010020 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010021 return 1;
10022 }
10023
Willy Tarreau9b28e032012-10-12 23:49:43 +020010024 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010025 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010026 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010027
Willy Tarreau506d0502013-07-06 13:29:24 +020010028 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10029 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010030 }
10031
Willy Tarreau506d0502013-07-06 13:29:24 +020010032 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010033 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010034 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010035
10036 /* otherwise everything's ready for the request */
10037 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010038 else {
10039 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010040 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10041 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010042 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010043 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010044 }
10045
10046 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010047 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010048 return 1;
10049}
Willy Tarreau8797c062007-05-07 00:55:35 +020010050
Willy Tarreau8797c062007-05-07 00:55:35 +020010051/* 1. Check on METHOD
10052 * We use the pre-parsed method if it is known, and store its number as an
10053 * integer. If it is unknown, we use the pointer and the length.
10054 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010055static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010056{
10057 int len, meth;
10058
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010059 len = strlen(text);
10060 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010061
10062 pattern->val.i = meth;
10063 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010064 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010065 pattern->len = len;
10066 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010067 else {
10068 pattern->ptr.str = NULL;
10069 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010070 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010071 return 1;
10072}
10073
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010074/* This function fetches the method of current HTTP request and stores
10075 * it in the global pattern struct as a chunk. There are two possibilities :
10076 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10077 * in <len> and <ptr> is NULL ;
10078 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10079 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010080 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010081 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010082static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010083smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010084{
10085 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +010010086 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010087
Willy Tarreau24e32d82012-04-23 23:55:44 +020010088 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010089
Willy Tarreaube508f12016-03-10 11:47:01 +010010090 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010091 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010092 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010093 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010094 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010095 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10096 /* ensure the indexes are not affected */
10097 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010098 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010099 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
10100 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010101 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010102 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010103 return 1;
10104}
10105
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010106/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010107static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010108{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010109 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010110 struct pattern_list *lst;
10111 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010112
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010113 list_for_each_entry(lst, &expr->patterns, list) {
10114 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010115
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010116 /* well-known method */
10117 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010118 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010119 return pattern;
10120 else
10121 continue;
10122 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010123
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010124 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010125 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010126 continue;
10127
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010128 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010129 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
10130 (!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 +010010131 return pattern;
10132 }
10133 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010134}
10135
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010136static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010137smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010138{
Willy Tarreaube508f12016-03-10 11:47:01 +010010139 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010140 char *ptr;
10141 int len;
10142
Willy Tarreauc0239e02012-04-16 14:42:55 +020010143 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010144
Willy Tarreaube508f12016-03-10 11:47:01 +010010145 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010146 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010147 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010148
10149 while ((len-- > 0) && (*ptr++ != '/'));
10150 if (len <= 0)
10151 return 0;
10152
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010153 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010154 smp->data.u.str.str = ptr;
10155 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010156
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010157 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010158 return 1;
10159}
10160
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010161static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010162smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010163{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010164 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010165 char *ptr;
10166 int len;
10167
Willy Tarreauc0239e02012-04-16 14:42:55 +020010168 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010169
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010170 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010171 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10172 return 0;
10173
Willy Tarreau8797c062007-05-07 00:55:35 +020010174 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010175 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010176
10177 while ((len-- > 0) && (*ptr++ != '/'));
10178 if (len <= 0)
10179 return 0;
10180
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010181 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010182 smp->data.u.str.str = ptr;
10183 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010184
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010185 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010186 return 1;
10187}
10188
10189/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010190static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010191smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010192{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010193 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010194 char *ptr;
10195 int len;
10196
Willy Tarreauc0239e02012-04-16 14:42:55 +020010197 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010198
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010199 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010200 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10201 return 0;
10202
Willy Tarreau8797c062007-05-07 00:55:35 +020010203 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010204 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010205
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010206 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010207 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010208 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010209 return 1;
10210}
10211
Thierry Fournierf4011dd2016-03-29 17:23:51 +020010212static int
10213smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
10214{
10215 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
10216 return 0;
10217
10218 if (!smp->strm->unique_id) {
10219 if ((smp->strm->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
10220 return 0;
10221 smp->strm->unique_id[0] = '\0';
10222 }
10223 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
10224 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
10225
10226 smp->data.type = SMP_T_STR;
10227 smp->data.u.str.str = smp->strm->unique_id;
10228 smp->flags = SMP_F_CONST;
10229 return 1;
10230}
10231
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010232/* returns the longest available part of the body. This requires that the body
10233 * has been waited for using http-buffer-request.
10234 */
10235static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010236smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010237{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010238 struct http_msg *msg;
10239 unsigned long len;
10240 unsigned long block1;
10241 char *body;
10242 struct chunk *temp;
10243
10244 CHECK_HTTP_MESSAGE_FIRST();
10245
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010246 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010247 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010248 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010249 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010250
10251 len = http_body_bytes(msg);
10252 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10253
10254 block1 = len;
10255 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10256 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10257
10258 if (block1 == len) {
10259 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010260 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010261 smp->data.u.str.str = body;
10262 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010263 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10264 }
10265 else {
10266 /* buffer is wrapped, we need to defragment it */
10267 temp = get_trash_chunk();
10268 memcpy(temp->str, body, block1);
10269 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010270 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010271 smp->data.u.str.str = temp->str;
10272 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010273 smp->flags = SMP_F_VOL_TEST;
10274 }
10275 return 1;
10276}
10277
10278
10279/* returns the available length of the body. This requires that the body
10280 * has been waited for using http-buffer-request.
10281 */
10282static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010283smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010284{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010285 struct http_msg *msg;
10286
10287 CHECK_HTTP_MESSAGE_FIRST();
10288
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010289 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010290 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010291 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010292 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010293
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010294 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010295 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010296
10297 smp->flags = SMP_F_VOL_TEST;
10298 return 1;
10299}
10300
10301
10302/* returns the advertised length of the body, or the advertised size of the
10303 * chunks available in the buffer. This requires that the body has been waited
10304 * for using http-buffer-request.
10305 */
10306static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010307smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010308{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010309 struct http_msg *msg;
10310
10311 CHECK_HTTP_MESSAGE_FIRST();
10312
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010313 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010314 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010315 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010316 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010317
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010318 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010319 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010320
10321 smp->flags = SMP_F_VOL_TEST;
10322 return 1;
10323}
10324
10325
Willy Tarreau8797c062007-05-07 00:55:35 +020010326/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010327static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010328smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010329{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010330 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010331
Willy Tarreauc0239e02012-04-16 14:42:55 +020010332 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010333
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010334 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010335 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010336 smp->data.u.str.len = txn->req.sl.rq.u_l;
10337 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010338 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010339 return 1;
10340}
10341
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010342static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010343smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010344{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010345 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010346 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010347
Willy Tarreauc0239e02012-04-16 14:42:55 +020010348 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010349
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010350 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010351 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 +020010352 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010353 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010354
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010355 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010356 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010357 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010358 return 1;
10359}
10360
10361static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010362smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010363{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010364 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010365 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010366
Willy Tarreauc0239e02012-04-16 14:42:55 +020010367 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010368
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010369 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010370 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 +020010371 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10372 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010373
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010374 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010375 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010376 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010377 return 1;
10378}
10379
Willy Tarreau185b5c42012-04-26 15:11:51 +020010380/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10381 * Accepts an optional argument of type string containing the header field name,
10382 * and an optional argument of type signed or unsigned integer to request an
10383 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010384 * headers are considered from the first one. It does not stop on commas and
10385 * returns full lines instead (useful for User-Agent or Date for example).
10386 */
10387static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010388smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010389{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010390 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010391 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010392 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010393 int occ = 0;
10394 const char *name_str = NULL;
10395 int name_len = 0;
10396
10397 if (!ctx) {
10398 /* first call */
10399 ctx = &static_hdr_ctx;
10400 ctx->idx = 0;
10401 smp->ctx.a[0] = ctx;
10402 }
10403
10404 if (args) {
10405 if (args[0].type != ARGT_STR)
10406 return 0;
10407 name_str = args[0].data.str.str;
10408 name_len = args[0].data.str.len;
10409
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010410 if (args[1].type == ARGT_SINT)
10411 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010412 }
10413
10414 CHECK_HTTP_MESSAGE_FIRST();
10415
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010416 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010417 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 +020010418
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010419 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10420 /* search for header from the beginning */
10421 ctx->idx = 0;
10422
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010423 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010424 /* no explicit occurrence and single fetch => last header by default */
10425 occ = -1;
10426
10427 if (!occ)
10428 /* prepare to report multiple occurrences for ACL fetches */
10429 smp->flags |= SMP_F_NOT_LAST;
10430
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010431 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010432 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010433 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 +020010434 return 1;
10435
10436 smp->flags &= ~SMP_F_NOT_LAST;
10437 return 0;
10438}
10439
10440/* 6. Check on HTTP header count. The number of occurrences is returned.
10441 * Accepts exactly 1 argument of type string. It does not stop on commas and
10442 * returns full lines instead (useful for User-Agent or Date for example).
10443 */
10444static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010445smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010446{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010447 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010448 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010449 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010450 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010451 const char *name = NULL;
10452 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010453
Willy Tarreau601a4d12015-04-01 19:16:09 +020010454 if (args && args->type == ARGT_STR) {
10455 name = args->data.str.str;
10456 len = args->data.str.len;
10457 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010458
10459 CHECK_HTTP_MESSAGE_FIRST();
10460
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010461 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010462 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 +020010463
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010464 ctx.idx = 0;
10465 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010466 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010467 cnt++;
10468
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010469 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010470 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010471 smp->flags = SMP_F_VOL_HDR;
10472 return 1;
10473}
10474
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010475static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010476smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010477{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010478 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010479 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010480 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010481 struct chunk *temp;
10482 char del = ',';
10483
10484 if (args && args->type == ARGT_STR)
10485 del = *args[0].data.str.str;
10486
10487 CHECK_HTTP_MESSAGE_FIRST();
10488
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010489 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010490 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 +020010491
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010492 temp = get_trash_chunk();
10493
10494 ctx.idx = 0;
10495 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10496 if (temp->len)
10497 temp->str[temp->len++] = del;
10498 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10499 temp->len += ctx.del;
10500 }
10501
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010502 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010503 smp->data.u.str.str = temp->str;
10504 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010505 smp->flags = SMP_F_VOL_HDR;
10506 return 1;
10507}
10508
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010509/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10510 * Accepts an optional argument of type string containing the header field name,
10511 * and an optional argument of type signed or unsigned integer to request an
10512 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010513 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010514 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010515static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010516smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010517{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010518 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010519 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010520 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010521 int occ = 0;
10522 const char *name_str = NULL;
10523 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010524
Willy Tarreaua890d072013-04-02 12:01:06 +020010525 if (!ctx) {
10526 /* first call */
10527 ctx = &static_hdr_ctx;
10528 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010529 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010530 }
10531
Willy Tarreau185b5c42012-04-26 15:11:51 +020010532 if (args) {
10533 if (args[0].type != ARGT_STR)
10534 return 0;
10535 name_str = args[0].data.str.str;
10536 name_len = args[0].data.str.len;
10537
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010538 if (args[1].type == ARGT_SINT)
10539 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010540 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010541
Willy Tarreaue333ec92012-04-16 16:26:40 +020010542 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010543
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010544 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010545 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 +020010546
Willy Tarreau185b5c42012-04-26 15:11:51 +020010547 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010548 /* search for header from the beginning */
10549 ctx->idx = 0;
10550
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010551 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010552 /* no explicit occurrence and single fetch => last header by default */
10553 occ = -1;
10554
10555 if (!occ)
10556 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010557 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010558
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010559 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010560 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010561 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 +020010562 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010563
Willy Tarreau37406352012-04-23 16:16:37 +020010564 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010565 return 0;
10566}
10567
Willy Tarreauc11416f2007-06-17 16:58:38 +020010568/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010569 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010570 */
10571static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010572smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010573{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010574 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010575 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010576 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010577 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010578 const char *name = NULL;
10579 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010580
Willy Tarreau601a4d12015-04-01 19:16:09 +020010581 if (args && args->type == ARGT_STR) {
10582 name = args->data.str.str;
10583 len = args->data.str.len;
10584 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010585
Willy Tarreaue333ec92012-04-16 16:26:40 +020010586 CHECK_HTTP_MESSAGE_FIRST();
10587
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010588 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010589 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 +020010590
Willy Tarreau33a7e692007-06-10 19:45:56 +020010591 ctx.idx = 0;
10592 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010593 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010594 cnt++;
10595
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010596 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010597 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010598 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010599 return 1;
10600}
10601
Willy Tarreau185b5c42012-04-26 15:11:51 +020010602/* Fetch an HTTP header's integer value. The integer value is returned. It
10603 * takes a mandatory argument of type string and an optional one of type int
10604 * to designate a specific occurrence. It returns an unsigned integer, which
10605 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010606 */
10607static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010608smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010609{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010610 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010611
Willy Tarreauf853c462012-04-23 18:53:56 +020010612 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010613 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010614 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010615 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010616
Willy Tarreaud53e2422012-04-16 17:21:11 +020010617 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010618}
10619
Cyril Bonté69fa9922012-10-25 00:01:06 +020010620/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10621 * and an optional one of type int to designate a specific occurrence.
10622 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010623 */
10624static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010625smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010626{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010627 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010628
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010629 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010630 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010631 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010632 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010633 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010634 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010635 if (smp->data.u.str.len < temp->size - 1) {
10636 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10637 temp->str[smp->data.u.str.len] = '\0';
10638 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010639 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010640 break;
10641 }
10642 }
10643 }
10644
Willy Tarreaud53e2422012-04-16 17:21:11 +020010645 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010646 if (!(smp->flags & SMP_F_NOT_LAST))
10647 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010648 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010649 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010650}
10651
Willy Tarreau737b0c12007-06-10 21:28:46 +020010652/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10653 * the first '/' after the possible hostname, and ends before the possible '?'.
10654 */
10655static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010656smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010657{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010658 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010659 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010660
Willy Tarreauc0239e02012-04-16 14:42:55 +020010661 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010662
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010663 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010664 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010665 ptr = http_get_path(txn);
10666 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010667 return 0;
10668
10669 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010670 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010671 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010672
10673 while (ptr < end && *ptr != '?')
10674 ptr++;
10675
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010676 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010677 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010678 return 1;
10679}
10680
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010681/* This produces a concatenation of the first occurrence of the Host header
10682 * followed by the path component if it begins with a slash ('/'). This means
10683 * that '*' will not be added, resulting in exactly the first Host entry.
10684 * If no Host header is found, then the path is returned as-is. The returned
10685 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010686 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010687 */
10688static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010689smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010690{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010691 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010692 char *ptr, *end, *beg;
10693 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010694 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010695
10696 CHECK_HTTP_MESSAGE_FIRST();
10697
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010698 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010699 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010700 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010701 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010702
10703 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010704 temp = get_trash_chunk();
10705 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010706 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010707 smp->data.u.str.str = temp->str;
10708 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010709
10710 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010711 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010712 beg = http_get_path(txn);
10713 if (!beg)
10714 beg = end;
10715
10716 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10717
10718 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010719 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10720 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010721 }
10722
10723 smp->flags = SMP_F_VOL_1ST;
10724 return 1;
10725}
10726
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010727/* This produces a 32-bit hash of the concatenation of the first occurrence of
10728 * the Host header followed by the path component if it begins with a slash ('/').
10729 * This means that '*' will not be added, resulting in exactly the first Host
10730 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010731 * is hashed using the path hash followed by a full avalanche hash and provides a
10732 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010733 * high-traffic sites without having to store whole paths.
10734 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010735int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010736smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010737{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010738 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010739 struct hdr_ctx ctx;
10740 unsigned int hash = 0;
10741 char *ptr, *beg, *end;
10742 int len;
10743
10744 CHECK_HTTP_MESSAGE_FIRST();
10745
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010746 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010747 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010748 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010749 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10750 ptr = ctx.line + ctx.val;
10751 len = ctx.vlen;
10752 while (len--)
10753 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10754 }
10755
10756 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010757 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010758 beg = http_get_path(txn);
10759 if (!beg)
10760 beg = end;
10761
10762 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10763
10764 if (beg < ptr && *beg == '/') {
10765 while (beg < ptr)
10766 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10767 }
10768 hash = full_hash(hash);
10769
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010770 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010771 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010772 smp->flags = SMP_F_VOL_1ST;
10773 return 1;
10774}
10775
Willy Tarreau4a550602012-12-09 14:53:32 +010010776/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010777 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10778 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10779 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010780 * that in environments where IPv6 is insignificant, truncating the output to
10781 * 8 bytes would still work.
10782 */
10783static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010784smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010785{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010786 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010787 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010788
10789 if (!cli_conn)
10790 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010791
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010792 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010793 return 0;
10794
Willy Tarreau47ca5452012-12-23 20:22:19 +010010795 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010796 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010797 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010798
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010799 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010800 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010801 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010802 temp->len += 4;
10803 break;
10804 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010805 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010806 temp->len += 16;
10807 break;
10808 default:
10809 return 0;
10810 }
10811
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010812 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010813 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010814 return 1;
10815}
10816
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010817/* Extracts the query string, which comes after the question mark '?'. If no
10818 * question mark is found, nothing is returned. Otherwise it returns a sample
10819 * of type string carrying the whole query string.
10820 */
10821static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010822smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010823{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010824 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010825 char *ptr, *end;
10826
10827 CHECK_HTTP_MESSAGE_FIRST();
10828
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010829 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010830 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10831 end = ptr + txn->req.sl.rq.u_l;
10832
10833 /* look up the '?' */
10834 do {
10835 if (ptr == end)
10836 return 0;
10837 } while (*ptr++ != '?');
10838
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010839 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010840 smp->data.u.str.str = ptr;
10841 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010842 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10843 return 1;
10844}
10845
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010846static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010847smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010848{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010849 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10850 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10851 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010852
Willy Tarreau24e32d82012-04-23 23:55:44 +020010853 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010854
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010855 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010856 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010857 return 1;
10858}
10859
Willy Tarreau7f18e522010-10-22 20:04:13 +020010860/* return a valid test if the current request is the first one on the connection */
10861static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010862smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010863{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010864 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010865 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010866 return 1;
10867}
10868
Willy Tarreau34db1082012-04-19 17:16:54 +020010869/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010870static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010871smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010872{
10873
Willy Tarreau24e32d82012-04-23 23:55:44 +020010874 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010875 return 0;
10876
Willy Tarreauc0239e02012-04-16 14:42:55 +020010877 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010878
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010879 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010880 return 0;
10881
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010882 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010883 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010884 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010885 return 1;
10886}
Willy Tarreau8797c062007-05-07 00:55:35 +020010887
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010888/* Accepts exactly 1 argument of type userlist */
10889static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010890smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010891{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010892 if (!args || args->type != ARGT_USR)
10893 return 0;
10894
10895 CHECK_HTTP_MESSAGE_FIRST();
10896
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010897 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010898 return 0;
10899
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010900 /* if the user does not belong to the userlist or has a wrong password,
10901 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010902 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010903 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010904 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10905 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010906 return 0;
10907
10908 /* pat_match_auth() will need the user list */
10909 smp->ctx.a[0] = args->data.usr;
10910
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010911 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010912 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010913 smp->data.u.str.str = smp->strm->txn->auth.user;
10914 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010915
10916 return 1;
10917}
10918
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010919/* Try to find the next occurrence of a cookie name in a cookie header value.
10920 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10921 * the cookie value is returned into *value and *value_l, and the function
10922 * returns a pointer to the next pointer to search from if the value was found.
10923 * Otherwise if the cookie was not found, NULL is returned and neither value
10924 * nor value_l are touched. The input <hdr> string should first point to the
10925 * header's value, and the <hdr_end> pointer must point to the first character
10926 * not part of the value. <list> must be non-zero if value may represent a list
10927 * of values (cookie headers). This makes it faster to abort parsing when no
10928 * list is expected.
10929 */
David Carlier4686f792015-09-25 14:10:50 +010010930char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010931extract_cookie_value(char *hdr, const char *hdr_end,
10932 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010933 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010934{
10935 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10936 char *next;
10937
10938 /* we search at least a cookie name followed by an equal, and more
10939 * generally something like this :
10940 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10941 */
10942 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10943 /* Iterate through all cookies on this line */
10944
Willy Tarreau2235b262016-11-05 15:50:20 +010010945 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010946 att_beg++;
10947
10948 /* find att_end : this is the first character after the last non
10949 * space before the equal. It may be equal to hdr_end.
10950 */
10951 equal = att_end = att_beg;
10952
10953 while (equal < hdr_end) {
10954 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10955 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010010956 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010957 continue;
10958 att_end = equal;
10959 }
10960
10961 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10962 * is between <att_beg> and <equal>, both may be identical.
10963 */
10964
10965 /* look for end of cookie if there is an equal sign */
10966 if (equal < hdr_end && *equal == '=') {
10967 /* look for the beginning of the value */
10968 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010010969 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010970 val_beg++;
10971
10972 /* find the end of the value, respecting quotes */
10973 next = find_cookie_value_end(val_beg, hdr_end);
10974
10975 /* make val_end point to the first white space or delimitor after the value */
10976 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010010977 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010978 val_end--;
10979 } else {
10980 val_beg = val_end = next = equal;
10981 }
10982
10983 /* We have nothing to do with attributes beginning with '$'. However,
10984 * they will automatically be removed if a header before them is removed,
10985 * since they're supposed to be linked together.
10986 */
10987 if (*att_beg == '$')
10988 continue;
10989
10990 /* Ignore cookies with no equal sign */
10991 if (equal == next)
10992 continue;
10993
10994 /* Now we have the cookie name between att_beg and att_end, and
10995 * its value between val_beg and val_end.
10996 */
10997
10998 if (att_end - att_beg == cookie_name_l &&
10999 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
11000 /* let's return this value and indicate where to go on from */
11001 *value = val_beg;
11002 *value_l = val_end - val_beg;
11003 return next + 1;
11004 }
11005
11006 /* Set-Cookie headers only have the name in the first attr=value part */
11007 if (!list)
11008 break;
11009 }
11010
11011 return NULL;
11012}
11013
William Lallemanda43ba4e2014-01-28 18:14:25 +010011014/* Fetch a captured HTTP request header. The index is the position of
11015 * the "capture" option in the configuration file
11016 */
11017static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011018smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011019{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011020 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011021 int idx;
11022
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011023 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011024 return 0;
11025
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011026 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011027
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011028 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 +010011029 return 0;
11030
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011031 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011032 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011033 smp->data.u.str.str = smp->strm->req_cap[idx];
11034 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011035
11036 return 1;
11037}
11038
11039/* Fetch a captured HTTP response header. The index is the position of
11040 * the "capture" option in the configuration file
11041 */
11042static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011043smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011044{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011045 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011046 int idx;
11047
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011048 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011049 return 0;
11050
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011051 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011052
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011053 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 +010011054 return 0;
11055
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011056 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011057 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011058 smp->data.u.str.str = smp->strm->res_cap[idx];
11059 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011060
11061 return 1;
11062}
11063
William Lallemand65ad6e12014-01-31 15:08:02 +010011064/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11065static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011066smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011067{
11068 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011069 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011070 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011071
Willy Tarreau15e91e12015-04-04 00:52:09 +020011072 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011073 return 0;
11074
William Lallemand96a77852014-02-05 00:30:02 +010011075 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011076
William Lallemand96a77852014-02-05 00:30:02 +010011077 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11078 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011079
William Lallemand96a77852014-02-05 00:30:02 +010011080 temp = get_trash_chunk();
11081 temp->str = txn->uri;
11082 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011083 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011084 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011085 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011086
11087 return 1;
11088
11089}
11090
11091/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11092static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011093smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011094{
11095 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011096 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011097 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011098
Willy Tarreau15e91e12015-04-04 00:52:09 +020011099 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011100 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011101
William Lallemand65ad6e12014-01-31 15:08:02 +010011102 ptr = txn->uri;
11103
11104 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11105 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011106
William Lallemand65ad6e12014-01-31 15:08:02 +010011107 if (!*ptr)
11108 return 0;
11109
11110 ptr++; /* skip the space */
11111
11112 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011113 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011114 if (!ptr)
11115 return 0;
11116 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11117 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011118
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011119 smp->data.u.str = *temp;
11120 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011121 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011122 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011123
11124 return 1;
11125}
11126
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011127/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11128 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11129 */
11130static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011131smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011132{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011133 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011134
Willy Tarreau15e91e12015-04-04 00:52:09 +020011135 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011136 return 0;
11137
11138 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011139 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011140 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011141 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011142
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011143 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011144 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011145 smp->flags = SMP_F_CONST;
11146 return 1;
11147
11148}
11149
11150/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11151 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11152 */
11153static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011154smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011155{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011156 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011157
Willy Tarreau15e91e12015-04-04 00:52:09 +020011158 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011159 return 0;
11160
11161 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011162 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011163 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011164 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011165
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011166 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011167 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011168 smp->flags = SMP_F_CONST;
11169 return 1;
11170
11171}
11172
William Lallemand65ad6e12014-01-31 15:08:02 +010011173
Willy Tarreaue333ec92012-04-16 16:26:40 +020011174/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011175 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011176 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011177 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011178 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011179 * Accepts exactly 1 argument of type string. If the input options indicate
11180 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011181 * The returned sample is of type CSTR. Can be used to parse cookies in other
11182 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011183 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011184int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011185{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011186 struct http_txn *txn;
11187 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011188 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011189 const struct http_msg *msg;
11190 const char *hdr_name;
11191 int hdr_name_len;
11192 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011193 int occ = 0;
11194 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011195
Willy Tarreau24e32d82012-04-23 23:55:44 +020011196 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011197 return 0;
11198
Willy Tarreaua890d072013-04-02 12:01:06 +020011199 if (!ctx) {
11200 /* first call */
11201 ctx = &static_hdr_ctx;
11202 ctx->idx = 0;
11203 smp->ctx.a[2] = ctx;
11204 }
11205
Willy Tarreaue333ec92012-04-16 16:26:40 +020011206 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011207
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011208 txn = smp->strm->txn;
11209 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011210
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011211 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011212 msg = &txn->req;
11213 hdr_name = "Cookie";
11214 hdr_name_len = 6;
11215 } else {
11216 msg = &txn->rsp;
11217 hdr_name = "Set-Cookie";
11218 hdr_name_len = 10;
11219 }
11220
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011221 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011222 /* no explicit occurrence and single fetch => last cookie by default */
11223 occ = -1;
11224
11225 /* OK so basically here, either we want only one value and it's the
11226 * last one, or we want to iterate over all of them and we fetch the
11227 * next one.
11228 */
11229
Willy Tarreau9b28e032012-10-12 23:49:43 +020011230 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011231 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011232 /* search for the header from the beginning, we must first initialize
11233 * the search parameters.
11234 */
Willy Tarreau37406352012-04-23 16:16:37 +020011235 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011236 ctx->idx = 0;
11237 }
11238
Willy Tarreau28376d62012-04-26 21:26:10 +020011239 smp->flags |= SMP_F_VOL_HDR;
11240
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011241 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011242 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11243 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011244 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11245 goto out;
11246
Willy Tarreau24e32d82012-04-23 23:55:44 +020011247 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011248 continue;
11249
Willy Tarreau37406352012-04-23 16:16:37 +020011250 smp->ctx.a[0] = ctx->line + ctx->val;
11251 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011252 }
11253
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011254 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011255 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011256 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011257 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011258 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011259 &smp->data.u.str.str,
11260 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011261 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011262 found = 1;
11263 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011264 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011265 smp->flags |= SMP_F_NOT_LAST;
11266 return 1;
11267 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011268 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011269 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011270 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011271 /* all cookie headers and values were scanned. If we're looking for the
11272 * last occurrence, we may return it now.
11273 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011274 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011275 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011276 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011277}
11278
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011279/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011280 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011281 * multiple cookies may be parsed on the same line. The returned sample is of
11282 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011283 */
11284static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011285smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011286{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011287 struct http_txn *txn;
11288 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011289 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011290 const struct http_msg *msg;
11291 const char *hdr_name;
11292 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011293 int cnt;
11294 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011295 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011296
Willy Tarreau24e32d82012-04-23 23:55:44 +020011297 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011298 return 0;
11299
Willy Tarreaue333ec92012-04-16 16:26:40 +020011300 CHECK_HTTP_MESSAGE_FIRST();
11301
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011302 txn = smp->strm->txn;
11303 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011304
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011305 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011306 msg = &txn->req;
11307 hdr_name = "Cookie";
11308 hdr_name_len = 6;
11309 } else {
11310 msg = &txn->rsp;
11311 hdr_name = "Set-Cookie";
11312 hdr_name_len = 10;
11313 }
11314
Willy Tarreau9b28e032012-10-12 23:49:43 +020011315 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011316 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011317 ctx.idx = 0;
11318 cnt = 0;
11319
11320 while (1) {
11321 /* Note: val_beg == NULL every time we need to fetch a new header */
11322 if (!val_beg) {
11323 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11324 break;
11325
Willy Tarreau24e32d82012-04-23 23:55:44 +020011326 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011327 continue;
11328
11329 val_beg = ctx.line + ctx.val;
11330 val_end = val_beg + ctx.vlen;
11331 }
11332
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011333 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011334 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011335 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011336 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011337 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011338 &smp->data.u.str.str,
11339 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011340 cnt++;
11341 }
11342 }
11343
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011344 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011345 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011346 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011347 return 1;
11348}
11349
Willy Tarreau51539362012-05-08 12:46:28 +020011350/* Fetch an cookie's integer value. The integer value is returned. It
11351 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11352 */
11353static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011354smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011355{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011356 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011357
11358 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011359 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011360 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011361 }
11362
11363 return ret;
11364}
11365
Willy Tarreau8797c062007-05-07 00:55:35 +020011366/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011367/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011368/************************************************************************/
11369
David Cournapeau16023ee2010-12-23 20:55:41 +090011370/*
11371 * Given a path string and its length, find the position of beginning of the
11372 * query string. Returns NULL if no query string is found in the path.
11373 *
11374 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11375 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011376 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011377 */
bedis4c75cca2012-10-05 08:38:24 +020011378static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011379{
11380 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011381
bedis4c75cca2012-10-05 08:38:24 +020011382 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011383 return p ? p + 1 : NULL;
11384}
11385
bedis4c75cca2012-10-05 08:38:24 +020011386static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011387{
bedis4c75cca2012-10-05 08:38:24 +020011388 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011389}
11390
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011391/* after increasing a pointer value, it can exceed the first buffer
11392 * size. This function transform the value of <ptr> according with
11393 * the expected position. <chunks> is an array of the one or two
11394 * avalaible chunks. The first value is the start of the first chunk,
11395 * the second value if the end+1 of the first chunks. The third value
11396 * is NULL or the start of the second chunk and the fourth value is
11397 * the end+1 of the second chunk. The function returns 1 if does a
11398 * wrap, else returns 0.
11399 */
11400static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11401{
11402 if (*ptr < chunks[1])
11403 return 0;
11404 if (!chunks[2])
11405 return 0;
11406 *ptr = chunks[2] + ( *ptr - chunks[1] );
11407 return 1;
11408}
11409
David Cournapeau16023ee2010-12-23 20:55:41 +090011410/*
11411 * Given a url parameter, find the starting position of the first occurence,
11412 * or NULL if the parameter is not found.
11413 *
11414 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11415 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011416 *
Willy Tarreauf6625822015-12-27 14:51:01 +010011417 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011418 * or the second chunk. The caller must be check the position before using the
11419 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011420 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011421static const char *
11422find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011423 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011424 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011425{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011426 const char *pos, *last, *equal;
11427 const char **bufs = chunks;
11428 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011429
David Cournapeau16023ee2010-12-23 20:55:41 +090011430
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011431 pos = bufs[0];
11432 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010011433 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011434 /* Check the equal. */
11435 equal = pos + url_param_name_l;
11436 if (fix_pointer_if_wrap(chunks, &equal)) {
11437 if (equal >= chunks[3])
11438 return NULL;
11439 } else {
11440 if (equal >= chunks[1])
11441 return NULL;
11442 }
11443 if (*equal == '=') {
11444 if (pos + url_param_name_l > last) {
11445 /* process wrap case, we detect a wrap. In this case, the
11446 * comparison is performed in two parts.
11447 */
11448
11449 /* This is the end, we dont have any other chunk. */
11450 if (bufs != chunks || !bufs[2])
11451 return NULL;
11452
11453 /* Compute the length of each part of the comparison. */
11454 l1 = last - pos;
11455 l2 = url_param_name_l - l1;
11456
11457 /* The second buffer is too short to contain the compared string. */
11458 if (bufs[2] + l2 > bufs[3])
11459 return NULL;
11460
11461 if (memcmp(pos, url_param_name, l1) == 0 &&
11462 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11463 return pos;
11464
11465 /* Perform wrapping and jump the string who fail the comparison. */
11466 bufs += 2;
11467 pos = bufs[0] + l2;
11468 last = bufs[1];
11469
11470 } else {
11471 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011472 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11473 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011474 pos += url_param_name_l + 1;
11475 if (fix_pointer_if_wrap(chunks, &pos))
11476 last = bufs[2];
11477 }
11478 }
11479
11480 while (1) {
11481 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011482 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011483 pos++;
11484 if (pos < last)
11485 break;
11486 /* process buffer wrapping. */
11487 if (bufs != chunks || !bufs[2])
11488 return NULL;
11489 bufs += 2;
11490 pos = bufs[0];
11491 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011492 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011493 pos++;
11494 }
11495 return NULL;
11496}
11497
11498/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011499 * Given a url parameter name and a query string, find the next value.
11500 * An empty url_param_name matches the first available parameter.
11501 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11502 * respectively provide a pointer to the value and its end.
11503 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011504 */
11505static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011506find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011507 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011508 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011509{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011510 const char *arg_start, *qs_end;
11511 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011512
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011513 arg_start = chunks[0];
11514 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011515 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011516 /* Looks for an argument name. */
11517 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011518 url_param_name, url_param_name_l,
11519 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011520 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011521 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011522 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011523 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011524 if (!arg_start)
11525 return 0;
11526
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011527 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011528 while (1) {
11529 /* looks for the first argument. */
11530 value_start = memchr(arg_start, '=', qs_end - arg_start);
11531 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011532 /* Check for wrapping. */
11533 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011534 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011535 chunks[2]) {
11536 arg_start = chunks[2];
11537 qs_end = chunks[3];
11538 continue;
11539 }
11540 return 0;
11541 }
11542 break;
11543 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011544 value_start++;
11545 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011546 else {
11547 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011548 value_start = arg_start + url_param_name_l + 1;
11549
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011550 /* Check for pointer wrapping. */
11551 if (fix_pointer_if_wrap(chunks, &value_start)) {
11552 /* Update the end pointer. */
11553 qs_end = chunks[3];
11554
11555 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011556 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011557 return 0;
11558 }
11559 }
11560
David Cournapeau16023ee2010-12-23 20:55:41 +090011561 value_end = value_start;
11562
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011563 while (1) {
11564 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11565 value_end++;
11566 if (value_end < qs_end)
11567 break;
11568 /* process buffer wrapping. */
11569 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011570 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011571 chunks[2]) {
11572 value_end = chunks[2];
11573 qs_end = chunks[3];
11574 continue;
11575 }
11576 break;
11577 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011578
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011579 *vstart = value_start;
11580 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011581 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011582}
11583
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011584/* This scans a URL-encoded query string. It takes an optionally wrapping
11585 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11586 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11587 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011588 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011589static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011590smp_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 +090011591{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011592 const char *vstart, *vend;
11593 struct chunk *temp;
11594 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011595
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011596 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011597 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011598 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011599 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011600 return 0;
11601
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011602 /* Create sample. If the value is contiguous, return the pointer as CONST,
11603 * if the value is wrapped, copy-it in a buffer.
11604 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011605 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011606 if (chunks[2] &&
11607 vstart >= chunks[0] && vstart <= chunks[1] &&
11608 vend >= chunks[2] && vend <= chunks[3]) {
11609 /* Wrapped case. */
11610 temp = get_trash_chunk();
11611 memcpy(temp->str, vstart, chunks[1] - vstart);
11612 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011613 smp->data.u.str.str = temp->str;
11614 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011615 } else {
11616 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011617 smp->data.u.str.str = (char *)vstart;
11618 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011619 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11620 }
11621
11622 /* Update context, check wrapping. */
11623 chunks[0] = vend;
11624 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11625 chunks[1] = chunks[3];
11626 chunks[2] = NULL;
11627 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011628
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011629 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011630 smp->flags |= SMP_F_NOT_LAST;
11631
David Cournapeau16023ee2010-12-23 20:55:41 +090011632 return 1;
11633}
11634
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011635/* This function iterates over each parameter of the query string. It uses
11636 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011637 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11638 * An optional parameter name is passed in args[0], otherwise any parameter is
11639 * considered. It supports an optional delimiter argument for the beginning of
11640 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011641 */
11642static int
11643smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11644{
11645 struct http_msg *msg;
11646 char delim = '?';
11647 const char *name;
11648 int name_len;
11649
Dragan Dosen26f77e52015-05-25 10:02:11 +020011650 if (!args ||
11651 (args[0].type && args[0].type != ARGT_STR) ||
11652 (args[1].type && args[1].type != ARGT_STR))
11653 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011654
Dragan Dosen26f77e52015-05-25 10:02:11 +020011655 name = "";
11656 name_len = 0;
11657 if (args->type == ARGT_STR) {
11658 name = args->data.str.str;
11659 name_len = args->data.str.len;
11660 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011661
Dragan Dosen26f77e52015-05-25 10:02:11 +020011662 if (args[1].type)
11663 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011664
Dragan Dosen26f77e52015-05-25 10:02:11 +020011665 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011666 CHECK_HTTP_MESSAGE_FIRST();
11667
11668 msg = &smp->strm->txn->req;
11669
11670 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11671 msg->sl.rq.u_l, delim);
11672 if (!smp->ctx.a[0])
11673 return 0;
11674
11675 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011676
11677 /* Assume that the context is filled with NULL pointer
11678 * before the first call.
11679 * smp->ctx.a[2] = NULL;
11680 * smp->ctx.a[3] = NULL;
11681 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011682 }
11683
11684 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11685}
11686
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011687/* This function iterates over each parameter of the body. This requires
11688 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011689 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11690 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11691 * optional second part if the body wraps at the end of the buffer. An optional
11692 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011693 */
11694static int
11695smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11696{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011697 struct http_msg *msg;
11698 unsigned long len;
11699 unsigned long block1;
11700 char *body;
11701 const char *name;
11702 int name_len;
11703
11704 if (!args || (args[0].type && args[0].type != ARGT_STR))
11705 return 0;
11706
11707 name = "";
11708 name_len = 0;
11709 if (args[0].type == ARGT_STR) {
11710 name = args[0].data.str.str;
11711 name_len = args[0].data.str.len;
11712 }
11713
11714 if (!smp->ctx.a[0]) { // first call, find the query string
11715 CHECK_HTTP_MESSAGE_FIRST();
11716
11717 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011718 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011719 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011720 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011721
11722 len = http_body_bytes(msg);
11723 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11724
11725 block1 = len;
11726 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11727 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11728
11729 if (block1 == len) {
11730 /* buffer is not wrapped (or empty) */
11731 smp->ctx.a[0] = body;
11732 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011733
11734 /* Assume that the context is filled with NULL pointer
11735 * before the first call.
11736 * smp->ctx.a[2] = NULL;
11737 * smp->ctx.a[3] = NULL;
11738 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011739 }
11740 else {
11741 /* buffer is wrapped, we need to defragment it */
11742 smp->ctx.a[0] = body;
11743 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011744 smp->ctx.a[2] = msg->chn->buf->data;
11745 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011746 }
11747 }
11748 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11749}
11750
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011751/* Return the signed integer value for the specified url parameter (see url_param
11752 * above).
11753 */
11754static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011755smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011756{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011757 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011758
11759 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011760 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011761 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011762 }
11763
11764 return ret;
11765}
11766
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011767/* This produces a 32-bit hash of the concatenation of the first occurrence of
11768 * the Host header followed by the path component if it begins with a slash ('/').
11769 * This means that '*' will not be added, resulting in exactly the first Host
11770 * entry. If no Host header is found, then the path is used. The resulting value
11771 * is hashed using the url hash followed by a full avalanche hash and provides a
11772 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11773 * high-traffic sites without having to store whole paths.
11774 * this differs from the base32 functions in that it includes the url parameters
11775 * as well as the path
11776 */
11777static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011778smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011779{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011780 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011781 struct hdr_ctx ctx;
11782 unsigned int hash = 0;
11783 char *ptr, *beg, *end;
11784 int len;
11785
11786 CHECK_HTTP_MESSAGE_FIRST();
11787
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011788 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011789 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011790 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011791 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11792 ptr = ctx.line + ctx.val;
11793 len = ctx.vlen;
11794 while (len--)
11795 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11796 }
11797
11798 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011799 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 +000011800 beg = http_get_path(txn);
11801 if (!beg)
11802 beg = end;
11803
11804 for (ptr = beg; ptr < end ; ptr++);
11805
11806 if (beg < ptr && *beg == '/') {
11807 while (beg < ptr)
11808 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11809 }
11810 hash = full_hash(hash);
11811
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011812 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011813 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011814 smp->flags = SMP_F_VOL_1ST;
11815 return 1;
11816}
11817
11818/* This concatenates the source address with the 32-bit hash of the Host and
11819 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11820 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11821 * on the source address length. The URL hash is stored before the address so
11822 * that in environments where IPv6 is insignificant, truncating the output to
11823 * 8 bytes would still work.
11824 */
11825static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011826smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011827{
11828 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011829 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011830
Dragan Dosendb5af612016-06-16 11:23:01 +020011831 if (!cli_conn)
11832 return 0;
11833
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011834 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011835 return 0;
11836
11837 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020011838 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
11839 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011840
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011841 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011842 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011843 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011844 temp->len += 4;
11845 break;
11846 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011847 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011848 temp->len += 16;
11849 break;
11850 default:
11851 return 0;
11852 }
11853
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011854 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011855 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011856 return 1;
11857}
11858
Willy Tarreau185b5c42012-04-26 15:11:51 +020011859/* This function is used to validate the arguments passed to any "hdr" fetch
11860 * keyword. These keywords support an optional positive or negative occurrence
11861 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11862 * is assumed that the types are already the correct ones. Returns 0 on error,
11863 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11864 * error message in case of error, that the caller is responsible for freeing.
11865 * The initial location must either be freeable or NULL.
11866 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011867int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011868{
11869 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011870 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011871 return 0;
11872 }
11873 return 1;
11874}
11875
Willy Tarreau276fae92013-07-25 14:36:01 +020011876/* takes an UINT value on input supposed to represent the time since EPOCH,
11877 * adds an optional offset found in args[0] and emits a string representing
11878 * the date in RFC-1123/5322 format.
11879 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011880static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011881{
Cyril Bontéf78d8962016-01-22 19:40:28 +010011882 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020011883 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11884 struct chunk *temp;
11885 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011886 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011887 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011888
11889 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011890 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011891 curr_date += args[0].data.sint;
11892
11893 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011894 if (!tm)
11895 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011896
11897 temp = get_trash_chunk();
11898 temp->len = snprintf(temp->str, temp->size - temp->len,
11899 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11900 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11901 tm->tm_hour, tm->tm_min, tm->tm_sec);
11902
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011903 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011904 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011905 return 1;
11906}
11907
Thierry FOURNIERad903512014-04-11 17:51:01 +020011908/* Match language range with language tag. RFC2616 14.4:
11909 *
11910 * A language-range matches a language-tag if it exactly equals
11911 * the tag, or if it exactly equals a prefix of the tag such
11912 * that the first tag character following the prefix is "-".
11913 *
11914 * Return 1 if the strings match, else return 0.
11915 */
11916static inline int language_range_match(const char *range, int range_len,
11917 const char *tag, int tag_len)
11918{
11919 const char *end = range + range_len;
11920 const char *tend = tag + tag_len;
11921 while (range < end) {
11922 if (*range == '-' && tag == tend)
11923 return 1;
11924 if (*range != *tag || tag == tend)
11925 return 0;
11926 range++;
11927 tag++;
11928 }
11929 /* Return true only if the last char of the tag is matched. */
11930 return tag == tend;
11931}
11932
11933/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011934static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011935{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011936 const char *al = smp->data.u.str.str;
11937 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011938 const char *token;
11939 int toklen;
11940 int qvalue;
11941 const char *str;
11942 const char *w;
11943 int best_q = 0;
11944
11945 /* Set the constant to the sample, because the output of the
11946 * function will be peek in the constant configuration string.
11947 */
11948 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011949 smp->data.u.str.size = 0;
11950 smp->data.u.str.str = "";
11951 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011952
11953 /* Parse the accept language */
11954 while (1) {
11955
11956 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011957 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011958 al++;
11959 if (al >= end)
11960 break;
11961
11962 /* Start of the fisrt word. */
11963 token = al;
11964
11965 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011966 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011967 al++;
11968 if (al == token)
11969 goto expect_comma;
11970
11971 /* Length of the token. */
11972 toklen = al - token;
11973 qvalue = 1000;
11974
11975 /* Check if the token exists in the list. If the token not exists,
11976 * jump to the next token.
11977 */
11978 str = args[0].data.str.str;
11979 w = str;
11980 while (1) {
11981 if (*str == ';' || *str == '\0') {
11982 if (language_range_match(token, toklen, w, str-w))
11983 goto look_for_q;
11984 if (*str == '\0')
11985 goto expect_comma;
11986 w = str + 1;
11987 }
11988 str++;
11989 }
11990 goto expect_comma;
11991
11992look_for_q:
11993
11994 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011995 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011996 al++;
11997 if (al >= end)
11998 goto process_value;
11999
12000 /* If ',' is found, process the result */
12001 if (*al == ',')
12002 goto process_value;
12003
12004 /* If the character is different from ';', look
12005 * for the end of the header part in best effort.
12006 */
12007 if (*al != ';')
12008 goto expect_comma;
12009
12010 /* Assumes that the char is ';', now expect "q=". */
12011 al++;
12012
12013 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012014 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012015 al++;
12016 if (al >= end)
12017 goto process_value;
12018
12019 /* Expect 'q'. If no 'q', continue in best effort */
12020 if (*al != 'q')
12021 goto process_value;
12022 al++;
12023
12024 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012025 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012026 al++;
12027 if (al >= end)
12028 goto process_value;
12029
12030 /* Expect '='. If no '=', continue in best effort */
12031 if (*al != '=')
12032 goto process_value;
12033 al++;
12034
12035 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012036 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012037 al++;
12038 if (al >= end)
12039 goto process_value;
12040
12041 /* Parse the q value. */
12042 qvalue = parse_qvalue(al, &al);
12043
12044process_value:
12045
12046 /* If the new q value is the best q value, then store the associated
12047 * language in the response. If qvalue is the biggest value (1000),
12048 * break the process.
12049 */
12050 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012051 smp->data.u.str.str = (char *)w;
12052 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012053 if (qvalue >= 1000)
12054 break;
12055 best_q = qvalue;
12056 }
12057
12058expect_comma:
12059
12060 /* Expect comma or end. If the end is detected, quit the loop. */
12061 while (al < end && *al != ',')
12062 al++;
12063 if (al >= end)
12064 break;
12065
12066 /* Comma is found, jump it and restart the analyzer. */
12067 al++;
12068 }
12069
12070 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012071 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
12072 smp->data.u.str.str = args[1].data.str.str;
12073 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012074 }
12075
12076 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012077 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012078}
12079
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012080/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012081static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012082{
12083 /* If the constant flag is set or if not size is avalaible at
12084 * the end of the buffer, copy the string in other buffer
12085 * before decoding.
12086 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012087 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012088 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012089 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
12090 smp->data.u.str.str = str->str;
12091 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012092 smp->flags &= ~SMP_F_CONST;
12093 }
12094
12095 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012096 smp->data.u.str.str[smp->data.u.str.len] = '\0';
12097 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012098 return 1;
12099}
12100
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012101static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
12102{
12103 struct proxy *fe = strm_fe(smp->strm);
12104 int idx, i;
12105 struct cap_hdr *hdr;
12106 int len;
12107
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012108 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012109 return 0;
12110
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012111 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012112
12113 /* Check the availibity of the capture id. */
12114 if (idx > fe->nb_req_cap - 1)
12115 return 0;
12116
12117 /* Look for the original configuration. */
12118 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
12119 hdr != NULL && i != idx ;
12120 i--, hdr = hdr->next);
12121 if (!hdr)
12122 return 0;
12123
12124 /* check for the memory allocation */
12125 if (smp->strm->req_cap[hdr->index] == NULL)
12126 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
12127 if (smp->strm->req_cap[hdr->index] == NULL)
12128 return 0;
12129
12130 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012131 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012132 if (len > hdr->len)
12133 len = hdr->len;
12134
12135 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012136 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012137 smp->strm->req_cap[idx][len] = '\0';
12138
12139 return 1;
12140}
12141
12142static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
12143{
12144 struct proxy *fe = strm_fe(smp->strm);
12145 int idx, i;
12146 struct cap_hdr *hdr;
12147 int len;
12148
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012149 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012150 return 0;
12151
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012152 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012153
12154 /* Check the availibity of the capture id. */
12155 if (idx > fe->nb_rsp_cap - 1)
12156 return 0;
12157
12158 /* Look for the original configuration. */
12159 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12160 hdr != NULL && i != idx ;
12161 i--, hdr = hdr->next);
12162 if (!hdr)
12163 return 0;
12164
12165 /* check for the memory allocation */
12166 if (smp->strm->res_cap[hdr->index] == NULL)
12167 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
12168 if (smp->strm->res_cap[hdr->index] == NULL)
12169 return 0;
12170
12171 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012172 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012173 if (len > hdr->len)
12174 len = hdr->len;
12175
12176 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012177 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012178 smp->strm->res_cap[idx][len] = '\0';
12179
12180 return 1;
12181}
12182
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012183/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012184 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012185 * the relevant part of the request line accordingly. Then it updates various
12186 * pointers to the next elements which were moved, and the total buffer length.
12187 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012188 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12189 * error, though this can be revisited when this code is finally exploited.
12190 *
12191 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12192 * query string and 3 to replace uri.
12193 *
12194 * In query string case, the mark question '?' must be set at the start of the
12195 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012196 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012197int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012198 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012199{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012200 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012201 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012202 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012203 int delta;
12204
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012205 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012206 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012207 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012208 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12209
12210 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012211 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012212 txn->req.sl.rq.m_l += delta;
12213 txn->req.sl.rq.u += delta;
12214 txn->req.sl.rq.v += delta;
12215 break;
12216
12217 case 1: // path
12218 cur_ptr = http_get_path(txn);
12219 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012220 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012221
12222 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012223 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 +010012224 cur_end++;
12225
12226 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012227 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012228 txn->req.sl.rq.u_l += delta;
12229 txn->req.sl.rq.v += delta;
12230 break;
12231
12232 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012233 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012234 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012235 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12236 while (cur_ptr < cur_end && *cur_ptr != '?')
12237 cur_ptr++;
12238
12239 /* skip the question mark or indicate that we must insert it
12240 * (but only if the format string is not empty then).
12241 */
12242 if (cur_ptr < cur_end)
12243 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012244 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012245 offset = 0;
12246
12247 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012248 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012249 txn->req.sl.rq.u_l += delta;
12250 txn->req.sl.rq.v += delta;
12251 break;
12252
12253 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012254 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012255 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12256
12257 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012258 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012259 txn->req.sl.rq.u_l += delta;
12260 txn->req.sl.rq.v += delta;
12261 break;
12262
12263 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012264 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012265 }
12266
12267 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012268 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012269 txn->req.sl.rq.l += delta;
12270 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012271 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012272 return 0;
12273}
12274
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012275/* This function replace the HTTP status code and the associated message. The
12276 * variable <status> contains the new status code. This function never fails.
12277 */
12278void http_set_status(unsigned int status, struct stream *s)
12279{
12280 struct http_txn *txn = s->txn;
12281 char *cur_ptr, *cur_end;
12282 int delta;
12283 char *res;
12284 int c_l;
12285 const char *msg;
12286 int msg_len;
12287
12288 chunk_reset(&trash);
12289
12290 res = ultoa_o(status, trash.str, trash.size);
12291 c_l = res - trash.str;
12292
12293 trash.str[c_l] = ' ';
12294 trash.len = c_l + 1;
12295
12296 msg = get_reason(status);
12297 msg_len = strlen(msg);
12298
12299 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
12300 trash.len += msg_len;
12301
12302 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
12303 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
12304
12305 /* commit changes and adjust message */
12306 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
12307
12308 /* adjust res line offsets and lengths */
12309 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
12310 txn->rsp.sl.st.c_l = c_l;
12311 txn->rsp.sl.st.r_l = msg_len;
12312
12313 delta = trash.len - (cur_end - cur_ptr);
12314 txn->rsp.sl.st.l += delta;
12315 txn->hdr_idx.v[0].len += delta;
12316 http_msg_move_end(&txn->rsp, delta);
12317}
12318
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012319/* This function executes one of the set-{method,path,query,uri} actions. It
12320 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012321 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012322 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012323 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12324 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012325 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012326enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012327 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012328{
12329 chunk_reset(&trash);
12330
12331 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012332 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012333 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012334 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012335
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012336 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012337 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012338}
12339
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012340/* This function is just a compliant action wrapper for "set-status". */
12341enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012342 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012343{
12344 http_set_status(rule->arg.status.code, s);
12345 return ACT_RET_CONT;
12346}
12347
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012348/* parse an http-request action among :
12349 * set-method
12350 * set-path
12351 * set-query
12352 * set-uri
12353 *
12354 * All of them accept a single argument of type string representing a log-format.
12355 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12356 * 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 +020012357 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012358 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012359enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12360 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012361{
12362 int cur_arg = *orig_arg;
12363
Thierry FOURNIER42148732015-09-02 17:17:33 +020012364 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012365
12366 switch (args[0][4]) {
12367 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012368 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012369 rule->action_ptr = http_action_set_req_line;
12370 break;
12371 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012372 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012373 rule->action_ptr = http_action_set_req_line;
12374 break;
12375 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012376 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012377 rule->action_ptr = http_action_set_req_line;
12378 break;
12379 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012380 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012381 rule->action_ptr = http_action_set_req_line;
12382 break;
12383 default:
12384 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012385 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012386 }
12387
12388 if (!*args[cur_arg] ||
12389 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12390 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012391 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012392 }
12393
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012394 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012395 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012396 parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012397 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
12398 proxy->conf.args.file, proxy->conf.args.line);
12399
12400 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012401 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012402}
12403
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012404/* parse set-status action:
12405 * This action accepts a single argument of type int representing
12406 * an http status code. It returns ACT_RET_PRS_OK on success,
12407 * ACT_RET_PRS_ERR on error.
12408 */
12409enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12410 struct act_rule *rule, char **err)
12411{
12412 char *error;
12413
Thierry FOURNIER42148732015-09-02 17:17:33 +020012414 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012415 rule->action_ptr = action_http_set_status;
12416
12417 /* Check if an argument is available */
12418 if (!*args[*orig_arg]) {
12419 memprintf(err, "expects exactly 1 argument <status>");
12420 return ACT_RET_PRS_ERR;
12421 }
12422
12423 /* convert status code as integer */
12424 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12425 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12426 memprintf(err, "expects an integer status code between 100 and 999");
12427 return ACT_RET_PRS_ERR;
12428 }
12429
12430 (*orig_arg)++;
12431 return ACT_RET_PRS_OK;
12432}
12433
Willy Tarreaua9083d02015-05-08 15:27:59 +020012434/* This function executes the "capture" action. It executes a fetch expression,
12435 * turns the result into a string and puts it in a capture slot. It always
12436 * returns 1. If an error occurs the action is cancelled, but the rule
12437 * processing continues.
12438 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012439enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012440 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012441{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012442 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012443 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012444 char **cap = s->req_cap;
12445 int len;
12446
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012447 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 +020012448 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012449 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012450
12451 if (cap[h->index] == NULL)
12452 cap[h->index] = pool_alloc2(h->pool);
12453
12454 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012455 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012456
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012457 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012458 if (len > h->len)
12459 len = h->len;
12460
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012461 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012462 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012463 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012464}
12465
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012466/* This function executes the "capture" action and store the result in a
12467 * capture slot if exists. It executes a fetch expression, turns the result
12468 * into a string and puts it in a capture slot. It always returns 1. If an
12469 * error occurs the action is cancelled, but the rule processing continues.
12470 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012471enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012472 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012473{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012474 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012475 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012476 char **cap = s->req_cap;
12477 struct proxy *fe = strm_fe(s);
12478 int len;
12479 int i;
12480
12481 /* Look for the original configuration. */
12482 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012483 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012484 i--, h = h->next);
12485 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012486 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012487
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012488 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 +020012489 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012490 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012491
12492 if (cap[h->index] == NULL)
12493 cap[h->index] = pool_alloc2(h->pool);
12494
12495 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012496 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012497
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012498 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012499 if (len > h->len)
12500 len = h->len;
12501
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012502 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012503 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012504 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012505}
12506
Willy Tarreaua9083d02015-05-08 15:27:59 +020012507/* parse an "http-request capture" action. It takes a single argument which is
12508 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012509 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012510 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012511 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012512enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12513 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012514{
12515 struct sample_expr *expr;
12516 struct cap_hdr *hdr;
12517 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012518 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012519
12520 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12521 if (strcmp(args[cur_arg], "if") == 0 ||
12522 strcmp(args[cur_arg], "unless") == 0)
12523 break;
12524
12525 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012526 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012527 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012528 }
12529
Willy Tarreaua9083d02015-05-08 15:27:59 +020012530 cur_arg = *orig_arg;
12531 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12532 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012533 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012534
12535 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12536 memprintf(err,
12537 "fetch method '%s' extracts information from '%s', none of which is available here",
12538 args[cur_arg-1], sample_src_names(expr->fetch->use));
12539 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012540 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012541 }
12542
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012543 if (!args[cur_arg] || !*args[cur_arg]) {
12544 memprintf(err, "expects 'len or 'id'");
12545 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012546 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012547 }
12548
Willy Tarreaua9083d02015-05-08 15:27:59 +020012549 if (strcmp(args[cur_arg], "len") == 0) {
12550 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012551
12552 if (!(px->cap & PR_CAP_FE)) {
12553 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012554 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012555 }
12556
12557 proxy->conf.args.ctx = ARGC_CAP;
12558
Willy Tarreaua9083d02015-05-08 15:27:59 +020012559 if (!args[cur_arg]) {
12560 memprintf(err, "missing length value");
12561 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012562 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012563 }
12564 /* we copy the table name for now, it will be resolved later */
12565 len = atoi(args[cur_arg]);
12566 if (len <= 0) {
12567 memprintf(err, "length must be > 0");
12568 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012569 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012570 }
12571 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012572
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012573 if (!len) {
12574 memprintf(err, "a positive 'len' argument is mandatory");
12575 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012576 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012577 }
12578
Vincent Bernat02779b62016-04-03 13:48:43 +020012579 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012580 hdr->next = px->req_cap;
12581 hdr->name = NULL; /* not a header capture */
12582 hdr->namelen = 0;
12583 hdr->len = len;
12584 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12585 hdr->index = px->nb_req_cap++;
12586
12587 px->req_cap = hdr;
12588 px->to_log |= LW_REQHDR;
12589
Thierry FOURNIER42148732015-09-02 17:17:33 +020012590 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012591 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012592 rule->arg.cap.expr = expr;
12593 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012594 }
12595
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012596 else if (strcmp(args[cur_arg], "id") == 0) {
12597 int id;
12598 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012599
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012600 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012601
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012602 if (!args[cur_arg]) {
12603 memprintf(err, "missing id value");
12604 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012605 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012606 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012607
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012608 id = strtol(args[cur_arg], &error, 10);
12609 if (*error != '\0') {
12610 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12611 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012612 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012613 }
12614 cur_arg++;
12615
12616 proxy->conf.args.ctx = ARGC_CAP;
12617
Thierry FOURNIER42148732015-09-02 17:17:33 +020012618 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012619 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012620 rule->arg.capid.expr = expr;
12621 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012622 }
12623
12624 else {
12625 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12626 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012627 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012628 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012629
12630 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012631 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012632}
12633
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012634/* This function executes the "capture" action and store the result in a
12635 * capture slot if exists. It executes a fetch expression, turns the result
12636 * into a string and puts it in a capture slot. It always returns 1. If an
12637 * error occurs the action is cancelled, but the rule processing continues.
12638 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012639enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012640 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012641{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012642 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012643 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012644 char **cap = s->res_cap;
12645 struct proxy *fe = strm_fe(s);
12646 int len;
12647 int i;
12648
12649 /* Look for the original configuration. */
12650 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012651 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012652 i--, h = h->next);
12653 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012654 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012655
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012656 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 +020012657 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012658 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012659
12660 if (cap[h->index] == NULL)
12661 cap[h->index] = pool_alloc2(h->pool);
12662
12663 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012664 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012665
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012666 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012667 if (len > h->len)
12668 len = h->len;
12669
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012670 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012671 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012672 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012673}
12674
12675/* parse an "http-response capture" action. It takes a single argument which is
12676 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12677 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012678 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012679 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012680enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12681 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012682{
12683 struct sample_expr *expr;
12684 int cur_arg;
12685 int id;
12686 char *error;
12687
12688 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12689 if (strcmp(args[cur_arg], "if") == 0 ||
12690 strcmp(args[cur_arg], "unless") == 0)
12691 break;
12692
12693 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012694 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012695 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012696 }
12697
12698 cur_arg = *orig_arg;
12699 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12700 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012701 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012702
12703 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12704 memprintf(err,
12705 "fetch method '%s' extracts information from '%s', none of which is available here",
12706 args[cur_arg-1], sample_src_names(expr->fetch->use));
12707 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012708 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012709 }
12710
12711 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012712 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012713 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012714 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012715 }
12716
12717 if (strcmp(args[cur_arg], "id") != 0) {
12718 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12719 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012720 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012721 }
12722
12723 cur_arg++;
12724
12725 if (!args[cur_arg]) {
12726 memprintf(err, "missing id value");
12727 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012728 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012729 }
12730
12731 id = strtol(args[cur_arg], &error, 10);
12732 if (*error != '\0') {
12733 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12734 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012735 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012736 }
12737 cur_arg++;
12738
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012739 proxy->conf.args.ctx = ARGC_CAP;
12740
Thierry FOURNIER42148732015-09-02 17:17:33 +020012741 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012742 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012743 rule->arg.capid.expr = expr;
12744 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012745
12746 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012747 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012748}
12749
William Lallemand73025dd2014-04-24 14:38:37 +020012750/*
12751 * Return the struct http_req_action_kw associated to a keyword.
12752 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012753struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012754{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012755 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012756}
12757
12758/*
12759 * Return the struct http_res_action_kw associated to a keyword.
12760 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012761struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012762{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012763 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012764}
12765
Willy Tarreau4a568972010-05-12 08:08:50 +020012766/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012767/* All supported ACL keywords must be declared here. */
12768/************************************************************************/
12769
12770/* Note: must not be declared <const> as its list will be overwritten.
12771 * Please take care of keeping this list alphabetically sorted.
12772 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012773static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012774 { "base", "base", PAT_MATCH_STR },
12775 { "base_beg", "base", PAT_MATCH_BEG },
12776 { "base_dir", "base", PAT_MATCH_DIR },
12777 { "base_dom", "base", PAT_MATCH_DOM },
12778 { "base_end", "base", PAT_MATCH_END },
12779 { "base_len", "base", PAT_MATCH_LEN },
12780 { "base_reg", "base", PAT_MATCH_REG },
12781 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012782
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012783 { "cook", "req.cook", PAT_MATCH_STR },
12784 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12785 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12786 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12787 { "cook_end", "req.cook", PAT_MATCH_END },
12788 { "cook_len", "req.cook", PAT_MATCH_LEN },
12789 { "cook_reg", "req.cook", PAT_MATCH_REG },
12790 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012791
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012792 { "hdr", "req.hdr", PAT_MATCH_STR },
12793 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12794 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12795 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12796 { "hdr_end", "req.hdr", PAT_MATCH_END },
12797 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12798 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12799 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012800
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012801 /* these two declarations uses strings with list storage (in place
12802 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12803 * and delete functions are relative to the list management. The parse
12804 * and match method are related to the corresponding fetch methods. This
12805 * is very particular ACL declaration mode.
12806 */
12807 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12808 { "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 +020012809
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012810 { "path", "path", PAT_MATCH_STR },
12811 { "path_beg", "path", PAT_MATCH_BEG },
12812 { "path_dir", "path", PAT_MATCH_DIR },
12813 { "path_dom", "path", PAT_MATCH_DOM },
12814 { "path_end", "path", PAT_MATCH_END },
12815 { "path_len", "path", PAT_MATCH_LEN },
12816 { "path_reg", "path", PAT_MATCH_REG },
12817 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012818
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012819 { "req_ver", "req.ver", PAT_MATCH_STR },
12820 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012821
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012822 { "scook", "res.cook", PAT_MATCH_STR },
12823 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12824 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12825 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12826 { "scook_end", "res.cook", PAT_MATCH_END },
12827 { "scook_len", "res.cook", PAT_MATCH_LEN },
12828 { "scook_reg", "res.cook", PAT_MATCH_REG },
12829 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012830
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012831 { "shdr", "res.hdr", PAT_MATCH_STR },
12832 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12833 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12834 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12835 { "shdr_end", "res.hdr", PAT_MATCH_END },
12836 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12837 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12838 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012839
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012840 { "url", "url", PAT_MATCH_STR },
12841 { "url_beg", "url", PAT_MATCH_BEG },
12842 { "url_dir", "url", PAT_MATCH_DIR },
12843 { "url_dom", "url", PAT_MATCH_DOM },
12844 { "url_end", "url", PAT_MATCH_END },
12845 { "url_len", "url", PAT_MATCH_LEN },
12846 { "url_reg", "url", PAT_MATCH_REG },
12847 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012848
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012849 { "urlp", "urlp", PAT_MATCH_STR },
12850 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12851 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12852 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12853 { "urlp_end", "urlp", PAT_MATCH_END },
12854 { "urlp_len", "urlp", PAT_MATCH_LEN },
12855 { "urlp_reg", "urlp", PAT_MATCH_REG },
12856 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012857
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012858 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012859}};
12860
12861/************************************************************************/
12862/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012863/************************************************************************/
12864/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012865static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012866 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012867 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012868 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12869
Willy Tarreau87b09662015-04-03 00:22:06 +020012870 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012871 { "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 +020012872
12873 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012874 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12875 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12876 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012877
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012878 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12879 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012880
Willy Tarreau409bcde2013-01-08 00:31:00 +010012881 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12882 * are only here to match the ACL's name, are request-only and are used
12883 * for ACL compatibility only.
12884 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012885 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12886 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012887 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12888 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012889
12890 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12891 * only here to match the ACL's name, are request-only and are used for
12892 * ACL compatibility only.
12893 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012894 { "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 +020012895 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012896 { "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 +020012897 { "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 +010012898
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012899 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012900 { "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 +010012901 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012902 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012903 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012904 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012905
12906 /* HTTP protocol on the request path */
12907 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012908 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012909
12910 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012911 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12912 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012913
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012914 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012915 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12916 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012917 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012918
Willy Tarreau18ed2562013-01-14 15:56:36 +010012919 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012920 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12921 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012922
Willy Tarreau18ed2562013-01-14 15:56:36 +010012923 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012924 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012925 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12926 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012927
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012928 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012929 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012930 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012931 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012932 { "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 +010012933 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012934 { "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 +010012935
12936 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012937 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012938 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12939 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012940
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012941 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012942 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012943 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012944 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012945 { "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 +010012946 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012947 { "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 +010012948
Willy Tarreau409bcde2013-01-08 00:31:00 +010012949 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012950 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012951 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12952 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012953 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012954
12955 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012956 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012957 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012958 { "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 +020012959 { "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 +010012960
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012961 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020012962 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012963 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012964 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012965 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012966 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012967 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020012968 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12969 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012970 { "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 +010012971 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012972}};
12973
Willy Tarreau8797c062007-05-07 00:55:35 +020012974
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012975/************************************************************************/
12976/* All supported converter keywords must be declared here. */
12977/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012978/* Note: must not be declared <const> as its list will be overwritten */
12979static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012980 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020012981 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012982 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
12983 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012984 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012985 { NULL, NULL, 0, 0, 0 },
12986}};
12987
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012988
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012989/************************************************************************/
12990/* All supported http-request action keywords must be declared here. */
12991/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012992struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012993 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020012994 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012995 { "set-method", parse_set_req_line },
12996 { "set-path", parse_set_req_line },
12997 { "set-query", parse_set_req_line },
12998 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012999 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013000 }
13001};
13002
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013003struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013004 .kw = {
13005 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020013006 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013007 { NULL, NULL }
13008 }
13009};
13010
Willy Tarreau8797c062007-05-07 00:55:35 +020013011__attribute__((constructor))
13012static void __http_protocol_init(void)
13013{
13014 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020013015 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020013016 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013017 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013018 http_res_keywords_register(&http_res_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020013019}
13020
13021
Willy Tarreau58f10d72006-12-04 02:26:12 +010013022/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020013023 * Local variables:
13024 * c-indent-level: 8
13025 * c-basic-offset: 8
13026 * End:
13027 */