blob: 36428b164024aaf929b081fd32bcd7bf5b15403e [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 Tarreau5f10ea32016-11-05 17:52:06 +01001522#if defined(__x86_64__) || \
1523 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
1524 defined(__ARM_ARCH_7A__)
1525 /* speedup: skip bytes not between 0x21 and 0x7e inclusive */
1526 while (ptr <= end - sizeof(int)) {
1527 int x = *(int *)ptr - 0x21212121;
1528 if (x & 0x80808080)
1529 break;
1530
1531 x -= 0x5e5e5e5e;
1532 if (!(x & 0x80808080))
1533 break;
1534
1535 ptr += sizeof(int);
1536 }
1537#endif
1538 http_msg_rquri2:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001539 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau5f10ea32016-11-05 17:52:06 +01001540 EAT_AND_JUMP_OR_RETURN(http_msg_rquri2, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001541
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001542 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001543 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001544 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1545 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001546
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001547 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001548 /* non-ASCII chars are forbidden unless option
1549 * accept-invalid-http-request is enabled in the frontend.
1550 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001551 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001552 if (msg->err_pos < -1)
1553 goto invalid_char;
1554 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001555 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001556 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1557 }
1558
1559 if (likely(HTTP_IS_CRLF(*ptr))) {
1560 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1561 goto http_msg_req09_uri_e;
1562 }
1563
1564 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001565 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001566 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001567 state = HTTP_MSG_ERROR;
1568 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001569
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001571 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001572 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001573 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001574 goto http_msg_rqver;
1575 }
1576 if (likely(HTTP_IS_SPHT(*ptr)))
1577 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1578 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1579 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001580
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001581 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001582 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001583 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001584 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001585
1586 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001587 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001588 http_msg_rqline_eol:
1589 /* We have seen the end of line. Note that we do not
1590 * necessarily have the \n yet, but at least we know that we
1591 * have EITHER \r OR \n, otherwise the request would not be
1592 * complete. We can then record the request length and return
1593 * to the caller which will be able to register it.
1594 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001595 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001596 return ptr;
1597 }
1598
1599 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001600 state = HTTP_MSG_ERROR;
1601 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001602
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001603 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001604#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001605 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1606 exit(1);
1607#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001608 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001609 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001610
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001611 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001612 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001613 if (ret_state)
1614 *ret_state = state;
1615 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001616 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001617 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001618}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001619
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001620/*
1621 * Returns the data from Authorization header. Function may be called more
1622 * than once so data is stored in txn->auth_data. When no header is found
1623 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001624 * searching again for something we are unable to find anyway. However, if
1625 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001626 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001627 */
1628
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001629/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1630 * set according to global.tune.bufsize.
1631 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001632char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001633
1634int
Willy Tarreau87b09662015-04-03 00:22:06 +02001635get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001636{
1637
Willy Tarreaueee5b512015-04-03 23:46:31 +02001638 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001639 struct chunk auth_method;
1640 struct hdr_ctx ctx;
1641 char *h, *p;
1642 int len;
1643
1644#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001645 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001646#endif
1647
1648 if (txn->auth.method == HTTP_AUTH_WRONG)
1649 return 0;
1650
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001651 txn->auth.method = HTTP_AUTH_WRONG;
1652
1653 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001654
1655 if (txn->flags & TX_USE_PX_CONN) {
1656 h = "Proxy-Authorization";
1657 len = strlen(h);
1658 } else {
1659 h = "Authorization";
1660 len = strlen(h);
1661 }
1662
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001663 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001664 return 0;
1665
1666 h = ctx.line + ctx.val;
1667
1668 p = memchr(h, ' ', ctx.vlen);
Willy Tarreau5c557d12016-03-13 08:17:02 +01001669 len = p - h;
1670 if (!p || len <= 0)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001671 return 0;
1672
David Carlier7365f7d2016-04-04 11:54:42 +01001673 if (chunk_initlen(&auth_method, h, 0, len) != 1)
1674 return 0;
1675
Willy Tarreau5c557d12016-03-13 08:17:02 +01001676 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001677
1678 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1679
1680 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001681 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001682
1683 if (len < 0)
1684 return 0;
1685
1686
1687 get_http_auth_buff[len] = '\0';
1688
1689 p = strchr(get_http_auth_buff, ':');
1690
1691 if (!p)
1692 return 0;
1693
1694 txn->auth.user = get_http_auth_buff;
1695 *p = '\0';
1696 txn->auth.pass = p+1;
1697
1698 txn->auth.method = HTTP_AUTH_BASIC;
1699 return 1;
1700 }
1701
1702 return 0;
1703}
1704
Willy Tarreau58f10d72006-12-04 02:26:12 +01001705
Willy Tarreau8973c702007-01-21 23:58:29 +01001706/*
1707 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001708 * depending on the initial msg->msg_state. The caller is responsible for
1709 * ensuring that the message does not wrap. The function can be preempted
1710 * everywhere when data are missing and recalled at the exact same location
1711 * with no information loss. The message may even be realigned between two
1712 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001713 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001714 * fields. Note that msg->sol will be initialized after completing the first
1715 * state, so that none of the msg pointers has to be initialized prior to the
1716 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001717 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001718void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001719{
Willy Tarreau3770f232013-12-07 00:01:53 +01001720 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001721 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001722 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001723
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001724 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001725 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001726 ptr = buf->p + msg->next;
1727 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001728
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001729 if (unlikely(ptr >= end))
1730 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001731
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001732 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001733 /*
1734 * First, states that are specific to the response only.
1735 * We check them first so that request and headers are
1736 * closer to each other (accessed more often).
1737 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001738 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001739 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001740 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001741 /* we have a start of message, but we have to check
1742 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001743 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001744 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001745 if (unlikely(ptr != buf->p)) {
1746 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001747 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001748 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001749 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001750 }
Willy Tarreau26927362012-05-18 23:22:52 +02001751 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001752 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001753 hdr_idx_init(idx);
1754 state = HTTP_MSG_RPVER;
1755 goto http_msg_rpver;
1756 }
1757
1758 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1759 goto http_msg_invalid;
1760
1761 if (unlikely(*ptr == '\n'))
1762 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1763 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1764 /* stop here */
1765
Willy Tarreau8973c702007-01-21 23:58:29 +01001766 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001767 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001768 EXPECT_LF_HERE(ptr, http_msg_invalid);
1769 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1770 /* stop here */
1771
Willy Tarreau8973c702007-01-21 23:58:29 +01001772 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001773 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001774 case HTTP_MSG_RPVER_SP:
1775 case HTTP_MSG_RPCODE:
1776 case HTTP_MSG_RPCODE_SP:
1777 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001778 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001779 state, ptr, end,
1780 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001781 if (unlikely(!ptr))
1782 return;
1783
1784 /* we have a full response and we know that we have either a CR
1785 * or an LF at <ptr>.
1786 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001787 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1788
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001789 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001790 if (likely(*ptr == '\r'))
1791 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1792 goto http_msg_rpline_end;
1793
Willy Tarreau8973c702007-01-21 23:58:29 +01001794 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001795 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001796 /* msg->sol must point to the first of CR or LF. */
1797 EXPECT_LF_HERE(ptr, http_msg_invalid);
1798 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1799 /* stop here */
1800
1801 /*
1802 * Second, states that are specific to the request only
1803 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001804 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001805 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001806 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001807 /* we have a start of message, but we have to check
1808 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001809 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001810 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001811 if (likely(ptr != buf->p)) {
1812 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001813 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001814 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001815 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001816 }
Willy Tarreau26927362012-05-18 23:22:52 +02001817 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001818 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001819 state = HTTP_MSG_RQMETH;
1820 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001821 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001822
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001823 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1824 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001825
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001826 if (unlikely(*ptr == '\n'))
1827 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1828 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001829 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001830
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001831 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001832 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001833 EXPECT_LF_HERE(ptr, http_msg_invalid);
1834 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001835 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001836
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001837 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001838 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001839 case HTTP_MSG_RQMETH_SP:
1840 case HTTP_MSG_RQURI:
1841 case HTTP_MSG_RQURI_SP:
1842 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001843 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001844 state, ptr, end,
1845 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001846 if (unlikely(!ptr))
1847 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001848
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001849 /* we have a full request and we know that we have either a CR
1850 * or an LF at <ptr>.
1851 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001852 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001853
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001854 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001855 if (likely(*ptr == '\r'))
1856 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001857 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001858
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001859 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001860 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001861 /* check for HTTP/0.9 request : no version information available.
1862 * msg->sol must point to the first of CR or LF.
1863 */
1864 if (unlikely(msg->sl.rq.v_l == 0))
1865 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001866
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001867 EXPECT_LF_HERE(ptr, http_msg_invalid);
1868 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001869 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001870
Willy Tarreau8973c702007-01-21 23:58:29 +01001871 /*
1872 * Common states below
1873 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001874 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001875 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001876 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001877 if (likely(!HTTP_IS_CRLF(*ptr))) {
1878 goto http_msg_hdr_name;
1879 }
1880
1881 if (likely(*ptr == '\r'))
1882 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1883 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001884
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001885 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001886 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001887 /* assumes msg->sol points to the first char */
1888 if (likely(HTTP_IS_TOKEN(*ptr)))
1889 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001890
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001891 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001892 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001893
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001894 if (likely(msg->err_pos < -1) || *ptr == '\n')
1895 goto http_msg_invalid;
1896
1897 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001898 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001899
1900 /* and we still accept this non-token character */
1901 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001902
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001903 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001904 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001905 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001906 if (likely(HTTP_IS_SPHT(*ptr)))
1907 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001908
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001909 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001910 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001911
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001912 if (likely(!HTTP_IS_CRLF(*ptr))) {
1913 goto http_msg_hdr_val;
1914 }
1915
1916 if (likely(*ptr == '\r'))
1917 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1918 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001919
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001920 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001921 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001922 EXPECT_LF_HERE(ptr, http_msg_invalid);
1923 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001924
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001925 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001926 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001927 if (likely(HTTP_IS_SPHT(*ptr))) {
1928 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001929 for (; buf->p + msg->sov < ptr; msg->sov++)
1930 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001931 goto http_msg_hdr_l1_sp;
1932 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001933 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001934 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001935 goto http_msg_complete_header;
1936
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001937 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001938 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001939 /* assumes msg->sol points to the first char, and msg->sov
1940 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001941 */
Willy Tarreau0431f9d2016-11-05 17:35:40 +01001942
1943 /* speedup: we'll skip packs of 4 or 8 bytes not containing bytes 0x0D
1944 * and lower. In fact since most of the time is spent in the loop, we
1945 * also remove the sign bit test so that bytes 0x8e..0x0d break the
1946 * loop, but we don't care since they're very rare in header values.
1947 */
1948#if defined(__x86_64__)
1949 while (ptr <= end - sizeof(long)) {
1950 if ((*(long *)ptr - 0x0e0e0e0e0e0e0e0eULL) & 0x8080808080808080ULL)
1951 goto http_msg_hdr_val2;
1952 ptr += sizeof(long);
1953 }
1954#endif
1955#if defined(__x86_64__) || \
1956 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
1957 defined(__ARM_ARCH_7A__)
1958 while (ptr <= end - sizeof(int)) {
1959 if ((*(int*)ptr - 0x0e0e0e0e) & 0x80808080)
1960 goto http_msg_hdr_val2;
1961 ptr += sizeof(int);
1962 }
1963#endif
1964 http_msg_hdr_val2:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001965 if (likely(!HTTP_IS_CRLF(*ptr)))
Willy Tarreau0431f9d2016-11-05 17:35:40 +01001966 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val2, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001967
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001968 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001969 /* Note: we could also copy eol into ->eoh so that we have the
1970 * real header end in case it ends with lots of LWS, but is this
1971 * really needed ?
1972 */
1973 if (likely(*ptr == '\r'))
1974 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1975 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001976
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001977 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001978 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001979 EXPECT_LF_HERE(ptr, http_msg_invalid);
1980 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001981
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001982 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001983 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001984 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1985 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001986 for (; buf->p + msg->eol < ptr; msg->eol++)
1987 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001988 goto http_msg_hdr_val;
1989 }
1990 http_msg_complete_header:
1991 /*
1992 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001993 * Assumes msg->sol points to the first char, msg->sov points
1994 * to the first character of the value and msg->eol to the
1995 * first CR or LF so we know how the line ends. We insert last
1996 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001997 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001998 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001999 idx, idx->tail) < 0))
2000 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002001
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002002 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002003 if (likely(!HTTP_IS_CRLF(*ptr))) {
2004 goto http_msg_hdr_name;
2005 }
2006
2007 if (likely(*ptr == '\r'))
2008 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
2009 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002010
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002011 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002012 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01002013 /* Assumes msg->sol points to the first of either CR or LF.
2014 * Sets ->sov and ->next to the total header length, ->eoh to
2015 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
2016 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002017 EXPECT_LF_HERE(ptr, http_msg_invalid);
2018 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002019 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01002020 msg->eoh = msg->sol;
2021 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01002022 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002023 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002024 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02002025
2026 case HTTP_MSG_ERROR:
2027 /* this may only happen if we call http_msg_analyser() twice with an error */
2028 break;
2029
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002030 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01002031#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002032 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
2033 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002034#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01002035 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002036 }
2037 http_msg_ood:
2038 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002039 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002040 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002041 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002042
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002043 http_msg_invalid:
2044 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002045 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002046 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002047 return;
2048}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002049
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002050/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
2051 * conversion succeeded, 0 in case of error. If the request was already 1.X,
2052 * nothing is done and 1 is returned.
2053 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002054static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002055{
2056 int delta;
2057 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002058 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002059
2060 if (msg->sl.rq.v_l != 0)
2061 return 1;
2062
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03002063 /* RFC 1945 allows only GET for HTTP/0.9 requests */
2064 if (txn->meth != HTTP_METH_GET)
2065 return 0;
2066
Willy Tarreau9b28e032012-10-12 23:49:43 +02002067 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002068
2069 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03002070 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
2071 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002072 }
2073 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002074 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01002075 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002076 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02002077 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002078 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002079 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002080 NULL, NULL);
2081 if (unlikely(!cur_end))
2082 return 0;
2083
2084 /* we have a full HTTP/1.0 request now and we know that
2085 * we have either a CR or an LF at <ptr>.
2086 */
2087 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
2088 return 1;
2089}
2090
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002091/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002092 * and "keep-alive" values. If we already know that some headers may safely
2093 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002094 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
2095 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01002096 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002097 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
2098 * found, and TX_CON_*_SET is adjusted depending on what is left so only
2099 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002100 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01002101 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002102void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01002103{
Willy Tarreau5b154472009-12-21 20:11:07 +01002104 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002105 const char *hdr_val = "Connection";
2106 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01002107
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002108 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01002109 return;
2110
Willy Tarreau88d349d2010-01-25 12:15:43 +01002111 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2112 hdr_val = "Proxy-Connection";
2113 hdr_len = 16;
2114 }
2115
Willy Tarreau5b154472009-12-21 20:11:07 +01002116 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002117 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002118 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002119 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2120 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002121 if (to_del & 2)
2122 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002123 else
2124 txn->flags |= TX_CON_KAL_SET;
2125 }
2126 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2127 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002128 if (to_del & 1)
2129 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002130 else
2131 txn->flags |= TX_CON_CLO_SET;
2132 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01002133 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
2134 txn->flags |= TX_HDR_CONN_UPG;
2135 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002136 }
2137
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002138 txn->flags |= TX_HDR_CONN_PRS;
2139 return;
2140}
Willy Tarreau5b154472009-12-21 20:11:07 +01002141
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002142/* Apply desired changes on the Connection: header. Values may be removed and/or
2143 * added depending on the <wanted> flags, which are exclusively composed of
2144 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2145 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2146 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002147void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002148{
2149 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002150 const char *hdr_val = "Connection";
2151 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002152
2153 ctx.idx = 0;
2154
Willy Tarreau88d349d2010-01-25 12:15:43 +01002155
2156 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2157 hdr_val = "Proxy-Connection";
2158 hdr_len = 16;
2159 }
2160
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002161 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002162 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002163 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2164 if (wanted & TX_CON_KAL_SET)
2165 txn->flags |= TX_CON_KAL_SET;
2166 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002167 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002168 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002169 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2170 if (wanted & TX_CON_CLO_SET)
2171 txn->flags |= TX_CON_CLO_SET;
2172 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002173 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002174 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002175 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002176
2177 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2178 return;
2179
2180 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2181 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002182 hdr_val = "Connection: close";
2183 hdr_len = 17;
2184 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2185 hdr_val = "Proxy-Connection: close";
2186 hdr_len = 23;
2187 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002188 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002189 }
2190
2191 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2192 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002193 hdr_val = "Connection: keep-alive";
2194 hdr_len = 22;
2195 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2196 hdr_val = "Proxy-Connection: keep-alive";
2197 hdr_len = 28;
2198 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002199 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002200 }
2201 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002202}
2203
Christopher Faulet113f7de2015-12-14 14:52:13 +01002204/* Parse the chunk size at msg->next. Once done, caller should adjust ->next to
2205 * point to the first byte of data after the chunk size, so that we know we can
2206 * forward exactly msg->next bytes. msg->sol contains the exact number of bytes
2207 * forming the chunk size. That way it is always possible to differentiate
2208 * between the start of the body and the start of the data. Return the number
2209 * of byte parsed on success, 0 when some data is missing, <0 on error. Note:
2210 * this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002211 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002212static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002213{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002214 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002215 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002216 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002217 const char *end = buf->data + buf->size;
2218 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01002219 unsigned int chunk = 0;
2220
2221 /* The chunk size is in the following form, though we are only
2222 * interested in the size and CRLF :
2223 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2224 */
2225 while (1) {
2226 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002227 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002228 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002229 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002230 if (c < 0) /* not a hex digit anymore */
2231 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002232 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002233 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002234 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002235 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002236 chunk = (chunk << 4) + c;
2237 }
2238
Willy Tarreaud98cf932009-12-27 22:54:55 +01002239 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002240 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002241 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002242
Willy Tarreau2235b262016-11-05 15:50:20 +01002243 while (HTTP_IS_SPHT(*ptr)) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01002244 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002245 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002246 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002247 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002248 }
2249
Willy Tarreaud98cf932009-12-27 22:54:55 +01002250 /* Up to there, we know that at least one byte is present at *ptr. Check
2251 * for the end of chunk size.
2252 */
2253 while (1) {
2254 if (likely(HTTP_IS_CRLF(*ptr))) {
2255 /* we now have a CR or an LF at ptr */
2256 if (likely(*ptr == '\r')) {
2257 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002258 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002259 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002260 return 0;
2261 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002262
Willy Tarreaud98cf932009-12-27 22:54:55 +01002263 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002264 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002265 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002266 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002267 /* done */
2268 break;
2269 }
2270 else if (*ptr == ';') {
2271 /* chunk extension, ends at next CRLF */
2272 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002273 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002274 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002275 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002276
2277 while (!HTTP_IS_CRLF(*ptr)) {
2278 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002279 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002280 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002281 return 0;
2282 }
2283 /* we have a CRLF now, loop above */
2284 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002285 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002286 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002287 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002288 }
2289
Christopher Faulet113f7de2015-12-14 14:52:13 +01002290 /* OK we found our CRLF and now <ptr> points to the next byte, which may
2291 * or may not be present. We save the number of bytes parsed into
2292 * msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002293 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002294 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002295 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002296 msg->sol += buf->size;
Willy Tarreau124d9912011-03-01 20:30:48 +01002297 msg->chunk_len = chunk;
2298 msg->body_len += chunk;
Christopher Faulet113f7de2015-12-14 14:52:13 +01002299 return msg->sol;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002300 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002301 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002302 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002303}
2304
Christopher Faulet113f7de2015-12-14 14:52:13 +01002305/* This function skips trailers in the buffer associated with HTTP message
2306 * <msg>. The first visited position is msg->next. If the end of the trailers is
2307 * found, the function returns >0. So, the caller can automatically schedul it
2308 * to be forwarded, and switch msg->msg_state to HTTP_MSG_DONE. If not enough
2309 * data are available, the function does not change anything except maybe
2310 * msg->sol if it could parse some lines, and returns zero. If a parse error
2311 * is encountered, the function returns < 0 and does not change anything except
2312 * maybe msg->sol. Note that the message must already be in HTTP_MSG_TRAILERS
2313 * state before calling this function, which implies that all non-trailers data
2314 * have already been scheduled for forwarding, and that msg->next exactly
2315 * matches the length of trailers already parsed and not forwarded. It is also
2316 * important to note that this function is designed to be able to parse wrapped
2317 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002318 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002319static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002320{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002321 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002322
Christopher Faulet113f7de2015-12-14 14:52:13 +01002323 /* we have msg->next which points to next line. Look for CRLF. But
2324 * first, we reset msg->sol */
2325 msg->sol = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002326 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002327 const char *p1 = NULL, *p2 = NULL;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002328 const char *start = b_ptr(buf, msg->next + msg->sol);
2329 const char *stop = bi_end(buf);
2330 const char *ptr = start;
2331 int bytes = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002332
2333 /* scan current line and stop at LF or CRLF */
2334 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002335 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002336 return 0;
2337
2338 if (*ptr == '\n') {
2339 if (!p1)
2340 p1 = ptr;
2341 p2 = ptr;
2342 break;
2343 }
2344
2345 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002346 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002347 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002348 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002349 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002350 p1 = ptr;
2351 }
2352
2353 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002354 if (ptr >= buf->data + buf->size)
2355 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002356 }
2357
2358 /* after LF; point to beginning of next line */
2359 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002360 if (p2 >= buf->data + buf->size)
2361 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002362
Christopher Faulet2fb28802015-12-01 10:40:57 +01002363 bytes = p2 - start;
Willy Tarreau638cd022010-01-03 07:42:04 +01002364 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002365 bytes += buf->size;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002366 msg->sol += bytes;
Willy Tarreau638cd022010-01-03 07:42:04 +01002367
Christopher Fauletd7c91962015-04-30 11:48:27 +02002368 /* LF/CRLF at beginning of line => end of trailers at p2.
2369 * Everything was scheduled for forwarding, there's nothing left
Christopher Faulet2fb28802015-12-01 10:40:57 +01002370 * from this message. */
2371 if (p1 == start)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002372 return 1;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002373
Willy Tarreaud98cf932009-12-27 22:54:55 +01002374 /* OK, next line then */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002375 }
2376}
2377
Christopher Faulet113f7de2015-12-14 14:52:13 +01002378/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
2379 * a possible LF alone at the end of a chunk. The caller should adjust msg->next
2380 * in order to include this part into the next forwarding phase. Note that the
2381 * caller must ensure that ->p points to the first byte to parse. It returns
2382 * the number of bytes parsed on success, so the caller can set msg_state to
2383 * HTTP_MSG_CHUNK_SIZE. If not enough data are available, the function does not
2384 * change anything and returns zero. If a parse error is encountered, the
2385 * function returns < 0. Note: this function is designed to parse wrapped CRLF
2386 * at the end of the buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002387 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002388static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002389{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002390 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002391 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002392 int bytes;
2393
2394 /* NB: we'll check data availabilty at the end. It's not a
2395 * problem because whatever we match first will be checked
2396 * against the correct length.
2397 */
2398 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002399 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002400 if (*ptr == '\r') {
2401 bytes++;
2402 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002403 if (ptr >= buf->data + buf->size)
2404 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002405 }
2406
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002407 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002408 return 0;
2409
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002410 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002411 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002412 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002413 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01002414 return bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002415}
Willy Tarreau5b154472009-12-21 20:11:07 +01002416
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002417/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2418 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2419 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2420 * Unparsable qvalues return 1000 as "q=1.000".
2421 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002422int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002423{
2424 int q = 1000;
2425
Willy Tarreau506c69a2014-07-08 00:59:48 +02002426 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002427 goto out;
2428 q = (*qvalue++ - '0') * 1000;
2429
2430 if (*qvalue++ != '.')
2431 goto out;
2432
Willy Tarreau506c69a2014-07-08 00:59:48 +02002433 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002434 goto out;
2435 q += (*qvalue++ - '0') * 100;
2436
Willy Tarreau506c69a2014-07-08 00:59:48 +02002437 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002438 goto out;
2439 q += (*qvalue++ - '0') * 10;
2440
Willy Tarreau506c69a2014-07-08 00:59:48 +02002441 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002442 goto out;
2443 q += (*qvalue++ - '0') * 1;
2444 out:
2445 if (q > 1000)
2446 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002447 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002448 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002449 return q;
2450}
William Lallemand82fe75c2012-10-23 10:25:10 +02002451
Willy Tarreau87b09662015-04-03 00:22:06 +02002452void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002453{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002454 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002455 int tmp = TX_CON_WANT_KAL;
2456
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002457 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2458 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002459 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2460 tmp = TX_CON_WANT_TUN;
2461
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002462 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002463 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2464 tmp = TX_CON_WANT_TUN;
2465 }
2466
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002467 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002468 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2469 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002470 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002471 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2472 tmp = TX_CON_WANT_CLO;
2473 else
2474 tmp = TX_CON_WANT_SCL;
2475 }
2476
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002477 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002478 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2479 tmp = TX_CON_WANT_CLO;
2480
2481 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2482 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2483
2484 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2485 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2486 /* parse the Connection header and possibly clean it */
2487 int to_del = 0;
2488 if ((msg->flags & HTTP_MSGF_VER_11) ||
2489 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002490 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002491 to_del |= 2; /* remove "keep-alive" */
2492 if (!(msg->flags & HTTP_MSGF_VER_11))
2493 to_del |= 1; /* remove "close" */
2494 http_parse_connection_header(txn, msg, to_del);
2495 }
2496
2497 /* check if client or config asks for explicit close in KAL/SCL */
2498 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2499 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2500 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2501 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2502 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002503 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002504 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2505}
William Lallemand82fe75c2012-10-23 10:25:10 +02002506
Willy Tarreaud787e662009-07-07 10:14:51 +02002507/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2508 * processing can continue on next analysers, or zero if it either needs more
2509 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002510 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002511 * when it has nothing left to do, and may remove any analyser when it wants to
2512 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002513 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002514int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002515{
Willy Tarreau59234e92008-11-30 23:51:27 +01002516 /*
2517 * We will parse the partial (or complete) lines.
2518 * We will check the request syntax, and also join multi-line
2519 * headers. An index of all the lines will be elaborated while
2520 * parsing.
2521 *
2522 * For the parsing, we use a 28 states FSM.
2523 *
2524 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002525 * req->buf->p = beginning of request
2526 * req->buf->p + msg->eoh = end of processed headers / start of current one
2527 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002528 * msg->eol = end of current header or line (LF or CRLF)
2529 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002530 *
2531 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02002532 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002533 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2534 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002535 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002536
Willy Tarreau59234e92008-11-30 23:51:27 +01002537 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002538 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002539 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01002540 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002541 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002542
Willy Tarreau87b09662015-04-03 00:22:06 +02002543 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 +01002544 now_ms, __FUNCTION__,
2545 s,
2546 req,
2547 req->rex, req->wex,
2548 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002549 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002550 req->analysers);
2551
Willy Tarreau52a0c602009-08-16 22:45:38 +02002552 /* we're speaking HTTP here, so let's speak HTTP to the client */
2553 s->srv_error = http_return_srv_error;
2554
Willy Tarreau83e3af02009-12-28 17:39:57 +01002555 /* There's a protected area at the end of the buffer for rewriting
2556 * purposes. We don't want to start to parse the request if the
2557 * protected area is affected, because we may have to move processed
2558 * data later, which is much more complicated.
2559 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002560 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002561
2562 /* This point is executed when some data is avalaible for analysis,
2563 * so we log the end of the idle time. */
2564 if (s->logs.t_idle == -1)
2565 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
2566
Willy Tarreau379357a2013-06-08 12:55:46 +02002567 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002568 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002569 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002570 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002571 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002572 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002573 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002574 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002575 return 0;
2576 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002577 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2578 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2579 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002580 }
2581
Willy Tarreau065e8332010-01-08 00:30:20 +01002582 /* Note that we have the same problem with the response ; we
2583 * may want to send a redirect, error or anything which requires
2584 * some spare space. So we'll ensure that we have at least
2585 * maxrewrite bytes available in the response buffer before
2586 * processing that one. This will only affect pipelined
2587 * keep-alive requests.
2588 */
2589 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002590 unlikely(!channel_is_rewritable(&s->res) ||
2591 bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) ||
2592 bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) {
2593 if (s->res.buf->o) {
2594 if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002595 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002596 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002597 channel_dont_connect(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002598 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
2599 s->res.flags |= CF_WAKE_WRITE;
2600 s->res.analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002601 return 0;
2602 }
2603 }
2604
Willy Tarreau9b28e032012-10-12 23:49:43 +02002605 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002606 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002607 }
2608
Willy Tarreau59234e92008-11-30 23:51:27 +01002609 /* 1: we might have to print this header in debug mode */
2610 if (unlikely((global.mode & MODE_DEBUG) &&
2611 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002612 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002613 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002614
Willy Tarreau9b28e032012-10-12 23:49:43 +02002615 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002616 /* this is a bit complex : in case of error on the request line,
2617 * we know that rq.l is still zero, so we display only the part
2618 * up to the end of the line (truncated by debug_hdr).
2619 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002620 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002621 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002622
Willy Tarreau59234e92008-11-30 23:51:27 +01002623 sol += hdr_idx_first_pos(&txn->hdr_idx);
2624 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002625
Willy Tarreau59234e92008-11-30 23:51:27 +01002626 while (cur_idx) {
2627 eol = sol + txn->hdr_idx.v[cur_idx].len;
2628 debug_hdr("clihdr", s, sol, eol);
2629 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2630 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002631 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002632 }
2633
Willy Tarreau58f10d72006-12-04 02:26:12 +01002634
Willy Tarreau59234e92008-11-30 23:51:27 +01002635 /*
2636 * Now we quickly check if we have found a full valid request.
2637 * If not so, we check the FD and buffer states before leaving.
2638 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002639 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002640 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02002641 * on a keep-alive stream, if we encounter and error, close, t/o,
2642 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002643 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02002644 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002645 * Last, we may increase some tracked counters' http request errors on
2646 * the cases that are deliberately the client's fault. For instance,
2647 * a timeout or connection reset is not counted as an error. However
2648 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002649 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002650
Willy Tarreau655dce92009-11-08 13:10:58 +01002651 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002652 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002653 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002654 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002655 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002656 stream_inc_http_req_ctr(s);
2657 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002658 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002659 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002660 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002661
Willy Tarreau59234e92008-11-30 23:51:27 +01002662 /* 1: Since we are in header mode, if there's no space
2663 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02002664 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01002665 * must terminate it now.
2666 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002667 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002668 /* FIXME: check if URI is set and return Status
2669 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002670 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002671 stream_inc_http_req_ctr(s);
2672 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002673 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002674 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002675 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002676 goto return_bad_req;
2677 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002678
Willy Tarreau59234e92008-11-30 23:51:27 +01002679 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002680 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002681 if (!(s->flags & SF_ERR_MASK))
2682 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002683
Willy Tarreaufcffa692010-01-10 14:21:19 +01002684 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002685 goto failed_keep_alive;
2686
Willy Tarreau0f228a02015-05-01 15:37:53 +02002687 if (sess->fe->options & PR_O_IGNORE_PRB)
2688 goto failed_keep_alive;
2689
Willy Tarreau59234e92008-11-30 23:51:27 +01002690 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002691 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002692 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002693 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002694 }
2695
Willy Tarreaudc979f22012-12-04 10:39:01 +01002696 txn->status = 400;
Willy Tarreau59234e92008-11-30 23:51:27 +01002697 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002698 http_reply_and_close(s, txn->status, NULL);
Christopher Fauletd7c91962015-04-30 11:48:27 +02002699 req->analysers &= AN_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02002700 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002701 proxy_inc_fe_req_ctr(sess->fe);
2702 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002703 if (sess->listener->counters)
2704 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002705
Willy Tarreaue7dff022015-04-03 01:14:29 +02002706 if (!(s->flags & SF_FINST_MASK))
2707 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002708 return 0;
2709 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002710
Willy Tarreau59234e92008-11-30 23:51:27 +01002711 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002712 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002713 if (!(s->flags & SF_ERR_MASK))
2714 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002715
Willy Tarreaufcffa692010-01-10 14:21:19 +01002716 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002717 goto failed_keep_alive;
2718
Willy Tarreau0f228a02015-05-01 15:37:53 +02002719 if (sess->fe->options & PR_O_IGNORE_PRB)
2720 goto failed_keep_alive;
2721
Willy Tarreau59234e92008-11-30 23:51:27 +01002722 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002723 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002724 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002725 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002726 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002727 txn->status = 408;
Willy Tarreau59234e92008-11-30 23:51:27 +01002728 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002729 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_408));
Christopher Fauletd7c91962015-04-30 11:48:27 +02002730 req->analysers &= AN_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002731
Willy Tarreau87b09662015-04-03 00:22:06 +02002732 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002733 proxy_inc_fe_req_ctr(sess->fe);
2734 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002735 if (sess->listener->counters)
2736 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002737
Willy Tarreaue7dff022015-04-03 01:14:29 +02002738 if (!(s->flags & SF_FINST_MASK))
2739 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002740 return 0;
2741 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002742
Willy Tarreau59234e92008-11-30 23:51:27 +01002743 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002744 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002745 if (!(s->flags & SF_ERR_MASK))
2746 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002747
Willy Tarreaufcffa692010-01-10 14:21:19 +01002748 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002749 goto failed_keep_alive;
2750
Willy Tarreau0f228a02015-05-01 15:37:53 +02002751 if (sess->fe->options & PR_O_IGNORE_PRB)
2752 goto failed_keep_alive;
2753
Willy Tarreau4076a152009-04-02 15:18:36 +02002754 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002755 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002756 txn->status = 400;
Willy Tarreau59234e92008-11-30 23:51:27 +01002757 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002758 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Christopher Fauletd7c91962015-04-30 11:48:27 +02002759 req->analysers &= AN_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02002760 stream_inc_http_err_ctr(s);
2761 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002762 proxy_inc_fe_req_ctr(sess->fe);
2763 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002764 if (sess->listener->counters)
2765 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002766
Willy Tarreaue7dff022015-04-03 01:14:29 +02002767 if (!(s->flags & SF_FINST_MASK))
2768 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002769 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002770 }
2771
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002772 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002773 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002774 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002775#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002776 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
2777 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002778 /* We need more data, we have to re-enable quick-ack in case we
2779 * previously disabled it, otherwise we might cause the client
2780 * to delay next data.
2781 */
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002782 setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002783 }
2784#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002785
Willy Tarreaufcffa692010-01-10 14:21:19 +01002786 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2787 /* If the client starts to talk, let's fall back to
2788 * request timeout processing.
2789 */
2790 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002791 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002792 }
2793
Willy Tarreau59234e92008-11-30 23:51:27 +01002794 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002795 if (!tick_isset(req->analyse_exp)) {
2796 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2797 (txn->flags & TX_WAIT_NEXT_RQ) &&
2798 tick_isset(s->be->timeout.httpka))
2799 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2800 else
2801 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2802 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002803
Willy Tarreau59234e92008-11-30 23:51:27 +01002804 /* we're not ready yet */
2805 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002806
2807 failed_keep_alive:
2808 /* Here we process low-level errors for keep-alive requests. In
2809 * short, if the request is not the first one and it experiences
2810 * a timeout, read error or shutdown, we just silently close so
2811 * that the client can try again.
2812 */
2813 txn->status = 0;
2814 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Fauletd7c91962015-04-30 11:48:27 +02002815 req->analysers &= AN_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002816 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002817 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002818 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002819 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002820 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002821 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002822
Willy Tarreaud787e662009-07-07 10:14:51 +02002823 /* OK now we have a complete HTTP request with indexed headers. Let's
2824 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002825 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002826 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002827 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002828 * byte after the last LF. msg->sov points to the first byte of data.
2829 * msg->eol cannot be trusted because it may have been left uninitialized
2830 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002831 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002832
Willy Tarreau87b09662015-04-03 00:22:06 +02002833 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002834 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002835
Willy Tarreaub16a5742010-01-10 14:46:16 +01002836 if (txn->flags & TX_WAIT_NEXT_RQ) {
2837 /* kill the pending keep-alive timeout */
2838 txn->flags &= ~TX_WAIT_NEXT_RQ;
2839 req->analyse_exp = TICK_ETERNITY;
2840 }
2841
2842
Willy Tarreaud787e662009-07-07 10:14:51 +02002843 /* Maybe we found in invalid header name while we were configured not
2844 * to block on that, so we have to capture it now.
2845 */
2846 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002847 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002848
Willy Tarreau59234e92008-11-30 23:51:27 +01002849 /*
2850 * 1: identify the method
2851 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002852 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002853
2854 /* we can make use of server redirect on GET and HEAD */
2855 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002856 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002857
Willy Tarreau59234e92008-11-30 23:51:27 +01002858 /*
2859 * 2: check if the URI matches the monitor_uri.
2860 * We have to do this for every request which gets in, because
2861 * the monitor-uri is defined by the frontend.
2862 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002863 if (unlikely((sess->fe->monitor_uri_len != 0) &&
2864 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002865 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002866 sess->fe->monitor_uri,
2867 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002868 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002869 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002870 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002871 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002872
Willy Tarreaue7dff022015-04-03 01:14:29 +02002873 s->flags |= SF_MONITOR;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002874 sess->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002875
Willy Tarreau59234e92008-11-30 23:51:27 +01002876 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002877 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002878 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002879
Willy Tarreau59234e92008-11-30 23:51:27 +01002880 ret = acl_pass(ret);
2881 if (cond->pol == ACL_COND_UNLESS)
2882 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002883
Willy Tarreau59234e92008-11-30 23:51:27 +01002884 if (ret) {
2885 /* we fail this request, let's return 503 service unavail */
2886 txn->status = 503;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002887 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_503));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002888 if (!(s->flags & SF_ERR_MASK))
2889 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002890 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002891 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002892 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002893
Willy Tarreau59234e92008-11-30 23:51:27 +01002894 /* nothing to fail, let's reply normaly */
2895 txn->status = 200;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002896 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_200));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002897 if (!(s->flags & SF_ERR_MASK))
2898 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002899 goto return_prx_cond;
2900 }
2901
2902 /*
2903 * 3: Maybe we have to copy the original REQURI for the logs ?
2904 * Note: we cannot log anymore if the request has been
2905 * classified as invalid.
2906 */
2907 if (unlikely(s->logs.logwait & LW_REQ)) {
2908 /* we have a complete HTTP request that we must log */
2909 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2910 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002911
Willy Tarreau59234e92008-11-30 23:51:27 +01002912 if (urilen >= REQURI_LEN)
2913 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002914 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002915 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002916
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002917 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002918 s->do_log(s);
2919 } else {
2920 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002921 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002922 }
Willy Tarreau06619262006-12-17 08:37:22 +01002923
Willy Tarreau91852eb2015-05-01 13:26:00 +02002924 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
2925 * exactly one digit "." one digit. This check may be disabled using
2926 * option accept-invalid-http-request.
2927 */
2928 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
2929 if (msg->sl.rq.v_l != 8) {
2930 msg->err_pos = msg->sl.rq.v;
2931 goto return_bad_req;
2932 }
2933
2934 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
2935 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
2936 req->buf->p[msg->sl.rq.v + 6] != '.' ||
2937 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
2938 msg->err_pos = msg->sl.rq.v + 4;
2939 goto return_bad_req;
2940 }
2941 }
Willy Tarreau13317662015-05-01 13:47:08 +02002942 else {
2943 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
2944 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
2945 goto return_bad_req;
2946 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02002947
Willy Tarreau5b154472009-12-21 20:11:07 +01002948 /* ... and check if the request is HTTP/1.1 or above */
2949 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002950 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2951 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2952 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002953 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002954
2955 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002956 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 +01002957
Willy Tarreau88d349d2010-01-25 12:15:43 +01002958 /* if the frontend has "option http-use-proxy-header", we'll check if
2959 * we have what looks like a proxied connection instead of a connection,
2960 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2961 * Note that this is *not* RFC-compliant, however browsers and proxies
2962 * happen to do that despite being non-standard :-(
2963 * We consider that a request not beginning with either '/' or '*' is
2964 * a proxied connection, which covers both "scheme://location" and
2965 * CONNECT ip:port.
2966 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002967 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002968 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002969 txn->flags |= TX_USE_PX_CONN;
2970
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002971 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002972 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002973
Willy Tarreau59234e92008-11-30 23:51:27 +01002974 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002975 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002976 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002977 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002978
Willy Tarreau557f1992015-05-01 10:05:17 +02002979 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
2980 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002981 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002982 * The length of a message body is determined by one of the following
2983 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02002984 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002985 * 1. Any response to a HEAD request and any response with a 1xx
2986 * (Informational), 204 (No Content), or 304 (Not Modified) status
2987 * code is always terminated by the first empty line after the
2988 * header fields, regardless of the header fields present in the
2989 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002990 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002991 * 2. Any 2xx (Successful) response to a CONNECT request implies that
2992 * the connection will become a tunnel immediately after the empty
2993 * line that concludes the header fields. A client MUST ignore any
2994 * Content-Length or Transfer-Encoding header fields received in
2995 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002996 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002997 * 3. If a Transfer-Encoding header field is present and the chunked
2998 * transfer coding (Section 4.1) is the final encoding, the message
2999 * body length is determined by reading and decoding the chunked
3000 * data until the transfer coding indicates the data is complete.
3001 *
3002 * If a Transfer-Encoding header field is present in a response and
3003 * the chunked transfer coding is not the final encoding, the
3004 * message body length is determined by reading the connection until
3005 * it is closed by the server. If a Transfer-Encoding header field
3006 * is present in a request and the chunked transfer coding is not
3007 * the final encoding, the message body length cannot be determined
3008 * reliably; the server MUST respond with the 400 (Bad Request)
3009 * status code and then close the connection.
3010 *
3011 * If a message is received with both a Transfer-Encoding and a
3012 * Content-Length header field, the Transfer-Encoding overrides the
3013 * Content-Length. Such a message might indicate an attempt to
3014 * perform request smuggling (Section 9.5) or response splitting
3015 * (Section 9.4) and ought to be handled as an error. A sender MUST
3016 * remove the received Content-Length field prior to forwarding such
3017 * a message downstream.
3018 *
3019 * 4. If a message is received without Transfer-Encoding and with
3020 * either multiple Content-Length header fields having differing
3021 * field-values or a single Content-Length header field having an
3022 * invalid value, then the message framing is invalid and the
3023 * recipient MUST treat it as an unrecoverable error. If this is a
3024 * request message, the server MUST respond with a 400 (Bad Request)
3025 * status code and then close the connection. If this is a response
3026 * message received by a proxy, the proxy MUST close the connection
3027 * to the server, discard the received response, and send a 502 (Bad
3028 * Gateway) response to the client. If this is a response message
3029 * received by a user agent, the user agent MUST close the
3030 * connection to the server and discard the received response.
3031 *
3032 * 5. If a valid Content-Length header field is present without
3033 * Transfer-Encoding, its decimal value defines the expected message
3034 * body length in octets. If the sender closes the connection or
3035 * the recipient times out before the indicated number of octets are
3036 * received, the recipient MUST consider the message to be
3037 * incomplete and close the connection.
3038 *
3039 * 6. If this is a request message and none of the above are true, then
3040 * the message body length is zero (no message body is present).
3041 *
3042 * 7. Otherwise, this is a response message without a declared message
3043 * body length, so the message body length is determined by the
3044 * number of octets received prior to the server closing the
3045 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003046 */
3047
Willy Tarreau32b47f42009-10-18 20:55:02 +02003048 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003049 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02003050 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003051 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003052 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
3053 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02003054 /* chunked not last, return badreq */
3055 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003056 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003057 }
3058
Willy Tarreau1c913912015-04-30 10:57:51 +02003059 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02003060 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02003061 if (msg->flags & HTTP_MSGF_TE_CHNK) {
3062 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
3063 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3064 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02003065 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003066 signed long long cl;
3067
Willy Tarreauad14f752011-09-02 20:33:27 +02003068 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003069 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003070 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003071 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003072
Willy Tarreauad14f752011-09-02 20:33:27 +02003073 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003074 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003075 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003076 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003077
Willy Tarreauad14f752011-09-02 20:33:27 +02003078 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003079 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003080 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003081 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003082
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003083 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003084 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003085 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003086 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003087
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003088 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003089 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003090 }
3091
Willy Tarreau34dfc602015-05-01 10:09:49 +02003092 /* even bodyless requests have a known length */
3093 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003094
Willy Tarreau179085c2014-04-28 16:48:56 +02003095 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3096 * only change if both the request and the config reference something else.
3097 * Option httpclose by itself sets tunnel mode where headers are mangled.
3098 * However, if another mode is set, it will affect it (eg: server-close/
3099 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3100 * if FE and BE have the same settings (common). The method consists in
3101 * checking if options changed between the two calls (implying that either
3102 * one is non-null, or one of them is non-null and we are there for the first
3103 * time.
3104 */
3105 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003106 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003107 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003108
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02003109 /* we may have to wait for the request's body */
3110 if ((s->be->options & PR_O_WREQ_BODY) &&
3111 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
3112 req->analysers |= AN_REQ_HTTP_BODY;
3113
Willy Tarreaud787e662009-07-07 10:14:51 +02003114 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003115 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003116 req->analyse_exp = TICK_ETERNITY;
3117 return 1;
3118
3119 return_bad_req:
3120 /* We centralize bad requests processing here */
3121 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3122 /* we detected a parsing error. We want to archive this request
3123 * in the dedicated proxy area for later troubleshooting.
3124 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003125 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003126 }
3127
3128 txn->req.msg_state = HTTP_MSG_ERROR;
3129 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003130 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003131
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003132 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003133 if (sess->listener->counters)
3134 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003135
3136 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003137 if (!(s->flags & SF_ERR_MASK))
3138 s->flags |= SF_ERR_PRXCOND;
3139 if (!(s->flags & SF_FINST_MASK))
3140 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02003141
Christopher Fauletd7c91962015-04-30 11:48:27 +02003142 req->analysers &= AN_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02003143 req->analyse_exp = TICK_ETERNITY;
3144 return 0;
3145}
3146
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003147
Willy Tarreau347a35d2013-11-22 17:51:09 +01003148/* This function prepares an applet to handle the stats. It can deal with the
3149 * "100-continue" expectation, check that admin rules are met for POST requests,
3150 * and program a response message if something was unexpected. It cannot fail
3151 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003152 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003153 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02003154 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003155 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003156int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003157{
3158 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003159 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02003160 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003161 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003162 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003163 struct uri_auth *uri_auth = s->be->uri_auth;
3164 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003165 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003166
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003167 appctx = si_appctx(si);
3168 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3169 appctx->st1 = appctx->st2 = 0;
3170 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3171 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003172 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003173 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003174
3175 uri = msg->chn->buf->p + msg->sl.rq.u;
3176 lookup = uri + uri_auth->uri_len;
3177
3178 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3179 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003180 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003181 break;
3182 }
3183 }
3184
3185 if (uri_auth->refresh) {
3186 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3187 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003188 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003189 break;
3190 }
3191 }
3192 }
3193
3194 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3195 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003196 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003197 break;
3198 }
3199 }
3200
Willy Tarreau1e62df92016-01-11 18:57:53 +01003201 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
3202 if (memcmp(h, ";typed", 6) == 0) {
3203 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
3204 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3205 break;
3206 }
3207 }
3208
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003209 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3210 if (memcmp(h, ";st=", 4) == 0) {
3211 int i;
3212 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003213 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003214 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3215 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003216 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003217 break;
3218 }
3219 }
3220 break;
3221 }
3222 }
3223
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003224 appctx->ctx.stats.scope_str = 0;
3225 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003226 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3227 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3228 int itx = 0;
3229 const char *h2;
3230 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3231 const char *err;
3232
3233 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3234 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003235 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003236 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3237 itx++;
3238 h++;
3239 }
3240
3241 if (itx > STAT_SCOPE_TXT_MAXLEN)
3242 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003243 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003244
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003245 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003246 memcpy(scope_txt, h2, itx);
3247 scope_txt[itx] = '\0';
3248 err = invalid_char(scope_txt);
3249 if (err) {
3250 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003251 appctx->ctx.stats.scope_str = 0;
3252 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003253 }
3254 break;
3255 }
3256 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003257
3258 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003259 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003260 int ret = 1;
3261
3262 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003263 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 +01003264 ret = acl_pass(ret);
3265 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3266 ret = !ret;
3267 }
3268
3269 if (ret) {
3270 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003271 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003272 break;
3273 }
3274 }
3275
3276 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003277 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003278 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003279 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003280 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
3281 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003282 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003283 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003284 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003285 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3286 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003287 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003288 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003289 else {
3290 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003291 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003292 }
3293
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003294 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003295 return 1;
3296}
3297
Lukas Tribus67db8df2013-06-23 17:37:13 +02003298/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3299 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3300 */
Vincent Bernat6e615892016-05-18 16:17:44 +02003301void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003302{
3303#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02003304 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003305 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3306#endif
3307#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02003308 if (from->ss_family == AF_INET6) {
3309 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02003310 /* v4-mapped addresses need IP_TOS */
3311 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3312 else
3313 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3314 }
3315#endif
3316}
3317
Willy Tarreau87b09662015-04-03 00:22:06 +02003318int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003319 const char* name, unsigned int name_len,
3320 const char *str, struct my_regex *re,
3321 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003322{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003323 struct hdr_ctx ctx;
3324 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003325 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003326 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3327 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003328 struct chunk *output = get_trash_chunk();
3329
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003330 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003331
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003332 /* Choose the header browsing function. */
3333 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003334 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003335 http_find_hdr_func = http_find_header2;
3336 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003337 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003338 http_find_hdr_func = http_find_full_header2;
3339 break;
3340 default: /* impossible */
3341 return -1;
3342 }
3343
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003344 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3345 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003346 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003347 char *val = ctx.line + ctx.val;
3348 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003349
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003350 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3351 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003352
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003353 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003354 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003355 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003356
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003357 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003358
3359 hdr->len += delta;
3360 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003361
3362 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003363 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003364 }
3365
3366 return 0;
3367}
3368
Willy Tarreau87b09662015-04-03 00:22:06 +02003369static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003370 const char* name, unsigned int name_len,
3371 struct list *fmt, struct my_regex *re,
3372 int action)
3373{
3374 struct chunk *replace = get_trash_chunk();
3375
3376 replace->len = build_logline(s, replace->str, replace->size, fmt);
3377 if (replace->len >= replace->size - 1)
3378 return -1;
3379
3380 return http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
3381}
3382
Willy Tarreau87b09662015-04-03 00:22:06 +02003383/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003384 * transaction <txn>. Returns the verdict of the first rule that prevents
3385 * further processing of the request (auth, deny, ...), and defaults to
3386 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3387 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02003388 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
3389 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
3390 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003391 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003392enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02003393http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003394{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003395 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003396 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003397 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003398 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003399 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003400 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02003401 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02003402 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003403
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003404 /* If "the current_rule_list" match the executed rule list, we are in
3405 * resume condition. If a resume is needed it is always in the action
3406 * and never in the ACL or converters. In this case, we initialise the
3407 * current rule, and go to the action execution point.
3408 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003409 if (s->current_rule) {
3410 rule = s->current_rule;
3411 s->current_rule = NULL;
3412 if (s->current_rule_list == rules)
3413 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003414 }
3415 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003416
Willy Tarreauff011f22011-01-06 17:51:27 +01003417 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003418
Willy Tarreau96257ec2012-12-27 10:46:37 +01003419 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003420 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003421 int ret;
3422
Willy Tarreau192252e2015-04-04 01:47:55 +02003423 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003424 ret = acl_pass(ret);
3425
Willy Tarreauff011f22011-01-06 17:51:27 +01003426 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003427 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003428
3429 if (!ret) /* condition not matched */
3430 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003431 }
3432
Willy Tarreauacc98002015-09-27 23:34:39 +02003433 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003434resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003435 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003436 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003437 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003438
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003439 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02003440 if (deny_status)
3441 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003442 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003443
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003444 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01003445 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003446 if (deny_status)
3447 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003448 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003449
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003450 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003451 /* Auth might be performed on regular http-req rules as well as on stats */
3452 auth_realm = rule->arg.auth.realm;
3453 if (!auth_realm) {
3454 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3455 auth_realm = STATS_DEFAULT_REALM;
3456 else
3457 auth_realm = px->id;
3458 }
3459 /* send 401/407 depending on whether we use a proxy or not. We still
3460 * count one error, because normal browsing won't significantly
3461 * increase the counter but brute force attempts will.
3462 */
3463 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3464 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003465 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003466 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003467 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003468
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003469 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003470 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3471 return HTTP_RULE_RES_BADREQ;
3472 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003473
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003474 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003475 s->task->nice = rule->arg.nice;
3476 break;
3477
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003478 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003479 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Vincent Bernat6e615892016-05-18 16:17:44 +02003480 inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003481 break;
3482
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003483 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003484#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003485 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003486 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003487#endif
3488 break;
3489
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003490 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003491 s->logs.level = rule->arg.loglevel;
3492 break;
3493
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003494 case ACT_HTTP_REPLACE_HDR:
3495 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003496 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3497 rule->arg.hdr_add.name_len,
3498 &rule->arg.hdr_add.fmt,
3499 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003500 return HTTP_RULE_RES_BADREQ;
3501 break;
3502
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003503 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003504 ctx.idx = 0;
3505 /* remove all occurrences of the header */
3506 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3507 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3508 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003509 }
Willy Tarreau85603282015-01-21 20:39:27 +01003510 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003511
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003512 case ACT_HTTP_SET_HDR:
3513 case ACT_HTTP_ADD_HDR:
Thierry Fournier4b788f72016-06-01 13:35:36 +02003514 /* The scope of the trash buffer must be limited to this function. The
3515 * build_logline() function can execute a lot of other function which
3516 * can use the trash buffer. So for limiting the scope of this global
3517 * buffer, we build first the header value using build_logline, and
3518 * after we store the header name.
3519 */
3520 len = rule->arg.hdr_add.name_len + 2,
3521 len += build_logline(s, trash.str + len, trash.size - len, &rule->arg.hdr_add.fmt);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003522 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
Thierry Fournier4b788f72016-06-01 13:35:36 +02003523 trash.str[rule->arg.hdr_add.name_len] = ':';
3524 trash.str[rule->arg.hdr_add.name_len + 1] = ' ';
3525 trash.len = len;
Willy Tarreau85603282015-01-21 20:39:27 +01003526
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003527 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003528 /* remove all occurrences of the header */
3529 ctx.idx = 0;
3530 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3531 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3532 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3533 }
3534 }
3535
Willy Tarreau96257ec2012-12-27 10:46:37 +01003536 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3537 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003538
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003539 case ACT_HTTP_DEL_ACL:
3540 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003541 struct pat_ref *ref;
3542 char *key;
3543 int len;
3544
3545 /* collect reference */
3546 ref = pat_ref_lookup(rule->arg.map.ref);
3547 if (!ref)
3548 continue;
3549
3550 /* collect key */
3551 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3552 key = trash.str;
3553 key[len] = '\0';
3554
3555 /* perform update */
3556 /* returned code: 1=ok, 0=ko */
3557 pat_ref_delete(ref, key);
3558
3559 break;
3560 }
3561
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003562 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003563 struct pat_ref *ref;
3564 char *key;
3565 struct chunk *trash_key;
3566 int len;
3567
3568 trash_key = get_trash_chunk();
3569
3570 /* collect reference */
3571 ref = pat_ref_lookup(rule->arg.map.ref);
3572 if (!ref)
3573 continue;
3574
3575 /* collect key */
3576 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3577 key = trash_key->str;
3578 key[len] = '\0';
3579
3580 /* perform update */
3581 /* add entry only if it does not already exist */
3582 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003583 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003584
3585 break;
3586 }
3587
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003588 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003589 struct pat_ref *ref;
3590 char *key, *value;
3591 struct chunk *trash_key, *trash_value;
3592 int len;
3593
3594 trash_key = get_trash_chunk();
3595 trash_value = get_trash_chunk();
3596
3597 /* collect reference */
3598 ref = pat_ref_lookup(rule->arg.map.ref);
3599 if (!ref)
3600 continue;
3601
3602 /* collect key */
3603 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3604 key = trash_key->str;
3605 key[len] = '\0';
3606
3607 /* collect value */
3608 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3609 value = trash_value->str;
3610 value[len] = '\0';
3611
3612 /* perform update */
3613 if (pat_ref_find_elt(ref, key) != NULL)
3614 /* update entry if it exists */
3615 pat_ref_set(ref, key, value, NULL);
3616 else
3617 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003618 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003619
3620 break;
3621 }
William Lallemand73025dd2014-04-24 14:38:37 +02003622
Thierry FOURNIER42148732015-09-02 17:17:33 +02003623 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003624 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3625 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003626
Willy Tarreauacc98002015-09-27 23:34:39 +02003627 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003628 case ACT_RET_ERR:
3629 case ACT_RET_CONT:
3630 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003631 case ACT_RET_STOP:
3632 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003633 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003634 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003635 return HTTP_RULE_RES_YIELD;
3636 }
William Lallemand73025dd2014-04-24 14:38:37 +02003637 break;
3638
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003639 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02003640 /* Note: only the first valid tracking parameter of each
3641 * applies.
3642 */
3643
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003644 if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02003645 struct stktable *t;
3646 struct stksess *ts;
3647 struct stktable_key *key;
3648 void *ptr;
3649
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02003650 t = rule->arg.trk_ctr.table.t;
3651 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 +02003652
3653 if (key && (ts = stktable_get_entry(t, key))) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003654 stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003655
3656 /* let's count a new HTTP request as it's the first time we do it */
3657 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3658 if (ptr)
3659 stktable_data_cast(ptr, http_req_cnt)++;
3660
3661 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3662 if (ptr)
3663 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3664 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3665
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003666 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003667 if (sess->fe != s->be)
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003668 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02003669 }
3670 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003671 break;
3672
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003673 /* other flags exists, but normaly, they never be matched. */
3674 default:
3675 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003676 }
3677 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003678
3679 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003680 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003681}
3682
Willy Tarreau71241ab2012-12-27 11:30:54 +01003683
Willy Tarreau51d861a2015-05-22 17:30:48 +02003684/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3685 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3686 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3687 * is returned, the process can continue the evaluation of next rule list. If
3688 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3689 * is returned, it means the operation could not be processed and a server error
3690 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3691 * deny rule. If *YIELD is returned, the caller must call again the function
3692 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003693 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003694static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003695http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003696{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003697 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003698 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003699 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003700 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003701 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02003702 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003703
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003704 /* If "the current_rule_list" match the executed rule list, we are in
3705 * resume condition. If a resume is needed it is always in the action
3706 * and never in the ACL or converters. In this case, we initialise the
3707 * current rule, and go to the action execution point.
3708 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003709 if (s->current_rule) {
3710 rule = s->current_rule;
3711 s->current_rule = NULL;
3712 if (s->current_rule_list == rules)
3713 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003714 }
3715 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003716
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003717 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003718
3719 /* check optional condition */
3720 if (rule->cond) {
3721 int ret;
3722
Willy Tarreau192252e2015-04-04 01:47:55 +02003723 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003724 ret = acl_pass(ret);
3725
3726 if (rule->cond->pol == ACL_COND_UNLESS)
3727 ret = !ret;
3728
3729 if (!ret) /* condition not matched */
3730 continue;
3731 }
3732
Willy Tarreauacc98002015-09-27 23:34:39 +02003733 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003734resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003735 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003736 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003737 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003738
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003739 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003740 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003741 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003742
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003743 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003744 s->task->nice = rule->arg.nice;
3745 break;
3746
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003747 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003748 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Vincent Bernat6e615892016-05-18 16:17:44 +02003749 inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003750 break;
3751
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003752 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003753#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003754 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003755 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003756#endif
3757 break;
3758
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003759 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003760 s->logs.level = rule->arg.loglevel;
3761 break;
3762
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003763 case ACT_HTTP_REPLACE_HDR:
3764 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003765 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3766 rule->arg.hdr_add.name_len,
3767 &rule->arg.hdr_add.fmt,
3768 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003769 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003770 break;
3771
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003772 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003773 ctx.idx = 0;
3774 /* remove all occurrences of the header */
3775 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3776 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3777 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3778 }
Willy Tarreau85603282015-01-21 20:39:27 +01003779 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003780
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003781 case ACT_HTTP_SET_HDR:
3782 case ACT_HTTP_ADD_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003783 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3784 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3785 trash.len = rule->arg.hdr_add.name_len;
3786 trash.str[trash.len++] = ':';
3787 trash.str[trash.len++] = ' ';
3788 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 +01003789
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003790 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003791 /* remove all occurrences of the header */
3792 ctx.idx = 0;
3793 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3794 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3795 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3796 }
3797 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003798 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3799 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003800
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003801 case ACT_HTTP_DEL_ACL:
3802 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003803 struct pat_ref *ref;
3804 char *key;
3805 int len;
3806
3807 /* collect reference */
3808 ref = pat_ref_lookup(rule->arg.map.ref);
3809 if (!ref)
3810 continue;
3811
3812 /* collect key */
3813 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3814 key = trash.str;
3815 key[len] = '\0';
3816
3817 /* perform update */
3818 /* returned code: 1=ok, 0=ko */
3819 pat_ref_delete(ref, key);
3820
3821 break;
3822 }
3823
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003824 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003825 struct pat_ref *ref;
3826 char *key;
3827 struct chunk *trash_key;
3828 int len;
3829
3830 trash_key = get_trash_chunk();
3831
3832 /* collect reference */
3833 ref = pat_ref_lookup(rule->arg.map.ref);
3834 if (!ref)
3835 continue;
3836
3837 /* collect key */
3838 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3839 key = trash_key->str;
3840 key[len] = '\0';
3841
3842 /* perform update */
3843 /* check if the entry already exists */
3844 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003845 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003846
3847 break;
3848 }
3849
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003850 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003851 struct pat_ref *ref;
3852 char *key, *value;
3853 struct chunk *trash_key, *trash_value;
3854 int len;
3855
3856 trash_key = get_trash_chunk();
3857 trash_value = get_trash_chunk();
3858
3859 /* collect reference */
3860 ref = pat_ref_lookup(rule->arg.map.ref);
3861 if (!ref)
3862 continue;
3863
3864 /* collect key */
3865 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3866 key = trash_key->str;
3867 key[len] = '\0';
3868
3869 /* collect value */
3870 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3871 value = trash_value->str;
3872 value[len] = '\0';
3873
3874 /* perform update */
3875 if (pat_ref_find_elt(ref, key) != NULL)
3876 /* update entry if it exists */
3877 pat_ref_set(ref, key, value, NULL);
3878 else
3879 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003880 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003881
3882 break;
3883 }
William Lallemand73025dd2014-04-24 14:38:37 +02003884
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003885 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003886 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3887 return HTTP_RULE_RES_BADREQ;
3888 return HTTP_RULE_RES_DONE;
3889
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003890 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3891 /* Note: only the first valid tracking parameter of each
3892 * applies.
3893 */
3894
3895 if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) {
3896 struct stktable *t;
3897 struct stksess *ts;
3898 struct stktable_key *key;
3899 void *ptr;
3900
3901 t = rule->arg.trk_ctr.table.t;
3902 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
3903
3904 if (key && (ts = stktable_get_entry(t, key))) {
3905 stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts);
3906
3907 /* let's count a new HTTP request as it's the first time we do it */
3908 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3909 if (ptr)
3910 stktable_data_cast(ptr, http_req_cnt)++;
3911
3912 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3913 if (ptr)
3914 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3915 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3916
3917 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3918 if (sess->fe != s->be)
3919 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3920
3921 /* When the client triggers a 4xx from the server, it's most often due
3922 * to a missing object or permission. These events should be tracked
3923 * because if they happen often, it may indicate a brute force or a
3924 * vulnerability scan. Normally this is done when receiving the response
3925 * but here we're tracking after this ought to have been done so we have
3926 * to do it on purpose.
3927 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02003928 if ((unsigned)(txn->status - 400) < 100) {
3929 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3930 if (ptr)
3931 stktable_data_cast(ptr, http_err_cnt)++;
3932
3933 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3934 if (ptr)
3935 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3936 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3937 }
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003938 }
3939 }
3940 break;
3941
Thierry FOURNIER42148732015-09-02 17:17:33 +02003942 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003943 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3944 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003945
Willy Tarreauacc98002015-09-27 23:34:39 +02003946 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003947 case ACT_RET_ERR:
3948 case ACT_RET_CONT:
3949 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003950 case ACT_RET_STOP:
3951 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003952 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003953 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003954 return HTTP_RULE_RES_YIELD;
3955 }
William Lallemand73025dd2014-04-24 14:38:37 +02003956 break;
3957
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003958 /* other flags exists, but normaly, they never be matched. */
3959 default:
3960 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003961 }
3962 }
3963
3964 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003965 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003966}
3967
3968
Willy Tarreau71241ab2012-12-27 11:30:54 +01003969/* Perform an HTTP redirect based on the information in <rule>. The function
3970 * returns non-zero on success, or zero in case of a, irrecoverable error such
3971 * as too large a request to build a valid response.
3972 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003973static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003974{
Willy Tarreaub329a312015-05-22 16:27:37 +02003975 struct http_msg *req = &txn->req;
3976 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003977 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003978 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003979
3980 /* build redirect message */
3981 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003982 case 308:
3983 msg_fmt = HTTP_308;
3984 break;
3985 case 307:
3986 msg_fmt = HTTP_307;
3987 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003988 case 303:
3989 msg_fmt = HTTP_303;
3990 break;
3991 case 301:
3992 msg_fmt = HTTP_301;
3993 break;
3994 case 302:
3995 default:
3996 msg_fmt = HTTP_302;
3997 break;
3998 }
3999
4000 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
4001 return 0;
4002
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004003 location = trash.str + trash.len;
4004
Willy Tarreau71241ab2012-12-27 11:30:54 +01004005 switch(rule->type) {
4006 case REDIRECT_TYPE_SCHEME: {
4007 const char *path;
4008 const char *host;
4009 struct hdr_ctx ctx;
4010 int pathlen;
4011 int hostlen;
4012
4013 host = "";
4014 hostlen = 0;
4015 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02004016 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004017 host = ctx.line + ctx.val;
4018 hostlen = ctx.vlen;
4019 }
4020
4021 path = http_get_path(txn);
4022 /* build message using path */
4023 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004024 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004025 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4026 int qs = 0;
4027 while (qs < pathlen) {
4028 if (path[qs] == '?') {
4029 pathlen = qs;
4030 break;
4031 }
4032 qs++;
4033 }
4034 }
4035 } else {
4036 path = "/";
4037 pathlen = 1;
4038 }
4039
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004040 if (rule->rdr_str) { /* this is an old "redirect" rule */
4041 /* check if we can add scheme + "://" + host + path */
4042 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
4043 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004044
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004045 /* add scheme */
4046 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4047 trash.len += rule->rdr_len;
4048 }
4049 else {
4050 /* add scheme with executing log format */
4051 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004052
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004053 /* check if we can add scheme + "://" + host + path */
4054 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
4055 return 0;
4056 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004057 /* add "://" */
4058 memcpy(trash.str + trash.len, "://", 3);
4059 trash.len += 3;
4060
4061 /* add host */
4062 memcpy(trash.str + trash.len, host, hostlen);
4063 trash.len += hostlen;
4064
4065 /* add path */
4066 memcpy(trash.str + trash.len, path, pathlen);
4067 trash.len += pathlen;
4068
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004069 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004070 if (trash.len && trash.str[trash.len - 1] != '/' &&
4071 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4072 if (trash.len > trash.size - 5)
4073 return 0;
4074 trash.str[trash.len] = '/';
4075 trash.len++;
4076 }
4077
4078 break;
4079 }
4080 case REDIRECT_TYPE_PREFIX: {
4081 const char *path;
4082 int pathlen;
4083
4084 path = http_get_path(txn);
4085 /* build message using path */
4086 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004087 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004088 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4089 int qs = 0;
4090 while (qs < pathlen) {
4091 if (path[qs] == '?') {
4092 pathlen = qs;
4093 break;
4094 }
4095 qs++;
4096 }
4097 }
4098 } else {
4099 path = "/";
4100 pathlen = 1;
4101 }
4102
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004103 if (rule->rdr_str) { /* this is an old "redirect" rule */
4104 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
4105 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004106
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004107 /* add prefix. Note that if prefix == "/", we don't want to
4108 * add anything, otherwise it makes it hard for the user to
4109 * configure a self-redirection.
4110 */
4111 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
4112 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4113 trash.len += rule->rdr_len;
4114 }
4115 }
4116 else {
4117 /* add prefix with executing log format */
4118 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
4119
4120 /* Check length */
4121 if (trash.len + pathlen > trash.size - 4)
4122 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004123 }
4124
4125 /* add path */
4126 memcpy(trash.str + trash.len, path, pathlen);
4127 trash.len += pathlen;
4128
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004129 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004130 if (trash.len && trash.str[trash.len - 1] != '/' &&
4131 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4132 if (trash.len > trash.size - 5)
4133 return 0;
4134 trash.str[trash.len] = '/';
4135 trash.len++;
4136 }
4137
4138 break;
4139 }
4140 case REDIRECT_TYPE_LOCATION:
4141 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004142 if (rule->rdr_str) { /* this is an old "redirect" rule */
4143 if (trash.len + rule->rdr_len > trash.size - 4)
4144 return 0;
4145
4146 /* add location */
4147 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4148 trash.len += rule->rdr_len;
4149 }
4150 else {
4151 /* add location with executing log format */
4152 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004153
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004154 /* Check left length */
4155 if (trash.len > trash.size - 4)
4156 return 0;
4157 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004158 break;
4159 }
4160
4161 if (rule->cookie_len) {
4162 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
4163 trash.len += 14;
4164 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4165 trash.len += rule->cookie_len;
4166 memcpy(trash.str + trash.len, "\r\n", 2);
4167 trash.len += 2;
4168 }
4169
4170 /* add end of headers and the keep-alive/close status.
4171 * We may choose to set keep-alive if the Location begins
4172 * with a slash, because the client will come back to the
4173 * same server.
4174 */
4175 txn->status = rule->code;
4176 /* let's log the request time */
4177 s->logs.tv_request = now;
4178
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004179 if (*location == '/' &&
Willy Tarreaub329a312015-05-22 16:27:37 +02004180 (req->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau2de8a502015-05-28 17:23:54 +02004181 ((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004182 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4183 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4184 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02004185 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004186 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4187 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4188 trash.len += 30;
4189 } else {
4190 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4191 trash.len += 24;
4192 }
4193 }
4194 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4195 trash.len += 4;
Christopher Faulet3e344292015-11-24 16:24:13 +01004196 FLT_STRM_CB(s, flt_http_reply(s, txn->status, &trash));
Willy Tarreaub329a312015-05-22 16:27:37 +02004197 bo_inject(res->chn, trash.str, trash.len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004198 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02004199 bi_fast_delete(req->chn->buf, req->sov);
4200 req->next -= req->sov;
4201 req->sov = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004202 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_FLT_END);
4203 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->req.analysers & AN_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02004204 req->msg_state = HTTP_MSG_CLOSED;
4205 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02004206 /* Trim any possible response */
4207 res->chn->buf->i = 0;
4208 res->next = res->sov = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004209 } else {
4210 /* keep-alive not possible */
4211 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4212 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4213 trash.len += 29;
4214 } else {
4215 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4216 trash.len += 23;
4217 }
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004218 http_reply_and_close(s, txn->status, &trash);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004219 req->chn->analysers &= AN_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004220 }
4221
Willy Tarreaue7dff022015-04-03 01:14:29 +02004222 if (!(s->flags & SF_ERR_MASK))
4223 s->flags |= SF_ERR_LOCAL;
4224 if (!(s->flags & SF_FINST_MASK))
4225 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004226
4227 return 1;
4228}
4229
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004230/* This stream analyser runs all HTTP request processing which is common to
4231 * frontends and backends, which means blocking ACLs, filters, connection-close,
4232 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004233 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004234 * either needs more data or wants to immediately abort the request (eg: deny,
4235 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004236 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004237int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004238{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004239 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004240 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004241 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004242 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004243 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004244 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004245 int deny_status = HTTP_ERR_403;
Willy Tarreaud787e662009-07-07 10:14:51 +02004246
Willy Tarreau655dce92009-11-08 13:10:58 +01004247 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004248 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004249 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004250 }
4251
Willy Tarreau87b09662015-04-03 00:22:06 +02004252 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 +02004253 now_ms, __FUNCTION__,
4254 s,
4255 req,
4256 req->rex, req->wex,
4257 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004258 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004259 req->analysers);
4260
Willy Tarreau65410832014-04-28 21:25:43 +02004261 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004262 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004263
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004264 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004265 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02004266 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01004267
Willy Tarreau0b748332014-04-29 00:13:29 +02004268 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004269 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4270 goto return_prx_yield;
4271
Willy Tarreau0b748332014-04-29 00:13:29 +02004272 case HTTP_RULE_RES_CONT:
4273 case HTTP_RULE_RES_STOP: /* nothing to do */
4274 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004275
Willy Tarreau0b748332014-04-29 00:13:29 +02004276 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4277 if (txn->flags & TX_CLTARPIT)
4278 goto tarpit;
4279 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004280
Willy Tarreau0b748332014-04-29 00:13:29 +02004281 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4282 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004283
Willy Tarreau0b748332014-04-29 00:13:29 +02004284 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004285 goto done;
4286
Willy Tarreau0b748332014-04-29 00:13:29 +02004287 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4288 goto return_bad_req;
4289 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004290 }
4291
Willy Tarreau52542592014-04-28 18:33:26 +02004292 /* OK at this stage, we know that the request was accepted according to
4293 * the http-request rules, we can check for the stats. Note that the
4294 * URI is detected *before* the req* rules in order not to be affected
4295 * by a possible reqrep, while they are processed *after* so that a
4296 * reqdeny can still block them. This clearly needs to change in 1.6!
4297 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004298 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004299 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004300 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004301 txn->status = 500;
4302 s->logs.tv_request = now;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004303 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004304
Willy Tarreaue7dff022015-04-03 01:14:29 +02004305 if (!(s->flags & SF_ERR_MASK))
4306 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004307 goto return_prx_cond;
4308 }
4309
4310 /* parse the whole stats request and extract the relevant information */
4311 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02004312 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02004313 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004314
Willy Tarreau0b748332014-04-29 00:13:29 +02004315 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4316 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004317
Willy Tarreau0b748332014-04-29 00:13:29 +02004318 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4319 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004320 }
4321
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004322 /* evaluate the req* rules except reqadd */
4323 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004324 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004325 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004326
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004327 if (txn->flags & TX_CLDENY)
4328 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004329
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004330 if (txn->flags & TX_CLTARPIT)
4331 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004332 }
Willy Tarreau06619262006-12-17 08:37:22 +01004333
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004334 /* add request headers from the rule sets in the same order */
4335 list_for_each_entry(wl, &px->req_add, list) {
4336 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004337 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004338 ret = acl_pass(ret);
4339 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4340 ret = !ret;
4341 if (!ret)
4342 continue;
4343 }
4344
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004345 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004346 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004347 }
4348
Willy Tarreau52542592014-04-28 18:33:26 +02004349
4350 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004351 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004352 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004353 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4354 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004355
Willy Tarreaue7dff022015-04-03 01:14:29 +02004356 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4357 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4358 if (!(s->flags & SF_FINST_MASK))
4359 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004360
Willy Tarreau70730dd2014-04-24 18:06:27 +02004361 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet309c6412015-12-02 09:57:32 +01004362 req->analysers &= (AN_REQ_HTTP_BODY | AN_FLT_HTTP_HDRS | AN_FLT_END);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004363 req->analysers &= ~AN_FLT_XFER_DATA;
4364 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004365 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004366 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004367
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004368 /* check whether we have some ACLs set to redirect this request */
4369 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004370 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004371 int ret;
4372
Willy Tarreau192252e2015-04-04 01:47:55 +02004373 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004374 ret = acl_pass(ret);
4375 if (rule->cond->pol == ACL_COND_UNLESS)
4376 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004377 if (!ret)
4378 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004379 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004380 if (!http_apply_redirect_rule(rule, s, txn))
4381 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004382 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004383 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004384
Willy Tarreau2be39392010-01-03 17:24:51 +01004385 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4386 * If this happens, then the data will not come immediately, so we must
4387 * send all what we have without waiting. Note that due to the small gain
4388 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004389 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004390 * itself once used.
4391 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004392 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004393
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004394 done: /* done with this analyser, continue with next ones that the calling
4395 * points will have set, if any.
4396 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004397 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004398 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4399 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004400 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004401
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004402 tarpit:
4403 /* When a connection is tarpitted, we use the tarpit timeout,
4404 * which may be the same as the connect timeout if unspecified.
4405 * If unset, then set it to zero because we really want it to
4406 * eventually expire. We build the tarpit as an analyser.
4407 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004408 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004409
4410 /* wipe the request out so that we can drop the connection early
4411 * if the client closes first.
4412 */
4413 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004414
4415 /* Allow cookie logging
4416 */
4417 if (s->be->cookie_name || sess->fe->capture_name)
4418 manage_client_side_cookies(s, req);
4419
Christopher Fauletd7c91962015-04-30 11:48:27 +02004420 req->analysers &= AN_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004421 req->analysers |= AN_REQ_HTTP_TARPIT;
4422 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4423 if (!req->analyse_exp)
4424 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004425 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004426 sess->fe->fe_counters.denied_req++;
4427 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004428 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004429 if (sess->listener->counters)
4430 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004431 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004432
4433 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004434
4435 /* Allow cookie logging
4436 */
4437 if (s->be->cookie_name || sess->fe->capture_name)
4438 manage_client_side_cookies(s, req);
4439
Willy Tarreau0b748332014-04-29 00:13:29 +02004440 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004441 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004442 s->logs.tv_request = now;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004443 http_reply_and_close(s, txn->status, http_error_message(s, deny_status));
Willy Tarreau87b09662015-04-03 00:22:06 +02004444 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004445 sess->fe->fe_counters.denied_req++;
4446 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004447 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004448 if (sess->listener->counters)
4449 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004450 goto return_prx_cond;
4451
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004452 return_bad_req:
4453 /* We centralize bad requests processing here */
4454 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4455 /* we detected a parsing error. We want to archive this request
4456 * in the dedicated proxy area for later troubleshooting.
4457 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004458 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004459 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004460
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004461 txn->req.msg_state = HTTP_MSG_ERROR;
4462 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004463 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004464
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004465 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004466 if (sess->listener->counters)
4467 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004468
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004469 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004470 if (!(s->flags & SF_ERR_MASK))
4471 s->flags |= SF_ERR_PRXCOND;
4472 if (!(s->flags & SF_FINST_MASK))
4473 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004474
Christopher Fauletd7c91962015-04-30 11:48:27 +02004475 req->analysers &= AN_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004476 req->analyse_exp = TICK_ETERNITY;
4477 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004478
4479 return_prx_yield:
4480 channel_dont_connect(req);
4481 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004482}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004483
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004484/* This function performs all the processing enabled for the current request.
4485 * It returns 1 if the processing can continue on next analysers, or zero if it
4486 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004487 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004488 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004489int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004490{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004491 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004492 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004493 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004494 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004495
Willy Tarreau655dce92009-11-08 13:10:58 +01004496 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004497 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004498 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004499 return 0;
4500 }
4501
Willy Tarreau87b09662015-04-03 00:22:06 +02004502 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 +02004503 now_ms, __FUNCTION__,
4504 s,
4505 req,
4506 req->rex, req->wex,
4507 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004508 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004509 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004510
Willy Tarreau59234e92008-11-30 23:51:27 +01004511 /*
4512 * Right now, we know that we have processed the entire headers
4513 * and that unwanted requests have been filtered out. We can do
4514 * whatever we want with the remaining request. Also, now we
4515 * may have separate values for ->fe, ->be.
4516 */
Willy Tarreau06619262006-12-17 08:37:22 +01004517
Willy Tarreau59234e92008-11-30 23:51:27 +01004518 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004519 * If HTTP PROXY is set we simply get remote server address parsing
4520 * incoming request. Note that this requires that a connection is
4521 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004522 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004523 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004524 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004525 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004526
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004527 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau973a5422015-08-05 21:47:23 +02004528 if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004529 txn->req.msg_state = HTTP_MSG_ERROR;
4530 txn->status = 500;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004531 req->analysers &= AN_FLT_END;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004532 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004533
Willy Tarreaue7dff022015-04-03 01:14:29 +02004534 if (!(s->flags & SF_ERR_MASK))
4535 s->flags |= SF_ERR_RESOURCE;
4536 if (!(s->flags & SF_FINST_MASK))
4537 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004538
4539 return 0;
4540 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004541
4542 path = http_get_path(txn);
4543 url2sa(req->buf->p + msg->sl.rq.u,
4544 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004545 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004546 /* if the path was found, we have to remove everything between
4547 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4548 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4549 * u_l characters by a single "/".
4550 */
4551 if (path) {
4552 char *cur_ptr = req->buf->p;
4553 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4554 int delta;
4555
4556 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4557 http_msg_move_end(&txn->req, delta);
4558 cur_end += delta;
4559 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4560 goto return_bad_req;
4561 }
4562 else {
4563 char *cur_ptr = req->buf->p;
4564 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4565 int delta;
4566
4567 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4568 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4569 http_msg_move_end(&txn->req, delta);
4570 cur_end += delta;
4571 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4572 goto return_bad_req;
4573 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004574 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004575
Willy Tarreau59234e92008-11-30 23:51:27 +01004576 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004577 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004578 * Note that doing so might move headers in the request, but
4579 * the fields will stay coherent and the URI will not move.
4580 * This should only be performed in the backend.
4581 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004582 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004583 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004584
William Lallemanda73203e2012-03-12 12:48:57 +01004585 /* add unique-id if "header-unique-id" is specified */
4586
Thierry Fournierf4011dd2016-03-29 17:23:51 +02004587 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004588 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4589 goto return_bad_req;
4590 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004591 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004592 }
William Lallemanda73203e2012-03-12 12:48:57 +01004593
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004594 if (sess->fe->header_unique_id && s->unique_id) {
4595 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004596 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004597 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004598 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004599 goto return_bad_req;
4600 }
4601
Cyril Bontéb21570a2009-11-29 20:04:48 +01004602 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004603 * 9: add X-Forwarded-For if either the frontend or the backend
4604 * asks for it.
4605 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004606 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004607 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004608 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4609 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4610 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004611 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004612 /* The header is set to be added only if none is present
4613 * and we found it, so don't do anything.
4614 */
4615 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004616 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004617 /* Add an X-Forwarded-For header unless the source IP is
4618 * in the 'except' network range.
4619 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004620 if ((!sess->fe->except_mask.s_addr ||
4621 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4622 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004623 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004624 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004625 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004626 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004627 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004628 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004629
4630 /* Note: we rely on the backend to get the header name to be used for
4631 * x-forwarded-for, because the header is really meant for the backends.
4632 * However, if the backend did not specify any option, we have to rely
4633 * on the frontend's header name.
4634 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004635 if (s->be->fwdfor_hdr_len) {
4636 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004637 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004638 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004639 len = sess->fe->fwdfor_hdr_len;
4640 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004641 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004642 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 +01004643
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004644 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004645 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004646 }
4647 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004648 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004649 /* FIXME: for the sake of completeness, we should also support
4650 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004651 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004652 int len;
4653 char pn[INET6_ADDRSTRLEN];
4654 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004655 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004656 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004657
Willy Tarreau59234e92008-11-30 23:51:27 +01004658 /* Note: we rely on the backend to get the header name to be used for
4659 * x-forwarded-for, because the header is really meant for the backends.
4660 * However, if the backend did not specify any option, we have to rely
4661 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004662 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004663 if (s->be->fwdfor_hdr_len) {
4664 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004665 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004666 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004667 len = sess->fe->fwdfor_hdr_len;
4668 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004669 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004670 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004671
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004672 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004673 goto return_bad_req;
4674 }
4675 }
4676
4677 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004678 * 10: add X-Original-To if either the frontend or the backend
4679 * asks for it.
4680 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004681 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004682
4683 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004684 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004685 /* Add an X-Original-To header unless the destination IP is
4686 * in the 'except' network range.
4687 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004688 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004689
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004690 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004691 ((!sess->fe->except_mask_to.s_addr ||
4692 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4693 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004694 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004695 (((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 +02004696 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004697 int len;
4698 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004699 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004700
4701 /* Note: we rely on the backend to get the header name to be used for
4702 * x-original-to, because the header is really meant for the backends.
4703 * However, if the backend did not specify any option, we have to rely
4704 * on the frontend's header name.
4705 */
4706 if (s->be->orgto_hdr_len) {
4707 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004708 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004709 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004710 len = sess->fe->orgto_hdr_len;
4711 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004712 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004713 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 +02004714
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004715 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004716 goto return_bad_req;
4717 }
4718 }
4719 }
4720
Willy Tarreau50fc7772012-11-11 22:19:57 +01004721 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4722 * If an "Upgrade" token is found, the header is left untouched in order not to have
4723 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4724 * "Upgrade" is present in the Connection header.
4725 */
4726 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4727 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004728 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004729 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004730 unsigned int want_flags = 0;
4731
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004732 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004733 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004734 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004735 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004736 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004737 want_flags |= TX_CON_CLO_SET;
4738 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004739 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004740 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004741 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004742 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004743 want_flags |= TX_CON_KAL_SET;
4744 }
4745
4746 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004747 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004748 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004749
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004750
Willy Tarreau522d6c02009-12-06 18:49:18 +01004751 /* If we have no server assigned yet and we're balancing on url_param
4752 * with a POST request, we may be interested in checking the body for
4753 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004754 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004755 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004756 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004757 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004758 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004759 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004760 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004761
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004762 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02004763 req->analysers &= ~AN_FLT_XFER_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004764 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004765#ifdef TCP_QUICKACK
4766 /* We expect some data from the client. Unless we know for sure
4767 * we already have a full request, we have to re-enable quick-ack
4768 * in case we previously disabled it, otherwise we might cause
4769 * the client to delay further data.
4770 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004771 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004772 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004773 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004774 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004775 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004776#endif
4777 }
Willy Tarreau03945942009-12-22 16:50:27 +01004778
Willy Tarreau59234e92008-11-30 23:51:27 +01004779 /*************************************************************
4780 * OK, that's finished for the headers. We have done what we *
4781 * could. Let's switch to the DATA state. *
4782 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004783 req->analyse_exp = TICK_ETERNITY;
4784 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004785
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004786 /* if the server closes the connection, we want to immediately react
4787 * and close the socket to save packets and syscalls.
4788 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004789 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004790 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004791
Willy Tarreau59234e92008-11-30 23:51:27 +01004792 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004793 /* OK let's go on with the BODY now */
4794 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004795
Willy Tarreau59234e92008-11-30 23:51:27 +01004796 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004797 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004798 /* we detected a parsing error. We want to archive this request
4799 * in the dedicated proxy area for later troubleshooting.
4800 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004801 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004802 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004803
Willy Tarreau59234e92008-11-30 23:51:27 +01004804 txn->req.msg_state = HTTP_MSG_ERROR;
4805 txn->status = 400;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004806 req->analysers &= AN_FLT_END;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004807 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004808
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004809 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004810 if (sess->listener->counters)
4811 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +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_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004817 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004818}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004819
Willy Tarreau60b85b02008-11-30 23:28:40 +01004820/* This function is an analyser which processes the HTTP tarpit. It always
4821 * returns zero, at the beginning because it prevents any other processing
4822 * from occurring, and at the end because it terminates the request.
4823 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004824int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004825{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004826 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004827
4828 /* This connection is being tarpitted. The CLIENT side has
4829 * already set the connect expiration date to the right
4830 * timeout. We just have to check that the client is still
4831 * there and that the timeout has not expired.
4832 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004833 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004834 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004835 !tick_is_expired(req->analyse_exp, now_ms))
4836 return 0;
4837
4838 /* We will set the queue timer to the time spent, just for
4839 * logging purposes. We fake a 500 server error, so that the
4840 * attacker will not suspect his connection has been tarpitted.
4841 * It will not cause trouble to the logs because we can exclude
4842 * the tarpitted connections by filtering on the 'PT' status flags.
4843 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004844 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4845
4846 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004847 if (!(req->flags & CF_READ_ERROR))
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004848 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004849
Christopher Fauletd7c91962015-04-30 11:48:27 +02004850 req->analysers &= AN_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004851 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004852
Willy Tarreaue7dff022015-04-03 01:14:29 +02004853 if (!(s->flags & SF_ERR_MASK))
4854 s->flags |= SF_ERR_PRXCOND;
4855 if (!(s->flags & SF_FINST_MASK))
4856 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004857 return 0;
4858}
4859
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004860/* This function is an analyser which waits for the HTTP request body. It waits
4861 * for either the buffer to be full, or the full advertised contents to have
4862 * reached the buffer. It must only be called after the standard HTTP request
4863 * processing has occurred, because it expects the request to be parsed and will
4864 * look for the Expect header. It may send a 100-Continue interim response. It
4865 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4866 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4867 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004868 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004869int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004870{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004871 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004872 struct http_txn *txn = s->txn;
4873 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004874
4875 /* We have to parse the HTTP request body to find any required data.
4876 * "balance url_param check_post" should have been the only way to get
4877 * into this. We were brought here after HTTP header analysis, so all
4878 * related structures are ready.
4879 */
4880
Willy Tarreau890988f2014-04-10 11:59:33 +02004881 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4882 /* This is the first call */
4883 if (msg->msg_state < HTTP_MSG_BODY)
4884 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004885
Willy Tarreau890988f2014-04-10 11:59:33 +02004886 if (msg->msg_state < HTTP_MSG_100_SENT) {
4887 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4888 * send an HTTP/1.1 100 Continue intermediate response.
4889 */
4890 if (msg->flags & HTTP_MSGF_VER_11) {
4891 struct hdr_ctx ctx;
4892 ctx.idx = 0;
4893 /* Expect is allowed in 1.1, look for it */
4894 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4895 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004896 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004897 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004898 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004899 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004900 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004901
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004902 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004903 * req->buf->p still points to the beginning of the message. We
4904 * must save the body in msg->next because it survives buffer
4905 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004906 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004907 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004908
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004909 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004910 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4911 else
4912 msg->msg_state = HTTP_MSG_DATA;
4913 }
4914
Willy Tarreau890988f2014-04-10 11:59:33 +02004915 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4916 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004917 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004918 goto missing_data;
4919
4920 /* OK we have everything we need now */
4921 goto http_end;
4922 }
4923
4924 /* OK here we're parsing a chunked-encoded message */
4925
Willy Tarreau522d6c02009-12-06 18:49:18 +01004926 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004927 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004928 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004929 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004930 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004931 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004932
Willy Tarreau115acb92009-12-26 13:56:06 +01004933 if (!ret)
4934 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004935 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004936 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004937 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004938 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01004939 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004940 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01004941 }
4942
Willy Tarreaud98cf932009-12-27 22:54:55 +01004943 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004944 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4945 * for at least a whole chunk or the whole content length bytes after
4946 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004947 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004948 if (msg->msg_state == HTTP_MSG_TRAILERS)
4949 goto http_end;
4950
Willy Tarreaue115b492015-05-01 23:05:14 +02004951 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004952 goto http_end;
4953
4954 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004955 /* we get here if we need to wait for more data. If the buffer is full,
4956 * we have the maximum we can expect.
4957 */
4958 if (buffer_full(req->buf, global.tune.maxrewrite))
4959 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004960
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004961 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004962 txn->status = 408;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004963 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004964
Willy Tarreaue7dff022015-04-03 01:14:29 +02004965 if (!(s->flags & SF_ERR_MASK))
4966 s->flags |= SF_ERR_CLITO;
4967 if (!(s->flags & SF_FINST_MASK))
4968 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004969 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004970 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004971
4972 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004973 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004974 /* Not enough data. We'll re-use the http-request
4975 * timeout here. Ideally, we should set the timeout
4976 * relative to the accept() date. We just set the
4977 * request timeout once at the beginning of the
4978 * request.
4979 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004980 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004981 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004982 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004983 return 0;
4984 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004985
4986 http_end:
4987 /* The situation will not evolve, so let's give up on the analysis. */
4988 s->logs.tv_request = now; /* update the request timer to reflect full request */
4989 req->analysers &= ~an_bit;
4990 req->analyse_exp = TICK_ETERNITY;
4991 return 1;
4992
4993 return_bad_req: /* let's centralize all bad requests */
4994 txn->req.msg_state = HTTP_MSG_ERROR;
4995 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004996 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004997
Willy Tarreaue7dff022015-04-03 01:14:29 +02004998 if (!(s->flags & SF_ERR_MASK))
4999 s->flags |= SF_ERR_PRXCOND;
5000 if (!(s->flags & SF_FINST_MASK))
5001 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005002
Willy Tarreau522d6c02009-12-06 18:49:18 +01005003 return_err_msg:
Christopher Fauletd7c91962015-04-30 11:48:27 +02005004 req->analysers &= AN_FLT_END;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005005 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005006 if (sess->listener->counters)
5007 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005008 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01005009}
5010
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005011/* send a server's name with an outgoing request over an established connection.
5012 * Note: this function is designed to be called once the request has been scheduled
5013 * for being forwarded. This is the reason why it rewinds the buffer before
5014 * proceeding.
5015 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01005016int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05005017
5018 struct hdr_ctx ctx;
5019
Mark Lamourinec2247f02012-01-04 13:02:01 -05005020 char *hdr_name = be->server_id_hdr_name;
5021 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02005022 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005023 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005024 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005025
William Lallemandd9e90662012-01-30 17:27:17 +01005026 ctx.idx = 0;
5027
Willy Tarreau211cdec2014-04-17 20:18:08 +02005028 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005029 if (old_o) {
5030 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005031 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02005032 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02005033 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005034 }
5035
Willy Tarreau9b28e032012-10-12 23:49:43 +02005036 old_i = chn->buf->i;
5037 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 -05005038 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005039 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005040 }
5041
5042 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005043 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005044 memcpy(hdr_val, hdr_name, hdr_name_len);
5045 hdr_val += hdr_name_len;
5046 *hdr_val++ = ':';
5047 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005048 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
5049 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005050
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005051 if (old_o) {
5052 /* If this was a forwarded request, we must readjust the amount of
5053 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02005054 * variations. Note that the current state is >= HTTP_MSG_BODY,
5055 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005056 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02005057 old_o += chn->buf->i - old_i;
5058 b_adv(chn->buf, old_o);
5059 txn->req.next -= old_o;
5060 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005061 }
5062
Mark Lamourinec2247f02012-01-04 13:02:01 -05005063 return 0;
5064}
5065
Willy Tarreau610ecce2010-01-04 21:15:02 +01005066/* Terminate current transaction and prepare a new one. This is very tricky
5067 * right now but it works.
5068 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005069void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005070{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005071 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005072 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01005073 struct proxy *be = s->be;
Willy Tarreau323a2d92015-08-04 19:00:17 +02005074 struct connection *srv_conn;
5075 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02005076 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01005077
Willy Tarreau610ecce2010-01-04 21:15:02 +01005078 /* FIXME: We need a more portable way of releasing a backend's and a
5079 * server's connections. We need a safer way to reinitialize buffer
5080 * flags. We also need a more accurate method for computing per-request
5081 * data.
5082 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02005083 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005084
Willy Tarreau4213a112013-12-15 10:25:42 +01005085 /* unless we're doing keep-alive, we want to quickly close the connection
5086 * to the server.
5087 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005088 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005089 !si_conn_ready(&s->si[1])) {
5090 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
5091 si_shutr(&s->si[1]);
5092 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005093 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005094
Willy Tarreaue7dff022015-04-03 01:14:29 +02005095 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau858b1032015-12-07 17:04:59 +01005096 be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01005097 if (unlikely(s->srv_conn))
5098 sess_change_server(s, NULL);
5099 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005100
5101 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02005102 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005103
Willy Tarreaueee5b512015-04-03 23:46:31 +02005104 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005105 int n;
5106
Willy Tarreaueee5b512015-04-03 23:46:31 +02005107 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005108 if (n < 1 || n > 5)
5109 n = 0;
5110
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005111 if (fe->mode == PR_MODE_HTTP) {
5112 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005113 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005114 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01005115 (be->mode == PR_MODE_HTTP)) {
5116 be->be_counters.p.http.rsp[n]++;
5117 be->be_counters.p.http.cum_req++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005118 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005119 }
5120
5121 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005122 s->logs.bytes_in -= s->req.buf->i;
5123 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005124
5125 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005126 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005127 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005128 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005129 s->do_log(s);
5130 }
5131
Willy Tarreaud713bcc2014-06-25 15:36:04 +02005132 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02005133 stream_stop_content_counters(s);
5134 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02005135
Willy Tarreau610ecce2010-01-04 21:15:02 +01005136 s->logs.accept_date = date; /* user-visible date for logging */
5137 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02005138 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
5139 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005140 tv_zero(&s->logs.tv_request);
5141 s->logs.t_queue = -1;
5142 s->logs.t_connect = -1;
5143 s->logs.t_data = -1;
5144 s->logs.t_close = 0;
5145 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5146 s->logs.srv_queue_size = 0; /* we will get this number soon */
5147
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005148 s->logs.bytes_in = s->req.total = s->req.buf->i;
5149 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005150
5151 if (s->pend_pos)
5152 pendconn_free(s->pend_pos);
5153
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005154 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005155 if (s->flags & SF_CURR_SESS) {
5156 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005157 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005158 }
Willy Tarreau858b1032015-12-07 17:04:59 +01005159 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005160 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005161 }
5162
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005163 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005164
Willy Tarreau4213a112013-12-15 10:25:42 +01005165 /* only release our endpoint if we don't intend to reuse the
5166 * connection.
5167 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005168 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005169 !si_conn_ready(&s->si[1])) {
5170 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005171 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005172 }
5173
Willy Tarreau350f4872014-11-28 14:42:25 +01005174 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5175 s->si[1].err_type = SI_ET_NONE;
5176 s->si[1].conn_retries = 0; /* used for logging too */
5177 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005178 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 +01005179 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);
5180 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 +02005181 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5182 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5183 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005184
Willy Tarreaueee5b512015-04-03 23:46:31 +02005185 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005186 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005187 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005188
5189 if (prev_status == 401 || prev_status == 407) {
5190 /* In HTTP keep-alive mode, if we receive a 401, we still have
5191 * a chance of being able to send the visitor again to the same
5192 * server over the same connection. This is required by some
5193 * broken protocols such as NTLM, and anyway whenever there is
5194 * an opportunity for sending the challenge to the proper place,
5195 * it's better to do it (at least it helps with debugging).
5196 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005197 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02005198 if (srv_conn)
5199 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01005200 }
5201
Willy Tarreau53f96852016-02-02 18:50:47 +01005202 /* Never ever allow to reuse a connection from a non-reuse backend */
5203 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
5204 srv_conn->flags |= CO_FL_PRIVATE;
5205
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005206 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005207 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005208
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005209 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005210 s->req.flags |= CF_NEVER_WAIT;
5211 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005212 }
5213
Willy Tarreau610ecce2010-01-04 21:15:02 +01005214 /* if the request buffer is not empty, it means we're
5215 * about to process another request, so send pending
5216 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005217 * Just don't do this if the buffer is close to be full,
5218 * because the request will wait for it to flush a little
5219 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005220 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005221 if (s->req.buf->i) {
5222 if (s->res.buf->o &&
5223 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5224 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5225 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005226 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005227
Willy Tarreau714ea782015-11-25 20:11:11 +01005228 /* we're removing the analysers, we MUST re-enable events detection.
5229 * We don't enable close on the response channel since it's either
5230 * already closed, or in keep-alive with an idle connection handler.
5231 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005232 channel_auto_read(&s->req);
5233 channel_auto_close(&s->req);
5234 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005235
Willy Tarreau1c59bd52015-11-02 20:20:11 +01005236 /* we're in keep-alive with an idle connection, monitor it if not already done */
5237 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02005238 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005239 if (!srv)
5240 si_idle_conn(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01005241 else if (srv_conn->flags & CO_FL_PRIVATE)
Willy Tarreau8dff9982015-08-04 20:45:52 +02005242 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02005243 else if (prev_flags & TX_NOT_FIRST)
5244 /* note: we check the request, not the connection, but
5245 * this is valid for strategies SAFE and AGGR, and in
5246 * case of ALWS, we don't care anyway.
5247 */
5248 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005249 else
5250 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005251 }
Willy Tarreau27375622013-12-17 00:00:28 +01005252
Christopher Faulet3e344292015-11-24 16:24:13 +01005253 if (HAS_FILTERS(s)) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02005254 s->req.analysers &= AN_FLT_END;
5255 s->res.analysers &= AN_FLT_END;
5256 }
Christopher Faulet3e344292015-11-24 16:24:13 +01005257 else {
5258 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
5259 s->res.analysers = 0;
5260 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005261}
5262
5263
5264/* This function updates the request state machine according to the response
5265 * state machine and buffer flags. It returns 1 if it changes anything (flag
5266 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5267 * it is only used to find when a request/response couple is complete. Both
5268 * this function and its equivalent should loop until both return zero. It
5269 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5270 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005271int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005272{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005273 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005274 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005275 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005276 unsigned int old_state = txn->req.msg_state;
5277
Willy Tarreau610ecce2010-01-04 21:15:02 +01005278 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5279 return 0;
5280
5281 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005282 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005283 * We can shut the read side unless we want to abort_on_close,
5284 * or we have a POST request. The issue with POST requests is
5285 * that some browsers still send a CRLF after the request, and
5286 * this CRLF must be read so that it does not remain in the kernel
5287 * buffers, otherwise a close could cause an RST on some systems
5288 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005289 * Note that if we're using keep-alive on the client side, we'd
5290 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005291 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005292 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005293 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005294 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5295 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5296 !(s->be->options & PR_O_ABRT_CLOSE) &&
5297 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005298 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005299
Willy Tarreau40f151a2012-12-20 12:10:09 +01005300 /* if the server closes the connection, we want to immediately react
5301 * and close the socket to save packets and syscalls.
5302 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005303 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005304
Willy Tarreau7f876a12015-11-18 11:59:55 +01005305 /* In any case we've finished parsing the request so we must
5306 * disable Nagle when sending data because 1) we're not going
5307 * to shut this side, and 2) the server is waiting for us to
5308 * send pending data.
5309 */
5310 chn->flags |= CF_NEVER_WAIT;
5311
Willy Tarreau610ecce2010-01-04 21:15:02 +01005312 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5313 goto wait_other_side;
5314
5315 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5316 /* The server has not finished to respond, so we
5317 * don't want to move in order not to upset it.
5318 */
5319 goto wait_other_side;
5320 }
5321
5322 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5323 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005324 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005325 txn->req.msg_state = HTTP_MSG_TUNNEL;
5326 goto wait_other_side;
5327 }
5328
5329 /* When we get here, it means that both the request and the
5330 * response have finished receiving. Depending on the connection
5331 * mode, we'll have to wait for the last bytes to leave in either
5332 * direction, and sometimes for a close to be effective.
5333 */
5334
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005335 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5336 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005337 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5338 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005339 }
5340 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5341 /* Option forceclose is set, or either side wants to close,
5342 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005343 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005344 * once both states are CLOSED.
5345 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005346 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5347 channel_shutr_now(chn);
5348 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005349 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005350 }
5351 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005352 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5353 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005354 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005355 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5356 channel_auto_read(chn);
5357 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005358 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005359 }
5360
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005361 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005362 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005363 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005364
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005365 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005366 txn->req.msg_state = HTTP_MSG_CLOSING;
5367 goto http_msg_closing;
5368 }
5369 else {
5370 txn->req.msg_state = HTTP_MSG_CLOSED;
5371 goto http_msg_closed;
5372 }
5373 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005374 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005375 }
5376
5377 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5378 http_msg_closing:
5379 /* nothing else to forward, just waiting for the output buffer
5380 * to be empty and for the shutw_now to take effect.
5381 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005382 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005383 txn->req.msg_state = HTTP_MSG_CLOSED;
5384 goto http_msg_closed;
5385 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005386 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005387 txn->req.msg_state = HTTP_MSG_ERROR;
5388 goto wait_other_side;
5389 }
5390 }
5391
5392 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5393 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005394 /* see above in MSG_DONE why we only do this in these states */
5395 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5396 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5397 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005398 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005399 goto wait_other_side;
5400 }
5401
5402 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005403 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005404}
5405
5406
5407/* This function updates the response state machine according to the request
5408 * state machine and buffer flags. It returns 1 if it changes anything (flag
5409 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5410 * it is only used to find when a request/response couple is complete. Both
5411 * this function and its equivalent should loop until both return zero. It
5412 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5413 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005414int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005415{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005416 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005417 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005418 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005419 unsigned int old_state = txn->rsp.msg_state;
5420
Willy Tarreau610ecce2010-01-04 21:15:02 +01005421 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5422 return 0;
5423
5424 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5425 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005426 * still monitor the server connection for a possible close
5427 * while the request is being uploaded, so we don't disable
5428 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005429 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005430 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005431
5432 if (txn->req.msg_state == HTTP_MSG_ERROR)
5433 goto wait_other_side;
5434
5435 if (txn->req.msg_state < HTTP_MSG_DONE) {
5436 /* The client seems to still be sending data, probably
5437 * because we got an error response during an upload.
5438 * We have the choice of either breaking the connection
5439 * or letting it pass through. Let's do the later.
5440 */
5441 goto wait_other_side;
5442 }
5443
5444 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5445 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005446 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005447 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005448 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005449 goto wait_other_side;
5450 }
5451
5452 /* When we get here, it means that both the request and the
5453 * response have finished receiving. Depending on the connection
5454 * mode, we'll have to wait for the last bytes to leave in either
5455 * direction, and sometimes for a close to be effective.
5456 */
5457
5458 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5459 /* Server-close mode : shut read and wait for the request
5460 * side to close its output buffer. The caller will detect
5461 * when we're in DONE and the other is in CLOSED and will
5462 * catch that for the final cleanup.
5463 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005464 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5465 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005466 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005467 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5468 /* Option forceclose is set, or either side wants to close,
5469 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005470 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005471 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005472 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005473 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5474 channel_shutr_now(chn);
5475 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005476 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005477 }
5478 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005479 /* The last possible modes are keep-alive and tunnel. Tunnel will
5480 * need to forward remaining data. Keep-alive will need to monitor
5481 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005482 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005483 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005484 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005485 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5486 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005487 }
5488
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005489 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005490 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005491 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005492 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5493 goto http_msg_closing;
5494 }
5495 else {
5496 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5497 goto http_msg_closed;
5498 }
5499 }
5500 goto wait_other_side;
5501 }
5502
5503 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5504 http_msg_closing:
5505 /* nothing else to forward, just waiting for the output buffer
5506 * to be empty and for the shutw_now to take effect.
5507 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005508 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005509 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5510 goto http_msg_closed;
5511 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005512 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005513 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005514 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005515 if (objt_server(s->target))
5516 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005517 goto wait_other_side;
5518 }
5519 }
5520
5521 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5522 http_msg_closed:
5523 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005524 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005525 channel_auto_close(chn);
5526 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005527 goto wait_other_side;
5528 }
5529
5530 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005531 /* We force the response to leave immediately if we're waiting for the
5532 * other side, since there is no pending shutdown to push it out.
5533 */
5534 if (!channel_is_empty(chn))
5535 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005536 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005537}
5538
5539
5540/* Resync the request and response state machines. Return 1 if either state
5541 * changes.
5542 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005543int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005544{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005545 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005546 int old_req_state = txn->req.msg_state;
5547 int old_res_state = txn->rsp.msg_state;
5548
Willy Tarreau610ecce2010-01-04 21:15:02 +01005549 http_sync_req_state(s);
5550 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005551 if (!http_sync_res_state(s))
5552 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005553 if (!http_sync_req_state(s))
5554 break;
5555 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005556
Willy Tarreau610ecce2010-01-04 21:15:02 +01005557 /* OK, both state machines agree on a compatible state.
5558 * There are a few cases we're interested in :
5559 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5560 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5561 * directions, so let's simply disable both analysers.
5562 * - HTTP_MSG_CLOSED on the response only means we must abort the
5563 * request.
5564 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5565 * with server-close mode means we've completed one request and we
5566 * must re-initialize the server connection.
5567 */
5568
5569 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5570 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5571 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5572 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02005573 s->req.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005574 channel_auto_close(&s->req);
5575 channel_auto_read(&s->req);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005576 s->res.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005577 channel_auto_close(&s->res);
5578 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005579 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005580 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005581 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005582 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005583 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02005584 s->res.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005585 channel_auto_close(&s->res);
5586 channel_auto_read(&s->res);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005587 s->req.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005588 channel_abort(&s->req);
5589 channel_auto_close(&s->req);
5590 channel_auto_read(&s->req);
5591 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005592 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005593 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5594 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005595 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005596 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5597 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5598 /* server-close/keep-alive: terminate this transaction,
5599 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005600 * a fresh-new transaction, but only once we're sure there's
5601 * enough room in the request and response buffer to process
5602 * another request. The request buffer must not hold any
5603 * pending output data and the request buffer must not have
5604 * output data occupying the reserve.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005605 */
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005606 if (s->req.buf->o)
5607 s->req.flags |= CF_WAKE_WRITE;
5608 else if (channel_congested(&s->res))
5609 s->res.flags |= CF_WAKE_WRITE;
5610 else
5611 http_end_txn_clean_session(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005612 }
5613
Willy Tarreau610ecce2010-01-04 21:15:02 +01005614 return txn->req.msg_state != old_req_state ||
5615 txn->rsp.msg_state != old_res_state;
5616}
5617
Willy Tarreaud98cf932009-12-27 22:54:55 +01005618/* This function is an analyser which forwards request body (including chunk
5619 * sizes if any). It is called as soon as we must forward, even if we forward
5620 * zero byte. The only situation where it must not be called is when we're in
5621 * tunnel mode and we want to forward till the close. It's used both to forward
5622 * remaining data and to resync after end of body. It expects the msg_state to
5623 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005624 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005625 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005626 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005627 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005628int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005629{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005630 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005631 struct http_txn *txn = s->txn;
5632 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01005633 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005634
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005635 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5636 return 0;
5637
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005638 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005639 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005640 /* Output closed while we were sending data. We must abort and
5641 * wake the other side up.
5642 */
5643 msg->msg_state = HTTP_MSG_ERROR;
5644 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005645 return 1;
5646 }
5647
Willy Tarreaud98cf932009-12-27 22:54:55 +01005648 /* Note that we don't have to send 100-continue back because we don't
5649 * need the data to complete our job, and it's up to the server to
5650 * decide whether to return 100, 417 or anything else in return of
5651 * an "Expect: 100-continue" header.
5652 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02005653 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005654 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5655 ? HTTP_MSG_CHUNK_SIZE
5656 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005657
5658 /* TODO/filters: when http-buffer-request option is set or if a
5659 * rule on url_param exists, the first chunk size could be
5660 * already parsed. In that case, msg->next is after the chunk
5661 * size (including the CRLF after the size). So this case should
5662 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005663 }
5664
Willy Tarreau7ba23542014-04-17 21:50:00 +02005665 /* Some post-connect processing might want us to refrain from starting to
5666 * forward data. Currently, the only reason for this is "balance url_param"
5667 * whichs need to parse/process the request after we've enabled forwarding.
5668 */
5669 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005670 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005671 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005672 req->flags |= CF_WAKE_CONNECT;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005673 goto missing_data_or_waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005674 }
5675 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5676 }
5677
Willy Tarreau80a92c02014-03-12 10:41:13 +01005678 /* in most states, we should abort in case of early close */
5679 channel_auto_close(req);
5680
Willy Tarreauefdf0942014-04-24 20:08:57 +02005681 if (req->to_forward) {
5682 /* We can't process the buffer's contents yet */
5683 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005684 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005685 }
5686
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005687 if (msg->msg_state < HTTP_MSG_DONE) {
5688 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5689 ? http_msg_forward_chunked_body(s, msg)
5690 : http_msg_forward_body(s, msg));
5691 if (!ret)
5692 goto missing_data_or_waiting;
5693 if (ret < 0)
5694 goto return_bad_req;
5695 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005696
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005697 /* other states, DONE...TUNNEL */
5698 /* we don't want to forward closes on DONE except in tunnel mode. */
5699 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5700 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005701
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005702 ret = msg->msg_state;
5703 if (http_resync_states(s)) {
5704 /* some state changes occurred, maybe the analyser
5705 * was disabled too. */
5706 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5707 if (req->flags & CF_SHUTW) {
5708 /* request errors are most likely due to the
5709 * server aborting the transfer. */
5710 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005711 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005712 if (msg->err_pos >= 0)
5713 http_capture_bad_message(&sess->fe->invalid_req, s, msg, ret, s->be);
5714 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005715 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005716 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005717 }
5718
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005719 /* If "option abortonclose" is set on the backend, we want to monitor
5720 * the client's connection and forward any shutdown notification to the
5721 * server, which will decide whether to close or to go on processing the
5722 * request. We only do that in tunnel mode, and not in other modes since
5723 * it can be abused to exhaust source ports. */
5724 if (s->be->options & PR_O_ABRT_CLOSE) {
5725 channel_auto_read(req);
5726 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5727 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5728 s->si[1].flags |= SI_FL_NOLINGER;
5729 channel_auto_close(req);
5730 }
5731 else if (s->txn->meth == HTTP_METH_POST) {
5732 /* POST requests may require to read extra CRLF sent by broken
5733 * browsers and which could cause an RST to be sent upon close
5734 * on some systems (eg: Linux). */
5735 channel_auto_read(req);
5736 }
5737 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005738
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005739 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005740 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005741 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005742 if (!(s->flags & SF_ERR_MASK))
5743 s->flags |= SF_ERR_CLICL;
5744 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005745 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005746 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005747 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005748 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005749 }
5750
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005751 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005752 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005753 if (objt_server(s->target))
5754 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005755
5756 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005757 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005758
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005759 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005760 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005761 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005762
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005763 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005764 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005765 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005766 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005767 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005768
Willy Tarreau5c620922011-05-11 19:56:11 +02005769 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005770 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005771 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005772 * modes are already handled by the stream sock layer. We must not do
5773 * this in content-length mode because it could present the MSG_MORE
5774 * flag with the last block of forwarded data, which would cause an
5775 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005776 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005777 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005778 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005779
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005780 return 0;
5781
Willy Tarreaud98cf932009-12-27 22:54:55 +01005782 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005783 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005784 if (sess->listener->counters)
5785 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005786
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005787 return_bad_req_stats_ok:
5788 txn->req.msg_state = HTTP_MSG_ERROR;
5789 if (txn->status) {
5790 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005791 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005792 } else {
5793 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005794 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005795 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005796 req->analysers &= AN_FLT_END;
5797 s->res.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005798
Willy Tarreaue7dff022015-04-03 01:14:29 +02005799 if (!(s->flags & SF_ERR_MASK))
5800 s->flags |= SF_ERR_PRXCOND;
5801 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005802 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005803 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005804 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005805 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005806 }
5807 return 0;
5808
5809 aborted_xfer:
5810 txn->req.msg_state = HTTP_MSG_ERROR;
5811 if (txn->status) {
5812 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005813 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005814 } else {
5815 txn->status = 502;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005816 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005817 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005818 req->analysers &= AN_FLT_END;
5819 s->res.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005820
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005821 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005822 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005823 if (objt_server(s->target))
5824 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005825
Willy Tarreaue7dff022015-04-03 01:14:29 +02005826 if (!(s->flags & SF_ERR_MASK))
5827 s->flags |= SF_ERR_SRVCL;
5828 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005829 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005830 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005831 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005832 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005833 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005834 return 0;
5835}
5836
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005837/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5838 * processing can continue on next analysers, or zero if it either needs more
5839 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005840 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005841 * when it has nothing left to do, and may remove any analyser when it wants to
5842 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005843 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005844int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005845{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005846 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005847 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005848 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005849 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005850 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005851 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005852 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005853
Willy Tarreau87b09662015-04-03 00:22:06 +02005854 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 +02005855 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005856 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005857 rep,
5858 rep->rex, rep->wex,
5859 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005860 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005861 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005862
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005863 /*
5864 * Now parse the partial (or complete) lines.
5865 * We will check the response syntax, and also join multi-line
5866 * headers. An index of all the lines will be elaborated while
5867 * parsing.
5868 *
5869 * For the parsing, we use a 28 states FSM.
5870 *
5871 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005872 * rep->buf->p = beginning of response
5873 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5874 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005875 * msg->eol = end of current header or line (LF or CRLF)
5876 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005877 */
5878
Willy Tarreau628c40c2014-04-24 19:11:26 +02005879 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005880 /* There's a protected area at the end of the buffer for rewriting
5881 * purposes. We don't want to start to parse the request if the
5882 * protected area is affected, because we may have to move processed
5883 * data later, which is much more complicated.
5884 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005885 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005886 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005887 /* some data has still not left the buffer, wake us once that's done */
5888 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5889 goto abort_response;
5890 channel_dont_close(rep);
5891 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005892 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005893 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005894 }
5895
Willy Tarreau379357a2013-06-08 12:55:46 +02005896 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5897 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5898 buffer_slow_realign(rep->buf);
5899
Willy Tarreau9b28e032012-10-12 23:49:43 +02005900 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005901 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005902 }
5903
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005904 /* 1: we might have to print this header in debug mode */
5905 if (unlikely((global.mode & MODE_DEBUG) &&
5906 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005907 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005908 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005909
Willy Tarreau9b28e032012-10-12 23:49:43 +02005910 sol = rep->buf->p;
5911 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005912 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005913
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005914 sol += hdr_idx_first_pos(&txn->hdr_idx);
5915 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005916
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005917 while (cur_idx) {
5918 eol = sol + txn->hdr_idx.v[cur_idx].len;
5919 debug_hdr("srvhdr", s, sol, eol);
5920 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5921 cur_idx = txn->hdr_idx.v[cur_idx].next;
5922 }
5923 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005924
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005925 /*
5926 * Now we quickly check if we have found a full valid response.
5927 * If not so, we check the FD and buffer states before leaving.
5928 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005929 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005930 * responses are checked first.
5931 *
5932 * Depending on whether the client is still there or not, we
5933 * may send an error response back or not. Note that normally
5934 * we should only check for HTTP status there, and check I/O
5935 * errors somewhere else.
5936 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005937
Willy Tarreau655dce92009-11-08 13:10:58 +01005938 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005939 /* Invalid response */
5940 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5941 /* we detected a parsing error. We want to archive this response
5942 * in the dedicated proxy area for later troubleshooting.
5943 */
5944 hdr_response_bad:
5945 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005946 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005947
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005948 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005949 if (objt_server(s->target)) {
5950 objt_server(s->target)->counters.failed_resp++;
5951 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005952 }
Willy Tarreau64648412010-03-05 10:41:54 +01005953 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005954 channel_auto_close(rep);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005955 rep->analysers &= AN_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005956 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005957 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005958 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005959 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005960
Willy Tarreaue7dff022015-04-03 01:14:29 +02005961 if (!(s->flags & SF_ERR_MASK))
5962 s->flags |= SF_ERR_PRXCOND;
5963 if (!(s->flags & SF_FINST_MASK))
5964 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005965
5966 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005967 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005968
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005969 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005970 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005971 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005972 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005973 goto hdr_response_bad;
5974 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005975
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005976 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005977 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005978 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005979 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005980 else if (txn->flags & TX_NOT_FIRST)
5981 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005982
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005983 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005984 if (objt_server(s->target)) {
5985 objt_server(s->target)->counters.failed_resp++;
5986 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005987 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005988
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005989 channel_auto_close(rep);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005990 rep->analysers &= AN_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005991 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005992 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005993 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005994 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005995
Willy Tarreaue7dff022015-04-03 01:14:29 +02005996 if (!(s->flags & SF_ERR_MASK))
5997 s->flags |= SF_ERR_SRVCL;
5998 if (!(s->flags & SF_FINST_MASK))
5999 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02006000 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006001 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006002
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02006003 /* read timeout : return a 504 to the client. */
6004 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006005 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006006 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006007
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006008 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006009 if (objt_server(s->target)) {
6010 objt_server(s->target)->counters.failed_resp++;
6011 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006012 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006013
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006014 channel_auto_close(rep);
Christopher Fauletd7c91962015-04-30 11:48:27 +02006015 rep->analysers &= AN_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006016 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01006017 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006018 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006019 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02006020
Willy Tarreaue7dff022015-04-03 01:14:29 +02006021 if (!(s->flags & SF_ERR_MASK))
6022 s->flags |= SF_ERR_SRVTO;
6023 if (!(s->flags & SF_FINST_MASK))
6024 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006025 return 0;
6026 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02006027
Willy Tarreauf003d372012-11-26 13:35:37 +01006028 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006029 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006030 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01006031 s->be->be_counters.cli_aborts++;
6032 if (objt_server(s->target))
6033 objt_server(s->target)->counters.cli_aborts++;
6034
Christopher Fauletd7c91962015-04-30 11:48:27 +02006035 rep->analysers &= AN_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01006036 channel_auto_close(rep);
6037
6038 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01006039 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006040 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01006041
Willy Tarreaue7dff022015-04-03 01:14:29 +02006042 if (!(s->flags & SF_ERR_MASK))
6043 s->flags |= SF_ERR_CLICL;
6044 if (!(s->flags & SF_FINST_MASK))
6045 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01006046
Willy Tarreau87b09662015-04-03 00:22:06 +02006047 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01006048 return 0;
6049 }
6050
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006051 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006052 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006053 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006054 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006055 else if (txn->flags & TX_NOT_FIRST)
6056 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006057
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006058 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006059 if (objt_server(s->target)) {
6060 objt_server(s->target)->counters.failed_resp++;
6061 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006062 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006063
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006064 channel_auto_close(rep);
Christopher Fauletd7c91962015-04-30 11:48:27 +02006065 rep->analysers &= AN_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006066 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006067 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006068 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006069 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01006070
Willy Tarreaue7dff022015-04-03 01:14:29 +02006071 if (!(s->flags & SF_ERR_MASK))
6072 s->flags |= SF_ERR_SRVCL;
6073 if (!(s->flags & SF_FINST_MASK))
6074 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006075 return 0;
6076 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006077
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006078 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006079 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006080 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006081 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006082 else if (txn->flags & TX_NOT_FIRST)
6083 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006084
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006085 s->be->be_counters.failed_resp++;
Christopher Fauletd7c91962015-04-30 11:48:27 +02006086 rep->analysers &= AN_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006087 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006088
Willy Tarreaue7dff022015-04-03 01:14:29 +02006089 if (!(s->flags & SF_ERR_MASK))
6090 s->flags |= SF_ERR_CLICL;
6091 if (!(s->flags & SF_FINST_MASK))
6092 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006093
Willy Tarreau87b09662015-04-03 00:22:06 +02006094 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006095 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006096 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006097
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006098 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006099 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006100 return 0;
6101 }
6102
6103 /* More interesting part now : we know that we have a complete
6104 * response which at least looks like HTTP. We have an indicator
6105 * of each header's length, so we can parse them quickly.
6106 */
6107
6108 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006109 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006110
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006111 /*
6112 * 1: get the status code
6113 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006114 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006115 if (n < 1 || n > 5)
6116 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006117 /* when the client triggers a 4xx from the server, it's most often due
6118 * to a missing object or permission. These events should be tracked
6119 * because if they happen often, it may indicate a brute force or a
6120 * vulnerability scan.
6121 */
6122 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006123 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006124
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006125 if (objt_server(s->target))
6126 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006127
Willy Tarreau91852eb2015-05-01 13:26:00 +02006128 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
6129 * exactly one digit "." one digit. This check may be disabled using
6130 * option accept-invalid-http-response.
6131 */
6132 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
6133 if (msg->sl.st.v_l != 8) {
6134 msg->err_pos = 0;
6135 goto hdr_response_bad;
6136 }
6137
6138 if (rep->buf->p[4] != '/' ||
6139 !isdigit((unsigned char)rep->buf->p[5]) ||
6140 rep->buf->p[6] != '.' ||
6141 !isdigit((unsigned char)rep->buf->p[7])) {
6142 msg->err_pos = 4;
6143 goto hdr_response_bad;
6144 }
6145 }
6146
Willy Tarreau5b154472009-12-21 20:11:07 +01006147 /* check if the response is HTTP/1.1 or above */
6148 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006149 ((rep->buf->p[5] > '1') ||
6150 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006151 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006152
6153 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006154 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 +01006155
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006156 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006157 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006158
Willy Tarreau9b28e032012-10-12 23:49:43 +02006159 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006160
Willy Tarreau39650402010-03-15 19:44:39 +01006161 /* Adjust server's health based on status code. Note: status codes 501
6162 * and 505 are triggered on demand by client request, so we must not
6163 * count them as server failures.
6164 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006165 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006166 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006167 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006168 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006169 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006170 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006171
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006172 /*
6173 * 2: check for cacheability.
6174 */
6175
6176 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006177 case 100:
6178 /*
6179 * We may be facing a 100-continue response, in which case this
6180 * is not the right response, and we're waiting for the next one.
6181 * Let's allow this response to go to the client and wait for the
6182 * next one.
6183 */
6184 hdr_idx_init(&txn->hdr_idx);
6185 msg->next -= channel_forward(rep, msg->next);
6186 msg->msg_state = HTTP_MSG_RPBEFORE;
6187 txn->status = 0;
6188 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01006189 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02006190 goto next_one;
6191
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006192 case 200:
6193 case 203:
6194 case 206:
6195 case 300:
6196 case 301:
6197 case 410:
6198 /* RFC2616 @13.4:
6199 * "A response received with a status code of
6200 * 200, 203, 206, 300, 301 or 410 MAY be stored
6201 * by a cache (...) unless a cache-control
6202 * directive prohibits caching."
6203 *
6204 * RFC2616 @9.5: POST method :
6205 * "Responses to this method are not cacheable,
6206 * unless the response includes appropriate
6207 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006208 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006209 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006210 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006211 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6212 break;
6213 default:
6214 break;
6215 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006216
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006217 /*
6218 * 3: we may need to capture headers
6219 */
6220 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006221 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006222 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006223 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006224
Willy Tarreau557f1992015-05-01 10:05:17 +02006225 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6226 * by RFC7230#3.3.3 :
6227 *
6228 * The length of a message body is determined by one of the following
6229 * (in order of precedence):
6230 *
6231 * 1. Any response to a HEAD request and any response with a 1xx
6232 * (Informational), 204 (No Content), or 304 (Not Modified) status
6233 * code is always terminated by the first empty line after the
6234 * header fields, regardless of the header fields present in the
6235 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006236 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006237 * 2. Any 2xx (Successful) response to a CONNECT request implies that
6238 * the connection will become a tunnel immediately after the empty
6239 * line that concludes the header fields. A client MUST ignore any
6240 * Content-Length or Transfer-Encoding header fields received in
6241 * such a message.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006242 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006243 * 3. If a Transfer-Encoding header field is present and the chunked
6244 * transfer coding (Section 4.1) is the final encoding, the message
6245 * body length is determined by reading and decoding the chunked
6246 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006247 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006248 * If a Transfer-Encoding header field is present in a response and
6249 * the chunked transfer coding is not the final encoding, the
6250 * message body length is determined by reading the connection until
6251 * it is closed by the server. If a Transfer-Encoding header field
6252 * is present in a request and the chunked transfer coding is not
6253 * the final encoding, the message body length cannot be determined
6254 * reliably; the server MUST respond with the 400 (Bad Request)
6255 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006256 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006257 * If a message is received with both a Transfer-Encoding and a
6258 * Content-Length header field, the Transfer-Encoding overrides the
6259 * Content-Length. Such a message might indicate an attempt to
6260 * perform request smuggling (Section 9.5) or response splitting
6261 * (Section 9.4) and ought to be handled as an error. A sender MUST
6262 * remove the received Content-Length field prior to forwarding such
6263 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006264 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006265 * 4. If a message is received without Transfer-Encoding and with
6266 * either multiple Content-Length header fields having differing
6267 * field-values or a single Content-Length header field having an
6268 * invalid value, then the message framing is invalid and the
6269 * recipient MUST treat it as an unrecoverable error. If this is a
6270 * request message, the server MUST respond with a 400 (Bad Request)
6271 * status code and then close the connection. If this is a response
6272 * message received by a proxy, the proxy MUST close the connection
6273 * to the server, discard the received response, and send a 502 (Bad
6274 * Gateway) response to the client. If this is a response message
6275 * received by a user agent, the user agent MUST close the
6276 * connection to the server and discard the received response.
6277 *
6278 * 5. If a valid Content-Length header field is present without
6279 * Transfer-Encoding, its decimal value defines the expected message
6280 * body length in octets. If the sender closes the connection or
6281 * the recipient times out before the indicated number of octets are
6282 * received, the recipient MUST consider the message to be
6283 * incomplete and close the connection.
6284 *
6285 * 6. If this is a request message and none of the above are true, then
6286 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006287 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006288 * 7. Otherwise, this is a response message without a declared message
6289 * body length, so the message body length is determined by the
6290 * number of octets received prior to the server closing the
6291 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006292 */
6293
6294 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006295 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006296 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006297 * FIXME: should we parse anyway and return an error on chunked encoding ?
6298 */
6299 if (txn->meth == HTTP_METH_HEAD ||
6300 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006301 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006302 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006303 goto skip_content_length;
6304 }
6305
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006306 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006307 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006308 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006309 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006310 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6311 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006312 /* bad transfer-encoding (chunked followed by something else) */
6313 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006314 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006315 break;
6316 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006317 }
6318
Willy Tarreau1c913912015-04-30 10:57:51 +02006319 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006320 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006321 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006322 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6323 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6324 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006325 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006326 signed long long cl;
6327
Willy Tarreauad14f752011-09-02 20:33:27 +02006328 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006329 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006330 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006331 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006332
Willy Tarreauad14f752011-09-02 20:33:27 +02006333 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006334 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006335 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006336 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006337
Willy Tarreauad14f752011-09-02 20:33:27 +02006338 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006339 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006340 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006341 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006342
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006343 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006344 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006345 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006346 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006347
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006348 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006349 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006350 }
6351
6352skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006353 /* Now we have to check if we need to modify the Connection header.
6354 * This is more difficult on the response than it is on the request,
6355 * because we can have two different HTTP versions and we don't know
6356 * how the client will interprete a response. For instance, let's say
6357 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6358 * HTTP/1.1 response without any header. Maybe it will bound itself to
6359 * HTTP/1.0 because it only knows about it, and will consider the lack
6360 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6361 * the lack of header as a keep-alive. Thus we will use two flags
6362 * indicating how a request MAY be understood by the client. In case
6363 * of multiple possibilities, we'll fix the header to be explicit. If
6364 * ambiguous cases such as both close and keepalive are seen, then we
6365 * will fall back to explicit close. Note that we won't take risks with
6366 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006367 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006368 */
6369
Willy Tarreaudc008c52010-02-01 16:20:08 +01006370 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6371 txn->status == 101)) {
6372 /* Either we've established an explicit tunnel, or we're
6373 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006374 * to understand the next protocols. We have to switch to tunnel
6375 * mode, so that we transfer the request and responses then let
6376 * this protocol pass unmodified. When we later implement specific
6377 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006378 * header which contains information about that protocol for
6379 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006380 */
6381 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6382 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006383 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6384 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006385 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006386 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006387 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006388
Willy Tarreau70dffda2014-01-30 03:07:23 +01006389 /* this situation happens when combining pretend-keepalive with httpclose. */
6390 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006391 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006392 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006393 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6394
Willy Tarreau60466522010-01-18 19:08:45 +01006395 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006396 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006397 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6398 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006399
Willy Tarreau60466522010-01-18 19:08:45 +01006400 /* now adjust header transformations depending on current state */
6401 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6402 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6403 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006404 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006405 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006406 }
Willy Tarreau60466522010-01-18 19:08:45 +01006407 else { /* SCL / KAL */
6408 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006409 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006410 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006411 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006412
Willy Tarreau60466522010-01-18 19:08:45 +01006413 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006414 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006415
Willy Tarreau60466522010-01-18 19:08:45 +01006416 /* Some keep-alive responses are converted to Server-close if
6417 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006418 */
Willy Tarreau60466522010-01-18 19:08:45 +01006419 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6420 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006421 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006422 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006423 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006424 }
6425
Willy Tarreau7959a552013-09-23 16:44:27 +02006426 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006427 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006428
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006429 /* end of job, return OK */
6430 rep->analysers &= ~an_bit;
6431 rep->analyse_exp = TICK_ETERNITY;
6432 channel_auto_close(rep);
6433 return 1;
6434
6435 abort_keep_alive:
6436 /* A keep-alive request to the server failed on a network error.
6437 * The client is required to retry. We need to close without returning
6438 * any other information so that the client retries.
6439 */
6440 txn->status = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +02006441 rep->analysers &= AN_FLT_END;
6442 s->req.analysers &= AN_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006443 channel_auto_close(rep);
6444 s->logs.logwait = 0;
6445 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006446 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006447 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006448 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006449 return 0;
6450}
6451
6452/* This function performs all the processing enabled for the current response.
6453 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006454 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006455 * other functions. It works like process_request (see indications above).
6456 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006457int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006458{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006459 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006460 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006461 struct http_msg *msg = &txn->rsp;
6462 struct proxy *cur_proxy;
6463 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006464 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006465
Willy Tarreau87b09662015-04-03 00:22:06 +02006466 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 +02006467 now_ms, __FUNCTION__,
6468 s,
6469 rep,
6470 rep->rex, rep->wex,
6471 rep->flags,
6472 rep->buf->i,
6473 rep->analysers);
6474
6475 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6476 return 0;
6477
Willy Tarreau70730dd2014-04-24 18:06:27 +02006478 /* The stats applet needs to adjust the Connection header but we don't
6479 * apply any filter there.
6480 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006481 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6482 rep->analysers &= ~an_bit;
6483 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006484 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006485 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006486
Willy Tarreau58975672014-04-24 21:13:57 +02006487 /*
6488 * We will have to evaluate the filters.
6489 * As opposed to version 1.2, now they will be evaluated in the
6490 * filters order and not in the header order. This means that
6491 * each filter has to be validated among all headers.
6492 *
6493 * Filters are tried with ->be first, then with ->fe if it is
6494 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006495 *
6496 * Maybe we are in resume condiion. In this case I choose the
6497 * "struct proxy" which contains the rule list matching the resume
6498 * pointer. If none of theses "struct proxy" match, I initialise
6499 * the process with the first one.
6500 *
6501 * In fact, I check only correspondance betwwen the current list
6502 * pointer and the ->fe rule list. If it doesn't match, I initialize
6503 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006504 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006505 if (s->current_rule_list == &sess->fe->http_res_rules)
6506 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006507 else
6508 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006509 while (1) {
6510 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006511
Willy Tarreau58975672014-04-24 21:13:57 +02006512 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02006513 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006514 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006515
Willy Tarreau51d861a2015-05-22 17:30:48 +02006516 if (ret == HTTP_RULE_RES_BADREQ)
6517 goto return_srv_prx_502;
6518
6519 if (ret == HTTP_RULE_RES_DONE) {
6520 rep->analysers &= ~an_bit;
6521 rep->analyse_exp = TICK_ETERNITY;
6522 return 1;
6523 }
6524 }
6525
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006526 /* we need to be called again. */
6527 if (ret == HTTP_RULE_RES_YIELD) {
6528 channel_dont_close(rep);
6529 return 0;
6530 }
6531
Willy Tarreau58975672014-04-24 21:13:57 +02006532 /* try headers filters */
6533 if (rule_set->rsp_exp != NULL) {
6534 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6535 return_bad_resp:
6536 if (objt_server(s->target)) {
6537 objt_server(s->target)->counters.failed_resp++;
6538 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006539 }
Willy Tarreau58975672014-04-24 21:13:57 +02006540 s->be->be_counters.failed_resp++;
6541 return_srv_prx_502:
Christopher Fauletd7c91962015-04-30 11:48:27 +02006542 rep->analysers &= AN_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02006543 txn->status = 502;
6544 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006545 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006546 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006547 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006548 if (!(s->flags & SF_ERR_MASK))
6549 s->flags |= SF_ERR_PRXCOND;
6550 if (!(s->flags & SF_FINST_MASK))
6551 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006552 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006553 }
Willy Tarreau58975672014-04-24 21:13:57 +02006554 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006555
Willy Tarreau58975672014-04-24 21:13:57 +02006556 /* has the response been denied ? */
6557 if (txn->flags & TX_SVDENY) {
6558 if (objt_server(s->target))
6559 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006560
Willy Tarreau58975672014-04-24 21:13:57 +02006561 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006562 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006563 if (sess->listener->counters)
6564 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006565
Willy Tarreau58975672014-04-24 21:13:57 +02006566 goto return_srv_prx_502;
6567 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006568
Willy Tarreau58975672014-04-24 21:13:57 +02006569 /* add response headers from the rule sets in the same order */
6570 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006571 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006572 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006573 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006574 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006575 ret = acl_pass(ret);
6576 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6577 ret = !ret;
6578 if (!ret)
6579 continue;
6580 }
6581 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6582 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006583 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006584
Willy Tarreau58975672014-04-24 21:13:57 +02006585 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006586 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006587 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006588 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006589 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006590
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006591 /* After this point, this anayzer can't return yield, so we can
6592 * remove the bit corresponding to this analyzer from the list.
6593 *
6594 * Note that the intermediate returns and goto found previously
6595 * reset the analyzers.
6596 */
6597 rep->analysers &= ~an_bit;
6598 rep->analyse_exp = TICK_ETERNITY;
6599
Willy Tarreau58975672014-04-24 21:13:57 +02006600 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006601 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006602 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006603
Willy Tarreau58975672014-04-24 21:13:57 +02006604 /*
6605 * Now check for a server cookie.
6606 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006607 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02006608 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006609
Willy Tarreau58975672014-04-24 21:13:57 +02006610 /*
6611 * Check for cache-control or pragma headers if required.
6612 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006613 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 +02006614 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006615
Willy Tarreau58975672014-04-24 21:13:57 +02006616 /*
6617 * Add server cookie in the response if needed
6618 */
6619 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6620 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006621 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006622 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6623 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6624 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6625 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6626 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006627 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006628 /* the server is known, it's not the one the client requested, or the
6629 * cookie's last seen date needs to be refreshed. We have to
6630 * insert a set-cookie here, except if we want to insert only on POST
6631 * requests and this one isn't. Note that servers which don't have cookies
6632 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006633 */
Willy Tarreau58975672014-04-24 21:13:57 +02006634 if (!objt_server(s->target)->cookie) {
6635 chunk_printf(&trash,
6636 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6637 s->be->cookie_name);
6638 }
6639 else {
6640 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006641
Willy Tarreau58975672014-04-24 21:13:57 +02006642 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6643 /* emit last_date, which is mandatory */
6644 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6645 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6646 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006647
Willy Tarreau58975672014-04-24 21:13:57 +02006648 if (s->be->cookie_maxlife) {
6649 /* emit first_date, which is either the original one or
6650 * the current date.
6651 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006652 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006653 s30tob64(txn->cookie_first_date ?
6654 txn->cookie_first_date >> 2 :
6655 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006656 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006657 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006658 }
Willy Tarreau58975672014-04-24 21:13:57 +02006659 chunk_appendf(&trash, "; path=/");
6660 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006661
Willy Tarreau58975672014-04-24 21:13:57 +02006662 if (s->be->cookie_domain)
6663 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006664
Willy Tarreau58975672014-04-24 21:13:57 +02006665 if (s->be->ck_opts & PR_CK_HTTPONLY)
6666 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006667
Willy Tarreau58975672014-04-24 21:13:57 +02006668 if (s->be->ck_opts & PR_CK_SECURE)
6669 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006670
Willy Tarreau58975672014-04-24 21:13:57 +02006671 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6672 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006673
Willy Tarreau58975672014-04-24 21:13:57 +02006674 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006675 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006676 /* the server did not change, only the date was updated */
6677 txn->flags |= TX_SCK_UPDATED;
6678 else
6679 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006680
Willy Tarreau58975672014-04-24 21:13:57 +02006681 /* Here, we will tell an eventual cache on the client side that we don't
6682 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6683 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6684 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006685 */
Willy Tarreau58975672014-04-24 21:13:57 +02006686 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006687
Willy Tarreau58975672014-04-24 21:13:57 +02006688 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006689
Willy Tarreau58975672014-04-24 21:13:57 +02006690 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6691 "Cache-control: private", 22) < 0))
6692 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006693 }
Willy Tarreau58975672014-04-24 21:13:57 +02006694 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006695
Willy Tarreau58975672014-04-24 21:13:57 +02006696 /*
6697 * Check if result will be cacheable with a cookie.
6698 * We'll block the response if security checks have caught
6699 * nasty things such as a cacheable cookie.
6700 */
6701 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6702 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6703 (s->be->options & PR_O_CHK_CACHE)) {
6704 /* we're in presence of a cacheable response containing
6705 * a set-cookie header. We'll block it as requested by
6706 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006707 */
Willy Tarreau58975672014-04-24 21:13:57 +02006708 if (objt_server(s->target))
6709 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006710
Willy Tarreau58975672014-04-24 21:13:57 +02006711 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006712 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006713 if (sess->listener->counters)
6714 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006715
Willy Tarreau58975672014-04-24 21:13:57 +02006716 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6717 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6718 send_log(s->be, LOG_ALERT,
6719 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6720 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6721 goto return_srv_prx_502;
6722 }
Willy Tarreau03945942009-12-22 16:50:27 +01006723
Willy Tarreau70730dd2014-04-24 18:06:27 +02006724 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006725 /*
6726 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6727 * If an "Upgrade" token is found, the header is left untouched in order
6728 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006729 * if anything but "Upgrade" is present in the Connection header. We don't
6730 * want to touch any 101 response either since it's switching to another
6731 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006732 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006733 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006734 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006735 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006736 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6737 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006738
Willy Tarreau58975672014-04-24 21:13:57 +02006739 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6740 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6741 /* we want a keep-alive response here. Keep-alive header
6742 * required if either side is not 1.1.
6743 */
6744 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6745 want_flags |= TX_CON_KAL_SET;
6746 }
6747 else {
6748 /* we want a close response here. Close header required if
6749 * the server is 1.1, regardless of the client.
6750 */
6751 if (msg->flags & HTTP_MSGF_VER_11)
6752 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006753 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006754
Willy Tarreau58975672014-04-24 21:13:57 +02006755 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6756 http_change_connection_header(txn, msg, want_flags);
6757 }
6758
6759 skip_header_mangling:
Christopher Faulet3e344292015-11-24 16:24:13 +01006760 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_FILTERS(s) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006761 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
6762 rep->analysers &= ~AN_FLT_XFER_DATA;
Willy Tarreau58975672014-04-24 21:13:57 +02006763 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Christopher Fauletd7c91962015-04-30 11:48:27 +02006764 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006765
Willy Tarreau58975672014-04-24 21:13:57 +02006766 /* if the user wants to log as soon as possible, without counting
6767 * bytes from the server, then this is the right moment. We have
6768 * to temporarily assign bytes_out to log what we currently have.
6769 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006770 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006771 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6772 s->logs.bytes_out = txn->rsp.eoh;
6773 s->do_log(s);
6774 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006775 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006776 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006777}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006778
Willy Tarreaud98cf932009-12-27 22:54:55 +01006779/* This function is an analyser which forwards response body (including chunk
6780 * sizes if any). It is called as soon as we must forward, even if we forward
6781 * zero byte. The only situation where it must not be called is when we're in
6782 * tunnel mode and we want to forward till the close. It's used both to forward
6783 * remaining data and to resync after end of body. It expects the msg_state to
6784 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006785 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006786 *
6787 * It is capable of compressing response data both in content-length mode and
6788 * in chunked mode. The state machines follows different flows depending on
6789 * whether content-length and chunked modes are used, since there are no
6790 * trailers in content-length :
6791 *
6792 * chk-mode cl-mode
6793 * ,----- BODY -----.
6794 * / \
6795 * V size > 0 V chk-mode
6796 * .--> SIZE -------------> DATA -------------> CRLF
6797 * | | size == 0 | last byte |
6798 * | v final crlf v inspected |
6799 * | TRAILERS -----------> DONE |
6800 * | |
6801 * `----------------------------------------------'
6802 *
6803 * Compression only happens in the DATA state, and must be flushed in final
6804 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6805 * is performed at once on final states for all bytes parsed, or when leaving
6806 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006807 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006808int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006809{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006810 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006811 struct http_txn *txn = s->txn;
6812 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01006813 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006814
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006815 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6816 return 0;
6817
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006818 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006819 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006820 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006821 /* Output closed while we were sending data. We must abort and
6822 * wake the other side up.
6823 */
6824 msg->msg_state = HTTP_MSG_ERROR;
6825 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006826 return 1;
6827 }
6828
Willy Tarreau4fe41902010-06-07 22:27:41 +02006829 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006830 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006831
Christopher Fauletd7c91962015-04-30 11:48:27 +02006832 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006833 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
6834 ? HTTP_MSG_CHUNK_SIZE
6835 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006836 }
6837
Willy Tarreauefdf0942014-04-24 20:08:57 +02006838 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006839 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02006840 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006841 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006842 }
6843
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006844 if (msg->msg_state < HTTP_MSG_DONE) {
6845 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6846 ? http_msg_forward_chunked_body(s, msg)
6847 : http_msg_forward_body(s, msg));
6848 if (!ret)
6849 goto missing_data_or_waiting;
6850 if (ret < 0)
6851 goto return_bad_res;
6852 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006853
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006854 /* other states, DONE...TUNNEL */
6855 /* for keep-alive we don't want to forward closes on DONE */
6856 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6857 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6858 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006859
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006860 ret = msg->msg_state;
6861 if (http_resync_states(s)) {
6862 /* some state changes occurred, maybe the analyser was disabled
6863 * too. */
6864 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6865 if (res->flags & CF_SHUTW) {
6866 /* response errors are most likely due to the
6867 * client aborting the transfer. */
6868 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006869 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006870 if (msg->err_pos >= 0)
6871 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, strm_fe(s));
6872 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006873 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006874 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006875 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006876 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006877
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006878 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006879 if (res->flags & CF_SHUTW)
6880 goto aborted_xfer;
6881
6882 /* stop waiting for data if the input is closed before the end. If the
6883 * client side was already closed, it means that the client has aborted,
6884 * so we don't want to count this as a server abort. Otherwise it's a
6885 * server abort.
6886 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006887 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006888 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006889 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006890 /* If we have some pending data, we continue the processing */
6891 if (!buffer_pending(res->buf)) {
6892 if (!(s->flags & SF_ERR_MASK))
6893 s->flags |= SF_ERR_SRVCL;
6894 s->be->be_counters.srv_aborts++;
6895 if (objt_server(s->target))
6896 objt_server(s->target)->counters.srv_aborts++;
6897 goto return_bad_res_stats_ok;
6898 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006899 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006900
Willy Tarreau40dba092010-03-04 18:14:51 +01006901 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006902 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006903 goto return_bad_res;
6904
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006905 /* When TE: chunked is used, we need to get there again to parse
6906 * remaining chunks even if the server has closed, so we don't want to
6907 * set CF_DONTCLOSE. Similarly, if the body length is undefined, if
6908 * keep-alive is set on the client side or if there are filters
6909 * registered on the stream, we don't want to forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006910 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006911 if ((msg->flags & HTTP_MSGF_TE_CHNK) || !msg->body_len ||
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006912 HAS_FILTERS(s) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006913 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6914 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006915 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006916
Willy Tarreau5c620922011-05-11 19:56:11 +02006917 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006918 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006919 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006920 * modes are already handled by the stream sock layer. We must not do
6921 * this in content-length mode because it could present the MSG_MORE
6922 * flag with the last block of forwarded data, which would cause an
6923 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006924 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006925 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006926 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006927
Willy Tarreau87b09662015-04-03 00:22:06 +02006928 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006929 return 0;
6930
Willy Tarreau40dba092010-03-04 18:14:51 +01006931 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006932 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006933 if (objt_server(s->target))
6934 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006935
6936 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006937 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006938 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006939 http_reply_and_close(s, txn->status, NULL);
Christopher Fauletd7c91962015-04-30 11:48:27 +02006940 res->analysers &= AN_FLT_END;
6941 s->req.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006942 if (objt_server(s->target))
6943 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006944
Willy Tarreaue7dff022015-04-03 01:14:29 +02006945 if (!(s->flags & SF_ERR_MASK))
6946 s->flags |= SF_ERR_PRXCOND;
6947 if (!(s->flags & SF_FINST_MASK))
6948 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006949 return 0;
6950
6951 aborted_xfer:
6952 txn->rsp.msg_state = HTTP_MSG_ERROR;
6953 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006954 http_reply_and_close(s, txn->status, NULL);
Christopher Fauletd7c91962015-04-30 11:48:27 +02006955 res->analysers &= AN_FLT_END;
6956 s->req.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006957
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006958 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006959 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006960 if (objt_server(s->target))
6961 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006962
Willy Tarreaue7dff022015-04-03 01:14:29 +02006963 if (!(s->flags & SF_ERR_MASK))
6964 s->flags |= SF_ERR_CLICL;
6965 if (!(s->flags & SF_FINST_MASK))
6966 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006967 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006968}
6969
6970
6971static inline int
6972http_msg_forward_body(struct stream *s, struct http_msg *msg)
6973{
6974 struct channel *chn = msg->chn;
6975 int ret;
6976
6977 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
6978
6979 if (msg->msg_state == HTTP_MSG_ENDING)
6980 goto ending;
6981
6982 /* Neither content-length, nor transfer-encoding was found, so we must
6983 * read the body until the server connection is closed. In that case, we
6984 * eat data as they come. Of course, this happens for response only. */
6985 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6986 unsigned long long len = (chn->buf->i - msg->next);
6987 msg->chunk_len += len;
6988 msg->body_len += len;
6989 }
Christopher Fauletda02e172015-12-04 09:25:05 +01006990 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6991 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6992 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006993 msg->next += ret;
6994 msg->chunk_len -= ret;
6995 if (msg->chunk_len) {
6996 /* input empty or output full */
6997 if (chn->buf->i > msg->next)
6998 chn->flags |= CF_WAKE_WRITE;
6999 goto missing_data_or_waiting;
7000 }
7001
7002 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && !(chn->flags & CF_SHUTR)) {
7003 /* The server still sending data */
7004 goto missing_data_or_waiting;
7005 }
7006 msg->msg_state = HTTP_MSG_ENDING;
7007
7008 ending:
7009 /* we may have some pending data starting at res->buf->p such as a last
7010 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01007011 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
7012 /* default_ret */ msg->next,
7013 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007014 b_adv(chn->buf, ret);
7015 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007016 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
7017 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02007018 if (msg->next)
7019 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007020
Christopher Fauletda02e172015-12-04 09:25:05 +01007021 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
7022 /* default_ret */ 1,
7023 /* on_error */ goto error,
7024 /* on_wait */ goto waiting);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007025 msg->msg_state = HTTP_MSG_DONE;
7026 return 1;
7027
7028 missing_data_or_waiting:
7029 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01007030 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
7031 /* default_ret */ msg->next,
7032 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007033 b_adv(chn->buf, ret);
7034 msg->next -= ret;
7035 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
7036 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01007037 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007038 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02007039 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007040 return 0;
7041 error:
7042 return -1;
7043}
7044
7045static inline int
7046http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
7047{
7048 struct channel *chn = msg->chn;
7049 int ret;
7050
7051 /* Here we have the guarantee to be in one of the following state:
7052 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
7053 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
7054
7055 switch_states:
7056 switch (msg->msg_state) {
7057 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01007058 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
7059 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
7060 /* on_error */ goto error);
7061 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007062 msg->chunk_len -= ret;
7063 if (msg->chunk_len) {
7064 /* input empty or output full */
7065 if (chn->buf->i > msg->next)
7066 chn->flags |= CF_WAKE_WRITE;
7067 goto missing_data_or_waiting;
7068 }
7069
7070 /* nothing left to forward for this chunk*/
7071 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
7072 /* fall through for HTTP_MSG_CHUNK_CRLF */
7073
7074 case HTTP_MSG_CHUNK_CRLF:
7075 /* we want the CRLF after the data */
7076 ret = http_skip_chunk_crlf(msg);
7077 if (ret == 0)
7078 goto missing_data_or_waiting;
7079 if (ret < 0)
7080 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01007081 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007082 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
7083 /* fall through for HTTP_MSG_CHUNK_SIZE */
7084
7085 case HTTP_MSG_CHUNK_SIZE:
7086 /* read the chunk size and assign it to ->chunk_len,
7087 * then set ->next to point to the body and switch to
7088 * DATA or TRAILERS state.
7089 */
7090 ret = http_parse_chunk_size(msg);
7091 if (ret == 0)
7092 goto missing_data_or_waiting;
7093 if (ret < 0)
7094 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01007095 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007096 if (msg->chunk_len) {
7097 msg->msg_state = HTTP_MSG_DATA;
7098 goto switch_states;
7099 }
7100 msg->msg_state = HTTP_MSG_TRAILERS;
7101 /* fall through for HTTP_MSG_TRAILERS */
7102
7103 case HTTP_MSG_TRAILERS:
7104 ret = http_forward_trailers(msg);
7105 if (ret < 0)
7106 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01007107 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
7108 /* default_ret */ 1,
7109 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007110 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007111 if (!ret)
7112 goto missing_data_or_waiting;
7113 break;
7114
7115 case HTTP_MSG_ENDING:
7116 goto ending;
7117
7118 default:
7119 /* This should no happen in this function */
7120 goto error;
7121 }
7122
7123 msg->msg_state = HTTP_MSG_ENDING;
7124 ending:
7125 /* we may have some pending data starting at res->buf->p such as a last
7126 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01007127 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007128 /* default_ret */ msg->next,
7129 /* on_error */ goto error);
7130 b_adv(chn->buf, ret);
7131 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007132 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
7133 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02007134 if (msg->next)
7135 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007136
Christopher Fauletda02e172015-12-04 09:25:05 +01007137 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007138 /* default_ret */ 1,
7139 /* on_error */ goto error,
7140 /* on_wait */ goto waiting);
7141 msg->msg_state = HTTP_MSG_DONE;
7142 return 1;
7143
7144 missing_data_or_waiting:
7145 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01007146 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007147 /* default_ret */ msg->next,
7148 /* on_error */ goto error);
7149 b_adv(chn->buf, ret);
7150 msg->next -= ret;
7151 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
7152 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01007153 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007154 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02007155 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007156 return 0;
7157
7158 chunk_parsing_error:
7159 if (msg->err_pos >= 0) {
7160 if (chn->flags & CF_ISRESP)
7161 http_capture_bad_message(&s->be->invalid_rep, s, msg,
7162 msg->msg_state, strm_fe(s));
7163 else
7164 http_capture_bad_message(&strm_fe(s)->invalid_req, s,
7165 msg, msg->msg_state, s->be);
7166 }
7167 error:
7168 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007169}
7170
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007171
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007172/* Iterate the same filter through all request headers.
7173 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007174 * Since it can manage the switch to another backend, it updates the per-proxy
7175 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007176 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007177int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007178{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007179 char *cur_ptr, *cur_end, *cur_next;
7180 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007181 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007182 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007183 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007184
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007185 last_hdr = 0;
7186
Willy Tarreau9b28e032012-10-12 23:49:43 +02007187 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007188 old_idx = 0;
7189
7190 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007191 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007192 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007193 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007194 (exp->action == ACT_ALLOW ||
7195 exp->action == ACT_DENY ||
7196 exp->action == ACT_TARPIT))
7197 return 0;
7198
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007199 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007200 if (!cur_idx)
7201 break;
7202
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007203 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007204 cur_ptr = cur_next;
7205 cur_end = cur_ptr + cur_hdr->len;
7206 cur_next = cur_end + cur_hdr->cr + 1;
7207
7208 /* Now we have one header between cur_ptr and cur_end,
7209 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007210 */
7211
Willy Tarreau15a53a42015-01-21 13:39:42 +01007212 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007213 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007214 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007215 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007216 last_hdr = 1;
7217 break;
7218
7219 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007220 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007221 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007222 break;
7223
7224 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007225 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007226 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007227 break;
7228
7229 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007230 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7231 if (trash.len < 0)
7232 return -1;
7233
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007234 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007235 /* FIXME: if the user adds a newline in the replacement, the
7236 * index will not be recalculated for now, and the new line
7237 * will not be counted as a new header.
7238 */
7239
7240 cur_end += delta;
7241 cur_next += delta;
7242 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007243 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007244 break;
7245
7246 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007247 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007248 cur_next += delta;
7249
Willy Tarreaufa355d42009-11-29 18:12:29 +01007250 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007251 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7252 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007253 cur_hdr->len = 0;
7254 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007255 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007256 break;
7257
7258 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007259 }
7260
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007261 /* keep the link from this header to next one in case of later
7262 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007263 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007264 old_idx = cur_idx;
7265 }
7266 return 0;
7267}
7268
7269
7270/* Apply the filter to the request line.
7271 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7272 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007273 * Since it can manage the switch to another backend, it updates the per-proxy
7274 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007275 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007276int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007277{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007278 char *cur_ptr, *cur_end;
7279 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007280 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007281 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007282
Willy Tarreau3d300592007-03-18 18:34:41 +01007283 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007284 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007285 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007286 (exp->action == ACT_ALLOW ||
7287 exp->action == ACT_DENY ||
7288 exp->action == ACT_TARPIT))
7289 return 0;
7290 else if (exp->action == ACT_REMOVE)
7291 return 0;
7292
7293 done = 0;
7294
Willy Tarreau9b28e032012-10-12 23:49:43 +02007295 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007296 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007297
7298 /* Now we have the request line between cur_ptr and cur_end */
7299
Willy Tarreau15a53a42015-01-21 13:39:42 +01007300 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007301 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007302 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007303 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007304 done = 1;
7305 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007306
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007307 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007308 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007309 done = 1;
7310 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007311
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007312 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007313 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007314 done = 1;
7315 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007316
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007317 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007318 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7319 if (trash.len < 0)
7320 return -1;
7321
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007322 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007323 /* FIXME: if the user adds a newline in the replacement, the
7324 * index will not be recalculated for now, and the new line
7325 * will not be counted as a new header.
7326 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007327
Willy Tarreaufa355d42009-11-29 18:12:29 +01007328 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007329 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007330 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007331 HTTP_MSG_RQMETH,
7332 cur_ptr, cur_end + 1,
7333 NULL, NULL);
7334 if (unlikely(!cur_end))
7335 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007336
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007337 /* we have a full request and we know that we have either a CR
7338 * or an LF at <ptr>.
7339 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007340 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7341 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007342 /* there is no point trying this regex on headers */
7343 return 1;
7344 }
7345 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007346 return done;
7347}
Willy Tarreau97de6242006-12-27 17:18:38 +01007348
Willy Tarreau58f10d72006-12-04 02:26:12 +01007349
Willy Tarreau58f10d72006-12-04 02:26:12 +01007350
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007351/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007352 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007353 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007354 * unparsable request. Since it can manage the switch to another backend, it
7355 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007356 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007357int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007358{
Willy Tarreau192252e2015-04-04 01:47:55 +02007359 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007360 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007361 struct hdr_exp *exp;
7362
7363 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007364 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007365
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007366 /*
7367 * The interleaving of transformations and verdicts
7368 * makes it difficult to decide to continue or stop
7369 * the evaluation.
7370 */
7371
Willy Tarreau6c123b12010-01-28 20:22:06 +01007372 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7373 break;
7374
Willy Tarreau3d300592007-03-18 18:34:41 +01007375 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007376 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007377 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007378 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007379
7380 /* if this filter had a condition, evaluate it now and skip to
7381 * next filter if the condition does not match.
7382 */
7383 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007384 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007385 ret = acl_pass(ret);
7386 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7387 ret = !ret;
7388
7389 if (!ret)
7390 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007391 }
7392
7393 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007394 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007395 if (unlikely(ret < 0))
7396 return -1;
7397
7398 if (likely(ret == 0)) {
7399 /* The filter did not match the request, it can be
7400 * iterated through all headers.
7401 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007402 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7403 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007404 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007405 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007406 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007407}
7408
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007409
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007410/* Find the end of a cookie value contained between <s> and <e>. It works the
7411 * same way as with headers above except that the semi-colon also ends a token.
7412 * See RFC2965 for more information. Note that it requires a valid header to
7413 * return a valid result.
7414 */
7415char *find_cookie_value_end(char *s, const char *e)
7416{
7417 int quoted, qdpair;
7418
7419 quoted = qdpair = 0;
7420 for (; s < e; s++) {
7421 if (qdpair) qdpair = 0;
7422 else if (quoted) {
7423 if (*s == '\\') qdpair = 1;
7424 else if (*s == '"') quoted = 0;
7425 }
7426 else if (*s == '"') quoted = 1;
7427 else if (*s == ',' || *s == ';') return s;
7428 }
7429 return s;
7430}
7431
7432/* Delete a value in a header between delimiters <from> and <next> in buffer
7433 * <buf>. The number of characters displaced is returned, and the pointer to
7434 * the first delimiter is updated if required. The function tries as much as
7435 * possible to respect the following principles :
7436 * - replace <from> delimiter by the <next> one unless <from> points to a
7437 * colon, in which case <next> is simply removed
7438 * - set exactly one space character after the new first delimiter, unless
7439 * there are not enough characters in the block being moved to do so.
7440 * - remove unneeded spaces before the previous delimiter and after the new
7441 * one.
7442 *
7443 * It is the caller's responsibility to ensure that :
7444 * - <from> points to a valid delimiter or the colon ;
7445 * - <next> points to a valid delimiter or the final CR/LF ;
7446 * - there are non-space chars before <from> ;
7447 * - there is a CR/LF at or after <next>.
7448 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007449int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007450{
7451 char *prev = *from;
7452
7453 if (*prev == ':') {
7454 /* We're removing the first value, preserve the colon and add a
7455 * space if possible.
7456 */
Willy Tarreau2235b262016-11-05 15:50:20 +01007457 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007458 next++;
7459 prev++;
7460 if (prev < next)
7461 *prev++ = ' ';
7462
Willy Tarreau2235b262016-11-05 15:50:20 +01007463 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007464 next++;
7465 } else {
7466 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01007467 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007468 prev--;
7469 *from = prev;
7470
7471 /* copy the delimiter and if possible a space if we're
7472 * not at the end of the line.
7473 */
Willy Tarreau2235b262016-11-05 15:50:20 +01007474 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007475 *prev++ = *next++;
7476 if (prev + 1 < next)
7477 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01007478 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007479 next++;
7480 }
7481 }
7482 return buffer_replace2(buf, prev, next, NULL, 0);
7483}
7484
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007485/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007486 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007487 * desirable to call it only when needed. This code is quite complex because
7488 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7489 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007490 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007491void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007492{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007493 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007494 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007495 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007496 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007497 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7498 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007499
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007500 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007501 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007502 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007503
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007504 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007505 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007506 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007507
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007508 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007509 hdr_beg = hdr_next;
7510 hdr_end = hdr_beg + cur_hdr->len;
7511 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007512
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007513 /* We have one full header between hdr_beg and hdr_end, and the
7514 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007515 * "Cookie:" headers.
7516 */
7517
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007518 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007519 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007520 old_idx = cur_idx;
7521 continue;
7522 }
7523
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007524 del_from = NULL; /* nothing to be deleted */
7525 preserve_hdr = 0; /* assume we may kill the whole header */
7526
Willy Tarreau58f10d72006-12-04 02:26:12 +01007527 /* Now look for cookies. Conforming to RFC2109, we have to support
7528 * attributes whose name begin with a '$', and associate them with
7529 * the right cookie, if we want to delete this cookie.
7530 * So there are 3 cases for each cookie read :
7531 * 1) it's a special attribute, beginning with a '$' : ignore it.
7532 * 2) it's a server id cookie that we *MAY* want to delete : save
7533 * some pointers on it (last semi-colon, beginning of cookie...)
7534 * 3) it's an application cookie : we *MAY* have to delete a previous
7535 * "special" cookie.
7536 * At the end of loop, if a "special" cookie remains, we may have to
7537 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007538 * *MUST* delete it.
7539 *
7540 * Note: RFC2965 is unclear about the processing of spaces around
7541 * the equal sign in the ATTR=VALUE form. A careful inspection of
7542 * the RFC explicitly allows spaces before it, and not within the
7543 * tokens (attrs or values). An inspection of RFC2109 allows that
7544 * too but section 10.1.3 lets one think that spaces may be allowed
7545 * after the equal sign too, resulting in some (rare) buggy
7546 * implementations trying to do that. So let's do what servers do.
7547 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7548 * allowed quoted strings in values, with any possible character
7549 * after a backslash, including control chars and delimitors, which
7550 * causes parsing to become ambiguous. Browsers also allow spaces
7551 * within values even without quotes.
7552 *
7553 * We have to keep multiple pointers in order to support cookie
7554 * removal at the beginning, middle or end of header without
7555 * corrupting the header. All of these headers are valid :
7556 *
7557 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7558 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7559 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7560 * | | | | | | | | |
7561 * | | | | | | | | hdr_end <--+
7562 * | | | | | | | +--> next
7563 * | | | | | | +----> val_end
7564 * | | | | | +-----------> val_beg
7565 * | | | | +--------------> equal
7566 * | | | +----------------> att_end
7567 * | | +---------------------> att_beg
7568 * | +--------------------------> prev
7569 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007570 */
7571
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007572 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7573 /* Iterate through all cookies on this line */
7574
7575 /* find att_beg */
7576 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007577 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007578 att_beg++;
7579
7580 /* find att_end : this is the first character after the last non
7581 * space before the equal. It may be equal to hdr_end.
7582 */
7583 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007584
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007585 while (equal < hdr_end) {
7586 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007587 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007588 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007589 continue;
7590 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007591 }
7592
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007593 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7594 * is between <att_beg> and <equal>, both may be identical.
7595 */
7596
7597 /* look for end of cookie if there is an equal sign */
7598 if (equal < hdr_end && *equal == '=') {
7599 /* look for the beginning of the value */
7600 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007601 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007602 val_beg++;
7603
7604 /* find the end of the value, respecting quotes */
7605 next = find_cookie_value_end(val_beg, hdr_end);
7606
7607 /* make val_end point to the first white space or delimitor after the value */
7608 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007609 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007610 val_end--;
7611 } else {
7612 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007613 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007614
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007615 /* We have nothing to do with attributes beginning with '$'. However,
7616 * they will automatically be removed if a header before them is removed,
7617 * since they're supposed to be linked together.
7618 */
7619 if (*att_beg == '$')
7620 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007621
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007622 /* Ignore cookies with no equal sign */
7623 if (equal == next) {
7624 /* This is not our cookie, so we must preserve it. But if we already
7625 * scheduled another cookie for removal, we cannot remove the
7626 * complete header, but we can remove the previous block itself.
7627 */
7628 preserve_hdr = 1;
7629 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007630 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007631 val_end += delta;
7632 next += delta;
7633 hdr_end += delta;
7634 hdr_next += delta;
7635 cur_hdr->len += delta;
7636 http_msg_move_end(&txn->req, delta);
7637 prev = del_from;
7638 del_from = NULL;
7639 }
7640 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007641 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007642
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007643 /* if there are spaces around the equal sign, we need to
7644 * strip them otherwise we'll get trouble for cookie captures,
7645 * or even for rewrites. Since this happens extremely rarely,
7646 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007647 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007648 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7649 int stripped_before = 0;
7650 int stripped_after = 0;
7651
7652 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007653 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007654 equal += stripped_before;
7655 val_beg += stripped_before;
7656 }
7657
7658 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007659 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007660 val_beg += stripped_after;
7661 stripped_before += stripped_after;
7662 }
7663
7664 val_end += stripped_before;
7665 next += stripped_before;
7666 hdr_end += stripped_before;
7667 hdr_next += stripped_before;
7668 cur_hdr->len += stripped_before;
7669 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007670 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007671 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007672
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007673 /* First, let's see if we want to capture this cookie. We check
7674 * that we don't already have a client side cookie, because we
7675 * can only capture one. Also as an optimisation, we ignore
7676 * cookies shorter than the declared name.
7677 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007678 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7679 (val_end - att_beg >= sess->fe->capture_namelen) &&
7680 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007681 int log_len = val_end - att_beg;
7682
7683 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7684 Alert("HTTP logging : out of memory.\n");
7685 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007686 if (log_len > sess->fe->capture_len)
7687 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007688 memcpy(txn->cli_cookie, att_beg, log_len);
7689 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007690 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007691 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007692
Willy Tarreaubca99692010-10-06 19:25:55 +02007693 /* Persistence cookies in passive, rewrite or insert mode have the
7694 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007695 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007696 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007697 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007698 * For cookies in prefix mode, the form is :
7699 *
7700 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007701 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007702 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7703 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7704 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007705 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007706
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007707 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7708 * have the server ID between val_beg and delim, and the original cookie between
7709 * delim+1 and val_end. Otherwise, delim==val_end :
7710 *
7711 * Cookie: NAME=SRV; # in all but prefix modes
7712 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7713 * | || || | |+-> next
7714 * | || || | +--> val_end
7715 * | || || +---------> delim
7716 * | || |+------------> val_beg
7717 * | || +-------------> att_end = equal
7718 * | |+-----------------> att_beg
7719 * | +------------------> prev
7720 * +-------------------------> hdr_beg
7721 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007722
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007723 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007724 for (delim = val_beg; delim < val_end; delim++)
7725 if (*delim == COOKIE_DELIM)
7726 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007727 } else {
7728 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007729 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007730 /* Now check if the cookie contains a date field, which would
7731 * appear after a vertical bar ('|') just after the server name
7732 * and before the delimiter.
7733 */
7734 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7735 if (vbar1) {
7736 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007737 * right is the last seen date. It is a base64 encoded
7738 * 30-bit value representing the UNIX date since the
7739 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007740 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007741 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007742 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007743 if (val_end - vbar1 >= 5) {
7744 val = b64tos30(vbar1);
7745 if (val > 0)
7746 txn->cookie_last_date = val << 2;
7747 }
7748 /* look for a second vertical bar */
7749 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7750 if (vbar1 && (val_end - vbar1 > 5)) {
7751 val = b64tos30(vbar1 + 1);
7752 if (val > 0)
7753 txn->cookie_first_date = val << 2;
7754 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007755 }
7756 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007757
Willy Tarreauf64d1412010-10-07 20:06:11 +02007758 /* if the cookie has an expiration date and the proxy wants to check
7759 * it, then we do that now. We first check if the cookie is too old,
7760 * then only if it has expired. We detect strict overflow because the
7761 * time resolution here is not great (4 seconds). Cookies with dates
7762 * in the future are ignored if their offset is beyond one day. This
7763 * allows an admin to fix timezone issues without expiring everyone
7764 * and at the same time avoids keeping unwanted side effects for too
7765 * long.
7766 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007767 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7768 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007769 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007770 txn->flags &= ~TX_CK_MASK;
7771 txn->flags |= TX_CK_OLD;
7772 delim = val_beg; // let's pretend we have not found the cookie
7773 txn->cookie_first_date = 0;
7774 txn->cookie_last_date = 0;
7775 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007776 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7777 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007778 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007779 txn->flags &= ~TX_CK_MASK;
7780 txn->flags |= TX_CK_EXPIRED;
7781 delim = val_beg; // let's pretend we have not found the cookie
7782 txn->cookie_first_date = 0;
7783 txn->cookie_last_date = 0;
7784 }
7785
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007786 /* Here, we'll look for the first running server which supports the cookie.
7787 * This allows to share a same cookie between several servers, for example
7788 * to dedicate backup servers to specific servers only.
7789 * However, to prevent clients from sticking to cookie-less backup server
7790 * when they have incidentely learned an empty cookie, we simply ignore
7791 * empty cookies and mark them as invalid.
7792 * The same behaviour is applied when persistence must be ignored.
7793 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007794 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007795 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007796
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007797 while (srv) {
7798 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7799 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007800 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007801 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007802 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007803 /* we found the server and we can use it */
7804 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007805 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007806 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007807 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007808 break;
7809 } else {
7810 /* we found a server, but it's down,
7811 * mark it as such and go on in case
7812 * another one is available.
7813 */
7814 txn->flags &= ~TX_CK_MASK;
7815 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007816 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007817 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007818 srv = srv->next;
7819 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007820
Willy Tarreauf64d1412010-10-07 20:06:11 +02007821 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007822 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007823 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007824 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007825 txn->flags |= TX_CK_UNUSED;
7826 else
7827 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007828 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007829
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007830 /* depending on the cookie mode, we may have to either :
7831 * - delete the complete cookie if we're in insert+indirect mode, so that
7832 * the server never sees it ;
7833 * - remove the server id from the cookie value, and tag the cookie as an
7834 * application cookie so that it does not get accidentely removed later,
7835 * if we're in cookie prefix mode
7836 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007837 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007838 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007839
Willy Tarreau9b28e032012-10-12 23:49:43 +02007840 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007841 val_end += delta;
7842 next += delta;
7843 hdr_end += delta;
7844 hdr_next += delta;
7845 cur_hdr->len += delta;
7846 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007847
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007848 del_from = NULL;
7849 preserve_hdr = 1; /* we want to keep this cookie */
7850 }
7851 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007852 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007853 del_from = prev;
7854 }
7855 } else {
7856 /* This is not our cookie, so we must preserve it. But if we already
7857 * scheduled another cookie for removal, we cannot remove the
7858 * complete header, but we can remove the previous block itself.
7859 */
7860 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007861
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007862 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007863 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007864 if (att_beg >= del_from)
7865 att_beg += delta;
7866 if (att_end >= del_from)
7867 att_end += delta;
7868 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007869 val_end += delta;
7870 next += delta;
7871 hdr_end += delta;
7872 hdr_next += delta;
7873 cur_hdr->len += delta;
7874 http_msg_move_end(&txn->req, delta);
7875 prev = del_from;
7876 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007877 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007878 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007879
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007880 /* continue with next cookie on this header line */
7881 att_beg = next;
7882 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007883
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007884 /* There are no more cookies on this line.
7885 * We may still have one (or several) marked for deletion at the
7886 * end of the line. We must do this now in two ways :
7887 * - if some cookies must be preserved, we only delete from the
7888 * mark to the end of line ;
7889 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007890 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007891 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007892 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007893 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007894 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007895 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007896 cur_hdr->len += delta;
7897 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007898 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007899
7900 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007901 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7902 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007903 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007904 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007905 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007906 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007907 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007908 }
7909
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007910 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007911 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007912 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007913}
7914
7915
Willy Tarreaua15645d2007-03-18 16:22:39 +01007916/* Iterate the same filter through all response headers contained in <rtr>.
7917 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7918 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007919int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007920{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007921 char *cur_ptr, *cur_end, *cur_next;
7922 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007923 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007924 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007925 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007926
7927 last_hdr = 0;
7928
Willy Tarreau9b28e032012-10-12 23:49:43 +02007929 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007930 old_idx = 0;
7931
7932 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007933 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007934 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007935 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007936 (exp->action == ACT_ALLOW ||
7937 exp->action == ACT_DENY))
7938 return 0;
7939
7940 cur_idx = txn->hdr_idx.v[old_idx].next;
7941 if (!cur_idx)
7942 break;
7943
7944 cur_hdr = &txn->hdr_idx.v[cur_idx];
7945 cur_ptr = cur_next;
7946 cur_end = cur_ptr + cur_hdr->len;
7947 cur_next = cur_end + cur_hdr->cr + 1;
7948
7949 /* Now we have one header between cur_ptr and cur_end,
7950 * and the next header starts at cur_next.
7951 */
7952
Willy Tarreau15a53a42015-01-21 13:39:42 +01007953 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007954 switch (exp->action) {
7955 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007956 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007957 last_hdr = 1;
7958 break;
7959
7960 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007961 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007962 last_hdr = 1;
7963 break;
7964
7965 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007966 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7967 if (trash.len < 0)
7968 return -1;
7969
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007970 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007971 /* FIXME: if the user adds a newline in the replacement, the
7972 * index will not be recalculated for now, and the new line
7973 * will not be counted as a new header.
7974 */
7975
7976 cur_end += delta;
7977 cur_next += delta;
7978 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007979 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007980 break;
7981
7982 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007983 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007984 cur_next += delta;
7985
Willy Tarreaufa355d42009-11-29 18:12:29 +01007986 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007987 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7988 txn->hdr_idx.used--;
7989 cur_hdr->len = 0;
7990 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007991 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007992 break;
7993
7994 }
7995 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007996
7997 /* keep the link from this header to next one in case of later
7998 * removal of next header.
7999 */
8000 old_idx = cur_idx;
8001 }
8002 return 0;
8003}
8004
8005
8006/* Apply the filter to the status line in the response buffer <rtr>.
8007 * Returns 0 if nothing has been done, 1 if the filter has been applied,
8008 * or -1 if a replacement resulted in an invalid status line.
8009 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008010int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008011{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008012 char *cur_ptr, *cur_end;
8013 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008014 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008015 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008016
8017
Willy Tarreau3d300592007-03-18 18:34:41 +01008018 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008019 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008020 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008021 (exp->action == ACT_ALLOW ||
8022 exp->action == ACT_DENY))
8023 return 0;
8024 else if (exp->action == ACT_REMOVE)
8025 return 0;
8026
8027 done = 0;
8028
Willy Tarreau9b28e032012-10-12 23:49:43 +02008029 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008030 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008031
8032 /* Now we have the status line between cur_ptr and cur_end */
8033
Willy Tarreau15a53a42015-01-21 13:39:42 +01008034 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008035 switch (exp->action) {
8036 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008037 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008038 done = 1;
8039 break;
8040
8041 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008042 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008043 done = 1;
8044 break;
8045
8046 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008047 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8048 if (trash.len < 0)
8049 return -1;
8050
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008051 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008052 /* FIXME: if the user adds a newline in the replacement, the
8053 * index will not be recalculated for now, and the new line
8054 * will not be counted as a new header.
8055 */
8056
Willy Tarreaufa355d42009-11-29 18:12:29 +01008057 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008058 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008059 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008060 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008061 cur_ptr, cur_end + 1,
8062 NULL, NULL);
8063 if (unlikely(!cur_end))
8064 return -1;
8065
8066 /* we have a full respnse and we know that we have either a CR
8067 * or an LF at <ptr>.
8068 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008069 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008070 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008071 /* there is no point trying this regex on headers */
8072 return 1;
8073 }
8074 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008075 return done;
8076}
8077
8078
8079
8080/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008081 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008082 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8083 * unparsable response.
8084 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008085int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008086{
Willy Tarreau192252e2015-04-04 01:47:55 +02008087 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008088 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008089 struct hdr_exp *exp;
8090
8091 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008092 int ret;
8093
8094 /*
8095 * The interleaving of transformations and verdicts
8096 * makes it difficult to decide to continue or stop
8097 * the evaluation.
8098 */
8099
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008100 if (txn->flags & TX_SVDENY)
8101 break;
8102
Willy Tarreau3d300592007-03-18 18:34:41 +01008103 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008104 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8105 exp->action == ACT_PASS)) {
8106 exp = exp->next;
8107 continue;
8108 }
8109
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008110 /* if this filter had a condition, evaluate it now and skip to
8111 * next filter if the condition does not match.
8112 */
8113 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008114 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008115 ret = acl_pass(ret);
8116 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8117 ret = !ret;
8118 if (!ret)
8119 continue;
8120 }
8121
Willy Tarreaua15645d2007-03-18 16:22:39 +01008122 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008123 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008124 if (unlikely(ret < 0))
8125 return -1;
8126
8127 if (likely(ret == 0)) {
8128 /* The filter did not match the response, it can be
8129 * iterated through all headers.
8130 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008131 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8132 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008133 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008134 }
8135 return 0;
8136}
8137
8138
Willy Tarreaua15645d2007-03-18 16:22:39 +01008139/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008140 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008141 * desirable to call it only when needed. This function is also used when we
8142 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008143 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008144void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008145{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008146 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008147 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008148 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008149 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008150 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008151 char *hdr_beg, *hdr_end, *hdr_next;
8152 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008153
Willy Tarreaua15645d2007-03-18 16:22:39 +01008154 /* Iterate through the headers.
8155 * we start with the start line.
8156 */
8157 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008158 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008159
8160 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8161 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008162 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008163
8164 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008165 hdr_beg = hdr_next;
8166 hdr_end = hdr_beg + cur_hdr->len;
8167 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008168
Willy Tarreau24581ba2010-08-31 22:39:35 +02008169 /* We have one full header between hdr_beg and hdr_end, and the
8170 * next header starts at hdr_next. We're only interested in
8171 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008172 */
8173
Willy Tarreau24581ba2010-08-31 22:39:35 +02008174 is_cookie2 = 0;
8175 prev = hdr_beg + 10;
8176 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008177 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008178 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8179 if (!val) {
8180 old_idx = cur_idx;
8181 continue;
8182 }
8183 is_cookie2 = 1;
8184 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008185 }
8186
Willy Tarreau24581ba2010-08-31 22:39:35 +02008187 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8188 * <prev> points to the colon.
8189 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008190 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008191
Willy Tarreau24581ba2010-08-31 22:39:35 +02008192 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8193 * check-cache is enabled) and we are not interested in checking
8194 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008195 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02008196 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008197 return;
8198
Willy Tarreau24581ba2010-08-31 22:39:35 +02008199 /* OK so now we know we have to process this response cookie.
8200 * The format of the Set-Cookie header is slightly different
8201 * from the format of the Cookie header in that it does not
8202 * support the comma as a cookie delimiter (thus the header
8203 * cannot be folded) because the Expires attribute described in
8204 * the original Netscape's spec may contain an unquoted date
8205 * with a comma inside. We have to live with this because
8206 * many browsers don't support Max-Age and some browsers don't
8207 * support quoted strings. However the Set-Cookie2 header is
8208 * clean.
8209 *
8210 * We have to keep multiple pointers in order to support cookie
8211 * removal at the beginning, middle or end of header without
8212 * corrupting the header (in case of set-cookie2). A special
8213 * pointer, <scav> points to the beginning of the set-cookie-av
8214 * fields after the first semi-colon. The <next> pointer points
8215 * either to the end of line (set-cookie) or next unquoted comma
8216 * (set-cookie2). All of these headers are valid :
8217 *
8218 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8219 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8220 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8221 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8222 * | | | | | | | | | |
8223 * | | | | | | | | +-> next hdr_end <--+
8224 * | | | | | | | +------------> scav
8225 * | | | | | | +--------------> val_end
8226 * | | | | | +--------------------> val_beg
8227 * | | | | +----------------------> equal
8228 * | | | +------------------------> att_end
8229 * | | +----------------------------> att_beg
8230 * | +------------------------------> prev
8231 * +-----------------------------------------> hdr_beg
8232 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008233
Willy Tarreau24581ba2010-08-31 22:39:35 +02008234 for (; prev < hdr_end; prev = next) {
8235 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008236
Willy Tarreau24581ba2010-08-31 22:39:35 +02008237 /* find att_beg */
8238 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01008239 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008240 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008241
Willy Tarreau24581ba2010-08-31 22:39:35 +02008242 /* find att_end : this is the first character after the last non
8243 * space before the equal. It may be equal to hdr_end.
8244 */
8245 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008246
Willy Tarreau24581ba2010-08-31 22:39:35 +02008247 while (equal < hdr_end) {
8248 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8249 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01008250 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008251 continue;
8252 att_end = equal;
8253 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008254
Willy Tarreau24581ba2010-08-31 22:39:35 +02008255 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8256 * is between <att_beg> and <equal>, both may be identical.
8257 */
8258
8259 /* look for end of cookie if there is an equal sign */
8260 if (equal < hdr_end && *equal == '=') {
8261 /* look for the beginning of the value */
8262 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01008263 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008264 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008265
Willy Tarreau24581ba2010-08-31 22:39:35 +02008266 /* find the end of the value, respecting quotes */
8267 next = find_cookie_value_end(val_beg, hdr_end);
8268
8269 /* make val_end point to the first white space or delimitor after the value */
8270 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01008271 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008272 val_end--;
8273 } else {
8274 /* <equal> points to next comma, semi-colon or EOL */
8275 val_beg = val_end = next = equal;
8276 }
8277
8278 if (next < hdr_end) {
8279 /* Set-Cookie2 supports multiple cookies, and <next> points to
8280 * a colon or semi-colon before the end. So skip all attr-value
8281 * pairs and look for the next comma. For Set-Cookie, since
8282 * commas are permitted in values, skip to the end.
8283 */
8284 if (is_cookie2)
8285 next = find_hdr_value_end(next, hdr_end);
8286 else
8287 next = hdr_end;
8288 }
8289
8290 /* Now everything is as on the diagram above */
8291
8292 /* Ignore cookies with no equal sign */
8293 if (equal == val_end)
8294 continue;
8295
8296 /* If there are spaces around the equal sign, we need to
8297 * strip them otherwise we'll get trouble for cookie captures,
8298 * or even for rewrites. Since this happens extremely rarely,
8299 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008300 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008301 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8302 int stripped_before = 0;
8303 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008304
Willy Tarreau24581ba2010-08-31 22:39:35 +02008305 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008306 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008307 equal += stripped_before;
8308 val_beg += stripped_before;
8309 }
8310
8311 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008312 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008313 val_beg += stripped_after;
8314 stripped_before += stripped_after;
8315 }
8316
8317 val_end += stripped_before;
8318 next += stripped_before;
8319 hdr_end += stripped_before;
8320 hdr_next += stripped_before;
8321 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008322 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008323 }
8324
8325 /* First, let's see if we want to capture this cookie. We check
8326 * that we don't already have a server side cookie, because we
8327 * can only capture one. Also as an optimisation, we ignore
8328 * cookies shorter than the declared name.
8329 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008330 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008331 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008332 (val_end - att_beg >= sess->fe->capture_namelen) &&
8333 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008334 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008335 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008336 Alert("HTTP logging : out of memory.\n");
8337 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008338 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008339 if (log_len > sess->fe->capture_len)
8340 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008341 memcpy(txn->srv_cookie, att_beg, log_len);
8342 txn->srv_cookie[log_len] = 0;
8343 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008344 }
8345
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008346 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008347 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008348 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008349 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8350 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008351 /* assume passive cookie by default */
8352 txn->flags &= ~TX_SCK_MASK;
8353 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008354
8355 /* If the cookie is in insert mode on a known server, we'll delete
8356 * this occurrence because we'll insert another one later.
8357 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008358 * a direct access.
8359 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008360 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008361 /* The "preserve" flag was set, we don't want to touch the
8362 * server's cookie.
8363 */
8364 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008365 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008366 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008367 /* this cookie must be deleted */
8368 if (*prev == ':' && next == hdr_end) {
8369 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008370 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008371 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8372 txn->hdr_idx.used--;
8373 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008374 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008375 hdr_next += delta;
8376 http_msg_move_end(&txn->rsp, delta);
8377 /* note: while both invalid now, <next> and <hdr_end>
8378 * are still equal, so the for() will stop as expected.
8379 */
8380 } else {
8381 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008382 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008383 next = prev;
8384 hdr_end += delta;
8385 hdr_next += delta;
8386 cur_hdr->len += delta;
8387 http_msg_move_end(&txn->rsp, delta);
8388 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008389 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008390 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008391 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008392 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008393 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008394 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008395 * with this server since we know it.
8396 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008397 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008398 next += delta;
8399 hdr_end += delta;
8400 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008401 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008402 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008403
Willy Tarreauf1348312010-10-07 15:54:11 +02008404 txn->flags &= ~TX_SCK_MASK;
8405 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008406 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008407 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008408 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008409 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008410 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008411 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008412 next += delta;
8413 hdr_end += delta;
8414 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008415 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008416 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008417
Willy Tarreau827aee92011-03-10 16:55:02 +01008418 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008419 txn->flags &= ~TX_SCK_MASK;
8420 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008421 }
8422 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008423 /* that's done for this cookie, check the next one on the same
8424 * line when next != hdr_end (only if is_cookie2).
8425 */
8426 }
8427 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008428 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008429 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008430}
8431
8432
Willy Tarreaua15645d2007-03-18 16:22:39 +01008433/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008434 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008435 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008436void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008437{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008438 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008439 char *p1, *p2;
8440
8441 char *cur_ptr, *cur_end, *cur_next;
8442 int cur_idx;
8443
Willy Tarreau5df51872007-11-25 16:20:08 +01008444 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008445 return;
8446
8447 /* Iterate through the headers.
8448 * we start with the start line.
8449 */
8450 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008451 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008452
8453 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8454 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008455 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008456
8457 cur_hdr = &txn->hdr_idx.v[cur_idx];
8458 cur_ptr = cur_next;
8459 cur_end = cur_ptr + cur_hdr->len;
8460 cur_next = cur_end + cur_hdr->cr + 1;
8461
8462 /* We have one full header between cur_ptr and cur_end, and the
8463 * next header starts at cur_next. We're only interested in
8464 * "Cookie:" headers.
8465 */
8466
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008467 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8468 if (val) {
8469 if ((cur_end - (cur_ptr + val) >= 8) &&
8470 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8471 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8472 return;
8473 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008474 }
8475
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008476 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8477 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008478 continue;
8479
8480 /* OK, right now we know we have a cache-control header at cur_ptr */
8481
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008482 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008483
8484 if (p1 >= cur_end) /* no more info */
8485 continue;
8486
8487 /* p1 is at the beginning of the value */
8488 p2 = p1;
8489
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008490 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008491 p2++;
8492
8493 /* we have a complete value between p1 and p2 */
8494 if (p2 < cur_end && *p2 == '=') {
8495 /* we have something of the form no-cache="set-cookie" */
8496 if ((cur_end - p1 >= 21) &&
8497 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8498 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008499 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008500 continue;
8501 }
8502
8503 /* OK, so we know that either p2 points to the end of string or to a comma */
8504 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008505 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008506 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8507 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8508 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008509 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008510 return;
8511 }
8512
8513 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008514 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008515 continue;
8516 }
8517 }
8518}
8519
Willy Tarreau58f10d72006-12-04 02:26:12 +01008520
Willy Tarreaub2513902006-12-17 14:52:38 +01008521/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008522 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008523 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008524 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008525 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008526 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008527 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008528 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008529 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008530int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008531{
8532 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008533 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008534 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008535
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008536 if (!uri_auth)
8537 return 0;
8538
Cyril Bonté70be45d2010-10-12 00:14:35 +02008539 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008540 return 0;
8541
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008542 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008543 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008544 return 0;
8545
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008546 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008547 return 0;
8548
Willy Tarreaub2513902006-12-17 14:52:38 +01008549 return 1;
8550}
8551
Willy Tarreau4076a152009-04-02 15:18:36 +02008552/*
8553 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008554 * By default it tries to report the error position as msg->err_pos. However if
8555 * this one is not set, it will then report msg->next, which is the last known
8556 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008557 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008558 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008559void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008560 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008561 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008562{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008563 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008564 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008565 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008566
Willy Tarreauf3764b72016-03-31 13:45:10 +02008567 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02008568 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008569 len1 = MIN(len1, es->len);
8570 len2 = es->len - len1; /* remaining data if buffer wraps */
8571
Willy Tarreauf3764b72016-03-31 13:45:10 +02008572 if (!es->buf)
8573 es->buf = malloc(global.tune.bufsize);
8574
8575 if (es->buf) {
8576 memcpy(es->buf, chn->buf->p, len1);
8577 if (len2)
8578 memcpy(es->buf + len1, chn->buf->data, len2);
8579 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008580
Willy Tarreau4076a152009-04-02 15:18:36 +02008581 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008582 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008583 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008584 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008585
Willy Tarreau4076a152009-04-02 15:18:36 +02008586 es->when = date; // user-visible date
8587 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008588 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008589 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008590 if (objt_conn(sess->origin))
8591 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008592 else
8593 memset(&es->src, 0, sizeof(es->src));
8594
Willy Tarreau078272e2010-12-12 12:46:33 +01008595 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008596 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008597 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008598 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008599 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008600 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008601 es->b_out = chn->buf->o;
8602 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008603 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008604 es->m_clen = msg->chunk_len;
8605 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008606}
Willy Tarreaub2513902006-12-17 14:52:38 +01008607
Willy Tarreau294c4732011-12-16 21:35:50 +01008608/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8609 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8610 * performed over the whole headers. Otherwise it must contain a valid header
8611 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8612 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8613 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8614 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008615 * -1. The value fetch stops at commas, so this function is suited for use with
8616 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008617 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008618 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008619unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008620 struct hdr_idx *idx, int occ,
8621 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008622{
Willy Tarreau294c4732011-12-16 21:35:50 +01008623 struct hdr_ctx local_ctx;
8624 char *ptr_hist[MAX_HDR_HISTORY];
8625 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008626 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008627 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008628
Willy Tarreau294c4732011-12-16 21:35:50 +01008629 if (!ctx) {
8630 local_ctx.idx = 0;
8631 ctx = &local_ctx;
8632 }
8633
Willy Tarreaubce70882009-09-07 11:51:47 +02008634 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008635 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008636 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008637 occ--;
8638 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008639 *vptr = ctx->line + ctx->val;
8640 *vlen = ctx->vlen;
8641 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008642 }
8643 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008644 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008645 }
8646
8647 /* negative occurrence, we scan all the list then walk back */
8648 if (-occ > MAX_HDR_HISTORY)
8649 return 0;
8650
Willy Tarreau294c4732011-12-16 21:35:50 +01008651 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008652 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008653 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8654 len_hist[hist_ptr] = ctx->vlen;
8655 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008656 hist_ptr = 0;
8657 found++;
8658 }
8659 if (-occ > found)
8660 return 0;
8661 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008662 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8663 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8664 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008665 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008666 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008667 if (hist_ptr >= MAX_HDR_HISTORY)
8668 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008669 *vptr = ptr_hist[hist_ptr];
8670 *vlen = len_hist[hist_ptr];
8671 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008672}
8673
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008674/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8675 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8676 * performed over the whole headers. Otherwise it must contain a valid header
8677 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8678 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8679 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8680 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8681 * -1. This function differs from http_get_hdr() in that it only returns full
8682 * line header values and does not stop at commas.
8683 * The return value is 0 if nothing was found, or non-zero otherwise.
8684 */
8685unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8686 struct hdr_idx *idx, int occ,
8687 struct hdr_ctx *ctx, char **vptr, int *vlen)
8688{
8689 struct hdr_ctx local_ctx;
8690 char *ptr_hist[MAX_HDR_HISTORY];
8691 int len_hist[MAX_HDR_HISTORY];
8692 unsigned int hist_ptr;
8693 int found;
8694
8695 if (!ctx) {
8696 local_ctx.idx = 0;
8697 ctx = &local_ctx;
8698 }
8699
8700 if (occ >= 0) {
8701 /* search from the beginning */
8702 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8703 occ--;
8704 if (occ <= 0) {
8705 *vptr = ctx->line + ctx->val;
8706 *vlen = ctx->vlen;
8707 return 1;
8708 }
8709 }
8710 return 0;
8711 }
8712
8713 /* negative occurrence, we scan all the list then walk back */
8714 if (-occ > MAX_HDR_HISTORY)
8715 return 0;
8716
8717 found = hist_ptr = 0;
8718 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8719 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8720 len_hist[hist_ptr] = ctx->vlen;
8721 if (++hist_ptr >= MAX_HDR_HISTORY)
8722 hist_ptr = 0;
8723 found++;
8724 }
8725 if (-occ > found)
8726 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008727
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008728 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008729 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8730 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8731 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008732 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008733 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008734 if (hist_ptr >= MAX_HDR_HISTORY)
8735 hist_ptr -= MAX_HDR_HISTORY;
8736 *vptr = ptr_hist[hist_ptr];
8737 *vlen = len_hist[hist_ptr];
8738 return 1;
8739}
8740
Willy Tarreaubaaee002006-06-26 02:48:02 +02008741/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008742 * Print a debug line with a header. Always stop at the first CR or LF char,
8743 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8744 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008745 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008746void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008747{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008748 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008749 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008750
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008751 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008752 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008753 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008754 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 +02008755
8756 for (max = 0; start + max < end; max++)
8757 if (start[max] == '\r' || start[max] == '\n')
8758 break;
8759
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008760 UBOUND(max, trash.size - trash.len - 3);
8761 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8762 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008763 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008764}
8765
Willy Tarreaueee5b512015-04-03 23:46:31 +02008766
8767/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8768 * The hdr_idx is allocated as well. In case of allocation failure, everything
8769 * allocated is freed and NULL is returned. Otherwise the new transaction is
8770 * assigned to the stream and returned.
8771 */
8772struct http_txn *http_alloc_txn(struct stream *s)
8773{
8774 struct http_txn *txn = s->txn;
8775
8776 if (txn)
8777 return txn;
8778
8779 txn = pool_alloc2(pool2_http_txn);
8780 if (!txn)
8781 return txn;
8782
8783 txn->hdr_idx.size = global.tune.max_http_hdr;
8784 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8785 if (!txn->hdr_idx.v) {
8786 pool_free2(pool2_http_txn, txn);
8787 return NULL;
8788 }
8789
8790 s->txn = txn;
8791 return txn;
8792}
8793
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008794void http_txn_reset_req(struct http_txn *txn)
8795{
8796 txn->req.flags = 0;
8797 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8798 txn->req.next = 0;
8799 txn->req.chunk_len = 0LL;
8800 txn->req.body_len = 0LL;
8801 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8802}
8803
8804void http_txn_reset_res(struct http_txn *txn)
8805{
8806 txn->rsp.flags = 0;
8807 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8808 txn->rsp.next = 0;
8809 txn->rsp.chunk_len = 0LL;
8810 txn->rsp.body_len = 0LL;
8811 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8812}
8813
Willy Tarreau0937bc42009-12-22 15:03:09 +01008814/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008815 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008816 * the required fields are properly allocated and that we only need to (re)init
8817 * them. This should be used before processing any new request.
8818 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008819void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008820{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008821 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008822 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008823
8824 txn->flags = 0;
8825 txn->status = -1;
8826
Willy Tarreauf64d1412010-10-07 20:06:11 +02008827 txn->cookie_first_date = 0;
8828 txn->cookie_last_date = 0;
8829
Willy Tarreaueee5b512015-04-03 23:46:31 +02008830 txn->srv_cookie = NULL;
8831 txn->cli_cookie = NULL;
8832 txn->uri = NULL;
8833
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008834 http_txn_reset_req(txn);
8835 http_txn_reset_res(txn);
8836
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008837 txn->req.chn = &s->req;
8838 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008839
8840 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008841
8842 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8843 if (fe->options2 & PR_O2_REQBUG_OK)
8844 txn->req.err_pos = -1; /* let buggy requests pass */
8845
Willy Tarreau0937bc42009-12-22 15:03:09 +01008846 if (txn->hdr_idx.v)
8847 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008848
8849 vars_init(&s->vars_txn, SCOPE_TXN);
8850 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008851}
8852
8853/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008854void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008855{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008856 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008857 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008858
8859 /* these ones will have been dynamically allocated */
8860 pool_free2(pool2_requri, txn->uri);
8861 pool_free2(pool2_capture, txn->cli_cookie);
8862 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008863 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008864
William Lallemanda73203e2012-03-12 12:48:57 +01008865 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008866 txn->uri = NULL;
8867 txn->srv_cookie = NULL;
8868 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008869
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008870 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008871 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008872 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008873 pool_free2(h->pool, s->req_cap[h->index]);
8874 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008875 }
8876
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008877 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008878 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008879 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008880 pool_free2(h->pool, s->res_cap[h->index]);
8881 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008882 }
8883
Willy Tarreau6204cd92016-03-10 16:33:04 +01008884 vars_prune(&s->vars_txn, s->sess, s);
8885 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008886}
8887
8888/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008889void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008890{
8891 http_end_txn(s);
8892 http_init_txn(s);
8893
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008894 /* reinitialise the current rule list pointer to NULL. We are sure that
8895 * any rulelist match the NULL pointer.
8896 */
8897 s->current_rule_list = NULL;
8898
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008899 s->be = strm_fe(s);
8900 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008901 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008902 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008903 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008904 /* re-init store persistence */
8905 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008906 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008907
Willy Tarreau0937bc42009-12-22 15:03:09 +01008908 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008909
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008910 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008911
Willy Tarreau739cfba2010-01-25 23:11:14 +01008912 /* We must trim any excess data from the response buffer, because we
8913 * may have blocked an invalid response from a server that we don't
8914 * want to accidentely forward once we disable the analysers, nor do
8915 * we want those data to come along with next response. A typical
8916 * example of such data would be from a buggy server responding to
8917 * a HEAD with some data, or sending more than the advertised
8918 * content-length.
8919 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008920 if (unlikely(s->res.buf->i))
8921 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008922
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008923 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008924 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008925
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008926 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008927 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008928
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008929 s->req.rex = TICK_ETERNITY;
8930 s->req.wex = TICK_ETERNITY;
8931 s->req.analyse_exp = TICK_ETERNITY;
8932 s->res.rex = TICK_ETERNITY;
8933 s->res.wex = TICK_ETERNITY;
8934 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008935}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008936
Sasha Pachev218f0642014-06-16 12:05:59 -06008937void free_http_res_rules(struct list *r)
8938{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008939 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008940
8941 list_for_each_entry_safe(pr, tr, r, list) {
8942 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008943 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008944 free(pr);
8945 }
8946}
8947
8948void free_http_req_rules(struct list *r)
8949{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008950 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01008951
8952 list_for_each_entry_safe(pr, tr, r, list) {
8953 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008954 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008955 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008956
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008957 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008958 free(pr);
8959 }
8960}
8961
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008962/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008963struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01008964{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008965 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008966 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008967 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008968 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008969
Vincent Bernat02779b62016-04-03 13:48:43 +02008970 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01008971 if (!rule) {
8972 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008973 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008974 }
8975
CJ Ess108b1dd2015-04-07 12:03:37 -04008976 rule->deny_status = HTTP_ERR_403;
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008977 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008978 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01008979 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02008980 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04008981 int code;
8982 int hc;
8983
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008984 rule->action = ACT_ACTION_DENY;
Willy Tarreauff011f22011-01-06 17:51:27 +01008985 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04008986 if (strcmp(args[cur_arg], "deny_status") == 0) {
8987 cur_arg++;
8988 if (!args[cur_arg]) {
8989 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
8990 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
8991 goto out_err;
8992 }
8993
8994 code = atol(args[cur_arg]);
8995 cur_arg++;
8996 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
8997 if (http_err_codes[hc] == code) {
8998 rule->deny_status = hc;
8999 break;
9000 }
9001 }
9002
9003 if (hc >= HTTP_ERR_SIZE) {
9004 Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n",
9005 file, linenum, code);
9006 }
9007 }
Willy Tarreauccbcc372012-12-27 12:37:57 +01009008 } else if (!strcmp(args[0], "tarpit")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009009 rule->action = ACT_HTTP_REQ_TARPIT;
Willy Tarreauccbcc372012-12-27 12:37:57 +01009010 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01009011 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009012 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009013 cur_arg = 1;
9014
9015 while(*args[cur_arg]) {
9016 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009017 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009018 cur_arg+=2;
9019 continue;
9020 } else
9021 break;
9022 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009023 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009024 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009025 cur_arg = 1;
9026
9027 if (!*args[cur_arg] ||
9028 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9029 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9030 file, linenum, args[0]);
9031 goto out_err;
9032 }
9033 rule->arg.nice = atoi(args[cur_arg]);
9034 if (rule->arg.nice < -1024)
9035 rule->arg.nice = -1024;
9036 else if (rule->arg.nice > 1024)
9037 rule->arg.nice = 1024;
9038 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009039 } else if (!strcmp(args[0], "set-tos")) {
9040#ifdef IP_TOS
9041 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009042 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009043 cur_arg = 1;
9044
9045 if (!*args[cur_arg] ||
9046 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9047 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9048 file, linenum, args[0]);
9049 goto out_err;
9050 }
9051
9052 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9053 if (err && *err != '\0') {
9054 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9055 file, linenum, err, args[0]);
9056 goto out_err;
9057 }
9058 cur_arg++;
9059#else
9060 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9061 goto out_err;
9062#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009063 } else if (!strcmp(args[0], "set-mark")) {
9064#ifdef SO_MARK
9065 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009066 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009067 cur_arg = 1;
9068
9069 if (!*args[cur_arg] ||
9070 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9071 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9072 file, linenum, args[0]);
9073 goto out_err;
9074 }
9075
9076 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9077 if (err && *err != '\0') {
9078 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9079 file, linenum, err, args[0]);
9080 goto out_err;
9081 }
9082 cur_arg++;
9083 global.last_checks |= LSTCHK_NETADM;
9084#else
9085 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9086 goto out_err;
9087#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009088 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009089 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009090 cur_arg = 1;
9091
9092 if (!*args[cur_arg] ||
9093 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9094 bad_log_level:
9095 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9096 file, linenum, args[0]);
9097 goto out_err;
9098 }
9099 if (strcmp(args[cur_arg], "silent") == 0)
9100 rule->arg.loglevel = -1;
9101 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9102 goto bad_log_level;
9103 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009104 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009105 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009106 cur_arg = 1;
9107
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009108 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9109 (*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 +01009110 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9111 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009112 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009113 }
9114
9115 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9116 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9117 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009118
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009119 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009120 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009121 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9122 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009123 free(proxy->conf.lfs_file);
9124 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9125 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009126 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009127 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009128 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009129 cur_arg = 1;
9130
9131 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009132 (*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 -06009133 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9134 file, linenum, args[0]);
9135 goto out_err;
9136 }
9137
9138 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9139 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9140 LIST_INIT(&rule->arg.hdr_add.fmt);
9141
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009142 error = NULL;
9143 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9144 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9145 args[cur_arg + 1], error);
9146 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009147 goto out_err;
9148 }
9149
9150 proxy->conf.args.ctx = ARGC_HRQ;
9151 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9152 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9153 file, linenum);
9154
9155 free(proxy->conf.lfs_file);
9156 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9157 proxy->conf.lfs_line = proxy->conf.args.line;
9158 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009159 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009160 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009161 cur_arg = 1;
9162
9163 if (!*args[cur_arg] ||
9164 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9165 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9166 file, linenum, args[0]);
9167 goto out_err;
9168 }
9169
9170 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9171 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9172
9173 proxy->conf.args.ctx = ARGC_HRQ;
9174 free(proxy->conf.lfs_file);
9175 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9176 proxy->conf.lfs_line = proxy->conf.args.line;
9177 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009178 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9179 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009180 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009181 struct sample_expr *expr;
9182 unsigned int where;
9183 char *err = NULL;
9184
9185 cur_arg = 1;
9186 proxy->conf.args.ctx = ARGC_TRK;
9187
9188 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9189 if (!expr) {
9190 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], err);
9192 free(err);
9193 goto out_err;
9194 }
9195
9196 where = 0;
9197 if (proxy->cap & PR_CAP_FE)
9198 where |= SMP_VAL_FE_HRQ_HDR;
9199 if (proxy->cap & PR_CAP_BE)
9200 where |= SMP_VAL_BE_HRQ_HDR;
9201
9202 if (!(expr->fetch->val & where)) {
9203 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9204 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9205 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9206 args[cur_arg-1], sample_src_names(expr->fetch->use));
9207 free(expr);
9208 goto out_err;
9209 }
9210
9211 if (strcmp(args[cur_arg], "table") == 0) {
9212 cur_arg++;
9213 if (!args[cur_arg]) {
9214 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9215 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9216 free(expr);
9217 goto out_err;
9218 }
9219 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009220 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02009221 cur_arg++;
9222 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009223 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009224 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009225 } else if (strcmp(args[0], "redirect") == 0) {
9226 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009227 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009228
Willy Tarreaube4653b2015-05-28 15:26:58 +02009229 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009230 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9231 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9232 goto out_err;
9233 }
9234
9235 /* this redirect rule might already contain a parsed condition which
9236 * we'll pass to the http-request rule.
9237 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009238 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009239 rule->arg.redir = redir;
9240 rule->cond = redir->cond;
9241 redir->cond = NULL;
9242 cur_arg = 2;
9243 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009244 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9245 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009246 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009247 /*
9248 * '+ 8' for 'add-acl('
9249 * '- 9' for 'add-acl(' + trailing ')'
9250 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009251 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009252
9253 cur_arg = 1;
9254
9255 if (!*args[cur_arg] ||
9256 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9257 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9258 file, linenum, args[0]);
9259 goto out_err;
9260 }
9261
9262 LIST_INIT(&rule->arg.map.key);
9263 proxy->conf.args.ctx = ARGC_HRQ;
9264 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9265 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9266 file, linenum);
9267 free(proxy->conf.lfs_file);
9268 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9269 proxy->conf.lfs_line = proxy->conf.args.line;
9270 cur_arg += 1;
9271 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9272 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009273 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009274 /*
9275 * '+ 8' for 'del-acl('
9276 * '- 9' for 'del-acl(' + trailing ')'
9277 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009278 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009279
9280 cur_arg = 1;
9281
9282 if (!*args[cur_arg] ||
9283 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9284 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9285 file, linenum, args[0]);
9286 goto out_err;
9287 }
9288
9289 LIST_INIT(&rule->arg.map.key);
9290 proxy->conf.args.ctx = ARGC_HRQ;
9291 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9292 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9293 file, linenum);
9294 free(proxy->conf.lfs_file);
9295 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9296 proxy->conf.lfs_line = proxy->conf.args.line;
9297 cur_arg += 1;
9298 } else if (strncmp(args[0], "del-map", 7) == 0) {
9299 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009300 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009301 /*
9302 * '+ 8' for 'del-map('
9303 * '- 9' for 'del-map(' + trailing ')'
9304 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009305 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009306
9307 cur_arg = 1;
9308
9309 if (!*args[cur_arg] ||
9310 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9311 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9312 file, linenum, args[0]);
9313 goto out_err;
9314 }
9315
9316 LIST_INIT(&rule->arg.map.key);
9317 proxy->conf.args.ctx = ARGC_HRQ;
9318 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9319 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9320 file, linenum);
9321 free(proxy->conf.lfs_file);
9322 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9323 proxy->conf.lfs_line = proxy->conf.args.line;
9324 cur_arg += 1;
9325 } else if (strncmp(args[0], "set-map", 7) == 0) {
9326 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009327 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009328 /*
9329 * '+ 8' for 'set-map('
9330 * '- 9' for 'set-map(' + trailing ')'
9331 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009332 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009333
9334 cur_arg = 1;
9335
9336 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9337 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9338 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9339 file, linenum, args[0]);
9340 goto out_err;
9341 }
9342
9343 LIST_INIT(&rule->arg.map.key);
9344 LIST_INIT(&rule->arg.map.value);
9345 proxy->conf.args.ctx = ARGC_HRQ;
9346
9347 /* key pattern */
9348 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9349 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9350 file, linenum);
9351
9352 /* value pattern */
9353 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9354 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9355 file, linenum);
9356 free(proxy->conf.lfs_file);
9357 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9358 proxy->conf.lfs_line = proxy->conf.args.line;
9359
9360 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009361 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9362 char *errmsg = NULL;
9363 cur_arg = 1;
9364 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009365 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009366 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009367 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009368 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9369 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9370 free(errmsg);
9371 goto out_err;
9372 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009373 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009374 action_build_list(&http_req_keywords.list, &trash);
9375 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
9376 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009377 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009378 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009379 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009380 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009381 }
9382
9383 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9384 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009385 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009386
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009387 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9388 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9389 file, linenum, args[0], errmsg);
9390 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009391 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009392 }
9393 rule->cond = cond;
9394 }
9395 else if (*args[cur_arg]) {
9396 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9397 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9398 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009399 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009400 }
9401
9402 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009403 out_err:
9404 free(rule);
9405 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009406}
9407
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009408/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009409struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009410{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009411 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009412 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009413 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009414 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009415
9416 rule = calloc(1, sizeof(*rule));
9417 if (!rule) {
9418 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9419 goto out_err;
9420 }
9421
9422 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009423 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009424 cur_arg = 1;
9425 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009426 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009427 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009428 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009429 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009430 cur_arg = 1;
9431
9432 if (!*args[cur_arg] ||
9433 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9434 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9435 file, linenum, args[0]);
9436 goto out_err;
9437 }
9438 rule->arg.nice = atoi(args[cur_arg]);
9439 if (rule->arg.nice < -1024)
9440 rule->arg.nice = -1024;
9441 else if (rule->arg.nice > 1024)
9442 rule->arg.nice = 1024;
9443 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009444 } else if (!strcmp(args[0], "set-tos")) {
9445#ifdef IP_TOS
9446 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009447 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009448 cur_arg = 1;
9449
9450 if (!*args[cur_arg] ||
9451 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9452 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9453 file, linenum, args[0]);
9454 goto out_err;
9455 }
9456
9457 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9458 if (err && *err != '\0') {
9459 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9460 file, linenum, err, args[0]);
9461 goto out_err;
9462 }
9463 cur_arg++;
9464#else
9465 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9466 goto out_err;
9467#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009468 } else if (!strcmp(args[0], "set-mark")) {
9469#ifdef SO_MARK
9470 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009471 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009472 cur_arg = 1;
9473
9474 if (!*args[cur_arg] ||
9475 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9476 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9477 file, linenum, args[0]);
9478 goto out_err;
9479 }
9480
9481 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9482 if (err && *err != '\0') {
9483 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9484 file, linenum, err, args[0]);
9485 goto out_err;
9486 }
9487 cur_arg++;
9488 global.last_checks |= LSTCHK_NETADM;
9489#else
9490 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9491 goto out_err;
9492#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009493 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009494 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009495 cur_arg = 1;
9496
9497 if (!*args[cur_arg] ||
9498 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9499 bad_log_level:
9500 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9501 file, linenum, args[0]);
9502 goto out_err;
9503 }
9504 if (strcmp(args[cur_arg], "silent") == 0)
9505 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08009506 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009507 goto bad_log_level;
9508 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009509 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009510 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009511 cur_arg = 1;
9512
9513 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9514 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9515 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9516 file, linenum, args[0]);
9517 goto out_err;
9518 }
9519
9520 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9521 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9522 LIST_INIT(&rule->arg.hdr_add.fmt);
9523
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009524 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009525 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009526 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9527 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009528 free(proxy->conf.lfs_file);
9529 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9530 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009531 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009532 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009533 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009534 cur_arg = 1;
9535
9536 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009537 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9538 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009539 file, linenum, args[0]);
9540 goto out_err;
9541 }
9542
9543 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9544 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9545 LIST_INIT(&rule->arg.hdr_add.fmt);
9546
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009547 error = NULL;
9548 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9549 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9550 args[cur_arg + 1], error);
9551 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009552 goto out_err;
9553 }
9554
9555 proxy->conf.args.ctx = ARGC_HRQ;
9556 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9557 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9558 file, linenum);
9559
9560 free(proxy->conf.lfs_file);
9561 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9562 proxy->conf.lfs_line = proxy->conf.args.line;
9563 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009564 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009565 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009566 cur_arg = 1;
9567
9568 if (!*args[cur_arg] ||
9569 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9570 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9571 file, linenum, args[0]);
9572 goto out_err;
9573 }
9574
9575 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9576 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9577
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009578 proxy->conf.args.ctx = ARGC_HRS;
9579 free(proxy->conf.lfs_file);
9580 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9581 proxy->conf.lfs_line = proxy->conf.args.line;
9582 cur_arg += 1;
9583 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9584 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009585 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009586 /*
9587 * '+ 8' for 'add-acl('
9588 * '- 9' for 'add-acl(' + trailing ')'
9589 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009590 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009591
9592 cur_arg = 1;
9593
9594 if (!*args[cur_arg] ||
9595 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9596 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9597 file, linenum, args[0]);
9598 goto out_err;
9599 }
9600
9601 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009602 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009603 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9604 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9605 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009606 free(proxy->conf.lfs_file);
9607 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9608 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009609
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009610 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009611 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9612 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009613 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009614 /*
9615 * '+ 8' for 'del-acl('
9616 * '- 9' for 'del-acl(' + trailing ')'
9617 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009618 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009619
9620 cur_arg = 1;
9621
9622 if (!*args[cur_arg] ||
9623 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9624 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9625 file, linenum, args[0]);
9626 goto out_err;
9627 }
9628
9629 LIST_INIT(&rule->arg.map.key);
9630 proxy->conf.args.ctx = ARGC_HRS;
9631 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9632 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9633 file, linenum);
9634 free(proxy->conf.lfs_file);
9635 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9636 proxy->conf.lfs_line = proxy->conf.args.line;
9637 cur_arg += 1;
9638 } else if (strncmp(args[0], "del-map", 7) == 0) {
9639 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009640 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009641 /*
9642 * '+ 8' for 'del-map('
9643 * '- 9' for 'del-map(' + trailing ')'
9644 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009645 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009646
9647 cur_arg = 1;
9648
9649 if (!*args[cur_arg] ||
9650 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9651 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9652 file, linenum, args[0]);
9653 goto out_err;
9654 }
9655
9656 LIST_INIT(&rule->arg.map.key);
9657 proxy->conf.args.ctx = ARGC_HRS;
9658 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9659 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9660 file, linenum);
9661 free(proxy->conf.lfs_file);
9662 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9663 proxy->conf.lfs_line = proxy->conf.args.line;
9664 cur_arg += 1;
9665 } else if (strncmp(args[0], "set-map", 7) == 0) {
9666 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009667 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009668 /*
9669 * '+ 8' for 'set-map('
9670 * '- 9' for 'set-map(' + trailing ')'
9671 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009672 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009673
9674 cur_arg = 1;
9675
9676 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9677 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9678 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9679 file, linenum, args[0]);
9680 goto out_err;
9681 }
9682
9683 LIST_INIT(&rule->arg.map.key);
9684 LIST_INIT(&rule->arg.map.value);
9685
9686 proxy->conf.args.ctx = ARGC_HRS;
9687
9688 /* key pattern */
9689 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9690 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9691 file, linenum);
9692
9693 /* value pattern */
9694 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9695 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9696 file, linenum);
9697
9698 free(proxy->conf.lfs_file);
9699 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9700 proxy->conf.lfs_line = proxy->conf.args.line;
9701
9702 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009703 } else if (strcmp(args[0], "redirect") == 0) {
9704 struct redirect_rule *redir;
9705 char *errmsg = NULL;
9706
9707 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9708 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9709 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9710 goto out_err;
9711 }
9712
9713 /* this redirect rule might already contain a parsed condition which
9714 * we'll pass to the http-request rule.
9715 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009716 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009717 rule->arg.redir = redir;
9718 rule->cond = redir->cond;
9719 redir->cond = NULL;
9720 cur_arg = 2;
9721 return rule;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009722 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9723 args[0][9] == '\0' && args[0][8] >= '0' &&
9724 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
9725 struct sample_expr *expr;
9726 unsigned int where;
9727 char *err = NULL;
9728
9729 cur_arg = 1;
9730 proxy->conf.args.ctx = ARGC_TRK;
9731
9732 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9733 if (!expr) {
9734 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9735 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9736 free(err);
9737 goto out_err;
9738 }
9739
9740 where = 0;
9741 if (proxy->cap & PR_CAP_FE)
9742 where |= SMP_VAL_FE_HRS_HDR;
9743 if (proxy->cap & PR_CAP_BE)
9744 where |= SMP_VAL_BE_HRS_HDR;
9745
9746 if (!(expr->fetch->val & where)) {
9747 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
9748 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9749 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9750 args[cur_arg-1], sample_src_names(expr->fetch->use));
9751 free(expr);
9752 goto out_err;
9753 }
9754
9755 if (strcmp(args[cur_arg], "table") == 0) {
9756 cur_arg++;
9757 if (!args[cur_arg]) {
9758 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9759 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9760 free(expr);
9761 goto out_err;
9762 }
9763 /* we copy the table name for now, it will be resolved later */
9764 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9765 cur_arg++;
9766 }
9767 rule->arg.trk_ctr.expr = expr;
9768 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
William Lallemand73025dd2014-04-24 14:38:37 +02009769 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9770 char *errmsg = NULL;
9771 cur_arg = 1;
9772 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009773 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009774 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009775 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009776 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9777 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9778 free(errmsg);
9779 goto out_err;
9780 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009781 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009782 action_build_list(&http_res_keywords.list, &trash);
9783 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9784 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009785 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009786 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009787 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009788 goto out_err;
9789 }
9790
9791 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9792 struct acl_cond *cond;
9793 char *errmsg = NULL;
9794
9795 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9796 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9797 file, linenum, args[0], errmsg);
9798 free(errmsg);
9799 goto out_err;
9800 }
9801 rule->cond = cond;
9802 }
9803 else if (*args[cur_arg]) {
9804 Alert("parsing [%s:%d]: 'http-response %s' expects"
9805 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9806 file, linenum, args[0], args[cur_arg]);
9807 goto out_err;
9808 }
9809
9810 return rule;
9811 out_err:
9812 free(rule);
9813 return NULL;
9814}
9815
Willy Tarreau4baae242012-12-27 12:00:31 +01009816/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009817 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009818 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9819 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009820 */
9821struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009822 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009823{
9824 struct redirect_rule *rule;
9825 int cur_arg;
9826 int type = REDIRECT_TYPE_NONE;
9827 int code = 302;
9828 const char *destination = NULL;
9829 const char *cookie = NULL;
9830 int cookie_set = 0;
9831 unsigned int flags = REDIRECT_FLAG_NONE;
9832 struct acl_cond *cond = NULL;
9833
9834 cur_arg = 0;
9835 while (*(args[cur_arg])) {
9836 if (strcmp(args[cur_arg], "location") == 0) {
9837 if (!*args[cur_arg + 1])
9838 goto missing_arg;
9839
9840 type = REDIRECT_TYPE_LOCATION;
9841 cur_arg++;
9842 destination = args[cur_arg];
9843 }
9844 else if (strcmp(args[cur_arg], "prefix") == 0) {
9845 if (!*args[cur_arg + 1])
9846 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009847 type = REDIRECT_TYPE_PREFIX;
9848 cur_arg++;
9849 destination = args[cur_arg];
9850 }
9851 else if (strcmp(args[cur_arg], "scheme") == 0) {
9852 if (!*args[cur_arg + 1])
9853 goto missing_arg;
9854
9855 type = REDIRECT_TYPE_SCHEME;
9856 cur_arg++;
9857 destination = args[cur_arg];
9858 }
9859 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9860 if (!*args[cur_arg + 1])
9861 goto missing_arg;
9862
9863 cur_arg++;
9864 cookie = args[cur_arg];
9865 cookie_set = 1;
9866 }
9867 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9868 if (!*args[cur_arg + 1])
9869 goto missing_arg;
9870
9871 cur_arg++;
9872 cookie = args[cur_arg];
9873 cookie_set = 0;
9874 }
9875 else if (strcmp(args[cur_arg], "code") == 0) {
9876 if (!*args[cur_arg + 1])
9877 goto missing_arg;
9878
9879 cur_arg++;
9880 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009881 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009882 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009883 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009884 args[cur_arg - 1], args[cur_arg]);
9885 return NULL;
9886 }
9887 }
9888 else if (!strcmp(args[cur_arg],"drop-query")) {
9889 flags |= REDIRECT_FLAG_DROP_QS;
9890 }
9891 else if (!strcmp(args[cur_arg],"append-slash")) {
9892 flags |= REDIRECT_FLAG_APPEND_SLASH;
9893 }
9894 else if (strcmp(args[cur_arg], "if") == 0 ||
9895 strcmp(args[cur_arg], "unless") == 0) {
9896 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9897 if (!cond) {
9898 memprintf(errmsg, "error in condition: %s", *errmsg);
9899 return NULL;
9900 }
9901 break;
9902 }
9903 else {
9904 memprintf(errmsg,
9905 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9906 args[cur_arg]);
9907 return NULL;
9908 }
9909 cur_arg++;
9910 }
9911
9912 if (type == REDIRECT_TYPE_NONE) {
9913 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9914 return NULL;
9915 }
9916
Willy Tarreaube4653b2015-05-28 15:26:58 +02009917 if (dir && type != REDIRECT_TYPE_LOCATION) {
9918 memprintf(errmsg, "response only supports redirect type 'location'");
9919 return NULL;
9920 }
9921
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02009922 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +01009923 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009924 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009925
9926 if (!use_fmt) {
9927 /* old-style static redirect rule */
9928 rule->rdr_str = strdup(destination);
9929 rule->rdr_len = strlen(destination);
9930 }
9931 else {
9932 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009933
9934 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9935 * if prefix == "/", we don't want to add anything, otherwise it
9936 * makes it hard for the user to configure a self-redirection.
9937 */
Godbachd9722032014-12-18 15:44:58 +08009938 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009939 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009940 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009941 dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR
9942 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009943 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009944 free(curproxy->conf.lfs_file);
9945 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9946 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009947 }
9948 }
9949
Willy Tarreau4baae242012-12-27 12:00:31 +01009950 if (cookie) {
9951 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9952 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9953 */
9954 rule->cookie_len = strlen(cookie);
9955 if (cookie_set) {
9956 rule->cookie_str = malloc(rule->cookie_len + 10);
9957 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9958 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9959 rule->cookie_len += 9;
9960 } else {
9961 rule->cookie_str = malloc(rule->cookie_len + 21);
9962 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9963 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9964 rule->cookie_len += 20;
9965 }
9966 }
9967 rule->type = type;
9968 rule->code = code;
9969 rule->flags = flags;
9970 LIST_INIT(&rule->list);
9971 return rule;
9972
9973 missing_arg:
9974 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9975 return NULL;
9976}
9977
Willy Tarreau8797c062007-05-07 00:55:35 +02009978/************************************************************************/
9979/* The code below is dedicated to ACL parsing and matching */
9980/************************************************************************/
9981
9982
Willy Tarreau14174bc2012-04-16 14:34:04 +02009983/* This function ensures that the prerequisites for an L7 fetch are ready,
9984 * which means that a request or response is ready. If some data is missing,
9985 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009986 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9987 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009988 *
9989 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009990 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9991 * decide whether or not an HTTP message is present ;
9992 * 0 if the requested data cannot be fetched or if it is certain that
9993 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009994 * 1 if an HTTP message is ready
9995 */
James Rosewell91a41cb2015-09-18 17:11:16 +01009996int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009997 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009998{
Willy Tarreau192252e2015-04-04 01:47:55 +02009999 struct http_txn *txn;
10000 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010001
Willy Tarreaube508f12016-03-10 11:47:01 +010010002 /* Note: it is possible that <s> is NULL when called before stream
10003 * initialization (eg: tcp-request connection), so this function is the
10004 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010005 */
Willy Tarreau192252e2015-04-04 01:47:55 +020010006 if (!s)
10007 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +010010008
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +020010009 if (!s->txn) {
10010 if (unlikely(!http_alloc_txn(s)))
10011 return 0; /* not enough memory */
10012 http_init_txn(s);
10013 }
Willy Tarreau192252e2015-04-04 01:47:55 +020010014 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +020010015 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010016
10017 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010018 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010019
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010020 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010021 /* If the buffer does not leave enough free space at the end,
10022 * we must first realign it.
10023 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010024 if (s->req.buf->p > s->req.buf->data &&
10025 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
10026 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +010010027
Willy Tarreau14174bc2012-04-16 14:34:04 +020010028 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +020010029 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +020010030 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010031
10032 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010033 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +020010034 http_msg_analyzer(msg, &txn->hdr_idx);
10035
10036 /* Still no valid request ? */
10037 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +020010038 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010039 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010040 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010041 }
10042 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010043 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010044 return 0;
10045 }
10046
10047 /* OK we just got a valid HTTP request. We have some minor
10048 * preparation to perform so that further checks can rely
10049 * on HTTP tests.
10050 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010051
10052 /* If the request was parsed but was too large, we must absolutely
10053 * return an error so that it is not processed. At the moment this
10054 * cannot happen, but if the parsers are to change in the future,
10055 * we want this check to be maintained.
10056 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010057 if (unlikely(s->req.buf->i + s->req.buf->p >
10058 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010059 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010060 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010061 return 1;
10062 }
10063
Willy Tarreau9b28e032012-10-12 23:49:43 +020010064 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010065 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010066 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010067
Willy Tarreau506d0502013-07-06 13:29:24 +020010068 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10069 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010070 }
10071
Willy Tarreau506d0502013-07-06 13:29:24 +020010072 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010073 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010074 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010075
10076 /* otherwise everything's ready for the request */
10077 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010078 else {
10079 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010080 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10081 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010082 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010083 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010084 }
10085
10086 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010087 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010088 return 1;
10089}
Willy Tarreau8797c062007-05-07 00:55:35 +020010090
Willy Tarreau8797c062007-05-07 00:55:35 +020010091/* 1. Check on METHOD
10092 * We use the pre-parsed method if it is known, and store its number as an
10093 * integer. If it is unknown, we use the pointer and the length.
10094 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010095static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010096{
10097 int len, meth;
10098
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010099 len = strlen(text);
10100 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010101
10102 pattern->val.i = meth;
10103 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010104 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010105 pattern->len = len;
10106 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010107 else {
10108 pattern->ptr.str = NULL;
10109 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010110 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010111 return 1;
10112}
10113
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010114/* This function fetches the method of current HTTP request and stores
10115 * it in the global pattern struct as a chunk. There are two possibilities :
10116 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10117 * in <len> and <ptr> is NULL ;
10118 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10119 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010120 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010121 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010122static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010123smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010124{
10125 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +010010126 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010127
Willy Tarreau24e32d82012-04-23 23:55:44 +020010128 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010129
Willy Tarreaube508f12016-03-10 11:47:01 +010010130 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010131 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010132 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010133 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010134 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010135 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10136 /* ensure the indexes are not affected */
10137 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010138 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010139 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
10140 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010141 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010142 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010143 return 1;
10144}
10145
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010146/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010147static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010148{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010149 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010150 struct pattern_list *lst;
10151 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010152
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010153 list_for_each_entry(lst, &expr->patterns, list) {
10154 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010155
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010156 /* well-known method */
10157 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010158 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010159 return pattern;
10160 else
10161 continue;
10162 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010163
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010164 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010165 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010166 continue;
10167
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010168 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010169 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
10170 (!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 +010010171 return pattern;
10172 }
10173 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010174}
10175
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010176static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010177smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010178{
Willy Tarreaube508f12016-03-10 11:47:01 +010010179 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010180 char *ptr;
10181 int len;
10182
Willy Tarreauc0239e02012-04-16 14:42:55 +020010183 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010184
Willy Tarreaube508f12016-03-10 11:47:01 +010010185 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010186 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010187 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010188
10189 while ((len-- > 0) && (*ptr++ != '/'));
10190 if (len <= 0)
10191 return 0;
10192
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010193 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010194 smp->data.u.str.str = ptr;
10195 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010196
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010197 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010198 return 1;
10199}
10200
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010201static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010202smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010203{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010204 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010205 char *ptr;
10206 int len;
10207
Willy Tarreauc0239e02012-04-16 14:42:55 +020010208 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010209
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010210 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010211 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10212 return 0;
10213
Willy Tarreau8797c062007-05-07 00:55:35 +020010214 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010215 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010216
10217 while ((len-- > 0) && (*ptr++ != '/'));
10218 if (len <= 0)
10219 return 0;
10220
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010221 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010222 smp->data.u.str.str = ptr;
10223 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010224
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010225 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010226 return 1;
10227}
10228
10229/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010230static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010231smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010232{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010233 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010234 char *ptr;
10235 int len;
10236
Willy Tarreauc0239e02012-04-16 14:42:55 +020010237 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010238
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010239 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010240 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10241 return 0;
10242
Willy Tarreau8797c062007-05-07 00:55:35 +020010243 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010244 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010245
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010246 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010247 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010248 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010249 return 1;
10250}
10251
Thierry Fournierf4011dd2016-03-29 17:23:51 +020010252static int
10253smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
10254{
10255 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
10256 return 0;
10257
10258 if (!smp->strm->unique_id) {
10259 if ((smp->strm->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
10260 return 0;
10261 smp->strm->unique_id[0] = '\0';
10262 }
10263 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
10264 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
10265
10266 smp->data.type = SMP_T_STR;
10267 smp->data.u.str.str = smp->strm->unique_id;
10268 smp->flags = SMP_F_CONST;
10269 return 1;
10270}
10271
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010272/* returns the longest available part of the body. This requires that the body
10273 * has been waited for using http-buffer-request.
10274 */
10275static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010276smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010277{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010278 struct http_msg *msg;
10279 unsigned long len;
10280 unsigned long block1;
10281 char *body;
10282 struct chunk *temp;
10283
10284 CHECK_HTTP_MESSAGE_FIRST();
10285
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010286 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010287 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010288 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010289 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010290
10291 len = http_body_bytes(msg);
10292 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10293
10294 block1 = len;
10295 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10296 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10297
10298 if (block1 == len) {
10299 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010300 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010301 smp->data.u.str.str = body;
10302 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010303 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10304 }
10305 else {
10306 /* buffer is wrapped, we need to defragment it */
10307 temp = get_trash_chunk();
10308 memcpy(temp->str, body, block1);
10309 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010310 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010311 smp->data.u.str.str = temp->str;
10312 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010313 smp->flags = SMP_F_VOL_TEST;
10314 }
10315 return 1;
10316}
10317
10318
10319/* returns the available length of the body. This requires that the body
10320 * has been waited for using http-buffer-request.
10321 */
10322static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010323smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010324{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010325 struct http_msg *msg;
10326
10327 CHECK_HTTP_MESSAGE_FIRST();
10328
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010329 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010330 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010331 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010332 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010333
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010334 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010335 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010336
10337 smp->flags = SMP_F_VOL_TEST;
10338 return 1;
10339}
10340
10341
10342/* returns the advertised length of the body, or the advertised size of the
10343 * chunks available in the buffer. This requires that the body has been waited
10344 * for using http-buffer-request.
10345 */
10346static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010347smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010348{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010349 struct http_msg *msg;
10350
10351 CHECK_HTTP_MESSAGE_FIRST();
10352
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010353 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010354 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010355 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010356 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010357
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010358 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010359 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010360
10361 smp->flags = SMP_F_VOL_TEST;
10362 return 1;
10363}
10364
10365
Willy Tarreau8797c062007-05-07 00:55:35 +020010366/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010367static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010368smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010369{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010370 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010371
Willy Tarreauc0239e02012-04-16 14:42:55 +020010372 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010373
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010374 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010375 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010376 smp->data.u.str.len = txn->req.sl.rq.u_l;
10377 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010378 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010379 return 1;
10380}
10381
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010382static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010383smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010384{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010385 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010386 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010387
Willy Tarreauc0239e02012-04-16 14:42:55 +020010388 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010389
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010390 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010391 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 +020010392 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010393 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010394
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010395 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010396 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010397 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010398 return 1;
10399}
10400
10401static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010402smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010403{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010404 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010405 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010406
Willy Tarreauc0239e02012-04-16 14:42:55 +020010407 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010408
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010409 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010410 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 +020010411 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10412 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010413
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010414 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010415 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010416 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010417 return 1;
10418}
10419
Willy Tarreau185b5c42012-04-26 15:11:51 +020010420/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10421 * Accepts an optional argument of type string containing the header field name,
10422 * and an optional argument of type signed or unsigned integer to request an
10423 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010424 * headers are considered from the first one. It does not stop on commas and
10425 * returns full lines instead (useful for User-Agent or Date for example).
10426 */
10427static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010428smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010429{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010430 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010431 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010432 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010433 int occ = 0;
10434 const char *name_str = NULL;
10435 int name_len = 0;
10436
10437 if (!ctx) {
10438 /* first call */
10439 ctx = &static_hdr_ctx;
10440 ctx->idx = 0;
10441 smp->ctx.a[0] = ctx;
10442 }
10443
10444 if (args) {
10445 if (args[0].type != ARGT_STR)
10446 return 0;
10447 name_str = args[0].data.str.str;
10448 name_len = args[0].data.str.len;
10449
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010450 if (args[1].type == ARGT_SINT)
10451 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010452 }
10453
10454 CHECK_HTTP_MESSAGE_FIRST();
10455
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010456 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010457 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 +020010458
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010459 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10460 /* search for header from the beginning */
10461 ctx->idx = 0;
10462
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010463 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010464 /* no explicit occurrence and single fetch => last header by default */
10465 occ = -1;
10466
10467 if (!occ)
10468 /* prepare to report multiple occurrences for ACL fetches */
10469 smp->flags |= SMP_F_NOT_LAST;
10470
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010471 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010472 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010473 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 +020010474 return 1;
10475
10476 smp->flags &= ~SMP_F_NOT_LAST;
10477 return 0;
10478}
10479
10480/* 6. Check on HTTP header count. The number of occurrences is returned.
10481 * Accepts exactly 1 argument of type string. It does not stop on commas and
10482 * returns full lines instead (useful for User-Agent or Date for example).
10483 */
10484static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010485smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010486{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010487 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010488 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010489 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010490 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010491 const char *name = NULL;
10492 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010493
Willy Tarreau601a4d12015-04-01 19:16:09 +020010494 if (args && args->type == ARGT_STR) {
10495 name = args->data.str.str;
10496 len = args->data.str.len;
10497 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010498
10499 CHECK_HTTP_MESSAGE_FIRST();
10500
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010501 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010502 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 +020010503
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010504 ctx.idx = 0;
10505 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010506 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010507 cnt++;
10508
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010509 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010510 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010511 smp->flags = SMP_F_VOL_HDR;
10512 return 1;
10513}
10514
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010515static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010516smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010517{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010518 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010519 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010520 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010521 struct chunk *temp;
10522 char del = ',';
10523
10524 if (args && args->type == ARGT_STR)
10525 del = *args[0].data.str.str;
10526
10527 CHECK_HTTP_MESSAGE_FIRST();
10528
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010529 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010530 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 +020010531
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010532 temp = get_trash_chunk();
10533
10534 ctx.idx = 0;
10535 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10536 if (temp->len)
10537 temp->str[temp->len++] = del;
10538 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10539 temp->len += ctx.del;
10540 }
10541
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010542 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010543 smp->data.u.str.str = temp->str;
10544 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010545 smp->flags = SMP_F_VOL_HDR;
10546 return 1;
10547}
10548
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010549/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10550 * Accepts an optional argument of type string containing the header field name,
10551 * and an optional argument of type signed or unsigned integer to request an
10552 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010553 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010554 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010555static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010556smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010557{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010558 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010559 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010560 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010561 int occ = 0;
10562 const char *name_str = NULL;
10563 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010564
Willy Tarreaua890d072013-04-02 12:01:06 +020010565 if (!ctx) {
10566 /* first call */
10567 ctx = &static_hdr_ctx;
10568 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010569 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010570 }
10571
Willy Tarreau185b5c42012-04-26 15:11:51 +020010572 if (args) {
10573 if (args[0].type != ARGT_STR)
10574 return 0;
10575 name_str = args[0].data.str.str;
10576 name_len = args[0].data.str.len;
10577
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010578 if (args[1].type == ARGT_SINT)
10579 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010580 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010581
Willy Tarreaue333ec92012-04-16 16:26:40 +020010582 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010583
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010584 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010585 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 +020010586
Willy Tarreau185b5c42012-04-26 15:11:51 +020010587 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010588 /* search for header from the beginning */
10589 ctx->idx = 0;
10590
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010591 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010592 /* no explicit occurrence and single fetch => last header by default */
10593 occ = -1;
10594
10595 if (!occ)
10596 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010597 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010598
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010599 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010600 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010601 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 +020010602 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010603
Willy Tarreau37406352012-04-23 16:16:37 +020010604 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010605 return 0;
10606}
10607
Willy Tarreauc11416f2007-06-17 16:58:38 +020010608/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010609 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010610 */
10611static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010612smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010613{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010614 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010615 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010616 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010617 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010618 const char *name = NULL;
10619 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010620
Willy Tarreau601a4d12015-04-01 19:16:09 +020010621 if (args && args->type == ARGT_STR) {
10622 name = args->data.str.str;
10623 len = args->data.str.len;
10624 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010625
Willy Tarreaue333ec92012-04-16 16:26:40 +020010626 CHECK_HTTP_MESSAGE_FIRST();
10627
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010628 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010629 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 +020010630
Willy Tarreau33a7e692007-06-10 19:45:56 +020010631 ctx.idx = 0;
10632 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010633 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010634 cnt++;
10635
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010636 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010637 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010638 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010639 return 1;
10640}
10641
Willy Tarreau185b5c42012-04-26 15:11:51 +020010642/* Fetch an HTTP header's integer value. The integer value is returned. It
10643 * takes a mandatory argument of type string and an optional one of type int
10644 * to designate a specific occurrence. It returns an unsigned integer, which
10645 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010646 */
10647static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010648smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010649{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010650 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010651
Willy Tarreauf853c462012-04-23 18:53:56 +020010652 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010653 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010654 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010655 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010656
Willy Tarreaud53e2422012-04-16 17:21:11 +020010657 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010658}
10659
Cyril Bonté69fa9922012-10-25 00:01:06 +020010660/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10661 * and an optional one of type int to designate a specific occurrence.
10662 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010663 */
10664static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010665smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010666{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010667 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010668
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010669 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010670 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010671 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010672 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010673 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010674 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010675 if (smp->data.u.str.len < temp->size - 1) {
10676 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10677 temp->str[smp->data.u.str.len] = '\0';
10678 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010679 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010680 break;
10681 }
10682 }
10683 }
10684
Willy Tarreaud53e2422012-04-16 17:21:11 +020010685 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010686 if (!(smp->flags & SMP_F_NOT_LAST))
10687 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010688 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010689 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010690}
10691
Willy Tarreau737b0c12007-06-10 21:28:46 +020010692/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10693 * the first '/' after the possible hostname, and ends before the possible '?'.
10694 */
10695static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010696smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010697{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010698 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010699 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010700
Willy Tarreauc0239e02012-04-16 14:42:55 +020010701 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010702
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010703 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010704 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010705 ptr = http_get_path(txn);
10706 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010707 return 0;
10708
10709 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010710 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010711 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010712
10713 while (ptr < end && *ptr != '?')
10714 ptr++;
10715
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010716 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010717 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010718 return 1;
10719}
10720
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010721/* This produces a concatenation of the first occurrence of the Host header
10722 * followed by the path component if it begins with a slash ('/'). This means
10723 * that '*' will not be added, resulting in exactly the first Host entry.
10724 * If no Host header is found, then the path is returned as-is. The returned
10725 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010726 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010727 */
10728static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010729smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010730{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010731 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010732 char *ptr, *end, *beg;
10733 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010734 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010735
10736 CHECK_HTTP_MESSAGE_FIRST();
10737
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010738 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010739 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010740 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010741 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010742
10743 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010744 temp = get_trash_chunk();
10745 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010746 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010747 smp->data.u.str.str = temp->str;
10748 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010749
10750 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010751 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010752 beg = http_get_path(txn);
10753 if (!beg)
10754 beg = end;
10755
10756 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10757
10758 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010759 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10760 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010761 }
10762
10763 smp->flags = SMP_F_VOL_1ST;
10764 return 1;
10765}
10766
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010767/* This produces a 32-bit hash of the concatenation of the first occurrence of
10768 * the Host header followed by the path component if it begins with a slash ('/').
10769 * This means that '*' will not be added, resulting in exactly the first Host
10770 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010771 * is hashed using the path hash followed by a full avalanche hash and provides a
10772 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010773 * high-traffic sites without having to store whole paths.
10774 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010775int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010776smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010777{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010778 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010779 struct hdr_ctx ctx;
10780 unsigned int hash = 0;
10781 char *ptr, *beg, *end;
10782 int len;
10783
10784 CHECK_HTTP_MESSAGE_FIRST();
10785
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010786 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010787 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010788 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010789 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10790 ptr = ctx.line + ctx.val;
10791 len = ctx.vlen;
10792 while (len--)
10793 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10794 }
10795
10796 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010797 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010798 beg = http_get_path(txn);
10799 if (!beg)
10800 beg = end;
10801
10802 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10803
10804 if (beg < ptr && *beg == '/') {
10805 while (beg < ptr)
10806 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10807 }
10808 hash = full_hash(hash);
10809
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010810 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010811 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010812 smp->flags = SMP_F_VOL_1ST;
10813 return 1;
10814}
10815
Willy Tarreau4a550602012-12-09 14:53:32 +010010816/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010817 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10818 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10819 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010820 * that in environments where IPv6 is insignificant, truncating the output to
10821 * 8 bytes would still work.
10822 */
10823static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010824smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010825{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010826 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010827 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010828
10829 if (!cli_conn)
10830 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010831
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010832 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010833 return 0;
10834
Willy Tarreau47ca5452012-12-23 20:22:19 +010010835 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010836 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010837 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010838
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010839 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010840 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010841 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010842 temp->len += 4;
10843 break;
10844 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010845 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010846 temp->len += 16;
10847 break;
10848 default:
10849 return 0;
10850 }
10851
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010852 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010853 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010854 return 1;
10855}
10856
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010857/* Extracts the query string, which comes after the question mark '?'. If no
10858 * question mark is found, nothing is returned. Otherwise it returns a sample
10859 * of type string carrying the whole query string.
10860 */
10861static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010862smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010863{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010864 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010865 char *ptr, *end;
10866
10867 CHECK_HTTP_MESSAGE_FIRST();
10868
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010869 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010870 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10871 end = ptr + txn->req.sl.rq.u_l;
10872
10873 /* look up the '?' */
10874 do {
10875 if (ptr == end)
10876 return 0;
10877 } while (*ptr++ != '?');
10878
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010879 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010880 smp->data.u.str.str = ptr;
10881 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010882 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10883 return 1;
10884}
10885
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010886static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010887smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010888{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010889 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10890 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10891 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010892
Willy Tarreau24e32d82012-04-23 23:55:44 +020010893 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010894
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010895 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010896 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010897 return 1;
10898}
10899
Willy Tarreau7f18e522010-10-22 20:04:13 +020010900/* return a valid test if the current request is the first one on the connection */
10901static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010902smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010903{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010904 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010905 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010906 return 1;
10907}
10908
Willy Tarreau34db1082012-04-19 17:16:54 +020010909/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010910static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010911smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010912{
10913
Willy Tarreau24e32d82012-04-23 23:55:44 +020010914 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010915 return 0;
10916
Willy Tarreauc0239e02012-04-16 14:42:55 +020010917 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010918
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010919 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010920 return 0;
10921
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010922 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010923 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010924 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010925 return 1;
10926}
Willy Tarreau8797c062007-05-07 00:55:35 +020010927
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010928/* Accepts exactly 1 argument of type userlist */
10929static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010930smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010931{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010932 if (!args || args->type != ARGT_USR)
10933 return 0;
10934
10935 CHECK_HTTP_MESSAGE_FIRST();
10936
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010937 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010938 return 0;
10939
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010940 /* if the user does not belong to the userlist or has a wrong password,
10941 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010942 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010943 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010944 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10945 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010946 return 0;
10947
10948 /* pat_match_auth() will need the user list */
10949 smp->ctx.a[0] = args->data.usr;
10950
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010951 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010952 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010953 smp->data.u.str.str = smp->strm->txn->auth.user;
10954 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010955
10956 return 1;
10957}
10958
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010959/* Try to find the next occurrence of a cookie name in a cookie header value.
10960 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10961 * the cookie value is returned into *value and *value_l, and the function
10962 * returns a pointer to the next pointer to search from if the value was found.
10963 * Otherwise if the cookie was not found, NULL is returned and neither value
10964 * nor value_l are touched. The input <hdr> string should first point to the
10965 * header's value, and the <hdr_end> pointer must point to the first character
10966 * not part of the value. <list> must be non-zero if value may represent a list
10967 * of values (cookie headers). This makes it faster to abort parsing when no
10968 * list is expected.
10969 */
David Carlier4686f792015-09-25 14:10:50 +010010970char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010971extract_cookie_value(char *hdr, const char *hdr_end,
10972 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010973 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010974{
10975 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10976 char *next;
10977
10978 /* we search at least a cookie name followed by an equal, and more
10979 * generally something like this :
10980 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10981 */
10982 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10983 /* Iterate through all cookies on this line */
10984
Willy Tarreau2235b262016-11-05 15:50:20 +010010985 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010986 att_beg++;
10987
10988 /* find att_end : this is the first character after the last non
10989 * space before the equal. It may be equal to hdr_end.
10990 */
10991 equal = att_end = att_beg;
10992
10993 while (equal < hdr_end) {
10994 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10995 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010010996 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010997 continue;
10998 att_end = equal;
10999 }
11000
11001 /* here, <equal> points to '=', a delimitor or the end. <att_end>
11002 * is between <att_beg> and <equal>, both may be identical.
11003 */
11004
11005 /* look for end of cookie if there is an equal sign */
11006 if (equal < hdr_end && *equal == '=') {
11007 /* look for the beginning of the value */
11008 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010011009 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011010 val_beg++;
11011
11012 /* find the end of the value, respecting quotes */
11013 next = find_cookie_value_end(val_beg, hdr_end);
11014
11015 /* make val_end point to the first white space or delimitor after the value */
11016 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010011017 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011018 val_end--;
11019 } else {
11020 val_beg = val_end = next = equal;
11021 }
11022
11023 /* We have nothing to do with attributes beginning with '$'. However,
11024 * they will automatically be removed if a header before them is removed,
11025 * since they're supposed to be linked together.
11026 */
11027 if (*att_beg == '$')
11028 continue;
11029
11030 /* Ignore cookies with no equal sign */
11031 if (equal == next)
11032 continue;
11033
11034 /* Now we have the cookie name between att_beg and att_end, and
11035 * its value between val_beg and val_end.
11036 */
11037
11038 if (att_end - att_beg == cookie_name_l &&
11039 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
11040 /* let's return this value and indicate where to go on from */
11041 *value = val_beg;
11042 *value_l = val_end - val_beg;
11043 return next + 1;
11044 }
11045
11046 /* Set-Cookie headers only have the name in the first attr=value part */
11047 if (!list)
11048 break;
11049 }
11050
11051 return NULL;
11052}
11053
William Lallemanda43ba4e2014-01-28 18:14:25 +010011054/* Fetch a captured HTTP request header. The index is the position of
11055 * the "capture" option in the configuration file
11056 */
11057static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011058smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011059{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011060 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011061 int idx;
11062
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011063 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011064 return 0;
11065
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011066 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011067
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011068 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 +010011069 return 0;
11070
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011071 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011072 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011073 smp->data.u.str.str = smp->strm->req_cap[idx];
11074 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011075
11076 return 1;
11077}
11078
11079/* Fetch a captured HTTP response header. The index is the position of
11080 * the "capture" option in the configuration file
11081 */
11082static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011083smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011084{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011085 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011086 int idx;
11087
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011088 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011089 return 0;
11090
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011091 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011092
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011093 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 +010011094 return 0;
11095
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011096 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011097 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011098 smp->data.u.str.str = smp->strm->res_cap[idx];
11099 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011100
11101 return 1;
11102}
11103
William Lallemand65ad6e12014-01-31 15:08:02 +010011104/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11105static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011106smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011107{
11108 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011109 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011110 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011111
Willy Tarreau15e91e12015-04-04 00:52:09 +020011112 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011113 return 0;
11114
William Lallemand96a77852014-02-05 00:30:02 +010011115 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011116
William Lallemand96a77852014-02-05 00:30:02 +010011117 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11118 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011119
William Lallemand96a77852014-02-05 00:30:02 +010011120 temp = get_trash_chunk();
11121 temp->str = txn->uri;
11122 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011123 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011124 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011125 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011126
11127 return 1;
11128
11129}
11130
11131/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11132static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011133smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011134{
11135 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011136 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011137 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011138
Willy Tarreau15e91e12015-04-04 00:52:09 +020011139 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011140 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011141
William Lallemand65ad6e12014-01-31 15:08:02 +010011142 ptr = txn->uri;
11143
11144 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11145 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011146
William Lallemand65ad6e12014-01-31 15:08:02 +010011147 if (!*ptr)
11148 return 0;
11149
11150 ptr++; /* skip the space */
11151
11152 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011153 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011154 if (!ptr)
11155 return 0;
11156 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11157 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011158
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011159 smp->data.u.str = *temp;
11160 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011161 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011162 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011163
11164 return 1;
11165}
11166
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011167/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11168 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11169 */
11170static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011171smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011172{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011173 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011174
Willy Tarreau15e91e12015-04-04 00:52:09 +020011175 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011176 return 0;
11177
11178 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011179 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011180 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011181 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011182
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011183 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011184 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011185 smp->flags = SMP_F_CONST;
11186 return 1;
11187
11188}
11189
11190/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11191 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11192 */
11193static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011194smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011195{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011196 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011197
Willy Tarreau15e91e12015-04-04 00:52:09 +020011198 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011199 return 0;
11200
11201 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011202 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011203 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011204 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011205
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011206 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011207 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011208 smp->flags = SMP_F_CONST;
11209 return 1;
11210
11211}
11212
William Lallemand65ad6e12014-01-31 15:08:02 +010011213
Willy Tarreaue333ec92012-04-16 16:26:40 +020011214/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011215 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011216 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011217 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011218 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011219 * Accepts exactly 1 argument of type string. If the input options indicate
11220 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011221 * The returned sample is of type CSTR. Can be used to parse cookies in other
11222 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011223 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011224int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011225{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011226 struct http_txn *txn;
11227 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011228 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011229 const struct http_msg *msg;
11230 const char *hdr_name;
11231 int hdr_name_len;
11232 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011233 int occ = 0;
11234 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011235
Willy Tarreau24e32d82012-04-23 23:55:44 +020011236 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011237 return 0;
11238
Willy Tarreaua890d072013-04-02 12:01:06 +020011239 if (!ctx) {
11240 /* first call */
11241 ctx = &static_hdr_ctx;
11242 ctx->idx = 0;
11243 smp->ctx.a[2] = ctx;
11244 }
11245
Willy Tarreaue333ec92012-04-16 16:26:40 +020011246 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011247
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011248 txn = smp->strm->txn;
11249 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011250
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011251 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011252 msg = &txn->req;
11253 hdr_name = "Cookie";
11254 hdr_name_len = 6;
11255 } else {
11256 msg = &txn->rsp;
11257 hdr_name = "Set-Cookie";
11258 hdr_name_len = 10;
11259 }
11260
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011261 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011262 /* no explicit occurrence and single fetch => last cookie by default */
11263 occ = -1;
11264
11265 /* OK so basically here, either we want only one value and it's the
11266 * last one, or we want to iterate over all of them and we fetch the
11267 * next one.
11268 */
11269
Willy Tarreau9b28e032012-10-12 23:49:43 +020011270 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011271 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011272 /* search for the header from the beginning, we must first initialize
11273 * the search parameters.
11274 */
Willy Tarreau37406352012-04-23 16:16:37 +020011275 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011276 ctx->idx = 0;
11277 }
11278
Willy Tarreau28376d62012-04-26 21:26:10 +020011279 smp->flags |= SMP_F_VOL_HDR;
11280
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011281 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011282 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11283 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011284 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11285 goto out;
11286
Willy Tarreau24e32d82012-04-23 23:55:44 +020011287 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011288 continue;
11289
Willy Tarreau37406352012-04-23 16:16:37 +020011290 smp->ctx.a[0] = ctx->line + ctx->val;
11291 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011292 }
11293
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011294 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011295 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011296 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011297 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011298 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011299 &smp->data.u.str.str,
11300 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011301 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011302 found = 1;
11303 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011304 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011305 smp->flags |= SMP_F_NOT_LAST;
11306 return 1;
11307 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011308 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011309 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011310 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011311 /* all cookie headers and values were scanned. If we're looking for the
11312 * last occurrence, we may return it now.
11313 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011314 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011315 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011316 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011317}
11318
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011319/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011320 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011321 * multiple cookies may be parsed on the same line. The returned sample is of
11322 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011323 */
11324static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011325smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011326{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011327 struct http_txn *txn;
11328 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011329 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011330 const struct http_msg *msg;
11331 const char *hdr_name;
11332 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011333 int cnt;
11334 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011335 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011336
Willy Tarreau24e32d82012-04-23 23:55:44 +020011337 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011338 return 0;
11339
Willy Tarreaue333ec92012-04-16 16:26:40 +020011340 CHECK_HTTP_MESSAGE_FIRST();
11341
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011342 txn = smp->strm->txn;
11343 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011344
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011345 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011346 msg = &txn->req;
11347 hdr_name = "Cookie";
11348 hdr_name_len = 6;
11349 } else {
11350 msg = &txn->rsp;
11351 hdr_name = "Set-Cookie";
11352 hdr_name_len = 10;
11353 }
11354
Willy Tarreau9b28e032012-10-12 23:49:43 +020011355 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011356 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011357 ctx.idx = 0;
11358 cnt = 0;
11359
11360 while (1) {
11361 /* Note: val_beg == NULL every time we need to fetch a new header */
11362 if (!val_beg) {
11363 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11364 break;
11365
Willy Tarreau24e32d82012-04-23 23:55:44 +020011366 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011367 continue;
11368
11369 val_beg = ctx.line + ctx.val;
11370 val_end = val_beg + ctx.vlen;
11371 }
11372
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011373 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011374 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011375 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011376 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011377 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011378 &smp->data.u.str.str,
11379 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011380 cnt++;
11381 }
11382 }
11383
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011384 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011385 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011386 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011387 return 1;
11388}
11389
Willy Tarreau51539362012-05-08 12:46:28 +020011390/* Fetch an cookie's integer value. The integer value is returned. It
11391 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11392 */
11393static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011394smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011395{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011396 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011397
11398 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011399 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011400 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011401 }
11402
11403 return ret;
11404}
11405
Willy Tarreau8797c062007-05-07 00:55:35 +020011406/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011407/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011408/************************************************************************/
11409
David Cournapeau16023ee2010-12-23 20:55:41 +090011410/*
11411 * Given a path string and its length, find the position of beginning of the
11412 * query string. Returns NULL if no query string is found in the path.
11413 *
11414 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11415 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011416 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011417 */
bedis4c75cca2012-10-05 08:38:24 +020011418static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011419{
11420 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011421
bedis4c75cca2012-10-05 08:38:24 +020011422 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011423 return p ? p + 1 : NULL;
11424}
11425
bedis4c75cca2012-10-05 08:38:24 +020011426static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011427{
bedis4c75cca2012-10-05 08:38:24 +020011428 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011429}
11430
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011431/* after increasing a pointer value, it can exceed the first buffer
11432 * size. This function transform the value of <ptr> according with
11433 * the expected position. <chunks> is an array of the one or two
11434 * avalaible chunks. The first value is the start of the first chunk,
11435 * the second value if the end+1 of the first chunks. The third value
11436 * is NULL or the start of the second chunk and the fourth value is
11437 * the end+1 of the second chunk. The function returns 1 if does a
11438 * wrap, else returns 0.
11439 */
11440static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11441{
11442 if (*ptr < chunks[1])
11443 return 0;
11444 if (!chunks[2])
11445 return 0;
11446 *ptr = chunks[2] + ( *ptr - chunks[1] );
11447 return 1;
11448}
11449
David Cournapeau16023ee2010-12-23 20:55:41 +090011450/*
11451 * Given a url parameter, find the starting position of the first occurence,
11452 * or NULL if the parameter is not found.
11453 *
11454 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11455 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011456 *
Willy Tarreauf6625822015-12-27 14:51:01 +010011457 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011458 * or the second chunk. The caller must be check the position before using the
11459 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011460 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011461static const char *
11462find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011463 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011464 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011465{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011466 const char *pos, *last, *equal;
11467 const char **bufs = chunks;
11468 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011469
David Cournapeau16023ee2010-12-23 20:55:41 +090011470
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011471 pos = bufs[0];
11472 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010011473 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011474 /* Check the equal. */
11475 equal = pos + url_param_name_l;
11476 if (fix_pointer_if_wrap(chunks, &equal)) {
11477 if (equal >= chunks[3])
11478 return NULL;
11479 } else {
11480 if (equal >= chunks[1])
11481 return NULL;
11482 }
11483 if (*equal == '=') {
11484 if (pos + url_param_name_l > last) {
11485 /* process wrap case, we detect a wrap. In this case, the
11486 * comparison is performed in two parts.
11487 */
11488
11489 /* This is the end, we dont have any other chunk. */
11490 if (bufs != chunks || !bufs[2])
11491 return NULL;
11492
11493 /* Compute the length of each part of the comparison. */
11494 l1 = last - pos;
11495 l2 = url_param_name_l - l1;
11496
11497 /* The second buffer is too short to contain the compared string. */
11498 if (bufs[2] + l2 > bufs[3])
11499 return NULL;
11500
11501 if (memcmp(pos, url_param_name, l1) == 0 &&
11502 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11503 return pos;
11504
11505 /* Perform wrapping and jump the string who fail the comparison. */
11506 bufs += 2;
11507 pos = bufs[0] + l2;
11508 last = bufs[1];
11509
11510 } else {
11511 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011512 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11513 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011514 pos += url_param_name_l + 1;
11515 if (fix_pointer_if_wrap(chunks, &pos))
11516 last = bufs[2];
11517 }
11518 }
11519
11520 while (1) {
11521 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011522 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011523 pos++;
11524 if (pos < last)
11525 break;
11526 /* process buffer wrapping. */
11527 if (bufs != chunks || !bufs[2])
11528 return NULL;
11529 bufs += 2;
11530 pos = bufs[0];
11531 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011532 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011533 pos++;
11534 }
11535 return NULL;
11536}
11537
11538/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011539 * Given a url parameter name and a query string, find the next value.
11540 * An empty url_param_name matches the first available parameter.
11541 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11542 * respectively provide a pointer to the value and its end.
11543 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011544 */
11545static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011546find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011547 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011548 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011549{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011550 const char *arg_start, *qs_end;
11551 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011552
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011553 arg_start = chunks[0];
11554 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011555 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011556 /* Looks for an argument name. */
11557 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011558 url_param_name, url_param_name_l,
11559 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011560 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011561 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011562 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011563 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011564 if (!arg_start)
11565 return 0;
11566
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011567 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011568 while (1) {
11569 /* looks for the first argument. */
11570 value_start = memchr(arg_start, '=', qs_end - arg_start);
11571 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011572 /* Check for wrapping. */
11573 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011574 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011575 chunks[2]) {
11576 arg_start = chunks[2];
11577 qs_end = chunks[3];
11578 continue;
11579 }
11580 return 0;
11581 }
11582 break;
11583 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011584 value_start++;
11585 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011586 else {
11587 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011588 value_start = arg_start + url_param_name_l + 1;
11589
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011590 /* Check for pointer wrapping. */
11591 if (fix_pointer_if_wrap(chunks, &value_start)) {
11592 /* Update the end pointer. */
11593 qs_end = chunks[3];
11594
11595 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011596 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011597 return 0;
11598 }
11599 }
11600
David Cournapeau16023ee2010-12-23 20:55:41 +090011601 value_end = value_start;
11602
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011603 while (1) {
11604 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11605 value_end++;
11606 if (value_end < qs_end)
11607 break;
11608 /* process buffer wrapping. */
11609 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011610 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011611 chunks[2]) {
11612 value_end = chunks[2];
11613 qs_end = chunks[3];
11614 continue;
11615 }
11616 break;
11617 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011618
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011619 *vstart = value_start;
11620 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011621 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011622}
11623
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011624/* This scans a URL-encoded query string. It takes an optionally wrapping
11625 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11626 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11627 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011628 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011629static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011630smp_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 +090011631{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011632 const char *vstart, *vend;
11633 struct chunk *temp;
11634 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011635
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011636 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011637 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011638 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011639 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011640 return 0;
11641
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011642 /* Create sample. If the value is contiguous, return the pointer as CONST,
11643 * if the value is wrapped, copy-it in a buffer.
11644 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011645 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011646 if (chunks[2] &&
11647 vstart >= chunks[0] && vstart <= chunks[1] &&
11648 vend >= chunks[2] && vend <= chunks[3]) {
11649 /* Wrapped case. */
11650 temp = get_trash_chunk();
11651 memcpy(temp->str, vstart, chunks[1] - vstart);
11652 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011653 smp->data.u.str.str = temp->str;
11654 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011655 } else {
11656 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011657 smp->data.u.str.str = (char *)vstart;
11658 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011659 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11660 }
11661
11662 /* Update context, check wrapping. */
11663 chunks[0] = vend;
11664 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11665 chunks[1] = chunks[3];
11666 chunks[2] = NULL;
11667 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011668
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011669 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011670 smp->flags |= SMP_F_NOT_LAST;
11671
David Cournapeau16023ee2010-12-23 20:55:41 +090011672 return 1;
11673}
11674
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011675/* This function iterates over each parameter of the query string. It uses
11676 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011677 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11678 * An optional parameter name is passed in args[0], otherwise any parameter is
11679 * considered. It supports an optional delimiter argument for the beginning of
11680 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011681 */
11682static int
11683smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11684{
11685 struct http_msg *msg;
11686 char delim = '?';
11687 const char *name;
11688 int name_len;
11689
Dragan Dosen26f77e52015-05-25 10:02:11 +020011690 if (!args ||
11691 (args[0].type && args[0].type != ARGT_STR) ||
11692 (args[1].type && args[1].type != ARGT_STR))
11693 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011694
Dragan Dosen26f77e52015-05-25 10:02:11 +020011695 name = "";
11696 name_len = 0;
11697 if (args->type == ARGT_STR) {
11698 name = args->data.str.str;
11699 name_len = args->data.str.len;
11700 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011701
Dragan Dosen26f77e52015-05-25 10:02:11 +020011702 if (args[1].type)
11703 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011704
Dragan Dosen26f77e52015-05-25 10:02:11 +020011705 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011706 CHECK_HTTP_MESSAGE_FIRST();
11707
11708 msg = &smp->strm->txn->req;
11709
11710 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11711 msg->sl.rq.u_l, delim);
11712 if (!smp->ctx.a[0])
11713 return 0;
11714
11715 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011716
11717 /* Assume that the context is filled with NULL pointer
11718 * before the first call.
11719 * smp->ctx.a[2] = NULL;
11720 * smp->ctx.a[3] = NULL;
11721 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011722 }
11723
11724 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11725}
11726
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011727/* This function iterates over each parameter of the body. This requires
11728 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011729 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11730 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11731 * optional second part if the body wraps at the end of the buffer. An optional
11732 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011733 */
11734static int
11735smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11736{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011737 struct http_msg *msg;
11738 unsigned long len;
11739 unsigned long block1;
11740 char *body;
11741 const char *name;
11742 int name_len;
11743
11744 if (!args || (args[0].type && args[0].type != ARGT_STR))
11745 return 0;
11746
11747 name = "";
11748 name_len = 0;
11749 if (args[0].type == ARGT_STR) {
11750 name = args[0].data.str.str;
11751 name_len = args[0].data.str.len;
11752 }
11753
11754 if (!smp->ctx.a[0]) { // first call, find the query string
11755 CHECK_HTTP_MESSAGE_FIRST();
11756
11757 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011758 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011759 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011760 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011761
11762 len = http_body_bytes(msg);
11763 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11764
11765 block1 = len;
11766 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11767 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11768
11769 if (block1 == len) {
11770 /* buffer is not wrapped (or empty) */
11771 smp->ctx.a[0] = body;
11772 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011773
11774 /* Assume that the context is filled with NULL pointer
11775 * before the first call.
11776 * smp->ctx.a[2] = NULL;
11777 * smp->ctx.a[3] = NULL;
11778 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011779 }
11780 else {
11781 /* buffer is wrapped, we need to defragment it */
11782 smp->ctx.a[0] = body;
11783 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011784 smp->ctx.a[2] = msg->chn->buf->data;
11785 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011786 }
11787 }
11788 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11789}
11790
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011791/* Return the signed integer value for the specified url parameter (see url_param
11792 * above).
11793 */
11794static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011795smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011796{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011797 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011798
11799 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011800 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011801 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011802 }
11803
11804 return ret;
11805}
11806
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011807/* This produces a 32-bit hash of the concatenation of the first occurrence of
11808 * the Host header followed by the path component if it begins with a slash ('/').
11809 * This means that '*' will not be added, resulting in exactly the first Host
11810 * entry. If no Host header is found, then the path is used. The resulting value
11811 * is hashed using the url hash followed by a full avalanche hash and provides a
11812 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11813 * high-traffic sites without having to store whole paths.
11814 * this differs from the base32 functions in that it includes the url parameters
11815 * as well as the path
11816 */
11817static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011818smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011819{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011820 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011821 struct hdr_ctx ctx;
11822 unsigned int hash = 0;
11823 char *ptr, *beg, *end;
11824 int len;
11825
11826 CHECK_HTTP_MESSAGE_FIRST();
11827
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011828 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011829 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011830 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011831 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11832 ptr = ctx.line + ctx.val;
11833 len = ctx.vlen;
11834 while (len--)
11835 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11836 }
11837
11838 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011839 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 +000011840 beg = http_get_path(txn);
11841 if (!beg)
11842 beg = end;
11843
11844 for (ptr = beg; ptr < end ; ptr++);
11845
11846 if (beg < ptr && *beg == '/') {
11847 while (beg < ptr)
11848 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11849 }
11850 hash = full_hash(hash);
11851
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011852 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011853 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011854 smp->flags = SMP_F_VOL_1ST;
11855 return 1;
11856}
11857
11858/* This concatenates the source address with the 32-bit hash of the Host and
11859 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11860 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11861 * on the source address length. The URL hash is stored before the address so
11862 * that in environments where IPv6 is insignificant, truncating the output to
11863 * 8 bytes would still work.
11864 */
11865static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011866smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011867{
11868 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011869 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011870
Dragan Dosendb5af612016-06-16 11:23:01 +020011871 if (!cli_conn)
11872 return 0;
11873
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011874 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011875 return 0;
11876
11877 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020011878 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
11879 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011880
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011881 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011882 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011883 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011884 temp->len += 4;
11885 break;
11886 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011887 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011888 temp->len += 16;
11889 break;
11890 default:
11891 return 0;
11892 }
11893
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011894 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011895 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011896 return 1;
11897}
11898
Willy Tarreau185b5c42012-04-26 15:11:51 +020011899/* This function is used to validate the arguments passed to any "hdr" fetch
11900 * keyword. These keywords support an optional positive or negative occurrence
11901 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11902 * is assumed that the types are already the correct ones. Returns 0 on error,
11903 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11904 * error message in case of error, that the caller is responsible for freeing.
11905 * The initial location must either be freeable or NULL.
11906 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011907int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011908{
11909 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011910 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011911 return 0;
11912 }
11913 return 1;
11914}
11915
Willy Tarreau276fae92013-07-25 14:36:01 +020011916/* takes an UINT value on input supposed to represent the time since EPOCH,
11917 * adds an optional offset found in args[0] and emits a string representing
11918 * the date in RFC-1123/5322 format.
11919 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011920static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011921{
Cyril Bontéf78d8962016-01-22 19:40:28 +010011922 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020011923 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11924 struct chunk *temp;
11925 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011926 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011927 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011928
11929 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011930 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011931 curr_date += args[0].data.sint;
11932
11933 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011934 if (!tm)
11935 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011936
11937 temp = get_trash_chunk();
11938 temp->len = snprintf(temp->str, temp->size - temp->len,
11939 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11940 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11941 tm->tm_hour, tm->tm_min, tm->tm_sec);
11942
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011943 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011944 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011945 return 1;
11946}
11947
Thierry FOURNIERad903512014-04-11 17:51:01 +020011948/* Match language range with language tag. RFC2616 14.4:
11949 *
11950 * A language-range matches a language-tag if it exactly equals
11951 * the tag, or if it exactly equals a prefix of the tag such
11952 * that the first tag character following the prefix is "-".
11953 *
11954 * Return 1 if the strings match, else return 0.
11955 */
11956static inline int language_range_match(const char *range, int range_len,
11957 const char *tag, int tag_len)
11958{
11959 const char *end = range + range_len;
11960 const char *tend = tag + tag_len;
11961 while (range < end) {
11962 if (*range == '-' && tag == tend)
11963 return 1;
11964 if (*range != *tag || tag == tend)
11965 return 0;
11966 range++;
11967 tag++;
11968 }
11969 /* Return true only if the last char of the tag is matched. */
11970 return tag == tend;
11971}
11972
11973/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011974static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011975{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011976 const char *al = smp->data.u.str.str;
11977 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011978 const char *token;
11979 int toklen;
11980 int qvalue;
11981 const char *str;
11982 const char *w;
11983 int best_q = 0;
11984
11985 /* Set the constant to the sample, because the output of the
11986 * function will be peek in the constant configuration string.
11987 */
11988 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011989 smp->data.u.str.size = 0;
11990 smp->data.u.str.str = "";
11991 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011992
11993 /* Parse the accept language */
11994 while (1) {
11995
11996 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011997 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011998 al++;
11999 if (al >= end)
12000 break;
12001
12002 /* Start of the fisrt word. */
12003 token = al;
12004
12005 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012006 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012007 al++;
12008 if (al == token)
12009 goto expect_comma;
12010
12011 /* Length of the token. */
12012 toklen = al - token;
12013 qvalue = 1000;
12014
12015 /* Check if the token exists in the list. If the token not exists,
12016 * jump to the next token.
12017 */
12018 str = args[0].data.str.str;
12019 w = str;
12020 while (1) {
12021 if (*str == ';' || *str == '\0') {
12022 if (language_range_match(token, toklen, w, str-w))
12023 goto look_for_q;
12024 if (*str == '\0')
12025 goto expect_comma;
12026 w = str + 1;
12027 }
12028 str++;
12029 }
12030 goto expect_comma;
12031
12032look_for_q:
12033
12034 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012035 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012036 al++;
12037 if (al >= end)
12038 goto process_value;
12039
12040 /* If ',' is found, process the result */
12041 if (*al == ',')
12042 goto process_value;
12043
12044 /* If the character is different from ';', look
12045 * for the end of the header part in best effort.
12046 */
12047 if (*al != ';')
12048 goto expect_comma;
12049
12050 /* Assumes that the char is ';', now expect "q=". */
12051 al++;
12052
12053 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012054 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012055 al++;
12056 if (al >= end)
12057 goto process_value;
12058
12059 /* Expect 'q'. If no 'q', continue in best effort */
12060 if (*al != 'q')
12061 goto process_value;
12062 al++;
12063
12064 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012065 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012066 al++;
12067 if (al >= end)
12068 goto process_value;
12069
12070 /* Expect '='. If no '=', continue in best effort */
12071 if (*al != '=')
12072 goto process_value;
12073 al++;
12074
12075 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012076 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012077 al++;
12078 if (al >= end)
12079 goto process_value;
12080
12081 /* Parse the q value. */
12082 qvalue = parse_qvalue(al, &al);
12083
12084process_value:
12085
12086 /* If the new q value is the best q value, then store the associated
12087 * language in the response. If qvalue is the biggest value (1000),
12088 * break the process.
12089 */
12090 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012091 smp->data.u.str.str = (char *)w;
12092 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012093 if (qvalue >= 1000)
12094 break;
12095 best_q = qvalue;
12096 }
12097
12098expect_comma:
12099
12100 /* Expect comma or end. If the end is detected, quit the loop. */
12101 while (al < end && *al != ',')
12102 al++;
12103 if (al >= end)
12104 break;
12105
12106 /* Comma is found, jump it and restart the analyzer. */
12107 al++;
12108 }
12109
12110 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012111 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
12112 smp->data.u.str.str = args[1].data.str.str;
12113 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012114 }
12115
12116 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012117 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012118}
12119
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012120/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012121static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012122{
12123 /* If the constant flag is set or if not size is avalaible at
12124 * the end of the buffer, copy the string in other buffer
12125 * before decoding.
12126 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012127 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012128 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012129 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
12130 smp->data.u.str.str = str->str;
12131 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012132 smp->flags &= ~SMP_F_CONST;
12133 }
12134
12135 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012136 smp->data.u.str.str[smp->data.u.str.len] = '\0';
12137 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012138 return 1;
12139}
12140
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012141static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
12142{
12143 struct proxy *fe = strm_fe(smp->strm);
12144 int idx, i;
12145 struct cap_hdr *hdr;
12146 int len;
12147
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012148 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012149 return 0;
12150
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012151 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012152
12153 /* Check the availibity of the capture id. */
12154 if (idx > fe->nb_req_cap - 1)
12155 return 0;
12156
12157 /* Look for the original configuration. */
12158 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
12159 hdr != NULL && i != idx ;
12160 i--, hdr = hdr->next);
12161 if (!hdr)
12162 return 0;
12163
12164 /* check for the memory allocation */
12165 if (smp->strm->req_cap[hdr->index] == NULL)
12166 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
12167 if (smp->strm->req_cap[hdr->index] == NULL)
12168 return 0;
12169
12170 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012171 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012172 if (len > hdr->len)
12173 len = hdr->len;
12174
12175 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012176 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012177 smp->strm->req_cap[idx][len] = '\0';
12178
12179 return 1;
12180}
12181
12182static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
12183{
12184 struct proxy *fe = strm_fe(smp->strm);
12185 int idx, i;
12186 struct cap_hdr *hdr;
12187 int len;
12188
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012189 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012190 return 0;
12191
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012192 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012193
12194 /* Check the availibity of the capture id. */
12195 if (idx > fe->nb_rsp_cap - 1)
12196 return 0;
12197
12198 /* Look for the original configuration. */
12199 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12200 hdr != NULL && i != idx ;
12201 i--, hdr = hdr->next);
12202 if (!hdr)
12203 return 0;
12204
12205 /* check for the memory allocation */
12206 if (smp->strm->res_cap[hdr->index] == NULL)
12207 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
12208 if (smp->strm->res_cap[hdr->index] == NULL)
12209 return 0;
12210
12211 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012212 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012213 if (len > hdr->len)
12214 len = hdr->len;
12215
12216 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012217 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012218 smp->strm->res_cap[idx][len] = '\0';
12219
12220 return 1;
12221}
12222
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012223/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012224 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012225 * the relevant part of the request line accordingly. Then it updates various
12226 * pointers to the next elements which were moved, and the total buffer length.
12227 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012228 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12229 * error, though this can be revisited when this code is finally exploited.
12230 *
12231 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12232 * query string and 3 to replace uri.
12233 *
12234 * In query string case, the mark question '?' must be set at the start of the
12235 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012236 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012237int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012238 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012239{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012240 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012241 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012242 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012243 int delta;
12244
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012245 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012246 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012247 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012248 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12249
12250 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012251 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012252 txn->req.sl.rq.m_l += delta;
12253 txn->req.sl.rq.u += delta;
12254 txn->req.sl.rq.v += delta;
12255 break;
12256
12257 case 1: // path
12258 cur_ptr = http_get_path(txn);
12259 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012260 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012261
12262 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012263 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 +010012264 cur_end++;
12265
12266 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012267 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012268 txn->req.sl.rq.u_l += delta;
12269 txn->req.sl.rq.v += delta;
12270 break;
12271
12272 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012273 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012274 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012275 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12276 while (cur_ptr < cur_end && *cur_ptr != '?')
12277 cur_ptr++;
12278
12279 /* skip the question mark or indicate that we must insert it
12280 * (but only if the format string is not empty then).
12281 */
12282 if (cur_ptr < cur_end)
12283 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012284 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012285 offset = 0;
12286
12287 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012288 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012289 txn->req.sl.rq.u_l += delta;
12290 txn->req.sl.rq.v += delta;
12291 break;
12292
12293 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012294 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012295 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12296
12297 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012298 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012299 txn->req.sl.rq.u_l += delta;
12300 txn->req.sl.rq.v += delta;
12301 break;
12302
12303 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012304 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012305 }
12306
12307 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012308 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012309 txn->req.sl.rq.l += delta;
12310 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012311 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012312 return 0;
12313}
12314
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012315/* This function replace the HTTP status code and the associated message. The
12316 * variable <status> contains the new status code. This function never fails.
12317 */
12318void http_set_status(unsigned int status, struct stream *s)
12319{
12320 struct http_txn *txn = s->txn;
12321 char *cur_ptr, *cur_end;
12322 int delta;
12323 char *res;
12324 int c_l;
12325 const char *msg;
12326 int msg_len;
12327
12328 chunk_reset(&trash);
12329
12330 res = ultoa_o(status, trash.str, trash.size);
12331 c_l = res - trash.str;
12332
12333 trash.str[c_l] = ' ';
12334 trash.len = c_l + 1;
12335
12336 msg = get_reason(status);
12337 msg_len = strlen(msg);
12338
12339 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
12340 trash.len += msg_len;
12341
12342 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
12343 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
12344
12345 /* commit changes and adjust message */
12346 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
12347
12348 /* adjust res line offsets and lengths */
12349 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
12350 txn->rsp.sl.st.c_l = c_l;
12351 txn->rsp.sl.st.r_l = msg_len;
12352
12353 delta = trash.len - (cur_end - cur_ptr);
12354 txn->rsp.sl.st.l += delta;
12355 txn->hdr_idx.v[0].len += delta;
12356 http_msg_move_end(&txn->rsp, delta);
12357}
12358
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012359/* This function executes one of the set-{method,path,query,uri} actions. It
12360 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012361 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012362 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012363 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12364 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012365 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012366enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012367 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012368{
12369 chunk_reset(&trash);
12370
12371 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012372 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012373 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012374 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012375
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012376 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012377 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012378}
12379
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012380/* This function is just a compliant action wrapper for "set-status". */
12381enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012382 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012383{
12384 http_set_status(rule->arg.status.code, s);
12385 return ACT_RET_CONT;
12386}
12387
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012388/* parse an http-request action among :
12389 * set-method
12390 * set-path
12391 * set-query
12392 * set-uri
12393 *
12394 * All of them accept a single argument of type string representing a log-format.
12395 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12396 * 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 +020012397 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012398 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012399enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12400 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012401{
12402 int cur_arg = *orig_arg;
12403
Thierry FOURNIER42148732015-09-02 17:17:33 +020012404 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012405
12406 switch (args[0][4]) {
12407 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012408 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012409 rule->action_ptr = http_action_set_req_line;
12410 break;
12411 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012412 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012413 rule->action_ptr = http_action_set_req_line;
12414 break;
12415 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012416 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012417 rule->action_ptr = http_action_set_req_line;
12418 break;
12419 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012420 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012421 rule->action_ptr = http_action_set_req_line;
12422 break;
12423 default:
12424 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012425 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012426 }
12427
12428 if (!*args[cur_arg] ||
12429 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12430 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012431 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012432 }
12433
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012434 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012435 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012436 parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012437 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
12438 proxy->conf.args.file, proxy->conf.args.line);
12439
12440 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012441 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012442}
12443
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012444/* parse set-status action:
12445 * This action accepts a single argument of type int representing
12446 * an http status code. It returns ACT_RET_PRS_OK on success,
12447 * ACT_RET_PRS_ERR on error.
12448 */
12449enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12450 struct act_rule *rule, char **err)
12451{
12452 char *error;
12453
Thierry FOURNIER42148732015-09-02 17:17:33 +020012454 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012455 rule->action_ptr = action_http_set_status;
12456
12457 /* Check if an argument is available */
12458 if (!*args[*orig_arg]) {
12459 memprintf(err, "expects exactly 1 argument <status>");
12460 return ACT_RET_PRS_ERR;
12461 }
12462
12463 /* convert status code as integer */
12464 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12465 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12466 memprintf(err, "expects an integer status code between 100 and 999");
12467 return ACT_RET_PRS_ERR;
12468 }
12469
12470 (*orig_arg)++;
12471 return ACT_RET_PRS_OK;
12472}
12473
Willy Tarreaua9083d02015-05-08 15:27:59 +020012474/* This function executes the "capture" action. It executes a fetch expression,
12475 * turns the result into a string and puts it in a capture slot. It always
12476 * returns 1. If an error occurs the action is cancelled, but the rule
12477 * processing continues.
12478 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012479enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012480 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012481{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012482 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012483 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012484 char **cap = s->req_cap;
12485 int len;
12486
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012487 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 +020012488 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012489 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012490
12491 if (cap[h->index] == NULL)
12492 cap[h->index] = pool_alloc2(h->pool);
12493
12494 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012495 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012496
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012497 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012498 if (len > h->len)
12499 len = h->len;
12500
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012501 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012502 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012503 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012504}
12505
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012506/* This function executes the "capture" action and store the result in a
12507 * capture slot if exists. It executes a fetch expression, turns the result
12508 * into a string and puts it in a capture slot. It always returns 1. If an
12509 * error occurs the action is cancelled, but the rule processing continues.
12510 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012511enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012512 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012513{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012514 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012515 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012516 char **cap = s->req_cap;
12517 struct proxy *fe = strm_fe(s);
12518 int len;
12519 int i;
12520
12521 /* Look for the original configuration. */
12522 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012523 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012524 i--, h = h->next);
12525 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012526 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012527
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012528 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 +020012529 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012530 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012531
12532 if (cap[h->index] == NULL)
12533 cap[h->index] = pool_alloc2(h->pool);
12534
12535 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012536 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012537
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012538 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012539 if (len > h->len)
12540 len = h->len;
12541
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012542 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012543 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012544 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012545}
12546
Willy Tarreaua9083d02015-05-08 15:27:59 +020012547/* parse an "http-request capture" action. It takes a single argument which is
12548 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012549 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012550 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012551 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012552enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12553 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012554{
12555 struct sample_expr *expr;
12556 struct cap_hdr *hdr;
12557 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012558 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012559
12560 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12561 if (strcmp(args[cur_arg], "if") == 0 ||
12562 strcmp(args[cur_arg], "unless") == 0)
12563 break;
12564
12565 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012566 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012567 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012568 }
12569
Willy Tarreaua9083d02015-05-08 15:27:59 +020012570 cur_arg = *orig_arg;
12571 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12572 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012573 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012574
12575 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12576 memprintf(err,
12577 "fetch method '%s' extracts information from '%s', none of which is available here",
12578 args[cur_arg-1], sample_src_names(expr->fetch->use));
12579 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012580 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012581 }
12582
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012583 if (!args[cur_arg] || !*args[cur_arg]) {
12584 memprintf(err, "expects 'len or 'id'");
12585 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012586 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012587 }
12588
Willy Tarreaua9083d02015-05-08 15:27:59 +020012589 if (strcmp(args[cur_arg], "len") == 0) {
12590 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012591
12592 if (!(px->cap & PR_CAP_FE)) {
12593 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012594 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012595 }
12596
12597 proxy->conf.args.ctx = ARGC_CAP;
12598
Willy Tarreaua9083d02015-05-08 15:27:59 +020012599 if (!args[cur_arg]) {
12600 memprintf(err, "missing length value");
12601 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012602 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012603 }
12604 /* we copy the table name for now, it will be resolved later */
12605 len = atoi(args[cur_arg]);
12606 if (len <= 0) {
12607 memprintf(err, "length must be > 0");
12608 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012609 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012610 }
12611 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012612
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012613 if (!len) {
12614 memprintf(err, "a positive 'len' argument is mandatory");
12615 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012616 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012617 }
12618
Vincent Bernat02779b62016-04-03 13:48:43 +020012619 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012620 hdr->next = px->req_cap;
12621 hdr->name = NULL; /* not a header capture */
12622 hdr->namelen = 0;
12623 hdr->len = len;
12624 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12625 hdr->index = px->nb_req_cap++;
12626
12627 px->req_cap = hdr;
12628 px->to_log |= LW_REQHDR;
12629
Thierry FOURNIER42148732015-09-02 17:17:33 +020012630 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012631 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012632 rule->arg.cap.expr = expr;
12633 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012634 }
12635
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012636 else if (strcmp(args[cur_arg], "id") == 0) {
12637 int id;
12638 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012639
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012640 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012641
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012642 if (!args[cur_arg]) {
12643 memprintf(err, "missing id value");
12644 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012645 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012646 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012647
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012648 id = strtol(args[cur_arg], &error, 10);
12649 if (*error != '\0') {
12650 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12651 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012652 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012653 }
12654 cur_arg++;
12655
12656 proxy->conf.args.ctx = ARGC_CAP;
12657
Thierry FOURNIER42148732015-09-02 17:17:33 +020012658 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012659 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012660 rule->arg.capid.expr = expr;
12661 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012662 }
12663
12664 else {
12665 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12666 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012667 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012668 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012669
12670 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012671 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012672}
12673
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012674/* This function executes the "capture" action and store the result in a
12675 * capture slot if exists. It executes a fetch expression, turns the result
12676 * into a string and puts it in a capture slot. It always returns 1. If an
12677 * error occurs the action is cancelled, but the rule processing continues.
12678 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012679enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012680 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012681{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012682 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012683 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012684 char **cap = s->res_cap;
12685 struct proxy *fe = strm_fe(s);
12686 int len;
12687 int i;
12688
12689 /* Look for the original configuration. */
12690 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012691 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012692 i--, h = h->next);
12693 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012694 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012695
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012696 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 +020012697 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012698 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012699
12700 if (cap[h->index] == NULL)
12701 cap[h->index] = pool_alloc2(h->pool);
12702
12703 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012704 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012705
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012706 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012707 if (len > h->len)
12708 len = h->len;
12709
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012710 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012711 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012712 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012713}
12714
12715/* parse an "http-response capture" action. It takes a single argument which is
12716 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12717 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012718 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012719 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012720enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12721 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012722{
12723 struct sample_expr *expr;
12724 int cur_arg;
12725 int id;
12726 char *error;
12727
12728 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12729 if (strcmp(args[cur_arg], "if") == 0 ||
12730 strcmp(args[cur_arg], "unless") == 0)
12731 break;
12732
12733 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012734 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012735 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012736 }
12737
12738 cur_arg = *orig_arg;
12739 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12740 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012741 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012742
12743 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12744 memprintf(err,
12745 "fetch method '%s' extracts information from '%s', none of which is available here",
12746 args[cur_arg-1], sample_src_names(expr->fetch->use));
12747 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012748 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012749 }
12750
12751 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012752 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012753 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012754 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012755 }
12756
12757 if (strcmp(args[cur_arg], "id") != 0) {
12758 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12759 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012760 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012761 }
12762
12763 cur_arg++;
12764
12765 if (!args[cur_arg]) {
12766 memprintf(err, "missing id value");
12767 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012768 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012769 }
12770
12771 id = strtol(args[cur_arg], &error, 10);
12772 if (*error != '\0') {
12773 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12774 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012775 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012776 }
12777 cur_arg++;
12778
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012779 proxy->conf.args.ctx = ARGC_CAP;
12780
Thierry FOURNIER42148732015-09-02 17:17:33 +020012781 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012782 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012783 rule->arg.capid.expr = expr;
12784 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012785
12786 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012787 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012788}
12789
William Lallemand73025dd2014-04-24 14:38:37 +020012790/*
12791 * Return the struct http_req_action_kw associated to a keyword.
12792 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012793struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012794{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012795 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012796}
12797
12798/*
12799 * Return the struct http_res_action_kw associated to a keyword.
12800 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012801struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012802{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012803 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012804}
12805
Willy Tarreau4a568972010-05-12 08:08:50 +020012806/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012807/* All supported ACL keywords must be declared here. */
12808/************************************************************************/
12809
12810/* Note: must not be declared <const> as its list will be overwritten.
12811 * Please take care of keeping this list alphabetically sorted.
12812 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012813static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012814 { "base", "base", PAT_MATCH_STR },
12815 { "base_beg", "base", PAT_MATCH_BEG },
12816 { "base_dir", "base", PAT_MATCH_DIR },
12817 { "base_dom", "base", PAT_MATCH_DOM },
12818 { "base_end", "base", PAT_MATCH_END },
12819 { "base_len", "base", PAT_MATCH_LEN },
12820 { "base_reg", "base", PAT_MATCH_REG },
12821 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012822
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012823 { "cook", "req.cook", PAT_MATCH_STR },
12824 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12825 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12826 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12827 { "cook_end", "req.cook", PAT_MATCH_END },
12828 { "cook_len", "req.cook", PAT_MATCH_LEN },
12829 { "cook_reg", "req.cook", PAT_MATCH_REG },
12830 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012831
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012832 { "hdr", "req.hdr", PAT_MATCH_STR },
12833 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12834 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12835 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12836 { "hdr_end", "req.hdr", PAT_MATCH_END },
12837 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12838 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12839 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012840
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012841 /* these two declarations uses strings with list storage (in place
12842 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12843 * and delete functions are relative to the list management. The parse
12844 * and match method are related to the corresponding fetch methods. This
12845 * is very particular ACL declaration mode.
12846 */
12847 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12848 { "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 +020012849
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012850 { "path", "path", PAT_MATCH_STR },
12851 { "path_beg", "path", PAT_MATCH_BEG },
12852 { "path_dir", "path", PAT_MATCH_DIR },
12853 { "path_dom", "path", PAT_MATCH_DOM },
12854 { "path_end", "path", PAT_MATCH_END },
12855 { "path_len", "path", PAT_MATCH_LEN },
12856 { "path_reg", "path", PAT_MATCH_REG },
12857 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012858
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012859 { "req_ver", "req.ver", PAT_MATCH_STR },
12860 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012861
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012862 { "scook", "res.cook", PAT_MATCH_STR },
12863 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12864 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12865 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12866 { "scook_end", "res.cook", PAT_MATCH_END },
12867 { "scook_len", "res.cook", PAT_MATCH_LEN },
12868 { "scook_reg", "res.cook", PAT_MATCH_REG },
12869 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012870
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012871 { "shdr", "res.hdr", PAT_MATCH_STR },
12872 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12873 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12874 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12875 { "shdr_end", "res.hdr", PAT_MATCH_END },
12876 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12877 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12878 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012879
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012880 { "url", "url", PAT_MATCH_STR },
12881 { "url_beg", "url", PAT_MATCH_BEG },
12882 { "url_dir", "url", PAT_MATCH_DIR },
12883 { "url_dom", "url", PAT_MATCH_DOM },
12884 { "url_end", "url", PAT_MATCH_END },
12885 { "url_len", "url", PAT_MATCH_LEN },
12886 { "url_reg", "url", PAT_MATCH_REG },
12887 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012888
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012889 { "urlp", "urlp", PAT_MATCH_STR },
12890 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12891 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12892 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12893 { "urlp_end", "urlp", PAT_MATCH_END },
12894 { "urlp_len", "urlp", PAT_MATCH_LEN },
12895 { "urlp_reg", "urlp", PAT_MATCH_REG },
12896 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012897
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012898 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012899}};
12900
12901/************************************************************************/
12902/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012903/************************************************************************/
12904/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012905static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012906 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012907 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012908 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12909
Willy Tarreau87b09662015-04-03 00:22:06 +020012910 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012911 { "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 +020012912
12913 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012914 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12915 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12916 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012917
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012918 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12919 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012920
Willy Tarreau409bcde2013-01-08 00:31:00 +010012921 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12922 * are only here to match the ACL's name, are request-only and are used
12923 * for ACL compatibility only.
12924 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012925 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12926 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012927 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12928 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012929
12930 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12931 * only here to match the ACL's name, are request-only and are used for
12932 * ACL compatibility only.
12933 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012934 { "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 +020012935 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012936 { "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 +020012937 { "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 +010012938
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012939 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012940 { "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 +010012941 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012942 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012943 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012944 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012945
12946 /* HTTP protocol on the request path */
12947 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012948 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012949
12950 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012951 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12952 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012953
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012954 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012955 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12956 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012957 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012958
Willy Tarreau18ed2562013-01-14 15:56:36 +010012959 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012960 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12961 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012962
Willy Tarreau18ed2562013-01-14 15:56:36 +010012963 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012964 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012965 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12966 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012967
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012968 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012969 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012970 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012971 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012972 { "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 +010012973 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012974 { "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 +010012975
12976 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012977 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012978 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12979 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012980
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012981 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012982 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012983 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012984 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012985 { "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 +010012986 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012987 { "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 +010012988
Willy Tarreau409bcde2013-01-08 00:31:00 +010012989 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012990 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012991 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12992 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012993 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012994
12995 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012996 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012997 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012998 { "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 +020012999 { "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 +010013000
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013001 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020013002 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013003 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013004 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000013005 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013006 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013007 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020013008 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13009 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013010 { "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 +010013011 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020013012}};
13013
Willy Tarreau8797c062007-05-07 00:55:35 +020013014
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013015/************************************************************************/
13016/* All supported converter keywords must be declared here. */
13017/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020013018/* Note: must not be declared <const> as its list will be overwritten */
13019static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013020 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020013021 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013022 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
13023 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020013024 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020013025 { NULL, NULL, 0, 0, 0 },
13026}};
13027
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020013028
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013029/************************************************************************/
13030/* All supported http-request action keywords must be declared here. */
13031/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013032struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013033 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020013034 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013035 { "set-method", parse_set_req_line },
13036 { "set-path", parse_set_req_line },
13037 { "set-query", parse_set_req_line },
13038 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020013039 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013040 }
13041};
13042
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013043struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013044 .kw = {
13045 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020013046 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013047 { NULL, NULL }
13048 }
13049};
13050
Willy Tarreau8797c062007-05-07 00:55:35 +020013051__attribute__((constructor))
13052static void __http_protocol_init(void)
13053{
13054 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020013055 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020013056 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013057 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013058 http_res_keywords_register(&http_res_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020013059}
13060
13061
Willy Tarreau58f10d72006-12-04 02:26:12 +010013062/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020013063 * Local variables:
13064 * c-indent-level: 8
13065 * c-basic-offset: 8
13066 * End:
13067 */