blob: b25d18a2f0323d13b2e7bb9ddd6a89fa5157b8e4 [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
Christopher Fauletdbe34eb2015-12-02 10:01:17 +0100278static int http_msg_forward_body(struct stream *s, struct http_msg *msg);
279static int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg);
280
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 Tarreau8d5d7f22007-01-21 19:16:41 +0100494 * RFC2616 for those chars.
495 */
496
497const char http_is_spht[256] = {
498 [' '] = 1, ['\t'] = 1,
499};
500
501const char http_is_crlf[256] = {
502 ['\r'] = 1, ['\n'] = 1,
503};
504
505const char http_is_lws[256] = {
506 [' '] = 1, ['\t'] = 1,
507 ['\r'] = 1, ['\n'] = 1,
508};
509
510const char http_is_sep[256] = {
511 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
512 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
513 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
514 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
515 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
516};
517
518const char http_is_ctl[256] = {
519 [0 ... 31] = 1,
520 [127] = 1,
521};
522
523/*
524 * A token is any ASCII char that is neither a separator nor a CTL char.
525 * Do not overwrite values in assignment since gcc-2.95 will not handle
526 * them correctly. Instead, define every non-CTL char's status.
527 */
528const char http_is_token[256] = {
529 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
530 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
531 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
532 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
533 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
534 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
535 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
536 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
537 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
538 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
539 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
540 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
541 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
542 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
543 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
544 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
545 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
546 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
547 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
548 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
549 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
550 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
551 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
552 ['|'] = 1, ['}'] = 0, ['~'] = 1,
553};
554
555
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100556/*
557 * An http ver_token is any ASCII which can be found in an HTTP version,
558 * which includes 'H', 'T', 'P', '/', '.' and any digit.
559 */
560const char http_is_ver_token[256] = {
561 ['.'] = 1, ['/'] = 1,
562 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
563 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
Thierry FOURNIER63d692c2015-02-28 19:03:56 +0100564 ['H'] = 1, ['P'] = 1, ['R'] = 1, ['S'] = 1, ['T'] = 1,
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100565};
566
567
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100568/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100569 * Adds a header and its CRLF at the tail of the message's buffer, just before
570 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100571 * The header is also automatically added to the index <hdr_idx>, and the end
572 * of headers is automatically adjusted. The number of bytes added is returned
573 * on success, otherwise <0 is returned indicating an error.
574 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100575int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100576{
577 int bytes, len;
578
579 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200580 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100581 if (!bytes)
582 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100583 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100584 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
585}
586
587/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100588 * Adds a header and its CRLF at the tail of the message's buffer, just before
589 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100590 * the buffer is only opened and the space reserved, but nothing is copied.
591 * The header is also automatically added to the index <hdr_idx>, and the end
592 * of headers is automatically adjusted. The number of bytes added is returned
593 * on success, otherwise <0 is returned indicating an error.
594 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100595int http_header_add_tail2(struct http_msg *msg,
596 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100597{
598 int bytes;
599
Willy Tarreau9b28e032012-10-12 23:49:43 +0200600 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100601 if (!bytes)
602 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100603 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100604 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
605}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200606
607/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100608 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
609 * If so, returns the position of the first non-space character relative to
610 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
611 * to return a pointer to the place after the first space. Returns 0 if the
612 * header name does not match. Checks are case-insensitive.
613 */
614int http_header_match2(const char *hdr, const char *end,
615 const char *name, int len)
616{
617 const char *val;
618
619 if (hdr + len >= end)
620 return 0;
621 if (hdr[len] != ':')
622 return 0;
623 if (strncasecmp(hdr, name, len) != 0)
624 return 0;
625 val = hdr + len + 1;
626 while (val < end && HTTP_IS_SPHT(*val))
627 val++;
628 if ((val >= end) && (len + 2 <= end - hdr))
629 return len + 2; /* we may replace starting from second space */
630 return val - hdr;
631}
632
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200633/* Find the first or next occurrence of header <name> in message buffer <sol>
634 * using headers index <idx>, and return it in the <ctx> structure. This
635 * structure holds everything necessary to use the header and find next
636 * occurrence. If its <idx> member is 0, the header is searched from the
637 * beginning. Otherwise, the next occurrence is returned. The function returns
638 * 1 when it finds a value, and 0 when there is no more. It is very similar to
639 * http_find_header2() except that it is designed to work with full-line headers
640 * whose comma is not a delimiter but is part of the syntax. As a special case,
641 * if ctx->val is NULL when searching for a new values of a header, the current
642 * header is rescanned. This allows rescanning after a header deletion.
643 */
644int http_find_full_header2(const char *name, int len,
645 char *sol, struct hdr_idx *idx,
646 struct hdr_ctx *ctx)
647{
648 char *eol, *sov;
649 int cur_idx, old_idx;
650
651 cur_idx = ctx->idx;
652 if (cur_idx) {
653 /* We have previously returned a header, let's search another one */
654 sol = ctx->line;
655 eol = sol + idx->v[cur_idx].len;
656 goto next_hdr;
657 }
658
659 /* first request for this header */
660 sol += hdr_idx_first_pos(idx);
661 old_idx = 0;
662 cur_idx = hdr_idx_first_idx(idx);
663 while (cur_idx) {
664 eol = sol + idx->v[cur_idx].len;
665
666 if (len == 0) {
667 /* No argument was passed, we want any header.
668 * To achieve this, we simply build a fake request. */
669 while (sol + len < eol && sol[len] != ':')
670 len++;
671 name = sol;
672 }
673
674 if ((len < eol - sol) &&
675 (sol[len] == ':') &&
676 (strncasecmp(sol, name, len) == 0)) {
677 ctx->del = len;
678 sov = sol + len + 1;
679 while (sov < eol && http_is_lws[(unsigned char)*sov])
680 sov++;
681
682 ctx->line = sol;
683 ctx->prev = old_idx;
684 ctx->idx = cur_idx;
685 ctx->val = sov - sol;
686 ctx->tws = 0;
687 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
688 eol--;
689 ctx->tws++;
690 }
691 ctx->vlen = eol - sov;
692 return 1;
693 }
694 next_hdr:
695 sol = eol + idx->v[cur_idx].cr + 1;
696 old_idx = cur_idx;
697 cur_idx = idx->v[cur_idx].next;
698 }
699 return 0;
700}
701
Willy Tarreauc90dc232015-02-20 13:51:36 +0100702/* Find the first or next header field in message buffer <sol> using headers
703 * index <idx>, and return it in the <ctx> structure. This structure holds
704 * everything necessary to use the header and find next occurrence. If its
705 * <idx> member is 0, the first header is retrieved. Otherwise, the next
706 * occurrence is returned. The function returns 1 when it finds a value, and
707 * 0 when there is no more. It is equivalent to http_find_full_header2() with
708 * no header name.
709 */
710int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
711{
712 char *eol, *sov;
713 int cur_idx, old_idx;
714 int len;
715
716 cur_idx = ctx->idx;
717 if (cur_idx) {
718 /* We have previously returned a header, let's search another one */
719 sol = ctx->line;
720 eol = sol + idx->v[cur_idx].len;
721 goto next_hdr;
722 }
723
724 /* first request for this header */
725 sol += hdr_idx_first_pos(idx);
726 old_idx = 0;
727 cur_idx = hdr_idx_first_idx(idx);
728 while (cur_idx) {
729 eol = sol + idx->v[cur_idx].len;
730
731 len = 0;
732 while (1) {
733 if (len >= eol - sol)
734 goto next_hdr;
735 if (sol[len] == ':')
736 break;
737 len++;
738 }
739
740 ctx->del = len;
741 sov = sol + len + 1;
742 while (sov < eol && http_is_lws[(unsigned char)*sov])
743 sov++;
744
745 ctx->line = sol;
746 ctx->prev = old_idx;
747 ctx->idx = cur_idx;
748 ctx->val = sov - sol;
749 ctx->tws = 0;
750
751 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
752 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 Tarreau68085d82010-01-18 14:54:04 +0100766/* Find the end of the header value contained between <s> and <e>. See RFC2616,
767 * par 2.2 for more information. Note that it requires a valid header to return
768 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200769 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100770char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200771{
772 int quoted, qdpair;
773
774 quoted = qdpair = 0;
775 for (; s < e; s++) {
776 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200777 else if (quoted) {
778 if (*s == '\\') qdpair = 1;
779 else if (*s == '"') quoted = 0;
780 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200781 else if (*s == '"') quoted = 1;
782 else if (*s == ',') return s;
783 }
784 return s;
785}
786
787/* Find the first or next occurrence of header <name> in message buffer <sol>
788 * using headers index <idx>, and return it in the <ctx> structure. This
789 * structure holds everything necessary to use the header and find next
790 * occurrence. If its <idx> member is 0, the header is searched from the
791 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100792 * 1 when it finds a value, and 0 when there is no more. It is designed to work
793 * with headers defined as comma-separated lists. As a special case, if ctx->val
794 * is NULL when searching for a new values of a header, the current header is
795 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200796 */
797int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100798 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200799 struct hdr_ctx *ctx)
800{
Willy Tarreau68085d82010-01-18 14:54:04 +0100801 char *eol, *sov;
802 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200803
Willy Tarreau68085d82010-01-18 14:54:04 +0100804 cur_idx = ctx->idx;
805 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200806 /* We have previously returned a value, let's search
807 * another one on the same line.
808 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200809 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200810 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100811 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200812 eol = sol + idx->v[cur_idx].len;
813
814 if (sov >= eol)
815 /* no more values in this header */
816 goto next_hdr;
817
Willy Tarreau68085d82010-01-18 14:54:04 +0100818 /* values remaining for this header, skip the comma but save it
819 * for later use (eg: for header deletion).
820 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200821 sov++;
822 while (sov < eol && http_is_lws[(unsigned char)*sov])
823 sov++;
824
825 goto return_hdr;
826 }
827
828 /* first request for this header */
829 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100830 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200831 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200832 while (cur_idx) {
833 eol = sol + idx->v[cur_idx].len;
834
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200835 if (len == 0) {
836 /* No argument was passed, we want any header.
837 * To achieve this, we simply build a fake request. */
838 while (sol + len < eol && sol[len] != ':')
839 len++;
840 name = sol;
841 }
842
Willy Tarreau33a7e692007-06-10 19:45:56 +0200843 if ((len < eol - sol) &&
844 (sol[len] == ':') &&
845 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100846 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200847 sov = sol + len + 1;
848 while (sov < eol && http_is_lws[(unsigned char)*sov])
849 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100850
Willy Tarreau33a7e692007-06-10 19:45:56 +0200851 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100852 ctx->prev = old_idx;
853 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200854 ctx->idx = cur_idx;
855 ctx->val = sov - sol;
856
857 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200858 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200859 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200860 eol--;
861 ctx->tws++;
862 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200863 ctx->vlen = eol - sov;
864 return 1;
865 }
866 next_hdr:
867 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100868 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200869 cur_idx = idx->v[cur_idx].next;
870 }
871 return 0;
872}
873
874int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100875 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200876 struct hdr_ctx *ctx)
877{
878 return http_find_header2(name, strlen(name), sol, idx, ctx);
879}
880
Willy Tarreau68085d82010-01-18 14:54:04 +0100881/* Remove one value of a header. This only works on a <ctx> returned by one of
882 * the http_find_header functions. The value is removed, as well as surrounding
883 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100884 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100885 * message <msg>. The new index is returned. If it is zero, it means there is
886 * no more header, so any processing may stop. The ctx is always left in a form
887 * that can be handled by http_find_header2() to find next occurrence.
888 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100889int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100890{
891 int cur_idx = ctx->idx;
892 char *sol = ctx->line;
893 struct hdr_idx_elem *hdr;
894 int delta, skip_comma;
895
896 if (!cur_idx)
897 return 0;
898
899 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200900 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100901 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200902 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100903 http_msg_move_end(msg, delta);
904 idx->used--;
905 hdr->len = 0; /* unused entry */
906 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100907 if (idx->tail == ctx->idx)
908 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100909 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100910 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100911 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200912 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100913 return ctx->idx;
914 }
915
916 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200917 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
918 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100919 */
920
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200921 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200922 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200923 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100924 NULL, 0);
925 hdr->len += delta;
926 http_msg_move_end(msg, delta);
927 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200928 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100929 return ctx->idx;
930}
931
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100932/* This function handles a server error at the stream interface level. The
933 * stream interface is assumed to be already in a closed state. An optional
934 * message is copied into the input buffer, and an HTTP status code stored.
935 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100936 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200937 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200938static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100939 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200940{
Christopher Faulet3e344292015-11-24 16:24:13 +0100941 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100942 channel_auto_read(si_oc(si));
943 channel_abort(si_oc(si));
944 channel_auto_close(si_oc(si));
945 channel_erase(si_oc(si));
946 channel_auto_close(si_ic(si));
947 channel_auto_read(si_ic(si));
Willy Tarreau0f772532006-12-23 20:51:41 +0100948 if (status > 0 && msg) {
Willy Tarreaueee5b512015-04-03 23:46:31 +0200949 s->txn->status = status;
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100950 bo_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200951 }
Willy Tarreaue7dff022015-04-03 01:14:29 +0200952 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200953 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200954 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200955 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200956}
957
Willy Tarreau87b09662015-04-03 00:22:06 +0200958/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100959 * and message.
960 */
961
Willy Tarreau87b09662015-04-03 00:22:06 +0200962struct chunk *http_error_message(struct stream *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100963{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200964 if (s->be->errmsg[msgnum].str)
965 return &s->be->errmsg[msgnum];
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200966 else if (strm_fe(s)->errmsg[msgnum].str)
967 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100968 else
969 return &http_err_chunks[msgnum];
970}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200971
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100972void
973http_reply_and_close(struct stream *s, short status, struct chunk *msg)
974{
Christopher Fauletd7c91962015-04-30 11:48:27 +0200975 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +0100976 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100977 stream_int_retnclose(&s->si[0], msg);
978}
979
Willy Tarreau53b6c742006-12-17 13:37:46 +0100980/*
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200981 * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown
982 * ones.
Willy Tarreau53b6c742006-12-17 13:37:46 +0100983 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100984enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100985{
986 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100987 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100988
989 m = ((unsigned)*str - 'A');
990
991 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100992 for (h = http_methods[m]; h->len > 0; h++) {
993 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100994 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100995 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100996 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100997 };
Willy Tarreau53b6c742006-12-17 13:37:46 +0100998 }
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200999 return HTTP_METH_OTHER;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001000}
1001
Willy Tarreau21d2af32008-02-14 20:25:24 +01001002/* Parse the URI from the given transaction (which is assumed to be in request
1003 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
1004 * It is returned otherwise.
1005 */
Thierry FOURNIER3c331782015-09-17 19:33:35 +02001006char *http_get_path(struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +01001007{
1008 char *ptr, *end;
1009
Willy Tarreau9b28e032012-10-12 23:49:43 +02001010 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +01001011 end = ptr + txn->req.sl.rq.u_l;
1012
1013 if (ptr >= end)
1014 return NULL;
1015
1016 /* RFC2616, par. 5.1.2 :
1017 * Request-URI = "*" | absuri | abspath | authority
1018 */
1019
1020 if (*ptr == '*')
1021 return NULL;
1022
1023 if (isalpha((unsigned char)*ptr)) {
1024 /* this is a scheme as described by RFC3986, par. 3.1 */
1025 ptr++;
1026 while (ptr < end &&
1027 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
1028 ptr++;
1029 /* skip '://' */
1030 if (ptr == end || *ptr++ != ':')
1031 return NULL;
1032 if (ptr == end || *ptr++ != '/')
1033 return NULL;
1034 if (ptr == end || *ptr++ != '/')
1035 return NULL;
1036 }
1037 /* skip [user[:passwd]@]host[:[port]] */
1038
1039 while (ptr < end && *ptr != '/')
1040 ptr++;
1041
1042 if (ptr == end)
1043 return NULL;
1044
1045 /* OK, we got the '/' ! */
1046 return ptr;
1047}
1048
William Lallemand65ad6e12014-01-31 15:08:02 +01001049/* Parse the URI from the given string and look for the "/" beginning the PATH.
1050 * If not found, return NULL. It is returned otherwise.
1051 */
1052static char *
1053http_get_path_from_string(char *str)
1054{
1055 char *ptr = str;
1056
1057 /* RFC2616, par. 5.1.2 :
1058 * Request-URI = "*" | absuri | abspath | authority
1059 */
1060
1061 if (*ptr == '*')
1062 return NULL;
1063
1064 if (isalpha((unsigned char)*ptr)) {
1065 /* this is a scheme as described by RFC3986, par. 3.1 */
1066 ptr++;
1067 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
1068 ptr++;
1069 /* skip '://' */
1070 if (*ptr == '\0' || *ptr++ != ':')
1071 return NULL;
1072 if (*ptr == '\0' || *ptr++ != '/')
1073 return NULL;
1074 if (*ptr == '\0' || *ptr++ != '/')
1075 return NULL;
1076 }
1077 /* skip [user[:passwd]@]host[:[port]] */
1078
1079 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
1080 ptr++;
1081
1082 if (*ptr == '\0' || *ptr == ' ')
1083 return NULL;
1084
1085 /* OK, we got the '/' ! */
1086 return ptr;
1087}
1088
Willy Tarreau71241ab2012-12-27 11:30:54 +01001089/* Returns a 302 for a redirectable request that reaches a server working in
1090 * in redirect mode. This may only be called just after the stream interface
1091 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
1092 * follow normal proxy processing. NOTE: this function is designed to support
1093 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001094 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001095void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001096{
1097 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001098 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001099 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001100 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001101
1102 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001103 trash.len = strlen(HTTP_302);
1104 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001105
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001106 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001107
Willy Tarreauefb453c2008-10-26 20:49:47 +01001108 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001109 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001110 return;
1111
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001112 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001113 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001114 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
1115 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001116 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001117
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001118 /* 3: add the request URI. Since it was already forwarded, we need
1119 * to temporarily rewind the buffer.
1120 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001121 txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001122 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001123
Willy Tarreauefb453c2008-10-26 20:49:47 +01001124 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001125 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 +02001126
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001127 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001128
Willy Tarreauefb453c2008-10-26 20:49:47 +01001129 if (!path)
1130 return;
1131
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001132 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001133 return;
1134
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001135 memcpy(trash.str + trash.len, path, len);
1136 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001137
1138 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001139 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1140 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001141 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001142 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1143 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001144 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001145
1146 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001147 si_shutr(si);
1148 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001149 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001150 si->state = SI_ST_CLO;
1151
1152 /* send the message */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001153 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001154
1155 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001156 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001157 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001158}
1159
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001160/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001161 * that the server side is closed. Note that err_type is actually a
1162 * bitmask, where almost only aborts may be cumulated with other
1163 * values. We consider that aborted operations are more important
1164 * than timeouts or errors due to the fact that nobody else in the
1165 * logs might explain incomplete retries. All others should avoid
1166 * being cumulated. It should normally not be possible to have multiple
1167 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001168 * Note that connection errors appearing on the second request of a keep-alive
1169 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001170 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001171void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001172{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001173 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001174
1175 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001176 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001177 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001178 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001179 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001180 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001181 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001182 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001183 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001184 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001185 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001186 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001187 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001188 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001189 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001190 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001191 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001192 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001193 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
1194 503, (s->flags & SF_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001195 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001196 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001197 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001198 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001199 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001200 else /* SI_ET_CONN_OTHER and others */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001201 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001202 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001203}
1204
Willy Tarreau42250582007-04-01 01:30:43 +02001205extern const char sess_term_cond[8];
1206extern const char sess_fin_state[8];
1207extern const char *monthname[12];
Willy Tarreau63986c72015-04-03 22:55:33 +02001208struct pool_head *pool2_http_txn;
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001209struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001210struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001211struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001212
Willy Tarreau117f59e2007-03-04 18:17:17 +01001213/*
1214 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001215 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001216 */
1217void capture_headers(char *som, struct hdr_idx *idx,
1218 char **cap, struct cap_hdr *cap_hdr)
1219{
1220 char *eol, *sol, *col, *sov;
1221 int cur_idx;
1222 struct cap_hdr *h;
1223 int len;
1224
1225 sol = som + hdr_idx_first_pos(idx);
1226 cur_idx = hdr_idx_first_idx(idx);
1227
1228 while (cur_idx) {
1229 eol = sol + idx->v[cur_idx].len;
1230
1231 col = sol;
1232 while (col < eol && *col != ':')
1233 col++;
1234
1235 sov = col + 1;
1236 while (sov < eol && http_is_lws[(unsigned char)*sov])
1237 sov++;
1238
1239 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001240 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001241 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1242 if (cap[h->index] == NULL)
1243 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001244 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001245
1246 if (cap[h->index] == NULL) {
1247 Alert("HTTP capture : out of memory.\n");
1248 continue;
1249 }
1250
1251 len = eol - sov;
1252 if (len > h->len)
1253 len = h->len;
1254
1255 memcpy(cap[h->index], sov, len);
1256 cap[h->index][len]=0;
1257 }
1258 }
1259 sol = eol + idx->v[cur_idx].cr + 1;
1260 cur_idx = idx->v[cur_idx].next;
1261 }
1262}
1263
1264
Willy Tarreau42250582007-04-01 01:30:43 +02001265/* either we find an LF at <ptr> or we jump to <bad>.
1266 */
1267#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1268
1269/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1270 * otherwise to <http_msg_ood> with <state> set to <st>.
1271 */
1272#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1273 ptr++; \
1274 if (likely(ptr < end)) \
1275 goto good; \
1276 else { \
1277 state = (st); \
1278 goto http_msg_ood; \
1279 } \
1280 } while (0)
1281
1282
Willy Tarreaubaaee002006-06-26 02:48:02 +02001283/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001284 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001285 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1286 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1287 * will give undefined results.
1288 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1289 * and that msg->sol points to the beginning of the response.
1290 * If a complete line is found (which implies that at least one CR or LF is
1291 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1292 * returned indicating an incomplete line (which does not mean that parts have
1293 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1294 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1295 * upon next call.
1296 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001297 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001298 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1299 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001300 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001301 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001302const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001303 enum ht_state state, const char *ptr, const char *end,
1304 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001305{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001306 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001307
Willy Tarreau8973c702007-01-21 23:58:29 +01001308 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001309 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001310 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001311 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001312 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1313
1314 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001315 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001316 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1317 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001318 state = HTTP_MSG_ERROR;
1319 break;
1320
Willy Tarreau8973c702007-01-21 23:58:29 +01001321 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001322 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001323 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001324 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001325 goto http_msg_rpcode;
1326 }
1327 if (likely(HTTP_IS_SPHT(*ptr)))
1328 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1329 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001330 state = HTTP_MSG_ERROR;
1331 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001332
Willy Tarreau8973c702007-01-21 23:58:29 +01001333 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001334 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001335 if (likely(!HTTP_IS_LWS(*ptr)))
1336 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1337
1338 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001339 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001340 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1341 }
1342
1343 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001344 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001345 http_msg_rsp_reason:
1346 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001347 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001348 msg->sl.st.r_l = 0;
1349 goto http_msg_rpline_eol;
1350
Willy Tarreau8973c702007-01-21 23:58:29 +01001351 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001352 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001353 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001354 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001355 goto http_msg_rpreason;
1356 }
1357 if (likely(HTTP_IS_SPHT(*ptr)))
1358 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1359 /* so it's a CR/LF, so there is no reason phrase */
1360 goto http_msg_rsp_reason;
1361
Willy Tarreau8973c702007-01-21 23:58:29 +01001362 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001363 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001364 if (likely(!HTTP_IS_CRLF(*ptr)))
1365 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001366 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001367 http_msg_rpline_eol:
1368 /* We have seen the end of line. Note that we do not
1369 * necessarily have the \n yet, but at least we know that we
1370 * have EITHER \r OR \n, otherwise the response would not be
1371 * complete. We can then record the response length and return
1372 * to the caller which will be able to register it.
1373 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001374 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001375 return ptr;
1376
Willy Tarreau8973c702007-01-21 23:58:29 +01001377 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001378#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001379 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1380 exit(1);
1381#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001382 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001383 }
1384
1385 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001386 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001387 if (ret_state)
1388 *ret_state = state;
1389 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001390 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001391 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001392}
1393
Willy Tarreau8973c702007-01-21 23:58:29 +01001394/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001395 * This function parses a request line between <ptr> and <end>, starting with
1396 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1397 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1398 * will give undefined results.
1399 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1400 * and that msg->sol points to the beginning of the request.
1401 * If a complete line is found (which implies that at least one CR or LF is
1402 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1403 * returned indicating an incomplete line (which does not mean that parts have
1404 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1405 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1406 * upon next call.
1407 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001408 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001409 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1410 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001411 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001412 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001413const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001414 enum ht_state state, const char *ptr, const char *end,
1415 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001416{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001417 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001418
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001419 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001421 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001422 if (likely(HTTP_IS_TOKEN(*ptr)))
1423 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001424
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001425 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001426 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001427 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1428 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001429
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001430 if (likely(HTTP_IS_CRLF(*ptr))) {
1431 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001432 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001433 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001434 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001435 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001436 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001437 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001438 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001439 msg->sl.rq.v_l = 0;
1440 goto http_msg_rqline_eol;
1441 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001442 state = HTTP_MSG_ERROR;
1443 break;
1444
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001445 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001446 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001447 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001448 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001449 goto http_msg_rquri;
1450 }
1451 if (likely(HTTP_IS_SPHT(*ptr)))
1452 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1453 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1454 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001455
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001456 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001457 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001458 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001459 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001460
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001461 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001462 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001463 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1464 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001465
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001466 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001467 /* non-ASCII chars are forbidden unless option
1468 * accept-invalid-http-request is enabled in the frontend.
1469 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001470 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001471 if (msg->err_pos < -1)
1472 goto invalid_char;
1473 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001474 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001475 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1476 }
1477
1478 if (likely(HTTP_IS_CRLF(*ptr))) {
1479 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1480 goto http_msg_req09_uri_e;
1481 }
1482
1483 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001484 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001485 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001486 state = HTTP_MSG_ERROR;
1487 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001488
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001489 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001490 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001492 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001493 goto http_msg_rqver;
1494 }
1495 if (likely(HTTP_IS_SPHT(*ptr)))
1496 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1497 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1498 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001499
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001500 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001501 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001502 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001503 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001504
1505 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001506 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001507 http_msg_rqline_eol:
1508 /* We have seen the end of line. Note that we do not
1509 * necessarily have the \n yet, but at least we know that we
1510 * have EITHER \r OR \n, otherwise the request would not be
1511 * complete. We can then record the request length and return
1512 * to the caller which will be able to register it.
1513 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001514 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001515 return ptr;
1516 }
1517
1518 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001519 state = HTTP_MSG_ERROR;
1520 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001521
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001522 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001523#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1525 exit(1);
1526#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001527 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001528 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001529
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001530 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001531 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001532 if (ret_state)
1533 *ret_state = state;
1534 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001535 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001536 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001538
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001539/*
1540 * Returns the data from Authorization header. Function may be called more
1541 * than once so data is stored in txn->auth_data. When no header is found
1542 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001543 * searching again for something we are unable to find anyway. However, if
1544 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001545 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001546 */
1547
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001548/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1549 * set according to global.tune.bufsize.
1550 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001551char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001552
1553int
Willy Tarreau87b09662015-04-03 00:22:06 +02001554get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001555{
1556
Willy Tarreaueee5b512015-04-03 23:46:31 +02001557 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001558 struct chunk auth_method;
1559 struct hdr_ctx ctx;
1560 char *h, *p;
1561 int len;
1562
1563#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001564 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001565#endif
1566
1567 if (txn->auth.method == HTTP_AUTH_WRONG)
1568 return 0;
1569
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001570 txn->auth.method = HTTP_AUTH_WRONG;
1571
1572 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001573
1574 if (txn->flags & TX_USE_PX_CONN) {
1575 h = "Proxy-Authorization";
1576 len = strlen(h);
1577 } else {
1578 h = "Authorization";
1579 len = strlen(h);
1580 }
1581
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001582 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001583 return 0;
1584
1585 h = ctx.line + ctx.val;
1586
1587 p = memchr(h, ' ', ctx.vlen);
1588 if (!p || p == h)
1589 return 0;
1590
1591 chunk_initlen(&auth_method, h, 0, p-h);
1592 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1593
1594 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1595
1596 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001597 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001598
1599 if (len < 0)
1600 return 0;
1601
1602
1603 get_http_auth_buff[len] = '\0';
1604
1605 p = strchr(get_http_auth_buff, ':');
1606
1607 if (!p)
1608 return 0;
1609
1610 txn->auth.user = get_http_auth_buff;
1611 *p = '\0';
1612 txn->auth.pass = p+1;
1613
1614 txn->auth.method = HTTP_AUTH_BASIC;
1615 return 1;
1616 }
1617
1618 return 0;
1619}
1620
Willy Tarreau58f10d72006-12-04 02:26:12 +01001621
Willy Tarreau8973c702007-01-21 23:58:29 +01001622/*
1623 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001624 * depending on the initial msg->msg_state. The caller is responsible for
1625 * ensuring that the message does not wrap. The function can be preempted
1626 * everywhere when data are missing and recalled at the exact same location
1627 * with no information loss. The message may even be realigned between two
1628 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001629 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001630 * fields. Note that msg->sol will be initialized after completing the first
1631 * state, so that none of the msg pointers has to be initialized prior to the
1632 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001633 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001634void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001635{
Willy Tarreau3770f232013-12-07 00:01:53 +01001636 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001637 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001638 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001639
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001640 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001641 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001642 ptr = buf->p + msg->next;
1643 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001644
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001645 if (unlikely(ptr >= end))
1646 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001647
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001648 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001649 /*
1650 * First, states that are specific to the response only.
1651 * We check them first so that request and headers are
1652 * closer to each other (accessed more often).
1653 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001654 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001655 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001656 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001657 /* we have a start of message, but we have to check
1658 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001659 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001660 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001661 if (unlikely(ptr != buf->p)) {
1662 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001663 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001664 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001665 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001666 }
Willy Tarreau26927362012-05-18 23:22:52 +02001667 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001668 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001669 hdr_idx_init(idx);
1670 state = HTTP_MSG_RPVER;
1671 goto http_msg_rpver;
1672 }
1673
1674 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1675 goto http_msg_invalid;
1676
1677 if (unlikely(*ptr == '\n'))
1678 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1679 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1680 /* stop here */
1681
Willy Tarreau8973c702007-01-21 23:58:29 +01001682 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001683 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001684 EXPECT_LF_HERE(ptr, http_msg_invalid);
1685 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1686 /* stop here */
1687
Willy Tarreau8973c702007-01-21 23:58:29 +01001688 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001689 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001690 case HTTP_MSG_RPVER_SP:
1691 case HTTP_MSG_RPCODE:
1692 case HTTP_MSG_RPCODE_SP:
1693 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001694 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001695 state, ptr, end,
1696 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001697 if (unlikely(!ptr))
1698 return;
1699
1700 /* we have a full response and we know that we have either a CR
1701 * or an LF at <ptr>.
1702 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001703 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1704
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001705 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001706 if (likely(*ptr == '\r'))
1707 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1708 goto http_msg_rpline_end;
1709
Willy Tarreau8973c702007-01-21 23:58:29 +01001710 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001711 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001712 /* msg->sol must point to the first of CR or LF. */
1713 EXPECT_LF_HERE(ptr, http_msg_invalid);
1714 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1715 /* stop here */
1716
1717 /*
1718 * Second, states that are specific to the request only
1719 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001720 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001721 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001722 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001723 /* we have a start of message, but we have to check
1724 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001725 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001726 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001727 if (likely(ptr != buf->p)) {
1728 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001729 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001730 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001731 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001732 }
Willy Tarreau26927362012-05-18 23:22:52 +02001733 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001734 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001735 state = HTTP_MSG_RQMETH;
1736 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001737 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001738
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001739 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1740 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001741
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001742 if (unlikely(*ptr == '\n'))
1743 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1744 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001745 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001746
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001747 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001748 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001749 EXPECT_LF_HERE(ptr, http_msg_invalid);
1750 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001751 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001752
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001753 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001754 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001755 case HTTP_MSG_RQMETH_SP:
1756 case HTTP_MSG_RQURI:
1757 case HTTP_MSG_RQURI_SP:
1758 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001759 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001760 state, ptr, end,
1761 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001762 if (unlikely(!ptr))
1763 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001764
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001765 /* we have a full request and we know that we have either a CR
1766 * or an LF at <ptr>.
1767 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001768 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001769
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001770 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001771 if (likely(*ptr == '\r'))
1772 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001773 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001774
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001775 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001776 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001777 /* check for HTTP/0.9 request : no version information available.
1778 * msg->sol must point to the first of CR or LF.
1779 */
1780 if (unlikely(msg->sl.rq.v_l == 0))
1781 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001782
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001783 EXPECT_LF_HERE(ptr, http_msg_invalid);
1784 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001785 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001786
Willy Tarreau8973c702007-01-21 23:58:29 +01001787 /*
1788 * Common states below
1789 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001790 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001791 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001792 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001793 if (likely(!HTTP_IS_CRLF(*ptr))) {
1794 goto http_msg_hdr_name;
1795 }
1796
1797 if (likely(*ptr == '\r'))
1798 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1799 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001800
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001801 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001802 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001803 /* assumes msg->sol points to the first char */
1804 if (likely(HTTP_IS_TOKEN(*ptr)))
1805 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001806
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001807 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001808 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001809
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001810 if (likely(msg->err_pos < -1) || *ptr == '\n')
1811 goto http_msg_invalid;
1812
1813 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001814 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001815
1816 /* and we still accept this non-token character */
1817 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001818
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001819 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001820 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001821 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001822 if (likely(HTTP_IS_SPHT(*ptr)))
1823 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001824
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001825 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001826 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001827
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001828 if (likely(!HTTP_IS_CRLF(*ptr))) {
1829 goto http_msg_hdr_val;
1830 }
1831
1832 if (likely(*ptr == '\r'))
1833 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1834 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001835
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001836 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001837 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001838 EXPECT_LF_HERE(ptr, http_msg_invalid);
1839 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001840
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001841 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001842 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001843 if (likely(HTTP_IS_SPHT(*ptr))) {
1844 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001845 for (; buf->p + msg->sov < ptr; msg->sov++)
1846 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001847 goto http_msg_hdr_l1_sp;
1848 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001849 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001850 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001851 goto http_msg_complete_header;
1852
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001853 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001854 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001855 /* assumes msg->sol points to the first char, and msg->sov
1856 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001857 */
1858 if (likely(!HTTP_IS_CRLF(*ptr)))
1859 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001860
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001861 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001862 /* Note: we could also copy eol into ->eoh so that we have the
1863 * real header end in case it ends with lots of LWS, but is this
1864 * really needed ?
1865 */
1866 if (likely(*ptr == '\r'))
1867 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1868 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001869
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001870 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001871 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001872 EXPECT_LF_HERE(ptr, http_msg_invalid);
1873 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001874
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001875 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001876 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001877 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1878 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001879 for (; buf->p + msg->eol < ptr; msg->eol++)
1880 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001881 goto http_msg_hdr_val;
1882 }
1883 http_msg_complete_header:
1884 /*
1885 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001886 * Assumes msg->sol points to the first char, msg->sov points
1887 * to the first character of the value and msg->eol to the
1888 * first CR or LF so we know how the line ends. We insert last
1889 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001890 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001891 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001892 idx, idx->tail) < 0))
1893 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001894
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001895 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001896 if (likely(!HTTP_IS_CRLF(*ptr))) {
1897 goto http_msg_hdr_name;
1898 }
1899
1900 if (likely(*ptr == '\r'))
1901 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1902 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001903
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001904 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001905 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001906 /* Assumes msg->sol points to the first of either CR or LF.
1907 * Sets ->sov and ->next to the total header length, ->eoh to
1908 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1909 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001910 EXPECT_LF_HERE(ptr, http_msg_invalid);
1911 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001912 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001913 msg->eoh = msg->sol;
1914 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001915 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001916 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001917 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001918
1919 case HTTP_MSG_ERROR:
1920 /* this may only happen if we call http_msg_analyser() twice with an error */
1921 break;
1922
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001923 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001924#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001925 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1926 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001927#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001928 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001929 }
1930 http_msg_ood:
1931 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001932 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001933 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001934 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001935
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001936 http_msg_invalid:
1937 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001938 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001939 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001940 return;
1941}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001942
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001943/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1944 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1945 * nothing is done and 1 is returned.
1946 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001947static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001948{
1949 int delta;
1950 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001951 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001952
1953 if (msg->sl.rq.v_l != 0)
1954 return 1;
1955
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001956 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1957 if (txn->meth != HTTP_METH_GET)
1958 return 0;
1959
Willy Tarreau9b28e032012-10-12 23:49:43 +02001960 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001961 delta = 0;
1962
1963 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001964 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1965 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001966 }
1967 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001968 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001969 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001970 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001971 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001972 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001973 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001974 NULL, NULL);
1975 if (unlikely(!cur_end))
1976 return 0;
1977
1978 /* we have a full HTTP/1.0 request now and we know that
1979 * we have either a CR or an LF at <ptr>.
1980 */
1981 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1982 return 1;
1983}
1984
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001985/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001986 * and "keep-alive" values. If we already know that some headers may safely
1987 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001988 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1989 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001990 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001991 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1992 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1993 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001994 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001995 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001996void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001997{
Willy Tarreau5b154472009-12-21 20:11:07 +01001998 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001999 const char *hdr_val = "Connection";
2000 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01002001
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002002 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01002003 return;
2004
Willy Tarreau88d349d2010-01-25 12:15:43 +01002005 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2006 hdr_val = "Proxy-Connection";
2007 hdr_len = 16;
2008 }
2009
Willy Tarreau5b154472009-12-21 20:11:07 +01002010 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002011 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002012 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002013 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2014 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002015 if (to_del & 2)
2016 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002017 else
2018 txn->flags |= TX_CON_KAL_SET;
2019 }
2020 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2021 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002022 if (to_del & 1)
2023 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002024 else
2025 txn->flags |= TX_CON_CLO_SET;
2026 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01002027 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
2028 txn->flags |= TX_HDR_CONN_UPG;
2029 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002030 }
2031
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002032 txn->flags |= TX_HDR_CONN_PRS;
2033 return;
2034}
Willy Tarreau5b154472009-12-21 20:11:07 +01002035
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002036/* Apply desired changes on the Connection: header. Values may be removed and/or
2037 * added depending on the <wanted> flags, which are exclusively composed of
2038 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2039 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2040 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002041void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002042{
2043 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002044 const char *hdr_val = "Connection";
2045 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002046
2047 ctx.idx = 0;
2048
Willy Tarreau88d349d2010-01-25 12:15:43 +01002049
2050 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2051 hdr_val = "Proxy-Connection";
2052 hdr_len = 16;
2053 }
2054
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002055 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002056 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002057 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2058 if (wanted & TX_CON_KAL_SET)
2059 txn->flags |= TX_CON_KAL_SET;
2060 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002061 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002062 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002063 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2064 if (wanted & TX_CON_CLO_SET)
2065 txn->flags |= TX_CON_CLO_SET;
2066 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002067 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002068 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002069 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002070
2071 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2072 return;
2073
2074 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2075 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002076 hdr_val = "Connection: close";
2077 hdr_len = 17;
2078 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2079 hdr_val = "Proxy-Connection: close";
2080 hdr_len = 23;
2081 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002082 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002083 }
2084
2085 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2086 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002087 hdr_val = "Connection: keep-alive";
2088 hdr_len = 22;
2089 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2090 hdr_val = "Proxy-Connection: keep-alive";
2091 hdr_len = 28;
2092 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002093 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002094 }
2095 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002096}
2097
Willy Tarreauc24715e2014-04-17 15:21:20 +02002098/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
2099 * the first byte of data after the chunk size, so that we know we can forward
2100 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
2101 * the chunk size. That way it is always possible to differentiate between the
2102 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01002103 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002104 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002105 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002106static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002107{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002108 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002109 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002110 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002111 const char *end = buf->data + buf->size;
2112 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01002113 unsigned int chunk = 0;
2114
2115 /* The chunk size is in the following form, though we are only
2116 * interested in the size and CRLF :
2117 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2118 */
2119 while (1) {
2120 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002121 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002122 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002123 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002124 if (c < 0) /* not a hex digit anymore */
2125 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002126 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002127 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002128 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002129 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002130 chunk = (chunk << 4) + c;
2131 }
2132
Willy Tarreaud98cf932009-12-27 22:54:55 +01002133 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002134 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002135 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002136
2137 while (http_is_spht[(unsigned char)*ptr]) {
2138 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002139 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002140 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002141 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002142 }
2143
Willy Tarreaud98cf932009-12-27 22:54:55 +01002144 /* Up to there, we know that at least one byte is present at *ptr. Check
2145 * for the end of chunk size.
2146 */
2147 while (1) {
2148 if (likely(HTTP_IS_CRLF(*ptr))) {
2149 /* we now have a CR or an LF at ptr */
2150 if (likely(*ptr == '\r')) {
2151 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002152 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002153 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002154 return 0;
2155 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002156
Willy Tarreaud98cf932009-12-27 22:54:55 +01002157 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002158 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002159 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002160 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002161 /* done */
2162 break;
2163 }
2164 else if (*ptr == ';') {
2165 /* chunk extension, ends at next CRLF */
2166 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002167 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002168 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002169 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002170
2171 while (!HTTP_IS_CRLF(*ptr)) {
2172 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002173 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002174 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002175 return 0;
2176 }
2177 /* we have a CRLF now, loop above */
2178 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002179 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002180 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002181 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002182 }
2183
Willy Tarreaud98cf932009-12-27 22:54:55 +01002184 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02002185 * which may or may not be present. We save that into ->next,
2186 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002187 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002188 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002189 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002190 msg->sol += buf->size;
Willy Tarreau124d9912011-03-01 20:30:48 +01002191 msg->chunk_len = chunk;
2192 msg->body_len += chunk;
Willy Tarreau115acb92009-12-26 13:56:06 +01002193 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002194 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002195 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002196 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002197}
2198
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002199/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002200 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002201 * the trailers is found, it is automatically scheduled to be forwarded,
2202 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2203 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002204 * except maybe msg->next if it could parse some lines, and returns zero.
2205 * If a parse error is encountered, the function returns < 0 and does not
2206 * change anything except maybe msg->next. Note that the message must
2207 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002208 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002209 * forwarding, and that msg->next exactly matches the length of trailers
2210 * already parsed and not forwarded. It is also important to note that this
2211 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002212 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002213static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002214{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002215 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002216
Willy Tarreaua458b672012-03-05 11:17:50 +01002217 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002218 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002219 const char *p1 = NULL, *p2 = NULL;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002220 const char *start = b_ptr(buf, msg->next + msg->sol);
2221 const char *stop = bi_end(buf);
2222 const char *ptr = start;
2223 int bytes = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002224
2225 /* scan current line and stop at LF or CRLF */
2226 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002227 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002228 return 0;
2229
2230 if (*ptr == '\n') {
2231 if (!p1)
2232 p1 = ptr;
2233 p2 = ptr;
2234 break;
2235 }
2236
2237 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002238 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002239 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002240 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002241 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002242 p1 = ptr;
2243 }
2244
2245 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002246 if (ptr >= buf->data + buf->size)
2247 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002248 }
2249
2250 /* after LF; point to beginning of next line */
2251 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002252 if (p2 >= buf->data + buf->size)
2253 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002254
Christopher Faulet2fb28802015-12-01 10:40:57 +01002255 bytes = p2 - start;
Willy Tarreau638cd022010-01-03 07:42:04 +01002256 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002257 bytes += buf->size;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002258 msg->sol += bytes;
Willy Tarreau638cd022010-01-03 07:42:04 +01002259
Christopher Fauletd7c91962015-04-30 11:48:27 +02002260 /* LF/CRLF at beginning of line => end of trailers at p2.
2261 * Everything was scheduled for forwarding, there's nothing left
Christopher Faulet2fb28802015-12-01 10:40:57 +01002262 * from this message. */
2263 if (p1 == start)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002264 return 1;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002265
Willy Tarreaud98cf932009-12-27 22:54:55 +01002266 /* OK, next line then */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002267 }
2268}
2269
Willy Tarreauc24715e2014-04-17 15:21:20 +02002270/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2271 * or a possible LF alone at the end of a chunk. It automatically adjusts
2272 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002273 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002274 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2275 * not enough data are available, the function does not change anything and
2276 * returns zero. If a parse error is encountered, the function returns < 0 and
2277 * does not change anything. Note: this function is designed to parse wrapped
2278 * CRLF at the end of the buffer.
2279 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002280static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002281{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002282 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002283 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002284 int bytes;
2285
2286 /* NB: we'll check data availabilty at the end. It's not a
2287 * problem because whatever we match first will be checked
2288 * against the correct length.
2289 */
2290 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002291 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002292 if (*ptr == '\r') {
2293 bytes++;
2294 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002295 if (ptr >= buf->data + buf->size)
2296 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002297 }
2298
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002299 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002300 return 0;
2301
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002302 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002303 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002304 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002305 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02002306 msg->sol = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002307 return 1;
2308}
Willy Tarreau5b154472009-12-21 20:11:07 +01002309
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002310/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2311 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2312 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2313 * Unparsable qvalues return 1000 as "q=1.000".
2314 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002315int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002316{
2317 int q = 1000;
2318
Willy Tarreau506c69a2014-07-08 00:59:48 +02002319 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002320 goto out;
2321 q = (*qvalue++ - '0') * 1000;
2322
2323 if (*qvalue++ != '.')
2324 goto out;
2325
Willy Tarreau506c69a2014-07-08 00:59:48 +02002326 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002327 goto out;
2328 q += (*qvalue++ - '0') * 100;
2329
Willy Tarreau506c69a2014-07-08 00:59:48 +02002330 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002331 goto out;
2332 q += (*qvalue++ - '0') * 10;
2333
Willy Tarreau506c69a2014-07-08 00:59:48 +02002334 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002335 goto out;
2336 q += (*qvalue++ - '0') * 1;
2337 out:
2338 if (q > 1000)
2339 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002340 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002341 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002342 return q;
2343}
William Lallemand82fe75c2012-10-23 10:25:10 +02002344
Willy Tarreau87b09662015-04-03 00:22:06 +02002345void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002346{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002347 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002348 int tmp = TX_CON_WANT_KAL;
2349
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002350 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2351 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002352 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2353 tmp = TX_CON_WANT_TUN;
2354
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002355 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002356 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2357 tmp = TX_CON_WANT_TUN;
2358 }
2359
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002360 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002361 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2362 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002363 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002364 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2365 tmp = TX_CON_WANT_CLO;
2366 else
2367 tmp = TX_CON_WANT_SCL;
2368 }
2369
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002370 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002371 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2372 tmp = TX_CON_WANT_CLO;
2373
2374 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2375 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2376
2377 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2378 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2379 /* parse the Connection header and possibly clean it */
2380 int to_del = 0;
2381 if ((msg->flags & HTTP_MSGF_VER_11) ||
2382 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002383 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002384 to_del |= 2; /* remove "keep-alive" */
2385 if (!(msg->flags & HTTP_MSGF_VER_11))
2386 to_del |= 1; /* remove "close" */
2387 http_parse_connection_header(txn, msg, to_del);
2388 }
2389
2390 /* check if client or config asks for explicit close in KAL/SCL */
2391 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2392 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2393 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2394 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2395 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002396 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002397 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2398}
William Lallemand82fe75c2012-10-23 10:25:10 +02002399
Willy Tarreaud787e662009-07-07 10:14:51 +02002400/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2401 * processing can continue on next analysers, or zero if it either needs more
2402 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002403 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002404 * when it has nothing left to do, and may remove any analyser when it wants to
2405 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002406 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002407int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002408{
Willy Tarreau59234e92008-11-30 23:51:27 +01002409 /*
2410 * We will parse the partial (or complete) lines.
2411 * We will check the request syntax, and also join multi-line
2412 * headers. An index of all the lines will be elaborated while
2413 * parsing.
2414 *
2415 * For the parsing, we use a 28 states FSM.
2416 *
2417 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002418 * req->buf->p = beginning of request
2419 * req->buf->p + msg->eoh = end of processed headers / start of current one
2420 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002421 * msg->eol = end of current header or line (LF or CRLF)
2422 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002423 *
2424 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02002425 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002426 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2427 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002428 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002429
Willy Tarreau59234e92008-11-30 23:51:27 +01002430 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002431 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002432 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01002433 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002434 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002435
Willy Tarreau87b09662015-04-03 00:22:06 +02002436 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 +01002437 now_ms, __FUNCTION__,
2438 s,
2439 req,
2440 req->rex, req->wex,
2441 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002442 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002443 req->analysers);
2444
Willy Tarreau52a0c602009-08-16 22:45:38 +02002445 /* we're speaking HTTP here, so let's speak HTTP to the client */
2446 s->srv_error = http_return_srv_error;
2447
Willy Tarreau83e3af02009-12-28 17:39:57 +01002448 /* There's a protected area at the end of the buffer for rewriting
2449 * purposes. We don't want to start to parse the request if the
2450 * protected area is affected, because we may have to move processed
2451 * data later, which is much more complicated.
2452 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002453 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002454 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002455 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002456 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002457 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002458 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002459 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002460 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002461 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002462 return 0;
2463 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002464 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2465 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2466 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002467 }
2468
Willy Tarreau065e8332010-01-08 00:30:20 +01002469 /* Note that we have the same problem with the response ; we
2470 * may want to send a redirect, error or anything which requires
2471 * some spare space. So we'll ensure that we have at least
2472 * maxrewrite bytes available in the response buffer before
2473 * processing that one. This will only affect pipelined
2474 * keep-alive requests.
2475 */
2476 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002477 unlikely(!channel_is_rewritable(&s->res) ||
2478 bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) ||
2479 bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) {
2480 if (s->res.buf->o) {
2481 if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002482 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002483 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002484 channel_dont_connect(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002485 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
2486 s->res.flags |= CF_WAKE_WRITE;
2487 s->res.analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002488 return 0;
2489 }
2490 }
2491
Willy Tarreau9b28e032012-10-12 23:49:43 +02002492 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002493 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002494 }
2495
Willy Tarreau59234e92008-11-30 23:51:27 +01002496 /* 1: we might have to print this header in debug mode */
2497 if (unlikely((global.mode & MODE_DEBUG) &&
2498 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002499 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002500 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002501
Willy Tarreau9b28e032012-10-12 23:49:43 +02002502 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002503 /* this is a bit complex : in case of error on the request line,
2504 * we know that rq.l is still zero, so we display only the part
2505 * up to the end of the line (truncated by debug_hdr).
2506 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002507 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002508 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002509
Willy Tarreau59234e92008-11-30 23:51:27 +01002510 sol += hdr_idx_first_pos(&txn->hdr_idx);
2511 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002512
Willy Tarreau59234e92008-11-30 23:51:27 +01002513 while (cur_idx) {
2514 eol = sol + txn->hdr_idx.v[cur_idx].len;
2515 debug_hdr("clihdr", s, sol, eol);
2516 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2517 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002518 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002519 }
2520
Willy Tarreau58f10d72006-12-04 02:26:12 +01002521
Willy Tarreau59234e92008-11-30 23:51:27 +01002522 /*
2523 * Now we quickly check if we have found a full valid request.
2524 * If not so, we check the FD and buffer states before leaving.
2525 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002526 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002527 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02002528 * on a keep-alive stream, if we encounter and error, close, t/o,
2529 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002530 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02002531 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002532 * Last, we may increase some tracked counters' http request errors on
2533 * the cases that are deliberately the client's fault. For instance,
2534 * a timeout or connection reset is not counted as an error. However
2535 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002536 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002537
Willy Tarreau655dce92009-11-08 13:10:58 +01002538 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002539 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002540 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002541 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002542 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002543 stream_inc_http_req_ctr(s);
2544 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002545 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002546 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002547 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002548
Willy Tarreau59234e92008-11-30 23:51:27 +01002549 /* 1: Since we are in header mode, if there's no space
2550 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02002551 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01002552 * must terminate it now.
2553 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002554 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002555 /* FIXME: check if URI is set and return Status
2556 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002557 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002558 stream_inc_http_req_ctr(s);
2559 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002560 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002561 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002562 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002563 goto return_bad_req;
2564 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002565
Willy Tarreau59234e92008-11-30 23:51:27 +01002566 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002567 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002568 if (!(s->flags & SF_ERR_MASK))
2569 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002570
Willy Tarreaufcffa692010-01-10 14:21:19 +01002571 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002572 goto failed_keep_alive;
2573
Willy Tarreau0f228a02015-05-01 15:37:53 +02002574 if (sess->fe->options & PR_O_IGNORE_PRB)
2575 goto failed_keep_alive;
2576
Willy Tarreau59234e92008-11-30 23:51:27 +01002577 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002578 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002579 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002580 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002581 }
2582
Willy Tarreaudc979f22012-12-04 10:39:01 +01002583 txn->status = 400;
Willy Tarreau59234e92008-11-30 23:51:27 +01002584 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002585 http_reply_and_close(s, txn->status, NULL);
Christopher Fauletd7c91962015-04-30 11:48:27 +02002586 req->analysers &= AN_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02002587 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002588 proxy_inc_fe_req_ctr(sess->fe);
2589 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002590 if (sess->listener->counters)
2591 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002592
Willy Tarreaue7dff022015-04-03 01:14:29 +02002593 if (!(s->flags & SF_FINST_MASK))
2594 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002595 return 0;
2596 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002597
Willy Tarreau59234e92008-11-30 23:51:27 +01002598 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002599 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002600 if (!(s->flags & SF_ERR_MASK))
2601 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002602
Willy Tarreaufcffa692010-01-10 14:21:19 +01002603 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002604 goto failed_keep_alive;
2605
Willy Tarreau0f228a02015-05-01 15:37:53 +02002606 if (sess->fe->options & PR_O_IGNORE_PRB)
2607 goto failed_keep_alive;
2608
Willy Tarreau59234e92008-11-30 23:51:27 +01002609 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002610 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002611 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002612 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002613 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002614 txn->status = 408;
Willy Tarreau59234e92008-11-30 23:51:27 +01002615 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002616 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_408));
Christopher Fauletd7c91962015-04-30 11:48:27 +02002617 req->analysers &= AN_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002618
Willy Tarreau87b09662015-04-03 00:22:06 +02002619 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002620 proxy_inc_fe_req_ctr(sess->fe);
2621 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002622 if (sess->listener->counters)
2623 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002624
Willy Tarreaue7dff022015-04-03 01:14:29 +02002625 if (!(s->flags & SF_FINST_MASK))
2626 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002627 return 0;
2628 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002629
Willy Tarreau59234e92008-11-30 23:51:27 +01002630 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002631 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002632 if (!(s->flags & SF_ERR_MASK))
2633 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002634
Willy Tarreaufcffa692010-01-10 14:21:19 +01002635 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002636 goto failed_keep_alive;
2637
Willy Tarreau0f228a02015-05-01 15:37:53 +02002638 if (sess->fe->options & PR_O_IGNORE_PRB)
2639 goto failed_keep_alive;
2640
Willy Tarreau4076a152009-04-02 15:18:36 +02002641 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002642 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002643 txn->status = 400;
Willy Tarreau59234e92008-11-30 23:51:27 +01002644 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002645 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Christopher Fauletd7c91962015-04-30 11:48:27 +02002646 req->analysers &= AN_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02002647 stream_inc_http_err_ctr(s);
2648 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002649 proxy_inc_fe_req_ctr(sess->fe);
2650 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002651 if (sess->listener->counters)
2652 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002653
Willy Tarreaue7dff022015-04-03 01:14:29 +02002654 if (!(s->flags & SF_FINST_MASK))
2655 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002656 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002657 }
2658
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002659 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002660 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002661 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002662#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002663 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
2664 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002665 /* We need more data, we have to re-enable quick-ack in case we
2666 * previously disabled it, otherwise we might cause the client
2667 * to delay next data.
2668 */
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002669 setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002670 }
2671#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002672
Willy Tarreaufcffa692010-01-10 14:21:19 +01002673 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2674 /* If the client starts to talk, let's fall back to
2675 * request timeout processing.
2676 */
2677 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002678 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002679 }
2680
Willy Tarreau59234e92008-11-30 23:51:27 +01002681 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002682 if (!tick_isset(req->analyse_exp)) {
2683 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2684 (txn->flags & TX_WAIT_NEXT_RQ) &&
2685 tick_isset(s->be->timeout.httpka))
2686 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2687 else
2688 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2689 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002690
Willy Tarreau59234e92008-11-30 23:51:27 +01002691 /* we're not ready yet */
2692 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002693
2694 failed_keep_alive:
2695 /* Here we process low-level errors for keep-alive requests. In
2696 * short, if the request is not the first one and it experiences
2697 * a timeout, read error or shutdown, we just silently close so
2698 * that the client can try again.
2699 */
2700 txn->status = 0;
2701 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Fauletd7c91962015-04-30 11:48:27 +02002702 req->analysers &= AN_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002703 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002704 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002705 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002706 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002707 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002708 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002709
Willy Tarreaud787e662009-07-07 10:14:51 +02002710 /* OK now we have a complete HTTP request with indexed headers. Let's
2711 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002712 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002713 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002714 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002715 * byte after the last LF. msg->sov points to the first byte of data.
2716 * msg->eol cannot be trusted because it may have been left uninitialized
2717 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002718 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002719
Willy Tarreau87b09662015-04-03 00:22:06 +02002720 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002721 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002722
Willy Tarreaub16a5742010-01-10 14:46:16 +01002723 if (txn->flags & TX_WAIT_NEXT_RQ) {
2724 /* kill the pending keep-alive timeout */
2725 txn->flags &= ~TX_WAIT_NEXT_RQ;
2726 req->analyse_exp = TICK_ETERNITY;
2727 }
2728
2729
Willy Tarreaud787e662009-07-07 10:14:51 +02002730 /* Maybe we found in invalid header name while we were configured not
2731 * to block on that, so we have to capture it now.
2732 */
2733 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002734 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002735
Willy Tarreau59234e92008-11-30 23:51:27 +01002736 /*
2737 * 1: identify the method
2738 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002739 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002740
2741 /* we can make use of server redirect on GET and HEAD */
2742 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002743 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002744
Willy Tarreau59234e92008-11-30 23:51:27 +01002745 /*
2746 * 2: check if the URI matches the monitor_uri.
2747 * We have to do this for every request which gets in, because
2748 * the monitor-uri is defined by the frontend.
2749 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002750 if (unlikely((sess->fe->monitor_uri_len != 0) &&
2751 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002752 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002753 sess->fe->monitor_uri,
2754 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002755 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002756 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002757 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002758 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002759
Willy Tarreaue7dff022015-04-03 01:14:29 +02002760 s->flags |= SF_MONITOR;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002761 sess->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002762
Willy Tarreau59234e92008-11-30 23:51:27 +01002763 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002764 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002765 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002766
Willy Tarreau59234e92008-11-30 23:51:27 +01002767 ret = acl_pass(ret);
2768 if (cond->pol == ACL_COND_UNLESS)
2769 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002770
Willy Tarreau59234e92008-11-30 23:51:27 +01002771 if (ret) {
2772 /* we fail this request, let's return 503 service unavail */
2773 txn->status = 503;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002774 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_503));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002775 if (!(s->flags & SF_ERR_MASK))
2776 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002777 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002778 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002779 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002780
Willy Tarreau59234e92008-11-30 23:51:27 +01002781 /* nothing to fail, let's reply normaly */
2782 txn->status = 200;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002783 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_200));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002784 if (!(s->flags & SF_ERR_MASK))
2785 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002786 goto return_prx_cond;
2787 }
2788
2789 /*
2790 * 3: Maybe we have to copy the original REQURI for the logs ?
2791 * Note: we cannot log anymore if the request has been
2792 * classified as invalid.
2793 */
2794 if (unlikely(s->logs.logwait & LW_REQ)) {
2795 /* we have a complete HTTP request that we must log */
2796 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2797 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002798
Willy Tarreau59234e92008-11-30 23:51:27 +01002799 if (urilen >= REQURI_LEN)
2800 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002801 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002802 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002803
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002804 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002805 s->do_log(s);
2806 } else {
2807 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002808 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002809 }
Willy Tarreau06619262006-12-17 08:37:22 +01002810
Willy Tarreau91852eb2015-05-01 13:26:00 +02002811 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
2812 * exactly one digit "." one digit. This check may be disabled using
2813 * option accept-invalid-http-request.
2814 */
2815 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
2816 if (msg->sl.rq.v_l != 8) {
2817 msg->err_pos = msg->sl.rq.v;
2818 goto return_bad_req;
2819 }
2820
2821 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
2822 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
2823 req->buf->p[msg->sl.rq.v + 6] != '.' ||
2824 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
2825 msg->err_pos = msg->sl.rq.v + 4;
2826 goto return_bad_req;
2827 }
2828 }
Willy Tarreau13317662015-05-01 13:47:08 +02002829 else {
2830 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
2831 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
2832 goto return_bad_req;
2833 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02002834
Willy Tarreau5b154472009-12-21 20:11:07 +01002835 /* ... and check if the request is HTTP/1.1 or above */
2836 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002837 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2838 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2839 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002840 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002841
2842 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002843 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 +01002844
Willy Tarreau88d349d2010-01-25 12:15:43 +01002845 /* if the frontend has "option http-use-proxy-header", we'll check if
2846 * we have what looks like a proxied connection instead of a connection,
2847 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2848 * Note that this is *not* RFC-compliant, however browsers and proxies
2849 * happen to do that despite being non-standard :-(
2850 * We consider that a request not beginning with either '/' or '*' is
2851 * a proxied connection, which covers both "scheme://location" and
2852 * CONNECT ip:port.
2853 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002854 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002855 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002856 txn->flags |= TX_USE_PX_CONN;
2857
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002858 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002859 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002860
Willy Tarreau59234e92008-11-30 23:51:27 +01002861 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002862 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002863 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002864 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002865
Willy Tarreau557f1992015-05-01 10:05:17 +02002866 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
2867 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002868 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002869 * The length of a message body is determined by one of the following
2870 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02002871 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002872 * 1. Any response to a HEAD request and any response with a 1xx
2873 * (Informational), 204 (No Content), or 304 (Not Modified) status
2874 * code is always terminated by the first empty line after the
2875 * header fields, regardless of the header fields present in the
2876 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002877 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002878 * 2. Any 2xx (Successful) response to a CONNECT request implies that
2879 * the connection will become a tunnel immediately after the empty
2880 * line that concludes the header fields. A client MUST ignore any
2881 * Content-Length or Transfer-Encoding header fields received in
2882 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002883 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002884 * 3. If a Transfer-Encoding header field is present and the chunked
2885 * transfer coding (Section 4.1) is the final encoding, the message
2886 * body length is determined by reading and decoding the chunked
2887 * data until the transfer coding indicates the data is complete.
2888 *
2889 * If a Transfer-Encoding header field is present in a response and
2890 * the chunked transfer coding is not the final encoding, the
2891 * message body length is determined by reading the connection until
2892 * it is closed by the server. If a Transfer-Encoding header field
2893 * is present in a request and the chunked transfer coding is not
2894 * the final encoding, the message body length cannot be determined
2895 * reliably; the server MUST respond with the 400 (Bad Request)
2896 * status code and then close the connection.
2897 *
2898 * If a message is received with both a Transfer-Encoding and a
2899 * Content-Length header field, the Transfer-Encoding overrides the
2900 * Content-Length. Such a message might indicate an attempt to
2901 * perform request smuggling (Section 9.5) or response splitting
2902 * (Section 9.4) and ought to be handled as an error. A sender MUST
2903 * remove the received Content-Length field prior to forwarding such
2904 * a message downstream.
2905 *
2906 * 4. If a message is received without Transfer-Encoding and with
2907 * either multiple Content-Length header fields having differing
2908 * field-values or a single Content-Length header field having an
2909 * invalid value, then the message framing is invalid and the
2910 * recipient MUST treat it as an unrecoverable error. If this is a
2911 * request message, the server MUST respond with a 400 (Bad Request)
2912 * status code and then close the connection. If this is a response
2913 * message received by a proxy, the proxy MUST close the connection
2914 * to the server, discard the received response, and send a 502 (Bad
2915 * Gateway) response to the client. If this is a response message
2916 * received by a user agent, the user agent MUST close the
2917 * connection to the server and discard the received response.
2918 *
2919 * 5. If a valid Content-Length header field is present without
2920 * Transfer-Encoding, its decimal value defines the expected message
2921 * body length in octets. If the sender closes the connection or
2922 * the recipient times out before the indicated number of octets are
2923 * received, the recipient MUST consider the message to be
2924 * incomplete and close the connection.
2925 *
2926 * 6. If this is a request message and none of the above are true, then
2927 * the message body length is zero (no message body is present).
2928 *
2929 * 7. Otherwise, this is a response message without a declared message
2930 * body length, so the message body length is determined by the
2931 * number of octets received prior to the server closing the
2932 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002933 */
2934
Willy Tarreau32b47f42009-10-18 20:55:02 +02002935 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002936 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02002937 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002938 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002939 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2940 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02002941 /* chunked not last, return badreq */
2942 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002943 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002944 }
2945
Willy Tarreau1c913912015-04-30 10:57:51 +02002946 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02002947 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02002948 if (msg->flags & HTTP_MSGF_TE_CHNK) {
2949 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
2950 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2951 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02002952 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002953 signed long long cl;
2954
Willy Tarreauad14f752011-09-02 20:33:27 +02002955 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002956 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002957 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002958 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002959
Willy Tarreauad14f752011-09-02 20:33:27 +02002960 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002961 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002962 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002963 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002964
Willy Tarreauad14f752011-09-02 20:33:27 +02002965 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002966 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002967 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002968 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002969
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002970 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002971 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002972 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002973 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002974
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002975 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002976 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002977 }
2978
Willy Tarreau34dfc602015-05-01 10:09:49 +02002979 /* even bodyless requests have a known length */
2980 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002981
Willy Tarreau179085c2014-04-28 16:48:56 +02002982 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
2983 * only change if both the request and the config reference something else.
2984 * Option httpclose by itself sets tunnel mode where headers are mangled.
2985 * However, if another mode is set, it will affect it (eg: server-close/
2986 * keep-alive + httpclose = close). Note that we avoid to redo the same work
2987 * if FE and BE have the same settings (common). The method consists in
2988 * checking if options changed between the two calls (implying that either
2989 * one is non-null, or one of them is non-null and we are there for the first
2990 * time.
2991 */
2992 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002993 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002994 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02002995
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02002996 /* we may have to wait for the request's body */
2997 if ((s->be->options & PR_O_WREQ_BODY) &&
2998 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
2999 req->analysers |= AN_REQ_HTTP_BODY;
3000
Willy Tarreaud787e662009-07-07 10:14:51 +02003001 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003002 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003003 req->analyse_exp = TICK_ETERNITY;
3004 return 1;
3005
3006 return_bad_req:
3007 /* We centralize bad requests processing here */
3008 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3009 /* we detected a parsing error. We want to archive this request
3010 * in the dedicated proxy area for later troubleshooting.
3011 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003012 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003013 }
3014
3015 txn->req.msg_state = HTTP_MSG_ERROR;
3016 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003017 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003018
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003019 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003020 if (sess->listener->counters)
3021 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003022
3023 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003024 if (!(s->flags & SF_ERR_MASK))
3025 s->flags |= SF_ERR_PRXCOND;
3026 if (!(s->flags & SF_FINST_MASK))
3027 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02003028
Christopher Fauletd7c91962015-04-30 11:48:27 +02003029 req->analysers &= AN_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02003030 req->analyse_exp = TICK_ETERNITY;
3031 return 0;
3032}
3033
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003034
Willy Tarreau347a35d2013-11-22 17:51:09 +01003035/* This function prepares an applet to handle the stats. It can deal with the
3036 * "100-continue" expectation, check that admin rules are met for POST requests,
3037 * and program a response message if something was unexpected. It cannot fail
3038 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003039 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003040 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02003041 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003042 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003043int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003044{
3045 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003046 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02003047 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003048 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003049 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003050 struct uri_auth *uri_auth = s->be->uri_auth;
3051 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003052 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003053
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003054 appctx = si_appctx(si);
3055 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3056 appctx->st1 = appctx->st2 = 0;
3057 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3058 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003059 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003060 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003061
3062 uri = msg->chn->buf->p + msg->sl.rq.u;
3063 lookup = uri + uri_auth->uri_len;
3064
3065 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3066 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003067 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003068 break;
3069 }
3070 }
3071
3072 if (uri_auth->refresh) {
3073 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3074 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003075 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003076 break;
3077 }
3078 }
3079 }
3080
3081 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3082 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003083 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003084 break;
3085 }
3086 }
3087
3088 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3089 if (memcmp(h, ";st=", 4) == 0) {
3090 int i;
3091 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003092 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003093 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3094 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003095 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003096 break;
3097 }
3098 }
3099 break;
3100 }
3101 }
3102
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003103 appctx->ctx.stats.scope_str = 0;
3104 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003105 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3106 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3107 int itx = 0;
3108 const char *h2;
3109 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3110 const char *err;
3111
3112 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3113 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003114 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003115 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3116 itx++;
3117 h++;
3118 }
3119
3120 if (itx > STAT_SCOPE_TXT_MAXLEN)
3121 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003122 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003123
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003124 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003125 memcpy(scope_txt, h2, itx);
3126 scope_txt[itx] = '\0';
3127 err = invalid_char(scope_txt);
3128 if (err) {
3129 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003130 appctx->ctx.stats.scope_str = 0;
3131 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003132 }
3133 break;
3134 }
3135 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003136
3137 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003138 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003139 int ret = 1;
3140
3141 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003142 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 +01003143 ret = acl_pass(ret);
3144 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3145 ret = !ret;
3146 }
3147
3148 if (ret) {
3149 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003150 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003151 break;
3152 }
3153 }
3154
3155 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003156 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003157 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003158 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003159 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
3160 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003161 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003162 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003163 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003164 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3165 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003166 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003167 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003168 else {
3169 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003170 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003171 }
3172
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003173 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003174 return 1;
3175}
3176
Lukas Tribus67db8df2013-06-23 17:37:13 +02003177/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3178 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3179 */
Thierry FOURNIER7fe75e02015-03-16 12:03:44 +01003180void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003181{
3182#ifdef IP_TOS
3183 if (from.ss_family == AF_INET)
3184 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3185#endif
3186#ifdef IPV6_TCLASS
3187 if (from.ss_family == AF_INET6) {
3188 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3189 /* v4-mapped addresses need IP_TOS */
3190 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3191 else
3192 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3193 }
3194#endif
3195}
3196
Willy Tarreau87b09662015-04-03 00:22:06 +02003197int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003198 const char* name, unsigned int name_len,
3199 const char *str, struct my_regex *re,
3200 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003201{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003202 struct hdr_ctx ctx;
3203 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003204 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003205 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3206 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003207 struct chunk *output = get_trash_chunk();
3208
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003209 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003210
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003211 /* Choose the header browsing function. */
3212 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003213 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003214 http_find_hdr_func = http_find_header2;
3215 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003216 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003217 http_find_hdr_func = http_find_full_header2;
3218 break;
3219 default: /* impossible */
3220 return -1;
3221 }
3222
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003223 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3224 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003225 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003226 char *val = ctx.line + ctx.val;
3227 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003228
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003229 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3230 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003231
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003232 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003233 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003234 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003235
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003236 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003237
3238 hdr->len += delta;
3239 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003240
3241 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003242 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003243 }
3244
3245 return 0;
3246}
3247
Willy Tarreau87b09662015-04-03 00:22:06 +02003248static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003249 const char* name, unsigned int name_len,
3250 struct list *fmt, struct my_regex *re,
3251 int action)
3252{
3253 struct chunk *replace = get_trash_chunk();
3254
3255 replace->len = build_logline(s, replace->str, replace->size, fmt);
3256 if (replace->len >= replace->size - 1)
3257 return -1;
3258
3259 return http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
3260}
3261
Willy Tarreau87b09662015-04-03 00:22:06 +02003262/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003263 * transaction <txn>. Returns the verdict of the first rule that prevents
3264 * further processing of the request (auth, deny, ...), and defaults to
3265 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3266 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3267 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003268 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003269enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003270http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003271{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003272 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003273 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003274 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003275 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003276 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003277 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02003278 int act_flags = 0;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003279
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003280 /* If "the current_rule_list" match the executed rule list, we are in
3281 * resume condition. If a resume is needed it is always in the action
3282 * and never in the ACL or converters. In this case, we initialise the
3283 * current rule, and go to the action execution point.
3284 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003285 if (s->current_rule) {
3286 rule = s->current_rule;
3287 s->current_rule = NULL;
3288 if (s->current_rule_list == rules)
3289 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003290 }
3291 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003292
Willy Tarreauff011f22011-01-06 17:51:27 +01003293 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003294
Willy Tarreau96257ec2012-12-27 10:46:37 +01003295 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003296 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003297 int ret;
3298
Willy Tarreau192252e2015-04-04 01:47:55 +02003299 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003300 ret = acl_pass(ret);
3301
Willy Tarreauff011f22011-01-06 17:51:27 +01003302 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003303 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003304
3305 if (!ret) /* condition not matched */
3306 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003307 }
3308
Willy Tarreauacc98002015-09-27 23:34:39 +02003309 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003310resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003311 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003312 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003313 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003314
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003315 case ACT_ACTION_DENY:
CJ Ess108b1dd2015-04-07 12:03:37 -04003316 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003317 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003318
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003319 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01003320 txn->flags |= TX_CLTARPIT;
CJ Ess108b1dd2015-04-07 12:03:37 -04003321 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003322 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003323
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003324 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003325 /* Auth might be performed on regular http-req rules as well as on stats */
3326 auth_realm = rule->arg.auth.realm;
3327 if (!auth_realm) {
3328 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3329 auth_realm = STATS_DEFAULT_REALM;
3330 else
3331 auth_realm = px->id;
3332 }
3333 /* send 401/407 depending on whether we use a proxy or not. We still
3334 * count one error, because normal browsing won't significantly
3335 * increase the counter but brute force attempts will.
3336 */
3337 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3338 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003339 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003340 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003341 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003342
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003343 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003344 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3345 return HTTP_RULE_RES_BADREQ;
3346 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003347
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003348 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003349 s->task->nice = rule->arg.nice;
3350 break;
3351
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003352 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003353 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003354 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003355 break;
3356
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003357 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003358#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003359 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003360 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003361#endif
3362 break;
3363
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003364 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003365 s->logs.level = rule->arg.loglevel;
3366 break;
3367
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003368 case ACT_HTTP_REPLACE_HDR:
3369 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003370 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3371 rule->arg.hdr_add.name_len,
3372 &rule->arg.hdr_add.fmt,
3373 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003374 return HTTP_RULE_RES_BADREQ;
3375 break;
3376
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003377 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003378 ctx.idx = 0;
3379 /* remove all occurrences of the header */
3380 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3381 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3382 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003383 }
Willy Tarreau85603282015-01-21 20:39:27 +01003384 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003385
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003386 case ACT_HTTP_SET_HDR:
3387 case ACT_HTTP_ADD_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003388 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3389 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3390 trash.len = rule->arg.hdr_add.name_len;
3391 trash.str[trash.len++] = ':';
3392 trash.str[trash.len++] = ' ';
3393 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 +01003394
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003395 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003396 /* remove all occurrences of the header */
3397 ctx.idx = 0;
3398 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3399 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3400 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3401 }
3402 }
3403
Willy Tarreau96257ec2012-12-27 10:46:37 +01003404 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3405 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003406
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003407 case ACT_HTTP_DEL_ACL:
3408 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003409 struct pat_ref *ref;
3410 char *key;
3411 int len;
3412
3413 /* collect reference */
3414 ref = pat_ref_lookup(rule->arg.map.ref);
3415 if (!ref)
3416 continue;
3417
3418 /* collect key */
3419 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3420 key = trash.str;
3421 key[len] = '\0';
3422
3423 /* perform update */
3424 /* returned code: 1=ok, 0=ko */
3425 pat_ref_delete(ref, key);
3426
3427 break;
3428 }
3429
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003430 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003431 struct pat_ref *ref;
3432 char *key;
3433 struct chunk *trash_key;
3434 int len;
3435
3436 trash_key = get_trash_chunk();
3437
3438 /* collect reference */
3439 ref = pat_ref_lookup(rule->arg.map.ref);
3440 if (!ref)
3441 continue;
3442
3443 /* collect key */
3444 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3445 key = trash_key->str;
3446 key[len] = '\0';
3447
3448 /* perform update */
3449 /* add entry only if it does not already exist */
3450 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003451 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003452
3453 break;
3454 }
3455
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003456 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003457 struct pat_ref *ref;
3458 char *key, *value;
3459 struct chunk *trash_key, *trash_value;
3460 int len;
3461
3462 trash_key = get_trash_chunk();
3463 trash_value = get_trash_chunk();
3464
3465 /* collect reference */
3466 ref = pat_ref_lookup(rule->arg.map.ref);
3467 if (!ref)
3468 continue;
3469
3470 /* collect key */
3471 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3472 key = trash_key->str;
3473 key[len] = '\0';
3474
3475 /* collect value */
3476 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3477 value = trash_value->str;
3478 value[len] = '\0';
3479
3480 /* perform update */
3481 if (pat_ref_find_elt(ref, key) != NULL)
3482 /* update entry if it exists */
3483 pat_ref_set(ref, key, value, NULL);
3484 else
3485 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003486 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003487
3488 break;
3489 }
William Lallemand73025dd2014-04-24 14:38:37 +02003490
Thierry FOURNIER42148732015-09-02 17:17:33 +02003491 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003492 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3493 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003494
Willy Tarreauacc98002015-09-27 23:34:39 +02003495 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003496 case ACT_RET_ERR:
3497 case ACT_RET_CONT:
3498 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003499 case ACT_RET_STOP:
3500 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003501 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003502 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003503 return HTTP_RULE_RES_YIELD;
3504 }
William Lallemand73025dd2014-04-24 14:38:37 +02003505 break;
3506
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003507 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02003508 /* Note: only the first valid tracking parameter of each
3509 * applies.
3510 */
3511
3512 if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) {
3513 struct stktable *t;
3514 struct stksess *ts;
3515 struct stktable_key *key;
3516 void *ptr;
3517
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02003518 t = rule->arg.trk_ctr.table.t;
3519 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 +02003520
3521 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreau87b09662015-04-03 00:22:06 +02003522 stream_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003523
3524 /* let's count a new HTTP request as it's the first time we do it */
3525 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3526 if (ptr)
3527 stktable_data_cast(ptr, http_req_cnt)++;
3528
3529 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3530 if (ptr)
3531 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3532 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3533
3534 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003535 if (sess->fe != s->be)
Willy Tarreau09448f72014-06-25 18:12:15 +02003536 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3537 }
3538 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003539 break;
3540
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003541 case ACT_HTTP_REQ_SET_SRC:
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003542 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
3543 struct sample *smp;
3544
Thierry FOURNIERa002dc92015-07-31 08:50:51 +02003545 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003546
3547 if (smp) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003548 if (smp->data.type == SMP_T_IPV4) {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003549 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_family = AF_INET;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003550 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003551 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003552 } else if (smp->data.type == SMP_T_IPV6) {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003553 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_family = AF_INET6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003554 memcpy(&((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, &smp->data.u.ipv6, sizeof(struct in6_addr));
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003555 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = 0;
3556 }
3557 }
3558 }
3559 break;
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003560
3561 /* other flags exists, but normaly, they never be matched. */
3562 default:
3563 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003564 }
3565 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003566
3567 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003568 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003569}
3570
Willy Tarreau71241ab2012-12-27 11:30:54 +01003571
Willy Tarreau51d861a2015-05-22 17:30:48 +02003572/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3573 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3574 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3575 * is returned, the process can continue the evaluation of next rule list. If
3576 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3577 * is returned, it means the operation could not be processed and a server error
3578 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3579 * deny rule. If *YIELD is returned, the caller must call again the function
3580 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003581 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003582static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003583http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003584{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003585 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003586 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003587 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003588 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003589 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02003590 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003591
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003592 /* If "the current_rule_list" match the executed rule list, we are in
3593 * resume condition. If a resume is needed it is always in the action
3594 * and never in the ACL or converters. In this case, we initialise the
3595 * current rule, and go to the action execution point.
3596 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003597 if (s->current_rule) {
3598 rule = s->current_rule;
3599 s->current_rule = NULL;
3600 if (s->current_rule_list == rules)
3601 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003602 }
3603 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003604
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003605 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003606
3607 /* check optional condition */
3608 if (rule->cond) {
3609 int ret;
3610
Willy Tarreau192252e2015-04-04 01:47:55 +02003611 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003612 ret = acl_pass(ret);
3613
3614 if (rule->cond->pol == ACL_COND_UNLESS)
3615 ret = !ret;
3616
3617 if (!ret) /* condition not matched */
3618 continue;
3619 }
3620
Willy Tarreauacc98002015-09-27 23:34:39 +02003621 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003622resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003623 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003624 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003625 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003626
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003627 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003628 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003629 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003630
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003631 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003632 s->task->nice = rule->arg.nice;
3633 break;
3634
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003635 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003636 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003637 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003638 break;
3639
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003640 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003641#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003642 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003643 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003644#endif
3645 break;
3646
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003647 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003648 s->logs.level = rule->arg.loglevel;
3649 break;
3650
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003651 case ACT_HTTP_REPLACE_HDR:
3652 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003653 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3654 rule->arg.hdr_add.name_len,
3655 &rule->arg.hdr_add.fmt,
3656 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003657 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003658 break;
3659
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003660 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003661 ctx.idx = 0;
3662 /* remove all occurrences of the header */
3663 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3664 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3665 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3666 }
Willy Tarreau85603282015-01-21 20:39:27 +01003667 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003668
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003669 case ACT_HTTP_SET_HDR:
3670 case ACT_HTTP_ADD_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003671 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3672 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3673 trash.len = rule->arg.hdr_add.name_len;
3674 trash.str[trash.len++] = ':';
3675 trash.str[trash.len++] = ' ';
3676 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 +01003677
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003678 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003679 /* remove all occurrences of the header */
3680 ctx.idx = 0;
3681 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3682 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3683 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3684 }
3685 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003686 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3687 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003688
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003689 case ACT_HTTP_DEL_ACL:
3690 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003691 struct pat_ref *ref;
3692 char *key;
3693 int len;
3694
3695 /* collect reference */
3696 ref = pat_ref_lookup(rule->arg.map.ref);
3697 if (!ref)
3698 continue;
3699
3700 /* collect key */
3701 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3702 key = trash.str;
3703 key[len] = '\0';
3704
3705 /* perform update */
3706 /* returned code: 1=ok, 0=ko */
3707 pat_ref_delete(ref, key);
3708
3709 break;
3710 }
3711
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003712 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003713 struct pat_ref *ref;
3714 char *key;
3715 struct chunk *trash_key;
3716 int len;
3717
3718 trash_key = get_trash_chunk();
3719
3720 /* collect reference */
3721 ref = pat_ref_lookup(rule->arg.map.ref);
3722 if (!ref)
3723 continue;
3724
3725 /* collect key */
3726 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3727 key = trash_key->str;
3728 key[len] = '\0';
3729
3730 /* perform update */
3731 /* check if the entry already exists */
3732 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003733 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003734
3735 break;
3736 }
3737
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003738 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003739 struct pat_ref *ref;
3740 char *key, *value;
3741 struct chunk *trash_key, *trash_value;
3742 int len;
3743
3744 trash_key = get_trash_chunk();
3745 trash_value = get_trash_chunk();
3746
3747 /* collect reference */
3748 ref = pat_ref_lookup(rule->arg.map.ref);
3749 if (!ref)
3750 continue;
3751
3752 /* collect key */
3753 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3754 key = trash_key->str;
3755 key[len] = '\0';
3756
3757 /* collect value */
3758 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3759 value = trash_value->str;
3760 value[len] = '\0';
3761
3762 /* perform update */
3763 if (pat_ref_find_elt(ref, key) != NULL)
3764 /* update entry if it exists */
3765 pat_ref_set(ref, key, value, NULL);
3766 else
3767 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003768 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003769
3770 break;
3771 }
William Lallemand73025dd2014-04-24 14:38:37 +02003772
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003773 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003774 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3775 return HTTP_RULE_RES_BADREQ;
3776 return HTTP_RULE_RES_DONE;
3777
Thierry FOURNIER42148732015-09-02 17:17:33 +02003778 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003779 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3780 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003781
Willy Tarreauacc98002015-09-27 23:34:39 +02003782 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003783 case ACT_RET_ERR:
3784 case ACT_RET_CONT:
3785 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003786 case ACT_RET_STOP:
3787 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003788 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003789 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003790 return HTTP_RULE_RES_YIELD;
3791 }
William Lallemand73025dd2014-04-24 14:38:37 +02003792 break;
3793
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003794 /* other flags exists, but normaly, they never be matched. */
3795 default:
3796 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003797 }
3798 }
3799
3800 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003801 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003802}
3803
3804
Willy Tarreau71241ab2012-12-27 11:30:54 +01003805/* Perform an HTTP redirect based on the information in <rule>. The function
3806 * returns non-zero on success, or zero in case of a, irrecoverable error such
3807 * as too large a request to build a valid response.
3808 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003809static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003810{
Willy Tarreaub329a312015-05-22 16:27:37 +02003811 struct http_msg *req = &txn->req;
3812 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003813 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003814 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003815
3816 /* build redirect message */
3817 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003818 case 308:
3819 msg_fmt = HTTP_308;
3820 break;
3821 case 307:
3822 msg_fmt = HTTP_307;
3823 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003824 case 303:
3825 msg_fmt = HTTP_303;
3826 break;
3827 case 301:
3828 msg_fmt = HTTP_301;
3829 break;
3830 case 302:
3831 default:
3832 msg_fmt = HTTP_302;
3833 break;
3834 }
3835
3836 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3837 return 0;
3838
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003839 location = trash.str + trash.len;
3840
Willy Tarreau71241ab2012-12-27 11:30:54 +01003841 switch(rule->type) {
3842 case REDIRECT_TYPE_SCHEME: {
3843 const char *path;
3844 const char *host;
3845 struct hdr_ctx ctx;
3846 int pathlen;
3847 int hostlen;
3848
3849 host = "";
3850 hostlen = 0;
3851 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02003852 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003853 host = ctx.line + ctx.val;
3854 hostlen = ctx.vlen;
3855 }
3856
3857 path = http_get_path(txn);
3858 /* build message using path */
3859 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003860 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003861 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3862 int qs = 0;
3863 while (qs < pathlen) {
3864 if (path[qs] == '?') {
3865 pathlen = qs;
3866 break;
3867 }
3868 qs++;
3869 }
3870 }
3871 } else {
3872 path = "/";
3873 pathlen = 1;
3874 }
3875
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003876 if (rule->rdr_str) { /* this is an old "redirect" rule */
3877 /* check if we can add scheme + "://" + host + path */
3878 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3879 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003880
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003881 /* add scheme */
3882 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3883 trash.len += rule->rdr_len;
3884 }
3885 else {
3886 /* add scheme with executing log format */
3887 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003888
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003889 /* check if we can add scheme + "://" + host + path */
3890 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3891 return 0;
3892 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003893 /* add "://" */
3894 memcpy(trash.str + trash.len, "://", 3);
3895 trash.len += 3;
3896
3897 /* add host */
3898 memcpy(trash.str + trash.len, host, hostlen);
3899 trash.len += hostlen;
3900
3901 /* add path */
3902 memcpy(trash.str + trash.len, path, pathlen);
3903 trash.len += pathlen;
3904
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003905 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003906 if (trash.len && trash.str[trash.len - 1] != '/' &&
3907 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3908 if (trash.len > trash.size - 5)
3909 return 0;
3910 trash.str[trash.len] = '/';
3911 trash.len++;
3912 }
3913
3914 break;
3915 }
3916 case REDIRECT_TYPE_PREFIX: {
3917 const char *path;
3918 int pathlen;
3919
3920 path = http_get_path(txn);
3921 /* build message using path */
3922 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003923 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003924 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3925 int qs = 0;
3926 while (qs < pathlen) {
3927 if (path[qs] == '?') {
3928 pathlen = qs;
3929 break;
3930 }
3931 qs++;
3932 }
3933 }
3934 } else {
3935 path = "/";
3936 pathlen = 1;
3937 }
3938
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003939 if (rule->rdr_str) { /* this is an old "redirect" rule */
3940 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3941 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003942
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003943 /* add prefix. Note that if prefix == "/", we don't want to
3944 * add anything, otherwise it makes it hard for the user to
3945 * configure a self-redirection.
3946 */
3947 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3948 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3949 trash.len += rule->rdr_len;
3950 }
3951 }
3952 else {
3953 /* add prefix with executing log format */
3954 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3955
3956 /* Check length */
3957 if (trash.len + pathlen > trash.size - 4)
3958 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003959 }
3960
3961 /* add path */
3962 memcpy(trash.str + trash.len, path, pathlen);
3963 trash.len += pathlen;
3964
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003965 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003966 if (trash.len && trash.str[trash.len - 1] != '/' &&
3967 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3968 if (trash.len > trash.size - 5)
3969 return 0;
3970 trash.str[trash.len] = '/';
3971 trash.len++;
3972 }
3973
3974 break;
3975 }
3976 case REDIRECT_TYPE_LOCATION:
3977 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003978 if (rule->rdr_str) { /* this is an old "redirect" rule */
3979 if (trash.len + rule->rdr_len > trash.size - 4)
3980 return 0;
3981
3982 /* add location */
3983 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3984 trash.len += rule->rdr_len;
3985 }
3986 else {
3987 /* add location with executing log format */
3988 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003989
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003990 /* Check left length */
3991 if (trash.len > trash.size - 4)
3992 return 0;
3993 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003994 break;
3995 }
3996
3997 if (rule->cookie_len) {
3998 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3999 trash.len += 14;
4000 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4001 trash.len += rule->cookie_len;
4002 memcpy(trash.str + trash.len, "\r\n", 2);
4003 trash.len += 2;
4004 }
4005
4006 /* add end of headers and the keep-alive/close status.
4007 * We may choose to set keep-alive if the Location begins
4008 * with a slash, because the client will come back to the
4009 * same server.
4010 */
4011 txn->status = rule->code;
4012 /* let's log the request time */
4013 s->logs.tv_request = now;
4014
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004015 if (*location == '/' &&
Willy Tarreaub329a312015-05-22 16:27:37 +02004016 (req->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau2de8a502015-05-28 17:23:54 +02004017 ((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004018 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4019 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4020 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02004021 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004022 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4023 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4024 trash.len += 30;
4025 } else {
4026 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4027 trash.len += 24;
4028 }
4029 }
4030 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4031 trash.len += 4;
Christopher Faulet3e344292015-11-24 16:24:13 +01004032 FLT_STRM_CB(s, flt_http_reply(s, txn->status, &trash));
Willy Tarreaub329a312015-05-22 16:27:37 +02004033 bo_inject(res->chn, trash.str, trash.len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004034 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02004035 bi_fast_delete(req->chn->buf, req->sov);
4036 req->next -= req->sov;
4037 req->sov = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004038 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_FLT_END);
4039 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->req.analysers & AN_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02004040 req->msg_state = HTTP_MSG_CLOSED;
4041 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02004042 /* Trim any possible response */
4043 res->chn->buf->i = 0;
4044 res->next = res->sov = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004045 } else {
4046 /* keep-alive not possible */
4047 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4048 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4049 trash.len += 29;
4050 } else {
4051 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4052 trash.len += 23;
4053 }
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004054 http_reply_and_close(s, txn->status, &trash);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004055 req->chn->analysers &= AN_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004056 }
4057
Willy Tarreaue7dff022015-04-03 01:14:29 +02004058 if (!(s->flags & SF_ERR_MASK))
4059 s->flags |= SF_ERR_LOCAL;
4060 if (!(s->flags & SF_FINST_MASK))
4061 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004062
4063 return 1;
4064}
4065
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004066/* This stream analyser runs all HTTP request processing which is common to
4067 * frontends and backends, which means blocking ACLs, filters, connection-close,
4068 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004069 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004070 * either needs more data or wants to immediately abort the request (eg: deny,
4071 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004072 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004073int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004074{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004075 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004076 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004077 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004078 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004079 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004080 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02004081
Willy Tarreau655dce92009-11-08 13:10:58 +01004082 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004083 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004084 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004085 }
4086
Willy Tarreau87b09662015-04-03 00:22:06 +02004087 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 +02004088 now_ms, __FUNCTION__,
4089 s,
4090 req,
4091 req->rex, req->wex,
4092 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004093 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004094 req->analysers);
4095
Willy Tarreau65410832014-04-28 21:25:43 +02004096 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004097 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004098
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004099 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004100 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004101 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Willy Tarreau51425942010-02-01 10:40:19 +01004102
Willy Tarreau0b748332014-04-29 00:13:29 +02004103 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004104 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4105 goto return_prx_yield;
4106
Willy Tarreau0b748332014-04-29 00:13:29 +02004107 case HTTP_RULE_RES_CONT:
4108 case HTTP_RULE_RES_STOP: /* nothing to do */
4109 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004110
Willy Tarreau0b748332014-04-29 00:13:29 +02004111 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4112 if (txn->flags & TX_CLTARPIT)
4113 goto tarpit;
4114 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004115
Willy Tarreau0b748332014-04-29 00:13:29 +02004116 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4117 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004118
Willy Tarreau0b748332014-04-29 00:13:29 +02004119 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004120 goto done;
4121
Willy Tarreau0b748332014-04-29 00:13:29 +02004122 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4123 goto return_bad_req;
4124 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004125 }
4126
Willy Tarreau52542592014-04-28 18:33:26 +02004127 /* OK at this stage, we know that the request was accepted according to
4128 * the http-request rules, we can check for the stats. Note that the
4129 * URI is detected *before* the req* rules in order not to be affected
4130 * by a possible reqrep, while they are processed *after* so that a
4131 * reqdeny can still block them. This clearly needs to change in 1.6!
4132 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004133 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004134 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004135 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004136 txn->status = 500;
4137 s->logs.tv_request = now;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004138 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004139
Willy Tarreaue7dff022015-04-03 01:14:29 +02004140 if (!(s->flags & SF_ERR_MASK))
4141 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004142 goto return_prx_cond;
4143 }
4144
4145 /* parse the whole stats request and extract the relevant information */
4146 http_handle_stats(s, req);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004147 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Willy Tarreau0b748332014-04-29 00:13:29 +02004148 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004149
Willy Tarreau0b748332014-04-29 00:13:29 +02004150 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4151 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004152
Willy Tarreau0b748332014-04-29 00:13:29 +02004153 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4154 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004155 }
4156
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004157 /* evaluate the req* rules except reqadd */
4158 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004159 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004160 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004161
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004162 if (txn->flags & TX_CLDENY)
4163 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004164
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004165 if (txn->flags & TX_CLTARPIT)
4166 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004167 }
Willy Tarreau06619262006-12-17 08:37:22 +01004168
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004169 /* add request headers from the rule sets in the same order */
4170 list_for_each_entry(wl, &px->req_add, list) {
4171 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004172 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004173 ret = acl_pass(ret);
4174 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4175 ret = !ret;
4176 if (!ret)
4177 continue;
4178 }
4179
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004180 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004181 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004182 }
4183
Willy Tarreau52542592014-04-28 18:33:26 +02004184
4185 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004186 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004187 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004188 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4189 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004190
Willy Tarreaue7dff022015-04-03 01:14:29 +02004191 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4192 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4193 if (!(s->flags & SF_FINST_MASK))
4194 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004195
Willy Tarreau70730dd2014-04-24 18:06:27 +02004196 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet309c6412015-12-02 09:57:32 +01004197 req->analysers &= (AN_REQ_HTTP_BODY | AN_FLT_HTTP_HDRS | AN_FLT_END);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004198 req->analysers &= ~AN_FLT_XFER_DATA;
4199 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004200 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004201 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004202
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004203 /* check whether we have some ACLs set to redirect this request */
4204 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004205 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004206 int ret;
4207
Willy Tarreau192252e2015-04-04 01:47:55 +02004208 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004209 ret = acl_pass(ret);
4210 if (rule->cond->pol == ACL_COND_UNLESS)
4211 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004212 if (!ret)
4213 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004214 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004215 if (!http_apply_redirect_rule(rule, s, txn))
4216 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004217 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004218 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004219
Willy Tarreau2be39392010-01-03 17:24:51 +01004220 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4221 * If this happens, then the data will not come immediately, so we must
4222 * send all what we have without waiting. Note that due to the small gain
4223 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004224 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004225 * itself once used.
4226 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004227 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004228
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004229 done: /* done with this analyser, continue with next ones that the calling
4230 * points will have set, if any.
4231 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004232 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004233 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4234 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004235 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004236
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004237 tarpit:
4238 /* When a connection is tarpitted, we use the tarpit timeout,
4239 * which may be the same as the connect timeout if unspecified.
4240 * If unset, then set it to zero because we really want it to
4241 * eventually expire. We build the tarpit as an analyser.
4242 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004243 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004244
4245 /* wipe the request out so that we can drop the connection early
4246 * if the client closes first.
4247 */
4248 channel_dont_connect(req);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004249 req->analysers &= AN_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004250 req->analysers |= AN_REQ_HTTP_TARPIT;
4251 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4252 if (!req->analyse_exp)
4253 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004254 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004255 sess->fe->fe_counters.denied_req++;
4256 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004257 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004258 if (sess->listener->counters)
4259 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004260 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004261
4262 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004263 txn->flags |= TX_CLDENY;
CJ Ess108b1dd2015-04-07 12:03:37 -04004264 txn->status = http_err_codes[txn->rule_deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004265 s->logs.tv_request = now;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004266 http_reply_and_close(s, txn->status, http_error_message(s, txn->rule_deny_status));
Willy Tarreau87b09662015-04-03 00:22:06 +02004267 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004268 sess->fe->fe_counters.denied_req++;
4269 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004270 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004271 if (sess->listener->counters)
4272 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004273 goto return_prx_cond;
4274
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004275 return_bad_req:
4276 /* We centralize bad requests processing here */
4277 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4278 /* we detected a parsing error. We want to archive this request
4279 * in the dedicated proxy area for later troubleshooting.
4280 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004281 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004282 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004283
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004284 txn->req.msg_state = HTTP_MSG_ERROR;
4285 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004286 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004287
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004288 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004289 if (sess->listener->counters)
4290 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004291
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004292 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004293 if (!(s->flags & SF_ERR_MASK))
4294 s->flags |= SF_ERR_PRXCOND;
4295 if (!(s->flags & SF_FINST_MASK))
4296 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004297
Christopher Fauletd7c91962015-04-30 11:48:27 +02004298 req->analysers &= AN_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004299 req->analyse_exp = TICK_ETERNITY;
4300 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004301
4302 return_prx_yield:
4303 channel_dont_connect(req);
4304 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004305}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004306
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004307/* This function performs all the processing enabled for the current request.
4308 * It returns 1 if the processing can continue on next analysers, or zero if it
4309 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004310 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004311 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004312int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004313{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004314 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004315 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004316 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004317 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004318
Willy Tarreau655dce92009-11-08 13:10:58 +01004319 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004320 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004321 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004322 return 0;
4323 }
4324
Willy Tarreau87b09662015-04-03 00:22:06 +02004325 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 +02004326 now_ms, __FUNCTION__,
4327 s,
4328 req,
4329 req->rex, req->wex,
4330 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004331 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004332 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004333
Willy Tarreau59234e92008-11-30 23:51:27 +01004334 /*
4335 * Right now, we know that we have processed the entire headers
4336 * and that unwanted requests have been filtered out. We can do
4337 * whatever we want with the remaining request. Also, now we
4338 * may have separate values for ->fe, ->be.
4339 */
Willy Tarreau06619262006-12-17 08:37:22 +01004340
Willy Tarreau59234e92008-11-30 23:51:27 +01004341 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004342 * If HTTP PROXY is set we simply get remote server address parsing
4343 * incoming request. Note that this requires that a connection is
4344 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004345 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004346 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004347 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004348 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004349
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004350 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau973a5422015-08-05 21:47:23 +02004351 if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004352 txn->req.msg_state = HTTP_MSG_ERROR;
4353 txn->status = 500;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004354 req->analysers &= AN_FLT_END;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004355 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004356
Willy Tarreaue7dff022015-04-03 01:14:29 +02004357 if (!(s->flags & SF_ERR_MASK))
4358 s->flags |= SF_ERR_RESOURCE;
4359 if (!(s->flags & SF_FINST_MASK))
4360 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004361
4362 return 0;
4363 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004364
4365 path = http_get_path(txn);
4366 url2sa(req->buf->p + msg->sl.rq.u,
4367 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004368 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004369 /* if the path was found, we have to remove everything between
4370 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4371 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4372 * u_l characters by a single "/".
4373 */
4374 if (path) {
4375 char *cur_ptr = req->buf->p;
4376 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4377 int delta;
4378
4379 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4380 http_msg_move_end(&txn->req, delta);
4381 cur_end += delta;
4382 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4383 goto return_bad_req;
4384 }
4385 else {
4386 char *cur_ptr = req->buf->p;
4387 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4388 int delta;
4389
4390 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4391 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4392 http_msg_move_end(&txn->req, delta);
4393 cur_end += delta;
4394 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4395 goto return_bad_req;
4396 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004397 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004398
Willy Tarreau59234e92008-11-30 23:51:27 +01004399 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004400 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004401 * Note that doing so might move headers in the request, but
4402 * the fields will stay coherent and the URI will not move.
4403 * This should only be performed in the backend.
4404 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02004405 if ((s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004406 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4407 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004408
William Lallemanda73203e2012-03-12 12:48:57 +01004409 /* add unique-id if "header-unique-id" is specified */
4410
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004411 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004412 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4413 goto return_bad_req;
4414 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004415 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004416 }
William Lallemanda73203e2012-03-12 12:48:57 +01004417
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004418 if (sess->fe->header_unique_id && s->unique_id) {
4419 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004420 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004421 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004422 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004423 goto return_bad_req;
4424 }
4425
Cyril Bontéb21570a2009-11-29 20:04:48 +01004426 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004427 * 9: add X-Forwarded-For if either the frontend or the backend
4428 * asks for it.
4429 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004430 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004431 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004432 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4433 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4434 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004435 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004436 /* The header is set to be added only if none is present
4437 * and we found it, so don't do anything.
4438 */
4439 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004440 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004441 /* Add an X-Forwarded-For header unless the source IP is
4442 * in the 'except' network range.
4443 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004444 if ((!sess->fe->except_mask.s_addr ||
4445 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4446 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004447 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004448 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004449 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004450 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004451 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004452 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004453
4454 /* Note: we rely on the backend to get the header name to be used for
4455 * x-forwarded-for, because the header is really meant for the backends.
4456 * However, if the backend did not specify any option, we have to rely
4457 * on the frontend's header name.
4458 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004459 if (s->be->fwdfor_hdr_len) {
4460 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004461 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004462 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004463 len = sess->fe->fwdfor_hdr_len;
4464 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004465 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004466 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 +01004467
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004468 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004469 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004470 }
4471 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004472 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004473 /* FIXME: for the sake of completeness, we should also support
4474 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004475 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004476 int len;
4477 char pn[INET6_ADDRSTRLEN];
4478 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004479 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004480 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004481
Willy Tarreau59234e92008-11-30 23:51:27 +01004482 /* Note: we rely on the backend to get the header name to be used for
4483 * x-forwarded-for, because the header is really meant for the backends.
4484 * However, if the backend did not specify any option, we have to rely
4485 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004486 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004487 if (s->be->fwdfor_hdr_len) {
4488 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004489 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004490 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004491 len = sess->fe->fwdfor_hdr_len;
4492 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004493 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004494 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004495
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004496 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004497 goto return_bad_req;
4498 }
4499 }
4500
4501 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004502 * 10: add X-Original-To if either the frontend or the backend
4503 * asks for it.
4504 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004505 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004506
4507 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004508 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004509 /* Add an X-Original-To header unless the destination IP is
4510 * in the 'except' network range.
4511 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004512 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004513
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004514 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004515 ((!sess->fe->except_mask_to.s_addr ||
4516 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4517 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004518 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004519 (((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 +02004520 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004521 int len;
4522 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004523 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004524
4525 /* Note: we rely on the backend to get the header name to be used for
4526 * x-original-to, because the header is really meant for the backends.
4527 * However, if the backend did not specify any option, we have to rely
4528 * on the frontend's header name.
4529 */
4530 if (s->be->orgto_hdr_len) {
4531 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004532 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004533 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004534 len = sess->fe->orgto_hdr_len;
4535 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004536 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004537 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 +02004538
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004539 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004540 goto return_bad_req;
4541 }
4542 }
4543 }
4544
Willy Tarreau50fc7772012-11-11 22:19:57 +01004545 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4546 * If an "Upgrade" token is found, the header is left untouched in order not to have
4547 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4548 * "Upgrade" is present in the Connection header.
4549 */
4550 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4551 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004552 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004553 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004554 unsigned int want_flags = 0;
4555
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004556 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004557 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004558 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004559 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004560 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004561 want_flags |= TX_CON_CLO_SET;
4562 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004563 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004564 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004565 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004566 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004567 want_flags |= TX_CON_KAL_SET;
4568 }
4569
4570 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004571 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004572 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004573
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004574
Willy Tarreau522d6c02009-12-06 18:49:18 +01004575 /* If we have no server assigned yet and we're balancing on url_param
4576 * with a POST request, we may be interested in checking the body for
4577 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004578 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004579 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004580 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004581 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004582 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004583 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004584 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004585
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004586 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02004587 req->analysers &= ~AN_FLT_XFER_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004588 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004589#ifdef TCP_QUICKACK
4590 /* We expect some data from the client. Unless we know for sure
4591 * we already have a full request, we have to re-enable quick-ack
4592 * in case we previously disabled it, otherwise we might cause
4593 * the client to delay further data.
4594 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004595 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004596 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004597 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004598 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004599 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004600#endif
4601 }
Willy Tarreau03945942009-12-22 16:50:27 +01004602
Willy Tarreau59234e92008-11-30 23:51:27 +01004603 /*************************************************************
4604 * OK, that's finished for the headers. We have done what we *
4605 * could. Let's switch to the DATA state. *
4606 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004607 req->analyse_exp = TICK_ETERNITY;
4608 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004609
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004610 /* if the server closes the connection, we want to immediately react
4611 * and close the socket to save packets and syscalls.
4612 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004613 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004614 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004615
Willy Tarreau59234e92008-11-30 23:51:27 +01004616 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004617 /* OK let's go on with the BODY now */
4618 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004619
Willy Tarreau59234e92008-11-30 23:51:27 +01004620 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004621 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004622 /* we detected a parsing error. We want to archive this request
4623 * in the dedicated proxy area for later troubleshooting.
4624 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004625 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004626 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004627
Willy Tarreau59234e92008-11-30 23:51:27 +01004628 txn->req.msg_state = HTTP_MSG_ERROR;
4629 txn->status = 400;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004630 req->analysers &= AN_FLT_END;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004631 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004632
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004633 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004634 if (sess->listener->counters)
4635 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004636
Willy Tarreaue7dff022015-04-03 01:14:29 +02004637 if (!(s->flags & SF_ERR_MASK))
4638 s->flags |= SF_ERR_PRXCOND;
4639 if (!(s->flags & SF_FINST_MASK))
4640 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004641 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004642}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004643
Willy Tarreau60b85b02008-11-30 23:28:40 +01004644/* This function is an analyser which processes the HTTP tarpit. It always
4645 * returns zero, at the beginning because it prevents any other processing
4646 * from occurring, and at the end because it terminates the request.
4647 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004648int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004649{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004650 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004651
4652 /* This connection is being tarpitted. The CLIENT side has
4653 * already set the connect expiration date to the right
4654 * timeout. We just have to check that the client is still
4655 * there and that the timeout has not expired.
4656 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004657 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004658 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004659 !tick_is_expired(req->analyse_exp, now_ms))
4660 return 0;
4661
4662 /* We will set the queue timer to the time spent, just for
4663 * logging purposes. We fake a 500 server error, so that the
4664 * attacker will not suspect his connection has been tarpitted.
4665 * It will not cause trouble to the logs because we can exclude
4666 * the tarpitted connections by filtering on the 'PT' status flags.
4667 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004668 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4669
4670 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004671 if (!(req->flags & CF_READ_ERROR))
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004672 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004673
Christopher Fauletd7c91962015-04-30 11:48:27 +02004674 req->analysers &= AN_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004675 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004676
Willy Tarreaue7dff022015-04-03 01:14:29 +02004677 if (!(s->flags & SF_ERR_MASK))
4678 s->flags |= SF_ERR_PRXCOND;
4679 if (!(s->flags & SF_FINST_MASK))
4680 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004681 return 0;
4682}
4683
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004684/* This function is an analyser which waits for the HTTP request body. It waits
4685 * for either the buffer to be full, or the full advertised contents to have
4686 * reached the buffer. It must only be called after the standard HTTP request
4687 * processing has occurred, because it expects the request to be parsed and will
4688 * look for the Expect header. It may send a 100-Continue interim response. It
4689 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4690 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4691 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004692 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004693int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004694{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004695 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004696 struct http_txn *txn = s->txn;
4697 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004698
4699 /* We have to parse the HTTP request body to find any required data.
4700 * "balance url_param check_post" should have been the only way to get
4701 * into this. We were brought here after HTTP header analysis, so all
4702 * related structures are ready.
4703 */
4704
Willy Tarreau890988f2014-04-10 11:59:33 +02004705 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4706 /* This is the first call */
4707 if (msg->msg_state < HTTP_MSG_BODY)
4708 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004709
Willy Tarreau890988f2014-04-10 11:59:33 +02004710 if (msg->msg_state < HTTP_MSG_100_SENT) {
4711 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4712 * send an HTTP/1.1 100 Continue intermediate response.
4713 */
4714 if (msg->flags & HTTP_MSGF_VER_11) {
4715 struct hdr_ctx ctx;
4716 ctx.idx = 0;
4717 /* Expect is allowed in 1.1, look for it */
4718 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4719 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004720 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004721 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004722 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004723 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004724 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004725
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004726 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004727 * req->buf->p still points to the beginning of the message. We
4728 * must save the body in msg->next because it survives buffer
4729 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004730 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004731 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004732
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004733 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004734 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4735 else
4736 msg->msg_state = HTTP_MSG_DATA;
4737 }
4738
Willy Tarreau890988f2014-04-10 11:59:33 +02004739 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4740 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004741 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004742 goto missing_data;
4743
4744 /* OK we have everything we need now */
4745 goto http_end;
4746 }
4747
4748 /* OK here we're parsing a chunked-encoded message */
4749
Willy Tarreau522d6c02009-12-06 18:49:18 +01004750 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004751 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004752 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004753 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004754 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004755 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004756
Willy Tarreau115acb92009-12-26 13:56:06 +01004757 if (!ret)
4758 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004759 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004760 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004761 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004762 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004763 msg->next += msg->sol;
4764 msg->sol = 0;
4765 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01004766 }
4767
Willy Tarreaud98cf932009-12-27 22:54:55 +01004768 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004769 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4770 * for at least a whole chunk or the whole content length bytes after
4771 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004772 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004773 if (msg->msg_state == HTTP_MSG_TRAILERS)
4774 goto http_end;
4775
Willy Tarreaue115b492015-05-01 23:05:14 +02004776 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004777 goto http_end;
4778
4779 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004780 /* we get here if we need to wait for more data. If the buffer is full,
4781 * we have the maximum we can expect.
4782 */
4783 if (buffer_full(req->buf, global.tune.maxrewrite))
4784 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004785
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004786 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004787 txn->status = 408;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004788 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004789
Willy Tarreaue7dff022015-04-03 01:14:29 +02004790 if (!(s->flags & SF_ERR_MASK))
4791 s->flags |= SF_ERR_CLITO;
4792 if (!(s->flags & SF_FINST_MASK))
4793 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004794 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004795 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004796
4797 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004798 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004799 /* Not enough data. We'll re-use the http-request
4800 * timeout here. Ideally, we should set the timeout
4801 * relative to the accept() date. We just set the
4802 * request timeout once at the beginning of the
4803 * request.
4804 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004805 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004806 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004807 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004808 return 0;
4809 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004810
4811 http_end:
4812 /* The situation will not evolve, so let's give up on the analysis. */
4813 s->logs.tv_request = now; /* update the request timer to reflect full request */
4814 req->analysers &= ~an_bit;
4815 req->analyse_exp = TICK_ETERNITY;
4816 return 1;
4817
4818 return_bad_req: /* let's centralize all bad requests */
4819 txn->req.msg_state = HTTP_MSG_ERROR;
4820 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004821 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004822
Willy Tarreaue7dff022015-04-03 01:14:29 +02004823 if (!(s->flags & SF_ERR_MASK))
4824 s->flags |= SF_ERR_PRXCOND;
4825 if (!(s->flags & SF_FINST_MASK))
4826 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004827
Willy Tarreau522d6c02009-12-06 18:49:18 +01004828 return_err_msg:
Christopher Fauletd7c91962015-04-30 11:48:27 +02004829 req->analysers &= AN_FLT_END;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004830 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004831 if (sess->listener->counters)
4832 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004833 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004834}
4835
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004836/* send a server's name with an outgoing request over an established connection.
4837 * Note: this function is designed to be called once the request has been scheduled
4838 * for being forwarded. This is the reason why it rewinds the buffer before
4839 * proceeding.
4840 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004841int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004842
4843 struct hdr_ctx ctx;
4844
Mark Lamourinec2247f02012-01-04 13:02:01 -05004845 char *hdr_name = be->server_id_hdr_name;
4846 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004847 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004848 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004849 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004850
William Lallemandd9e90662012-01-30 17:27:17 +01004851 ctx.idx = 0;
4852
Willy Tarreau211cdec2014-04-17 20:18:08 +02004853 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004854 if (old_o) {
4855 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004856 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004857 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004858 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004859 }
4860
Willy Tarreau9b28e032012-10-12 23:49:43 +02004861 old_i = chn->buf->i;
4862 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 -05004863 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004864 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004865 }
4866
4867 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004868 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004869 memcpy(hdr_val, hdr_name, hdr_name_len);
4870 hdr_val += hdr_name_len;
4871 *hdr_val++ = ':';
4872 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004873 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4874 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004875
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004876 if (old_o) {
4877 /* If this was a forwarded request, we must readjust the amount of
4878 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004879 * variations. Note that the current state is >= HTTP_MSG_BODY,
4880 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004881 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004882 old_o += chn->buf->i - old_i;
4883 b_adv(chn->buf, old_o);
4884 txn->req.next -= old_o;
4885 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004886 }
4887
Mark Lamourinec2247f02012-01-04 13:02:01 -05004888 return 0;
4889}
4890
Willy Tarreau610ecce2010-01-04 21:15:02 +01004891/* Terminate current transaction and prepare a new one. This is very tricky
4892 * right now but it works.
4893 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004894void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004895{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004896 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02004897 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01004898 struct proxy *be = s->be;
Willy Tarreau323a2d92015-08-04 19:00:17 +02004899 struct connection *srv_conn;
4900 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02004901 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01004902
Willy Tarreau610ecce2010-01-04 21:15:02 +01004903 /* FIXME: We need a more portable way of releasing a backend's and a
4904 * server's connections. We need a safer way to reinitialize buffer
4905 * flags. We also need a more accurate method for computing per-request
4906 * data.
4907 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02004908 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004909
Willy Tarreau4213a112013-12-15 10:25:42 +01004910 /* unless we're doing keep-alive, we want to quickly close the connection
4911 * to the server.
4912 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004913 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004914 !si_conn_ready(&s->si[1])) {
4915 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4916 si_shutr(&s->si[1]);
4917 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004918 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004919
Willy Tarreaue7dff022015-04-03 01:14:29 +02004920 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau858b1032015-12-07 17:04:59 +01004921 be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004922 if (unlikely(s->srv_conn))
4923 sess_change_server(s, NULL);
4924 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004925
4926 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02004927 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004928
Willy Tarreaueee5b512015-04-03 23:46:31 +02004929 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004930 int n;
4931
Willy Tarreaueee5b512015-04-03 23:46:31 +02004932 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004933 if (n < 1 || n > 5)
4934 n = 0;
4935
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004936 if (fe->mode == PR_MODE_HTTP) {
4937 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004938 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02004939 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01004940 (be->mode == PR_MODE_HTTP)) {
4941 be->be_counters.p.http.rsp[n]++;
4942 be->be_counters.p.http.cum_req++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004943 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004944 }
4945
4946 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004947 s->logs.bytes_in -= s->req.buf->i;
4948 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004949
4950 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004951 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02004952 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004953 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004954 s->do_log(s);
4955 }
4956
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004957 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02004958 stream_stop_content_counters(s);
4959 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004960
Willy Tarreau610ecce2010-01-04 21:15:02 +01004961 s->logs.accept_date = date; /* user-visible date for logging */
4962 s->logs.tv_accept = now; /* corrected date for internal use */
4963 tv_zero(&s->logs.tv_request);
4964 s->logs.t_queue = -1;
4965 s->logs.t_connect = -1;
4966 s->logs.t_data = -1;
4967 s->logs.t_close = 0;
4968 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4969 s->logs.srv_queue_size = 0; /* we will get this number soon */
4970
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004971 s->logs.bytes_in = s->req.total = s->req.buf->i;
4972 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004973
4974 if (s->pend_pos)
4975 pendconn_free(s->pend_pos);
4976
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004977 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004978 if (s->flags & SF_CURR_SESS) {
4979 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004980 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004981 }
Willy Tarreau858b1032015-12-07 17:04:59 +01004982 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004983 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004984 }
4985
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004986 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004987
Willy Tarreau4213a112013-12-15 10:25:42 +01004988 /* only release our endpoint if we don't intend to reuse the
4989 * connection.
4990 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004991 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004992 !si_conn_ready(&s->si[1])) {
4993 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02004994 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004995 }
4996
Willy Tarreau350f4872014-11-28 14:42:25 +01004997 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
4998 s->si[1].err_type = SI_ET_NONE;
4999 s->si[1].conn_retries = 0; /* used for logging too */
5000 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005001 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 +01005002 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);
5003 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 +02005004 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5005 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5006 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005007
Willy Tarreaueee5b512015-04-03 23:46:31 +02005008 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005009 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005010 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005011
5012 if (prev_status == 401 || prev_status == 407) {
5013 /* In HTTP keep-alive mode, if we receive a 401, we still have
5014 * a chance of being able to send the visitor again to the same
5015 * server over the same connection. This is required by some
5016 * broken protocols such as NTLM, and anyway whenever there is
5017 * an opportunity for sending the challenge to the proper place,
5018 * it's better to do it (at least it helps with debugging).
5019 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005020 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02005021 if (srv_conn)
5022 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01005023 }
5024
Willy Tarreau53f96852016-02-02 18:50:47 +01005025 /* Never ever allow to reuse a connection from a non-reuse backend */
5026 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
5027 srv_conn->flags |= CO_FL_PRIVATE;
5028
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005029 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005030 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005031
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005032 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005033 s->req.flags |= CF_NEVER_WAIT;
5034 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005035 }
5036
Willy Tarreau610ecce2010-01-04 21:15:02 +01005037 /* if the request buffer is not empty, it means we're
5038 * about to process another request, so send pending
5039 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005040 * Just don't do this if the buffer is close to be full,
5041 * because the request will wait for it to flush a little
5042 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005043 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005044 if (s->req.buf->i) {
5045 if (s->res.buf->o &&
5046 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5047 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5048 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005049 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005050
Willy Tarreau714ea782015-11-25 20:11:11 +01005051 /* we're removing the analysers, we MUST re-enable events detection.
5052 * We don't enable close on the response channel since it's either
5053 * already closed, or in keep-alive with an idle connection handler.
5054 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005055 channel_auto_read(&s->req);
5056 channel_auto_close(&s->req);
5057 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005058
Willy Tarreau1c59bd52015-11-02 20:20:11 +01005059 /* we're in keep-alive with an idle connection, monitor it if not already done */
5060 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02005061 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005062 if (!srv)
5063 si_idle_conn(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01005064 else if (srv_conn->flags & CO_FL_PRIVATE)
Willy Tarreau8dff9982015-08-04 20:45:52 +02005065 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02005066 else if (prev_flags & TX_NOT_FIRST)
5067 /* note: we check the request, not the connection, but
5068 * this is valid for strategies SAFE and AGGR, and in
5069 * case of ALWS, we don't care anyway.
5070 */
5071 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005072 else
5073 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005074 }
Willy Tarreau27375622013-12-17 00:00:28 +01005075
Christopher Faulet3e344292015-11-24 16:24:13 +01005076 if (HAS_FILTERS(s)) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02005077 s->req.analysers &= AN_FLT_END;
5078 s->res.analysers &= AN_FLT_END;
5079 }
Christopher Faulet3e344292015-11-24 16:24:13 +01005080 else {
5081 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
5082 s->res.analysers = 0;
5083 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005084}
5085
5086
5087/* This function updates the request state machine according to the response
5088 * state machine and buffer flags. It returns 1 if it changes anything (flag
5089 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5090 * it is only used to find when a request/response couple is complete. Both
5091 * this function and its equivalent should loop until both return zero. It
5092 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5093 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005094int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005095{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005096 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005097 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005098 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005099 unsigned int old_state = txn->req.msg_state;
5100
Willy Tarreau610ecce2010-01-04 21:15:02 +01005101 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5102 return 0;
5103
5104 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005105 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005106 * We can shut the read side unless we want to abort_on_close,
5107 * or we have a POST request. The issue with POST requests is
5108 * that some browsers still send a CRLF after the request, and
5109 * this CRLF must be read so that it does not remain in the kernel
5110 * buffers, otherwise a close could cause an RST on some systems
5111 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005112 * Note that if we're using keep-alive on the client side, we'd
5113 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005114 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005115 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005116 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005117 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5118 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5119 !(s->be->options & PR_O_ABRT_CLOSE) &&
5120 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005121 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005122
Willy Tarreau40f151a2012-12-20 12:10:09 +01005123 /* if the server closes the connection, we want to immediately react
5124 * and close the socket to save packets and syscalls.
5125 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005126 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005127
Willy Tarreau7f876a12015-11-18 11:59:55 +01005128 /* In any case we've finished parsing the request so we must
5129 * disable Nagle when sending data because 1) we're not going
5130 * to shut this side, and 2) the server is waiting for us to
5131 * send pending data.
5132 */
5133 chn->flags |= CF_NEVER_WAIT;
5134
Willy Tarreau610ecce2010-01-04 21:15:02 +01005135 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5136 goto wait_other_side;
5137
5138 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5139 /* The server has not finished to respond, so we
5140 * don't want to move in order not to upset it.
5141 */
5142 goto wait_other_side;
5143 }
5144
5145 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5146 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005147 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005148 txn->req.msg_state = HTTP_MSG_TUNNEL;
5149 goto wait_other_side;
5150 }
5151
5152 /* When we get here, it means that both the request and the
5153 * response have finished receiving. Depending on the connection
5154 * mode, we'll have to wait for the last bytes to leave in either
5155 * direction, and sometimes for a close to be effective.
5156 */
5157
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005158 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5159 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005160 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5161 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005162 }
5163 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5164 /* Option forceclose is set, or either side wants to close,
5165 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005166 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005167 * once both states are CLOSED.
5168 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005169 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5170 channel_shutr_now(chn);
5171 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005172 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005173 }
5174 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005175 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5176 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005177 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005178 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5179 channel_auto_read(chn);
5180 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005181 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005182 }
5183
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005184 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005185 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005186 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005187
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005188 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005189 txn->req.msg_state = HTTP_MSG_CLOSING;
5190 goto http_msg_closing;
5191 }
5192 else {
5193 txn->req.msg_state = HTTP_MSG_CLOSED;
5194 goto http_msg_closed;
5195 }
5196 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005197 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005198 }
5199
5200 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5201 http_msg_closing:
5202 /* nothing else to forward, just waiting for the output buffer
5203 * to be empty and for the shutw_now to take effect.
5204 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005205 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005206 txn->req.msg_state = HTTP_MSG_CLOSED;
5207 goto http_msg_closed;
5208 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005209 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005210 txn->req.msg_state = HTTP_MSG_ERROR;
5211 goto wait_other_side;
5212 }
5213 }
5214
5215 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5216 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005217 /* see above in MSG_DONE why we only do this in these states */
5218 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5219 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5220 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005221 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005222 goto wait_other_side;
5223 }
5224
5225 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005226 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005227}
5228
5229
5230/* This function updates the response state machine according to the request
5231 * state machine and buffer flags. It returns 1 if it changes anything (flag
5232 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5233 * it is only used to find when a request/response couple is complete. Both
5234 * this function and its equivalent should loop until both return zero. It
5235 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5236 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005237int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005238{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005239 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005240 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005241 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005242 unsigned int old_state = txn->rsp.msg_state;
5243
Willy Tarreau610ecce2010-01-04 21:15:02 +01005244 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5245 return 0;
5246
5247 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5248 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005249 * still monitor the server connection for a possible close
5250 * while the request is being uploaded, so we don't disable
5251 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005252 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005253 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005254
5255 if (txn->req.msg_state == HTTP_MSG_ERROR)
5256 goto wait_other_side;
5257
5258 if (txn->req.msg_state < HTTP_MSG_DONE) {
5259 /* The client seems to still be sending data, probably
5260 * because we got an error response during an upload.
5261 * We have the choice of either breaking the connection
5262 * or letting it pass through. Let's do the later.
5263 */
5264 goto wait_other_side;
5265 }
5266
5267 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5268 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005269 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005270 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005271 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005272 goto wait_other_side;
5273 }
5274
5275 /* When we get here, it means that both the request and the
5276 * response have finished receiving. Depending on the connection
5277 * mode, we'll have to wait for the last bytes to leave in either
5278 * direction, and sometimes for a close to be effective.
5279 */
5280
5281 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5282 /* Server-close mode : shut read and wait for the request
5283 * side to close its output buffer. The caller will detect
5284 * when we're in DONE and the other is in CLOSED and will
5285 * catch that for the final cleanup.
5286 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005287 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5288 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005289 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005290 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5291 /* Option forceclose is set, or either side wants to close,
5292 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005293 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005294 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005295 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005296 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5297 channel_shutr_now(chn);
5298 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005299 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005300 }
5301 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005302 /* The last possible modes are keep-alive and tunnel. Tunnel will
5303 * need to forward remaining data. Keep-alive will need to monitor
5304 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005305 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005306 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005307 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005308 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5309 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005310 }
5311
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005312 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005313 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005314 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005315 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5316 goto http_msg_closing;
5317 }
5318 else {
5319 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5320 goto http_msg_closed;
5321 }
5322 }
5323 goto wait_other_side;
5324 }
5325
5326 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5327 http_msg_closing:
5328 /* nothing else to forward, just waiting for the output buffer
5329 * to be empty and for the shutw_now to take effect.
5330 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005331 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005332 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5333 goto http_msg_closed;
5334 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005335 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005336 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005337 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005338 if (objt_server(s->target))
5339 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005340 goto wait_other_side;
5341 }
5342 }
5343
5344 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5345 http_msg_closed:
5346 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005347 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005348 channel_auto_close(chn);
5349 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005350 goto wait_other_side;
5351 }
5352
5353 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005354 /* We force the response to leave immediately if we're waiting for the
5355 * other side, since there is no pending shutdown to push it out.
5356 */
5357 if (!channel_is_empty(chn))
5358 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005359 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005360}
5361
5362
5363/* Resync the request and response state machines. Return 1 if either state
5364 * changes.
5365 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005366int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005367{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005368 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005369 int old_req_state = txn->req.msg_state;
5370 int old_res_state = txn->rsp.msg_state;
5371
Willy Tarreau610ecce2010-01-04 21:15:02 +01005372 http_sync_req_state(s);
5373 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005374 if (!http_sync_res_state(s))
5375 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005376 if (!http_sync_req_state(s))
5377 break;
5378 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005379
Willy Tarreau610ecce2010-01-04 21:15:02 +01005380 /* OK, both state machines agree on a compatible state.
5381 * There are a few cases we're interested in :
5382 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5383 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5384 * directions, so let's simply disable both analysers.
5385 * - HTTP_MSG_CLOSED on the response only means we must abort the
5386 * request.
5387 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5388 * with server-close mode means we've completed one request and we
5389 * must re-initialize the server connection.
5390 */
5391
5392 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5393 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5394 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5395 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02005396 s->req.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005397 channel_auto_close(&s->req);
5398 channel_auto_read(&s->req);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005399 s->res.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005400 channel_auto_close(&s->res);
5401 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005402 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005403 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005404 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005405 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005406 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02005407 s->res.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005408 channel_auto_close(&s->res);
5409 channel_auto_read(&s->res);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005410 s->req.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005411 channel_abort(&s->req);
5412 channel_auto_close(&s->req);
5413 channel_auto_read(&s->req);
5414 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005415 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005416 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5417 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005418 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005419 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5420 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5421 /* server-close/keep-alive: terminate this transaction,
5422 * possibly killing the server connection and reinitialize
5423 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005424 */
5425 http_end_txn_clean_session(s);
5426 }
5427
Willy Tarreau610ecce2010-01-04 21:15:02 +01005428 return txn->req.msg_state != old_req_state ||
5429 txn->rsp.msg_state != old_res_state;
5430}
5431
Willy Tarreaud98cf932009-12-27 22:54:55 +01005432/* This function is an analyser which forwards request body (including chunk
5433 * sizes if any). It is called as soon as we must forward, even if we forward
5434 * zero byte. The only situation where it must not be called is when we're in
5435 * tunnel mode and we want to forward till the close. It's used both to forward
5436 * remaining data and to resync after end of body. It expects the msg_state to
5437 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005438 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005439 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005440 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005441 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005442int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005443{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005444 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005445 struct http_txn *txn = s->txn;
5446 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01005447 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005448
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005449 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5450 return 0;
5451
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005452 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005453 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005454 /* Output closed while we were sending data. We must abort and
5455 * wake the other side up.
5456 */
5457 msg->msg_state = HTTP_MSG_ERROR;
5458 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005459 return 1;
5460 }
5461
Willy Tarreaud98cf932009-12-27 22:54:55 +01005462 /* Note that we don't have to send 100-continue back because we don't
5463 * need the data to complete our job, and it's up to the server to
5464 * decide whether to return 100, 417 or anything else in return of
5465 * an "Expect: 100-continue" header.
5466 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02005467 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005468 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5469 ? HTTP_MSG_CHUNK_SIZE
5470 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005471
5472 /* TODO/filters: when http-buffer-request option is set or if a
5473 * rule on url_param exists, the first chunk size could be
5474 * already parsed. In that case, msg->next is after the chunk
5475 * size (including the CRLF after the size). So this case should
5476 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005477 }
5478
Willy Tarreau7ba23542014-04-17 21:50:00 +02005479 /* Some post-connect processing might want us to refrain from starting to
5480 * forward data. Currently, the only reason for this is "balance url_param"
5481 * whichs need to parse/process the request after we've enabled forwarding.
5482 */
5483 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005484 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005485 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005486 req->flags |= CF_WAKE_CONNECT;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005487 goto missing_data_or_waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005488 }
5489 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5490 }
5491
Willy Tarreau80a92c02014-03-12 10:41:13 +01005492 /* in most states, we should abort in case of early close */
5493 channel_auto_close(req);
5494
Willy Tarreauefdf0942014-04-24 20:08:57 +02005495 if (req->to_forward) {
5496 /* We can't process the buffer's contents yet */
5497 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005498 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005499 }
5500
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005501 if (msg->msg_state < HTTP_MSG_DONE) {
5502 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5503 ? http_msg_forward_chunked_body(s, msg)
5504 : http_msg_forward_body(s, msg));
5505 if (!ret)
5506 goto missing_data_or_waiting;
5507 if (ret < 0)
5508 goto return_bad_req;
5509 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005510
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005511 /* other states, DONE...TUNNEL */
5512 /* we don't want to forward closes on DONE except in tunnel mode. */
5513 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5514 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005515
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005516 ret = msg->msg_state;
5517 if (http_resync_states(s)) {
5518 /* some state changes occurred, maybe the analyser
5519 * was disabled too. */
5520 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5521 if (req->flags & CF_SHUTW) {
5522 /* request errors are most likely due to the
5523 * server aborting the transfer. */
5524 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005525 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005526 if (msg->err_pos >= 0)
5527 http_capture_bad_message(&sess->fe->invalid_req, s, msg, ret, s->be);
5528 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005529 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005530 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005531 }
5532
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005533 /* If "option abortonclose" is set on the backend, we want to monitor
5534 * the client's connection and forward any shutdown notification to the
5535 * server, which will decide whether to close or to go on processing the
5536 * request. We only do that in tunnel mode, and not in other modes since
5537 * it can be abused to exhaust source ports. */
5538 if (s->be->options & PR_O_ABRT_CLOSE) {
5539 channel_auto_read(req);
5540 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5541 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5542 s->si[1].flags |= SI_FL_NOLINGER;
5543 channel_auto_close(req);
5544 }
5545 else if (s->txn->meth == HTTP_METH_POST) {
5546 /* POST requests may require to read extra CRLF sent by broken
5547 * browsers and which could cause an RST to be sent upon close
5548 * on some systems (eg: Linux). */
5549 channel_auto_read(req);
5550 }
5551 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005552
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005553 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005554 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005555 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005556 if (!(s->flags & SF_ERR_MASK))
5557 s->flags |= SF_ERR_CLICL;
5558 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005559 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005560 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005561 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005562 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005563 }
5564
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005565 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005566 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005567 if (objt_server(s->target))
5568 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005569
5570 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005571 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005572
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005573 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005574 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005575 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005576
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005577 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005578 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005579 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005580 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005581 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005582
Willy Tarreau5c620922011-05-11 19:56:11 +02005583 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005584 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005585 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005586 * modes are already handled by the stream sock layer. We must not do
5587 * this in content-length mode because it could present the MSG_MORE
5588 * flag with the last block of forwarded data, which would cause an
5589 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005590 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005591 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005592 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005593
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005594 return 0;
5595
Willy Tarreaud98cf932009-12-27 22:54:55 +01005596 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005597 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005598 if (sess->listener->counters)
5599 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005600
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005601 return_bad_req_stats_ok:
5602 txn->req.msg_state = HTTP_MSG_ERROR;
5603 if (txn->status) {
5604 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005605 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005606 } else {
5607 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005608 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005609 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005610 req->analysers &= AN_FLT_END;
5611 s->res.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005612
Willy Tarreaue7dff022015-04-03 01:14:29 +02005613 if (!(s->flags & SF_ERR_MASK))
5614 s->flags |= SF_ERR_PRXCOND;
5615 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005616 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005617 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005618 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005619 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005620 }
5621 return 0;
5622
5623 aborted_xfer:
5624 txn->req.msg_state = HTTP_MSG_ERROR;
5625 if (txn->status) {
5626 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005627 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005628 } else {
5629 txn->status = 502;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005630 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005631 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005632 req->analysers &= AN_FLT_END;
5633 s->res.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005634
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005635 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005636 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005637 if (objt_server(s->target))
5638 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005639
Willy Tarreaue7dff022015-04-03 01:14:29 +02005640 if (!(s->flags & SF_ERR_MASK))
5641 s->flags |= SF_ERR_SRVCL;
5642 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005643 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005644 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005645 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005646 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005647 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005648 return 0;
5649}
5650
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005651/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5652 * processing can continue on next analysers, or zero if it either needs more
5653 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005654 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005655 * when it has nothing left to do, and may remove any analyser when it wants to
5656 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005657 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005658int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005659{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005660 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005661 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005662 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005663 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005664 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005665 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005666 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005667
Willy Tarreau87b09662015-04-03 00:22:06 +02005668 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 +02005669 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005670 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005671 rep,
5672 rep->rex, rep->wex,
5673 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005674 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005675 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005676
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005677 /*
5678 * Now parse the partial (or complete) lines.
5679 * We will check the response syntax, and also join multi-line
5680 * headers. An index of all the lines will be elaborated while
5681 * parsing.
5682 *
5683 * For the parsing, we use a 28 states FSM.
5684 *
5685 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005686 * rep->buf->p = beginning of response
5687 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5688 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005689 * msg->eol = end of current header or line (LF or CRLF)
5690 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005691 */
5692
Willy Tarreau628c40c2014-04-24 19:11:26 +02005693 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005694 /* There's a protected area at the end of the buffer for rewriting
5695 * purposes. We don't want to start to parse the request if the
5696 * protected area is affected, because we may have to move processed
5697 * data later, which is much more complicated.
5698 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005699 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005700 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005701 /* some data has still not left the buffer, wake us once that's done */
5702 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5703 goto abort_response;
5704 channel_dont_close(rep);
5705 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005706 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005707 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005708 }
5709
Willy Tarreau379357a2013-06-08 12:55:46 +02005710 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5711 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5712 buffer_slow_realign(rep->buf);
5713
Willy Tarreau9b28e032012-10-12 23:49:43 +02005714 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005715 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005716 }
5717
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005718 /* 1: we might have to print this header in debug mode */
5719 if (unlikely((global.mode & MODE_DEBUG) &&
5720 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005721 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005722 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005723
Willy Tarreau9b28e032012-10-12 23:49:43 +02005724 sol = rep->buf->p;
5725 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005726 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005727
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005728 sol += hdr_idx_first_pos(&txn->hdr_idx);
5729 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005730
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005731 while (cur_idx) {
5732 eol = sol + txn->hdr_idx.v[cur_idx].len;
5733 debug_hdr("srvhdr", s, sol, eol);
5734 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5735 cur_idx = txn->hdr_idx.v[cur_idx].next;
5736 }
5737 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005738
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005739 /*
5740 * Now we quickly check if we have found a full valid response.
5741 * If not so, we check the FD and buffer states before leaving.
5742 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005743 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005744 * responses are checked first.
5745 *
5746 * Depending on whether the client is still there or not, we
5747 * may send an error response back or not. Note that normally
5748 * we should only check for HTTP status there, and check I/O
5749 * errors somewhere else.
5750 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005751
Willy Tarreau655dce92009-11-08 13:10:58 +01005752 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005753 /* Invalid response */
5754 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5755 /* we detected a parsing error. We want to archive this response
5756 * in the dedicated proxy area for later troubleshooting.
5757 */
5758 hdr_response_bad:
5759 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005760 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005761
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005762 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005763 if (objt_server(s->target)) {
5764 objt_server(s->target)->counters.failed_resp++;
5765 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005766 }
Willy Tarreau64648412010-03-05 10:41:54 +01005767 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005768 channel_auto_close(rep);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005769 rep->analysers &= AN_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005770 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005771 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005772 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005773 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005774
Willy Tarreaue7dff022015-04-03 01:14:29 +02005775 if (!(s->flags & SF_ERR_MASK))
5776 s->flags |= SF_ERR_PRXCOND;
5777 if (!(s->flags & SF_FINST_MASK))
5778 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005779
5780 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005781 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005782
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005783 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005784 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005785 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005786 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005787 goto hdr_response_bad;
5788 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005789
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005790 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005791 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005792 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005793 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005794 else if (txn->flags & TX_NOT_FIRST)
5795 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005796
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005797 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005798 if (objt_server(s->target)) {
5799 objt_server(s->target)->counters.failed_resp++;
5800 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005801 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005802
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005803 channel_auto_close(rep);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005804 rep->analysers &= AN_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005805 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005806 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005807 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005808 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005809
Willy Tarreaue7dff022015-04-03 01:14:29 +02005810 if (!(s->flags & SF_ERR_MASK))
5811 s->flags |= SF_ERR_SRVCL;
5812 if (!(s->flags & SF_FINST_MASK))
5813 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005814 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005815 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005816
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005817 /* read timeout : return a 504 to the client. */
5818 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005819 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005820 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005821
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005822 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005823 if (objt_server(s->target)) {
5824 objt_server(s->target)->counters.failed_resp++;
5825 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005826 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005827
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005828 channel_auto_close(rep);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005829 rep->analysers &= AN_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005830 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005831 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005832 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005833 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005834
Willy Tarreaue7dff022015-04-03 01:14:29 +02005835 if (!(s->flags & SF_ERR_MASK))
5836 s->flags |= SF_ERR_SRVTO;
5837 if (!(s->flags & SF_FINST_MASK))
5838 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005839 return 0;
5840 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005841
Willy Tarreauf003d372012-11-26 13:35:37 +01005842 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005843 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005844 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01005845 s->be->be_counters.cli_aborts++;
5846 if (objt_server(s->target))
5847 objt_server(s->target)->counters.cli_aborts++;
5848
Christopher Fauletd7c91962015-04-30 11:48:27 +02005849 rep->analysers &= AN_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01005850 channel_auto_close(rep);
5851
5852 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005853 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005854 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01005855
Willy Tarreaue7dff022015-04-03 01:14:29 +02005856 if (!(s->flags & SF_ERR_MASK))
5857 s->flags |= SF_ERR_CLICL;
5858 if (!(s->flags & SF_FINST_MASK))
5859 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01005860
Willy Tarreau87b09662015-04-03 00:22:06 +02005861 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01005862 return 0;
5863 }
5864
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005865 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005866 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005867 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005868 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005869 else if (txn->flags & TX_NOT_FIRST)
5870 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005871
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005872 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005873 if (objt_server(s->target)) {
5874 objt_server(s->target)->counters.failed_resp++;
5875 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005876 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005877
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005878 channel_auto_close(rep);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005879 rep->analysers &= AN_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005880 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005881 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005882 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005883 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005884
Willy Tarreaue7dff022015-04-03 01:14:29 +02005885 if (!(s->flags & SF_ERR_MASK))
5886 s->flags |= SF_ERR_SRVCL;
5887 if (!(s->flags & SF_FINST_MASK))
5888 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005889 return 0;
5890 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005891
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005892 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005893 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005894 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005895 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005896 else if (txn->flags & TX_NOT_FIRST)
5897 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005898
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005899 s->be->be_counters.failed_resp++;
Christopher Fauletd7c91962015-04-30 11:48:27 +02005900 rep->analysers &= AN_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005901 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005902
Willy Tarreaue7dff022015-04-03 01:14:29 +02005903 if (!(s->flags & SF_ERR_MASK))
5904 s->flags |= SF_ERR_CLICL;
5905 if (!(s->flags & SF_FINST_MASK))
5906 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005907
Willy Tarreau87b09662015-04-03 00:22:06 +02005908 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005909 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005910 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005911
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005912 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005913 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005914 return 0;
5915 }
5916
5917 /* More interesting part now : we know that we have a complete
5918 * response which at least looks like HTTP. We have an indicator
5919 * of each header's length, so we can parse them quickly.
5920 */
5921
5922 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005923 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005924
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005925 /*
5926 * 1: get the status code
5927 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005928 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005929 if (n < 1 || n > 5)
5930 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005931 /* when the client triggers a 4xx from the server, it's most often due
5932 * to a missing object or permission. These events should be tracked
5933 * because if they happen often, it may indicate a brute force or a
5934 * vulnerability scan.
5935 */
5936 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02005937 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02005938
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005939 if (objt_server(s->target))
5940 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005941
Willy Tarreau91852eb2015-05-01 13:26:00 +02005942 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
5943 * exactly one digit "." one digit. This check may be disabled using
5944 * option accept-invalid-http-response.
5945 */
5946 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
5947 if (msg->sl.st.v_l != 8) {
5948 msg->err_pos = 0;
5949 goto hdr_response_bad;
5950 }
5951
5952 if (rep->buf->p[4] != '/' ||
5953 !isdigit((unsigned char)rep->buf->p[5]) ||
5954 rep->buf->p[6] != '.' ||
5955 !isdigit((unsigned char)rep->buf->p[7])) {
5956 msg->err_pos = 4;
5957 goto hdr_response_bad;
5958 }
5959 }
5960
Willy Tarreau5b154472009-12-21 20:11:07 +01005961 /* check if the response is HTTP/1.1 or above */
5962 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005963 ((rep->buf->p[5] > '1') ||
5964 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005965 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005966
5967 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005968 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 +01005969
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005970 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005971 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005972
Willy Tarreau9b28e032012-10-12 23:49:43 +02005973 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005974
Willy Tarreau39650402010-03-15 19:44:39 +01005975 /* Adjust server's health based on status code. Note: status codes 501
5976 * and 505 are triggered on demand by client request, so we must not
5977 * count them as server failures.
5978 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005979 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005980 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005981 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005982 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005983 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005984 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005985
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005986 /*
5987 * 2: check for cacheability.
5988 */
5989
5990 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005991 case 100:
5992 /*
5993 * We may be facing a 100-continue response, in which case this
5994 * is not the right response, and we're waiting for the next one.
5995 * Let's allow this response to go to the client and wait for the
5996 * next one.
5997 */
5998 hdr_idx_init(&txn->hdr_idx);
5999 msg->next -= channel_forward(rep, msg->next);
6000 msg->msg_state = HTTP_MSG_RPBEFORE;
6001 txn->status = 0;
6002 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01006003 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02006004 goto next_one;
6005
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006006 case 200:
6007 case 203:
6008 case 206:
6009 case 300:
6010 case 301:
6011 case 410:
6012 /* RFC2616 @13.4:
6013 * "A response received with a status code of
6014 * 200, 203, 206, 300, 301 or 410 MAY be stored
6015 * by a cache (...) unless a cache-control
6016 * directive prohibits caching."
6017 *
6018 * RFC2616 @9.5: POST method :
6019 * "Responses to this method are not cacheable,
6020 * unless the response includes appropriate
6021 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006022 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006023 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006024 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006025 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6026 break;
6027 default:
6028 break;
6029 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006030
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006031 /*
6032 * 3: we may need to capture headers
6033 */
6034 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006035 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006036 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006037 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006038
Willy Tarreau557f1992015-05-01 10:05:17 +02006039 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6040 * by RFC7230#3.3.3 :
6041 *
6042 * The length of a message body is determined by one of the following
6043 * (in order of precedence):
6044 *
6045 * 1. Any response to a HEAD request and any response with a 1xx
6046 * (Informational), 204 (No Content), or 304 (Not Modified) status
6047 * code is always terminated by the first empty line after the
6048 * header fields, regardless of the header fields present in the
6049 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006050 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006051 * 2. Any 2xx (Successful) response to a CONNECT request implies that
6052 * the connection will become a tunnel immediately after the empty
6053 * line that concludes the header fields. A client MUST ignore any
6054 * Content-Length or Transfer-Encoding header fields received in
6055 * such a message.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006056 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006057 * 3. If a Transfer-Encoding header field is present and the chunked
6058 * transfer coding (Section 4.1) is the final encoding, the message
6059 * body length is determined by reading and decoding the chunked
6060 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006061 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006062 * If a Transfer-Encoding header field is present in a response and
6063 * the chunked transfer coding is not the final encoding, the
6064 * message body length is determined by reading the connection until
6065 * it is closed by the server. If a Transfer-Encoding header field
6066 * is present in a request and the chunked transfer coding is not
6067 * the final encoding, the message body length cannot be determined
6068 * reliably; the server MUST respond with the 400 (Bad Request)
6069 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006070 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006071 * If a message is received with both a Transfer-Encoding and a
6072 * Content-Length header field, the Transfer-Encoding overrides the
6073 * Content-Length. Such a message might indicate an attempt to
6074 * perform request smuggling (Section 9.5) or response splitting
6075 * (Section 9.4) and ought to be handled as an error. A sender MUST
6076 * remove the received Content-Length field prior to forwarding such
6077 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006078 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006079 * 4. If a message is received without Transfer-Encoding and with
6080 * either multiple Content-Length header fields having differing
6081 * field-values or a single Content-Length header field having an
6082 * invalid value, then the message framing is invalid and the
6083 * recipient MUST treat it as an unrecoverable error. If this is a
6084 * request message, the server MUST respond with a 400 (Bad Request)
6085 * status code and then close the connection. If this is a response
6086 * message received by a proxy, the proxy MUST close the connection
6087 * to the server, discard the received response, and send a 502 (Bad
6088 * Gateway) response to the client. If this is a response message
6089 * received by a user agent, the user agent MUST close the
6090 * connection to the server and discard the received response.
6091 *
6092 * 5. If a valid Content-Length header field is present without
6093 * Transfer-Encoding, its decimal value defines the expected message
6094 * body length in octets. If the sender closes the connection or
6095 * the recipient times out before the indicated number of octets are
6096 * received, the recipient MUST consider the message to be
6097 * incomplete and close the connection.
6098 *
6099 * 6. If this is a request message and none of the above are true, then
6100 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006101 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006102 * 7. Otherwise, this is a response message without a declared message
6103 * body length, so the message body length is determined by the
6104 * number of octets received prior to the server closing the
6105 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006106 */
6107
6108 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006109 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006110 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006111 * FIXME: should we parse anyway and return an error on chunked encoding ?
6112 */
6113 if (txn->meth == HTTP_METH_HEAD ||
6114 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006115 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006116 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006117 goto skip_content_length;
6118 }
6119
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006120 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006121 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006122 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006123 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006124 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6125 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006126 /* bad transfer-encoding (chunked followed by something else) */
6127 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006128 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006129 break;
6130 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006131 }
6132
Willy Tarreau1c913912015-04-30 10:57:51 +02006133 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006134 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006135 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006136 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6137 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6138 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006139 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006140 signed long long cl;
6141
Willy Tarreauad14f752011-09-02 20:33:27 +02006142 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006143 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006144 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006145 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006146
Willy Tarreauad14f752011-09-02 20:33:27 +02006147 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006148 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006149 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006150 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006151
Willy Tarreauad14f752011-09-02 20:33:27 +02006152 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006153 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006154 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006155 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006156
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006157 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006158 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006159 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006160 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006161
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006162 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006163 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006164 }
6165
6166skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006167 /* Now we have to check if we need to modify the Connection header.
6168 * This is more difficult on the response than it is on the request,
6169 * because we can have two different HTTP versions and we don't know
6170 * how the client will interprete a response. For instance, let's say
6171 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6172 * HTTP/1.1 response without any header. Maybe it will bound itself to
6173 * HTTP/1.0 because it only knows about it, and will consider the lack
6174 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6175 * the lack of header as a keep-alive. Thus we will use two flags
6176 * indicating how a request MAY be understood by the client. In case
6177 * of multiple possibilities, we'll fix the header to be explicit. If
6178 * ambiguous cases such as both close and keepalive are seen, then we
6179 * will fall back to explicit close. Note that we won't take risks with
6180 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006181 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006182 */
6183
Willy Tarreaudc008c52010-02-01 16:20:08 +01006184 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6185 txn->status == 101)) {
6186 /* Either we've established an explicit tunnel, or we're
6187 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006188 * to understand the next protocols. We have to switch to tunnel
6189 * mode, so that we transfer the request and responses then let
6190 * this protocol pass unmodified. When we later implement specific
6191 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006192 * header which contains information about that protocol for
6193 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006194 */
6195 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6196 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006197 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6198 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006199 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006200 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006201 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006202
Willy Tarreau70dffda2014-01-30 03:07:23 +01006203 /* this situation happens when combining pretend-keepalive with httpclose. */
6204 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006205 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006206 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006207 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6208
Willy Tarreau60466522010-01-18 19:08:45 +01006209 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006210 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006211 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6212 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006213
Willy Tarreau60466522010-01-18 19:08:45 +01006214 /* now adjust header transformations depending on current state */
6215 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6216 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6217 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006218 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006219 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006220 }
Willy Tarreau60466522010-01-18 19:08:45 +01006221 else { /* SCL / KAL */
6222 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006223 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006224 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006225 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006226
Willy Tarreau60466522010-01-18 19:08:45 +01006227 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006228 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006229
Willy Tarreau60466522010-01-18 19:08:45 +01006230 /* Some keep-alive responses are converted to Server-close if
6231 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006232 */
Willy Tarreau60466522010-01-18 19:08:45 +01006233 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6234 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006235 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006236 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006237 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006238 }
6239
Willy Tarreau7959a552013-09-23 16:44:27 +02006240 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006241 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006242
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006243 /* end of job, return OK */
6244 rep->analysers &= ~an_bit;
6245 rep->analyse_exp = TICK_ETERNITY;
6246 channel_auto_close(rep);
6247 return 1;
6248
6249 abort_keep_alive:
6250 /* A keep-alive request to the server failed on a network error.
6251 * The client is required to retry. We need to close without returning
6252 * any other information so that the client retries.
6253 */
6254 txn->status = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +02006255 rep->analysers &= AN_FLT_END;
6256 s->req.analysers &= AN_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006257 channel_auto_close(rep);
6258 s->logs.logwait = 0;
6259 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006260 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006261 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006262 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006263 return 0;
6264}
6265
6266/* This function performs all the processing enabled for the current response.
6267 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006268 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006269 * other functions. It works like process_request (see indications above).
6270 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006271int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006272{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006273 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006274 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006275 struct http_msg *msg = &txn->rsp;
6276 struct proxy *cur_proxy;
6277 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006278 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006279
Willy Tarreau87b09662015-04-03 00:22:06 +02006280 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 +02006281 now_ms, __FUNCTION__,
6282 s,
6283 rep,
6284 rep->rex, rep->wex,
6285 rep->flags,
6286 rep->buf->i,
6287 rep->analysers);
6288
6289 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6290 return 0;
6291
Willy Tarreau70730dd2014-04-24 18:06:27 +02006292 /* The stats applet needs to adjust the Connection header but we don't
6293 * apply any filter there.
6294 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006295 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6296 rep->analysers &= ~an_bit;
6297 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006298 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006299 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006300
Willy Tarreau58975672014-04-24 21:13:57 +02006301 /*
6302 * We will have to evaluate the filters.
6303 * As opposed to version 1.2, now they will be evaluated in the
6304 * filters order and not in the header order. This means that
6305 * each filter has to be validated among all headers.
6306 *
6307 * Filters are tried with ->be first, then with ->fe if it is
6308 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006309 *
6310 * Maybe we are in resume condiion. In this case I choose the
6311 * "struct proxy" which contains the rule list matching the resume
6312 * pointer. If none of theses "struct proxy" match, I initialise
6313 * the process with the first one.
6314 *
6315 * In fact, I check only correspondance betwwen the current list
6316 * pointer and the ->fe rule list. If it doesn't match, I initialize
6317 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006318 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006319 if (s->current_rule_list == &sess->fe->http_res_rules)
6320 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006321 else
6322 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006323 while (1) {
6324 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006325
Willy Tarreau58975672014-04-24 21:13:57 +02006326 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02006327 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006328 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006329
Willy Tarreau51d861a2015-05-22 17:30:48 +02006330 if (ret == HTTP_RULE_RES_BADREQ)
6331 goto return_srv_prx_502;
6332
6333 if (ret == HTTP_RULE_RES_DONE) {
6334 rep->analysers &= ~an_bit;
6335 rep->analyse_exp = TICK_ETERNITY;
6336 return 1;
6337 }
6338 }
6339
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006340 /* we need to be called again. */
6341 if (ret == HTTP_RULE_RES_YIELD) {
6342 channel_dont_close(rep);
6343 return 0;
6344 }
6345
Willy Tarreau58975672014-04-24 21:13:57 +02006346 /* try headers filters */
6347 if (rule_set->rsp_exp != NULL) {
6348 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6349 return_bad_resp:
6350 if (objt_server(s->target)) {
6351 objt_server(s->target)->counters.failed_resp++;
6352 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006353 }
Willy Tarreau58975672014-04-24 21:13:57 +02006354 s->be->be_counters.failed_resp++;
6355 return_srv_prx_502:
Christopher Fauletd7c91962015-04-30 11:48:27 +02006356 rep->analysers &= AN_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02006357 txn->status = 502;
6358 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006359 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006360 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006361 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006362 if (!(s->flags & SF_ERR_MASK))
6363 s->flags |= SF_ERR_PRXCOND;
6364 if (!(s->flags & SF_FINST_MASK))
6365 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006366 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006367 }
Willy Tarreau58975672014-04-24 21:13:57 +02006368 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006369
Willy Tarreau58975672014-04-24 21:13:57 +02006370 /* has the response been denied ? */
6371 if (txn->flags & TX_SVDENY) {
6372 if (objt_server(s->target))
6373 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006374
Willy Tarreau58975672014-04-24 21:13:57 +02006375 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006376 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006377 if (sess->listener->counters)
6378 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006379
Willy Tarreau58975672014-04-24 21:13:57 +02006380 goto return_srv_prx_502;
6381 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006382
Willy Tarreau58975672014-04-24 21:13:57 +02006383 /* add response headers from the rule sets in the same order */
6384 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006385 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006386 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006387 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006388 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006389 ret = acl_pass(ret);
6390 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6391 ret = !ret;
6392 if (!ret)
6393 continue;
6394 }
6395 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6396 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006397 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006398
Willy Tarreau58975672014-04-24 21:13:57 +02006399 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006400 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006401 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006402 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006403 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006404
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006405 /* After this point, this anayzer can't return yield, so we can
6406 * remove the bit corresponding to this analyzer from the list.
6407 *
6408 * Note that the intermediate returns and goto found previously
6409 * reset the analyzers.
6410 */
6411 rep->analysers &= ~an_bit;
6412 rep->analyse_exp = TICK_ETERNITY;
6413
Willy Tarreau58975672014-04-24 21:13:57 +02006414 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006415 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006416 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006417
Willy Tarreau58975672014-04-24 21:13:57 +02006418 /*
6419 * Now check for a server cookie.
6420 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006421 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02006422 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006423
Willy Tarreau58975672014-04-24 21:13:57 +02006424 /*
6425 * Check for cache-control or pragma headers if required.
6426 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006427 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 +02006428 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006429
Willy Tarreau58975672014-04-24 21:13:57 +02006430 /*
6431 * Add server cookie in the response if needed
6432 */
6433 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6434 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006435 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006436 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6437 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6438 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6439 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6440 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006441 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006442 /* the server is known, it's not the one the client requested, or the
6443 * cookie's last seen date needs to be refreshed. We have to
6444 * insert a set-cookie here, except if we want to insert only on POST
6445 * requests and this one isn't. Note that servers which don't have cookies
6446 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006447 */
Willy Tarreau58975672014-04-24 21:13:57 +02006448 if (!objt_server(s->target)->cookie) {
6449 chunk_printf(&trash,
6450 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6451 s->be->cookie_name);
6452 }
6453 else {
6454 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006455
Willy Tarreau58975672014-04-24 21:13:57 +02006456 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6457 /* emit last_date, which is mandatory */
6458 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6459 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6460 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006461
Willy Tarreau58975672014-04-24 21:13:57 +02006462 if (s->be->cookie_maxlife) {
6463 /* emit first_date, which is either the original one or
6464 * the current date.
6465 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006466 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006467 s30tob64(txn->cookie_first_date ?
6468 txn->cookie_first_date >> 2 :
6469 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006470 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006471 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006472 }
Willy Tarreau58975672014-04-24 21:13:57 +02006473 chunk_appendf(&trash, "; path=/");
6474 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006475
Willy Tarreau58975672014-04-24 21:13:57 +02006476 if (s->be->cookie_domain)
6477 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006478
Willy Tarreau58975672014-04-24 21:13:57 +02006479 if (s->be->ck_opts & PR_CK_HTTPONLY)
6480 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006481
Willy Tarreau58975672014-04-24 21:13:57 +02006482 if (s->be->ck_opts & PR_CK_SECURE)
6483 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006484
Willy Tarreau58975672014-04-24 21:13:57 +02006485 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6486 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006487
Willy Tarreau58975672014-04-24 21:13:57 +02006488 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006489 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006490 /* the server did not change, only the date was updated */
6491 txn->flags |= TX_SCK_UPDATED;
6492 else
6493 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006494
Willy Tarreau58975672014-04-24 21:13:57 +02006495 /* Here, we will tell an eventual cache on the client side that we don't
6496 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6497 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6498 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006499 */
Willy Tarreau58975672014-04-24 21:13:57 +02006500 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006501
Willy Tarreau58975672014-04-24 21:13:57 +02006502 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006503
Willy Tarreau58975672014-04-24 21:13:57 +02006504 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6505 "Cache-control: private", 22) < 0))
6506 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006507 }
Willy Tarreau58975672014-04-24 21:13:57 +02006508 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006509
Willy Tarreau58975672014-04-24 21:13:57 +02006510 /*
6511 * Check if result will be cacheable with a cookie.
6512 * We'll block the response if security checks have caught
6513 * nasty things such as a cacheable cookie.
6514 */
6515 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6516 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6517 (s->be->options & PR_O_CHK_CACHE)) {
6518 /* we're in presence of a cacheable response containing
6519 * a set-cookie header. We'll block it as requested by
6520 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006521 */
Willy Tarreau58975672014-04-24 21:13:57 +02006522 if (objt_server(s->target))
6523 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006524
Willy Tarreau58975672014-04-24 21:13:57 +02006525 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006526 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006527 if (sess->listener->counters)
6528 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006529
Willy Tarreau58975672014-04-24 21:13:57 +02006530 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6531 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6532 send_log(s->be, LOG_ALERT,
6533 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6534 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6535 goto return_srv_prx_502;
6536 }
Willy Tarreau03945942009-12-22 16:50:27 +01006537
Willy Tarreau70730dd2014-04-24 18:06:27 +02006538 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006539 /*
6540 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6541 * If an "Upgrade" token is found, the header is left untouched in order
6542 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006543 * if anything but "Upgrade" is present in the Connection header. We don't
6544 * want to touch any 101 response either since it's switching to another
6545 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006546 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006547 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006548 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006549 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006550 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6551 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006552
Willy Tarreau58975672014-04-24 21:13:57 +02006553 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6554 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6555 /* we want a keep-alive response here. Keep-alive header
6556 * required if either side is not 1.1.
6557 */
6558 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6559 want_flags |= TX_CON_KAL_SET;
6560 }
6561 else {
6562 /* we want a close response here. Close header required if
6563 * the server is 1.1, regardless of the client.
6564 */
6565 if (msg->flags & HTTP_MSGF_VER_11)
6566 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006567 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006568
Willy Tarreau58975672014-04-24 21:13:57 +02006569 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6570 http_change_connection_header(txn, msg, want_flags);
6571 }
6572
6573 skip_header_mangling:
Christopher Faulet3e344292015-11-24 16:24:13 +01006574 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_FILTERS(s) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006575 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
6576 rep->analysers &= ~AN_FLT_XFER_DATA;
Willy Tarreau58975672014-04-24 21:13:57 +02006577 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Christopher Fauletd7c91962015-04-30 11:48:27 +02006578 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006579
Willy Tarreau58975672014-04-24 21:13:57 +02006580 /* if the user wants to log as soon as possible, without counting
6581 * bytes from the server, then this is the right moment. We have
6582 * to temporarily assign bytes_out to log what we currently have.
6583 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006584 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006585 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6586 s->logs.bytes_out = txn->rsp.eoh;
6587 s->do_log(s);
6588 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006589 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006590 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006591}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006592
Willy Tarreaud98cf932009-12-27 22:54:55 +01006593/* This function is an analyser which forwards response body (including chunk
6594 * sizes if any). It is called as soon as we must forward, even if we forward
6595 * zero byte. The only situation where it must not be called is when we're in
6596 * tunnel mode and we want to forward till the close. It's used both to forward
6597 * remaining data and to resync after end of body. It expects the msg_state to
6598 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006599 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006600 *
6601 * It is capable of compressing response data both in content-length mode and
6602 * in chunked mode. The state machines follows different flows depending on
6603 * whether content-length and chunked modes are used, since there are no
6604 * trailers in content-length :
6605 *
6606 * chk-mode cl-mode
6607 * ,----- BODY -----.
6608 * / \
6609 * V size > 0 V chk-mode
6610 * .--> SIZE -------------> DATA -------------> CRLF
6611 * | | size == 0 | last byte |
6612 * | v final crlf v inspected |
6613 * | TRAILERS -----------> DONE |
6614 * | |
6615 * `----------------------------------------------'
6616 *
6617 * Compression only happens in the DATA state, and must be flushed in final
6618 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6619 * is performed at once on final states for all bytes parsed, or when leaving
6620 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006621 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006622int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006623{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006624 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006625 struct http_txn *txn = s->txn;
6626 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01006627 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006628
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006629 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6630 return 0;
6631
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006632 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006633 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006634 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006635 /* Output closed while we were sending data. We must abort and
6636 * wake the other side up.
6637 */
6638 msg->msg_state = HTTP_MSG_ERROR;
6639 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006640 return 1;
6641 }
6642
Willy Tarreau4fe41902010-06-07 22:27:41 +02006643 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006644 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006645
Christopher Fauletd7c91962015-04-30 11:48:27 +02006646 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006647 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
6648 ? HTTP_MSG_CHUNK_SIZE
6649 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006650 }
6651
Willy Tarreauefdf0942014-04-24 20:08:57 +02006652 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006653 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02006654 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006655 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006656 }
6657
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006658 if (msg->msg_state < HTTP_MSG_DONE) {
6659 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6660 ? http_msg_forward_chunked_body(s, msg)
6661 : http_msg_forward_body(s, msg));
6662 if (!ret)
6663 goto missing_data_or_waiting;
6664 if (ret < 0)
6665 goto return_bad_res;
6666 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006667
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006668 /* other states, DONE...TUNNEL */
6669 /* for keep-alive we don't want to forward closes on DONE */
6670 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6671 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6672 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006673
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006674 ret = msg->msg_state;
6675 if (http_resync_states(s)) {
6676 /* some state changes occurred, maybe the analyser was disabled
6677 * too. */
6678 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6679 if (res->flags & CF_SHUTW) {
6680 /* response errors are most likely due to the
6681 * client aborting the transfer. */
6682 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006683 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006684 if (msg->err_pos >= 0)
6685 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, strm_fe(s));
6686 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006687 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006688 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006689 }
6690
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006691 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006692 if (res->flags & CF_SHUTW)
6693 goto aborted_xfer;
6694
6695 /* stop waiting for data if the input is closed before the end. If the
6696 * client side was already closed, it means that the client has aborted,
6697 * so we don't want to count this as a server abort. Otherwise it's a
6698 * server abort.
6699 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006700 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006701 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006702 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006703 /* If we have some pending data, we continue the processing */
6704 if (!buffer_pending(res->buf)) {
6705 if (!(s->flags & SF_ERR_MASK))
6706 s->flags |= SF_ERR_SRVCL;
6707 s->be->be_counters.srv_aborts++;
6708 if (objt_server(s->target))
6709 objt_server(s->target)->counters.srv_aborts++;
6710 goto return_bad_res_stats_ok;
6711 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006712 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006713
Willy Tarreau40dba092010-03-04 18:14:51 +01006714 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006715 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006716 goto return_bad_res;
6717
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006718 /* When TE: chunked is used, we need to get there again to parse
6719 * remaining chunks even if the server has closed, so we don't want to
6720 * set CF_DONTCLOSE. Similarly, if the body length is undefined, if
6721 * keep-alive is set on the client side or if there are filters
6722 * registered on the stream, we don't want to forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006723 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006724 if ((msg->flags & HTTP_MSGF_TE_CHNK) || !msg->body_len ||
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006725 HAS_FILTERS(s) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006726 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6727 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006728 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006729
Willy Tarreau5c620922011-05-11 19:56:11 +02006730 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006731 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006732 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006733 * modes are already handled by the stream sock layer. We must not do
6734 * this in content-length mode because it could present the MSG_MORE
6735 * flag with the last block of forwarded data, which would cause an
6736 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006737 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006738 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006739 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006740
Willy Tarreau87b09662015-04-03 00:22:06 +02006741 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006742 return 0;
6743
Willy Tarreau40dba092010-03-04 18:14:51 +01006744 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006745 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006746 if (objt_server(s->target))
6747 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006748
6749 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006750 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006751 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006752 http_reply_and_close(s, txn->status, NULL);
Christopher Fauletd7c91962015-04-30 11:48:27 +02006753 res->analysers &= AN_FLT_END;
6754 s->req.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006755 if (objt_server(s->target))
6756 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006757
Willy Tarreaue7dff022015-04-03 01:14:29 +02006758 if (!(s->flags & SF_ERR_MASK))
6759 s->flags |= SF_ERR_PRXCOND;
6760 if (!(s->flags & SF_FINST_MASK))
6761 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006762 return 0;
6763
6764 aborted_xfer:
6765 txn->rsp.msg_state = HTTP_MSG_ERROR;
6766 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006767 http_reply_and_close(s, txn->status, NULL);
Christopher Fauletd7c91962015-04-30 11:48:27 +02006768 res->analysers &= AN_FLT_END;
6769 s->req.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006770
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006771 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006772 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006773 if (objt_server(s->target))
6774 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006775
Willy Tarreaue7dff022015-04-03 01:14:29 +02006776 if (!(s->flags & SF_ERR_MASK))
6777 s->flags |= SF_ERR_CLICL;
6778 if (!(s->flags & SF_FINST_MASK))
6779 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006780 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006781}
6782
6783
6784static inline int
6785http_msg_forward_body(struct stream *s, struct http_msg *msg)
6786{
6787 struct channel *chn = msg->chn;
6788 int ret;
6789
6790 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
6791
6792 if (msg->msg_state == HTTP_MSG_ENDING)
6793 goto ending;
6794
6795 /* Neither content-length, nor transfer-encoding was found, so we must
6796 * read the body until the server connection is closed. In that case, we
6797 * eat data as they come. Of course, this happens for response only. */
6798 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6799 unsigned long long len = (chn->buf->i - msg->next);
6800 msg->chunk_len += len;
6801 msg->body_len += len;
6802 }
6803 ret = FLT_STRM_CB(s, flt_http_data(s, msg),
6804 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6805 /* on_error */ goto error);
6806 msg->next += ret;
6807 msg->chunk_len -= ret;
6808 if (msg->chunk_len) {
6809 /* input empty or output full */
6810 if (chn->buf->i > msg->next)
6811 chn->flags |= CF_WAKE_WRITE;
6812 goto missing_data_or_waiting;
6813 }
6814
6815 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && !(chn->flags & CF_SHUTR)) {
6816 /* The server still sending data */
6817 goto missing_data_or_waiting;
6818 }
6819 msg->msg_state = HTTP_MSG_ENDING;
6820
6821 ending:
6822 /* we may have some pending data starting at res->buf->p such as a last
6823 * chunk of data or trailers. */
6824 ret = FLT_STRM_CB(s, flt_http_forward_data(s, msg, msg->next),
6825 /* default_ret */ msg->next,
6826 /* on_error */ goto error);
6827 b_adv(chn->buf, ret);
6828 msg->next -= ret;
6829 if (msg->next)
6830 goto missing_data_or_waiting;
6831
6832 FLT_STRM_CB(s, flt_http_end(s, msg),
6833 /* default_ret */ 1,
6834 /* on_error */ goto error,
6835 /* on_wait */ goto waiting);
6836 msg->msg_state = HTTP_MSG_DONE;
6837 return 1;
6838
6839 missing_data_or_waiting:
6840 /* we may have some pending data starting at chn->buf->p */
6841 ret = FLT_STRM_CB(s, flt_http_forward_data(s, msg, msg->next),
6842 /* default_ret */ msg->next,
6843 /* on_error */ goto error);
6844 b_adv(chn->buf, ret);
6845 msg->next -= ret;
6846 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6847 msg->sov -= ret;
6848 if (!HAS_FILTERS(s))
6849 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
6850 waiting:
6851 return 0;
6852 error:
6853 return -1;
6854}
6855
6856static inline int
6857http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
6858{
6859 struct channel *chn = msg->chn;
6860 int ret;
6861
6862 /* Here we have the guarantee to be in one of the following state:
6863 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
6864 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
6865
6866 switch_states:
6867 switch (msg->msg_state) {
6868 case HTTP_MSG_DATA:
6869 ret = FLT_STRM_CB(s, flt_http_data(s, msg),
6870 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6871 /* on_error */ goto error);
6872 msg->next += ret;
6873 msg->chunk_len -= ret;
6874 if (msg->chunk_len) {
6875 /* input empty or output full */
6876 if (chn->buf->i > msg->next)
6877 chn->flags |= CF_WAKE_WRITE;
6878 goto missing_data_or_waiting;
6879 }
6880
6881 /* nothing left to forward for this chunk*/
6882 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
6883 /* fall through for HTTP_MSG_CHUNK_CRLF */
6884
6885 case HTTP_MSG_CHUNK_CRLF:
6886 /* we want the CRLF after the data */
6887 ret = http_skip_chunk_crlf(msg);
6888 if (ret == 0)
6889 goto missing_data_or_waiting;
6890 if (ret < 0)
6891 goto chunk_parsing_error;
6892 msg->next += msg->sol;
6893 msg->sol = 0;
6894 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6895 /* fall through for HTTP_MSG_CHUNK_SIZE */
6896
6897 case HTTP_MSG_CHUNK_SIZE:
6898 /* read the chunk size and assign it to ->chunk_len,
6899 * then set ->next to point to the body and switch to
6900 * DATA or TRAILERS state.
6901 */
6902 ret = http_parse_chunk_size(msg);
6903 if (ret == 0)
6904 goto missing_data_or_waiting;
6905 if (ret < 0)
6906 goto chunk_parsing_error;
6907 msg->next += msg->sol;
6908 msg->sol = 0;
6909 if (msg->chunk_len) {
6910 msg->msg_state = HTTP_MSG_DATA;
6911 goto switch_states;
6912 }
6913 msg->msg_state = HTTP_MSG_TRAILERS;
6914 /* fall through for HTTP_MSG_TRAILERS */
6915
6916 case HTTP_MSG_TRAILERS:
6917 ret = http_forward_trailers(msg);
6918 if (ret < 0)
6919 goto chunk_parsing_error;
6920 FLT_STRM_CB(s, flt_http_chunk_trailers(s, msg),
6921 /* default_ret */ 1,
6922 /* on_error */ goto error);
6923 msg->next += msg->sol;
6924 msg->sol = 0;
6925 if (!ret)
6926 goto missing_data_or_waiting;
6927 break;
6928
6929 case HTTP_MSG_ENDING:
6930 goto ending;
6931
6932 default:
6933 /* This should no happen in this function */
6934 goto error;
6935 }
6936
6937 msg->msg_state = HTTP_MSG_ENDING;
6938 ending:
6939 /* we may have some pending data starting at res->buf->p such as a last
6940 * chunk of data or trailers. */
6941 ret = FLT_STRM_CB(s, flt_http_forward_data(s, msg, msg->next),
6942 /* default_ret */ msg->next,
6943 /* on_error */ goto error);
6944 b_adv(chn->buf, ret);
6945 msg->next -= ret;
6946 if (msg->next)
6947 goto missing_data_or_waiting;
6948
6949 FLT_STRM_CB(s, flt_http_end(s, msg),
6950 /* default_ret */ 1,
6951 /* on_error */ goto error,
6952 /* on_wait */ goto waiting);
6953 msg->msg_state = HTTP_MSG_DONE;
6954 return 1;
6955
6956 missing_data_or_waiting:
6957 /* we may have some pending data starting at chn->buf->p */
6958 ret = FLT_STRM_CB(s, flt_http_forward_data(s, msg, msg->next),
6959 /* default_ret */ msg->next,
6960 /* on_error */ goto error);
6961 b_adv(chn->buf, ret);
6962 msg->next -= ret;
6963 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6964 msg->sov -= ret;
6965 if (!HAS_FILTERS(s))
6966 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
6967 waiting:
6968 return 0;
6969
6970 chunk_parsing_error:
6971 if (msg->err_pos >= 0) {
6972 if (chn->flags & CF_ISRESP)
6973 http_capture_bad_message(&s->be->invalid_rep, s, msg,
6974 msg->msg_state, strm_fe(s));
6975 else
6976 http_capture_bad_message(&strm_fe(s)->invalid_req, s,
6977 msg, msg->msg_state, s->be);
6978 }
6979 error:
6980 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006981}
6982
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006983
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006984/* Iterate the same filter through all request headers.
6985 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006986 * Since it can manage the switch to another backend, it updates the per-proxy
6987 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006988 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006989int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006990{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006991 char *cur_ptr, *cur_end, *cur_next;
6992 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006993 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006994 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006995 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006996
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006997 last_hdr = 0;
6998
Willy Tarreau9b28e032012-10-12 23:49:43 +02006999 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007000 old_idx = 0;
7001
7002 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007003 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007004 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007005 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007006 (exp->action == ACT_ALLOW ||
7007 exp->action == ACT_DENY ||
7008 exp->action == ACT_TARPIT))
7009 return 0;
7010
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007011 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007012 if (!cur_idx)
7013 break;
7014
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007015 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007016 cur_ptr = cur_next;
7017 cur_end = cur_ptr + cur_hdr->len;
7018 cur_next = cur_end + cur_hdr->cr + 1;
7019
7020 /* Now we have one header between cur_ptr and cur_end,
7021 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007022 */
7023
Willy Tarreau15a53a42015-01-21 13:39:42 +01007024 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007025 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007026 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007027 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007028 last_hdr = 1;
7029 break;
7030
7031 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007032 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007033 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007034 break;
7035
7036 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007037 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007038 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007039 break;
7040
7041 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007042 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7043 if (trash.len < 0)
7044 return -1;
7045
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007046 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007047 /* FIXME: if the user adds a newline in the replacement, the
7048 * index will not be recalculated for now, and the new line
7049 * will not be counted as a new header.
7050 */
7051
7052 cur_end += delta;
7053 cur_next += delta;
7054 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007055 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007056 break;
7057
7058 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007059 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007060 cur_next += delta;
7061
Willy Tarreaufa355d42009-11-29 18:12:29 +01007062 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007063 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7064 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007065 cur_hdr->len = 0;
7066 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007067 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007068 break;
7069
7070 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007071 }
7072
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007073 /* keep the link from this header to next one in case of later
7074 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007075 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007076 old_idx = cur_idx;
7077 }
7078 return 0;
7079}
7080
7081
7082/* Apply the filter to the request line.
7083 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7084 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007085 * Since it can manage the switch to another backend, it updates the per-proxy
7086 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007087 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007088int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007089{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007090 char *cur_ptr, *cur_end;
7091 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007092 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007093 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007094
Willy Tarreau3d300592007-03-18 18:34:41 +01007095 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007096 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007097 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007098 (exp->action == ACT_ALLOW ||
7099 exp->action == ACT_DENY ||
7100 exp->action == ACT_TARPIT))
7101 return 0;
7102 else if (exp->action == ACT_REMOVE)
7103 return 0;
7104
7105 done = 0;
7106
Willy Tarreau9b28e032012-10-12 23:49:43 +02007107 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007108 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007109
7110 /* Now we have the request line between cur_ptr and cur_end */
7111
Willy Tarreau15a53a42015-01-21 13:39:42 +01007112 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007113 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007114 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007115 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007116 done = 1;
7117 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007118
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007119 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007120 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007121 done = 1;
7122 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007123
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007124 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007125 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007126 done = 1;
7127 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007128
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007129 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007130 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7131 if (trash.len < 0)
7132 return -1;
7133
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007134 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007135 /* FIXME: if the user adds a newline in the replacement, the
7136 * index will not be recalculated for now, and the new line
7137 * will not be counted as a new header.
7138 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007139
Willy Tarreaufa355d42009-11-29 18:12:29 +01007140 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007141 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007142 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007143 HTTP_MSG_RQMETH,
7144 cur_ptr, cur_end + 1,
7145 NULL, NULL);
7146 if (unlikely(!cur_end))
7147 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007148
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007149 /* we have a full request and we know that we have either a CR
7150 * or an LF at <ptr>.
7151 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007152 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7153 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007154 /* there is no point trying this regex on headers */
7155 return 1;
7156 }
7157 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007158 return done;
7159}
Willy Tarreau97de6242006-12-27 17:18:38 +01007160
Willy Tarreau58f10d72006-12-04 02:26:12 +01007161
Willy Tarreau58f10d72006-12-04 02:26:12 +01007162
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007163/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007164 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007165 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007166 * unparsable request. Since it can manage the switch to another backend, it
7167 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007168 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007169int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007170{
Willy Tarreau192252e2015-04-04 01:47:55 +02007171 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007172 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007173 struct hdr_exp *exp;
7174
7175 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007176 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007177
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007178 /*
7179 * The interleaving of transformations and verdicts
7180 * makes it difficult to decide to continue or stop
7181 * the evaluation.
7182 */
7183
Willy Tarreau6c123b12010-01-28 20:22:06 +01007184 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7185 break;
7186
Willy Tarreau3d300592007-03-18 18:34:41 +01007187 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007188 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007189 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007190 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007191
7192 /* if this filter had a condition, evaluate it now and skip to
7193 * next filter if the condition does not match.
7194 */
7195 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007196 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007197 ret = acl_pass(ret);
7198 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7199 ret = !ret;
7200
7201 if (!ret)
7202 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007203 }
7204
7205 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007206 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007207 if (unlikely(ret < 0))
7208 return -1;
7209
7210 if (likely(ret == 0)) {
7211 /* The filter did not match the request, it can be
7212 * iterated through all headers.
7213 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007214 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7215 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007216 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007217 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007218 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007219}
7220
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007221
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007222/* Find the end of a cookie value contained between <s> and <e>. It works the
7223 * same way as with headers above except that the semi-colon also ends a token.
7224 * See RFC2965 for more information. Note that it requires a valid header to
7225 * return a valid result.
7226 */
7227char *find_cookie_value_end(char *s, const char *e)
7228{
7229 int quoted, qdpair;
7230
7231 quoted = qdpair = 0;
7232 for (; s < e; s++) {
7233 if (qdpair) qdpair = 0;
7234 else if (quoted) {
7235 if (*s == '\\') qdpair = 1;
7236 else if (*s == '"') quoted = 0;
7237 }
7238 else if (*s == '"') quoted = 1;
7239 else if (*s == ',' || *s == ';') return s;
7240 }
7241 return s;
7242}
7243
7244/* Delete a value in a header between delimiters <from> and <next> in buffer
7245 * <buf>. The number of characters displaced is returned, and the pointer to
7246 * the first delimiter is updated if required. The function tries as much as
7247 * possible to respect the following principles :
7248 * - replace <from> delimiter by the <next> one unless <from> points to a
7249 * colon, in which case <next> is simply removed
7250 * - set exactly one space character after the new first delimiter, unless
7251 * there are not enough characters in the block being moved to do so.
7252 * - remove unneeded spaces before the previous delimiter and after the new
7253 * one.
7254 *
7255 * It is the caller's responsibility to ensure that :
7256 * - <from> points to a valid delimiter or the colon ;
7257 * - <next> points to a valid delimiter or the final CR/LF ;
7258 * - there are non-space chars before <from> ;
7259 * - there is a CR/LF at or after <next>.
7260 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007261int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007262{
7263 char *prev = *from;
7264
7265 if (*prev == ':') {
7266 /* We're removing the first value, preserve the colon and add a
7267 * space if possible.
7268 */
7269 if (!http_is_crlf[(unsigned char)*next])
7270 next++;
7271 prev++;
7272 if (prev < next)
7273 *prev++ = ' ';
7274
7275 while (http_is_spht[(unsigned char)*next])
7276 next++;
7277 } else {
7278 /* Remove useless spaces before the old delimiter. */
7279 while (http_is_spht[(unsigned char)*(prev-1)])
7280 prev--;
7281 *from = prev;
7282
7283 /* copy the delimiter and if possible a space if we're
7284 * not at the end of the line.
7285 */
7286 if (!http_is_crlf[(unsigned char)*next]) {
7287 *prev++ = *next++;
7288 if (prev + 1 < next)
7289 *prev++ = ' ';
7290 while (http_is_spht[(unsigned char)*next])
7291 next++;
7292 }
7293 }
7294 return buffer_replace2(buf, prev, next, NULL, 0);
7295}
7296
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007297/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007298 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007299 * desirable to call it only when needed. This code is quite complex because
7300 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7301 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007302 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007303void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007304{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007305 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007306 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007307 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007308 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007309 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7310 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007311
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007312 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007313 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007314 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007315
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007316 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007317 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007318 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007319
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007320 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007321 hdr_beg = hdr_next;
7322 hdr_end = hdr_beg + cur_hdr->len;
7323 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007324
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007325 /* We have one full header between hdr_beg and hdr_end, and the
7326 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007327 * "Cookie:" headers.
7328 */
7329
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007330 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007331 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007332 old_idx = cur_idx;
7333 continue;
7334 }
7335
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007336 del_from = NULL; /* nothing to be deleted */
7337 preserve_hdr = 0; /* assume we may kill the whole header */
7338
Willy Tarreau58f10d72006-12-04 02:26:12 +01007339 /* Now look for cookies. Conforming to RFC2109, we have to support
7340 * attributes whose name begin with a '$', and associate them with
7341 * the right cookie, if we want to delete this cookie.
7342 * So there are 3 cases for each cookie read :
7343 * 1) it's a special attribute, beginning with a '$' : ignore it.
7344 * 2) it's a server id cookie that we *MAY* want to delete : save
7345 * some pointers on it (last semi-colon, beginning of cookie...)
7346 * 3) it's an application cookie : we *MAY* have to delete a previous
7347 * "special" cookie.
7348 * At the end of loop, if a "special" cookie remains, we may have to
7349 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007350 * *MUST* delete it.
7351 *
7352 * Note: RFC2965 is unclear about the processing of spaces around
7353 * the equal sign in the ATTR=VALUE form. A careful inspection of
7354 * the RFC explicitly allows spaces before it, and not within the
7355 * tokens (attrs or values). An inspection of RFC2109 allows that
7356 * too but section 10.1.3 lets one think that spaces may be allowed
7357 * after the equal sign too, resulting in some (rare) buggy
7358 * implementations trying to do that. So let's do what servers do.
7359 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7360 * allowed quoted strings in values, with any possible character
7361 * after a backslash, including control chars and delimitors, which
7362 * causes parsing to become ambiguous. Browsers also allow spaces
7363 * within values even without quotes.
7364 *
7365 * We have to keep multiple pointers in order to support cookie
7366 * removal at the beginning, middle or end of header without
7367 * corrupting the header. All of these headers are valid :
7368 *
7369 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7370 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7371 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7372 * | | | | | | | | |
7373 * | | | | | | | | hdr_end <--+
7374 * | | | | | | | +--> next
7375 * | | | | | | +----> val_end
7376 * | | | | | +-----------> val_beg
7377 * | | | | +--------------> equal
7378 * | | | +----------------> att_end
7379 * | | +---------------------> att_beg
7380 * | +--------------------------> prev
7381 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007382 */
7383
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007384 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7385 /* Iterate through all cookies on this line */
7386
7387 /* find att_beg */
7388 att_beg = prev + 1;
7389 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7390 att_beg++;
7391
7392 /* find att_end : this is the first character after the last non
7393 * space before the equal. It may be equal to hdr_end.
7394 */
7395 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007396
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007397 while (equal < hdr_end) {
7398 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007399 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007400 if (http_is_spht[(unsigned char)*equal++])
7401 continue;
7402 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007403 }
7404
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007405 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7406 * is between <att_beg> and <equal>, both may be identical.
7407 */
7408
7409 /* look for end of cookie if there is an equal sign */
7410 if (equal < hdr_end && *equal == '=') {
7411 /* look for the beginning of the value */
7412 val_beg = equal + 1;
7413 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7414 val_beg++;
7415
7416 /* find the end of the value, respecting quotes */
7417 next = find_cookie_value_end(val_beg, hdr_end);
7418
7419 /* make val_end point to the first white space or delimitor after the value */
7420 val_end = next;
7421 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7422 val_end--;
7423 } else {
7424 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007425 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007426
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007427 /* We have nothing to do with attributes beginning with '$'. However,
7428 * they will automatically be removed if a header before them is removed,
7429 * since they're supposed to be linked together.
7430 */
7431 if (*att_beg == '$')
7432 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007433
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007434 /* Ignore cookies with no equal sign */
7435 if (equal == next) {
7436 /* This is not our cookie, so we must preserve it. But if we already
7437 * scheduled another cookie for removal, we cannot remove the
7438 * complete header, but we can remove the previous block itself.
7439 */
7440 preserve_hdr = 1;
7441 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007442 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007443 val_end += delta;
7444 next += delta;
7445 hdr_end += delta;
7446 hdr_next += delta;
7447 cur_hdr->len += delta;
7448 http_msg_move_end(&txn->req, delta);
7449 prev = del_from;
7450 del_from = NULL;
7451 }
7452 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007453 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007454
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007455 /* if there are spaces around the equal sign, we need to
7456 * strip them otherwise we'll get trouble for cookie captures,
7457 * or even for rewrites. Since this happens extremely rarely,
7458 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007459 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007460 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7461 int stripped_before = 0;
7462 int stripped_after = 0;
7463
7464 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007465 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007466 equal += stripped_before;
7467 val_beg += stripped_before;
7468 }
7469
7470 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007471 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007472 val_beg += stripped_after;
7473 stripped_before += stripped_after;
7474 }
7475
7476 val_end += stripped_before;
7477 next += stripped_before;
7478 hdr_end += stripped_before;
7479 hdr_next += stripped_before;
7480 cur_hdr->len += stripped_before;
7481 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007482 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007483 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007484
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007485 /* First, let's see if we want to capture this cookie. We check
7486 * that we don't already have a client side cookie, because we
7487 * can only capture one. Also as an optimisation, we ignore
7488 * cookies shorter than the declared name.
7489 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007490 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7491 (val_end - att_beg >= sess->fe->capture_namelen) &&
7492 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007493 int log_len = val_end - att_beg;
7494
7495 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7496 Alert("HTTP logging : out of memory.\n");
7497 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007498 if (log_len > sess->fe->capture_len)
7499 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007500 memcpy(txn->cli_cookie, att_beg, log_len);
7501 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007502 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007503 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007504
Willy Tarreaubca99692010-10-06 19:25:55 +02007505 /* Persistence cookies in passive, rewrite or insert mode have the
7506 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007507 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007508 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007509 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007510 * For cookies in prefix mode, the form is :
7511 *
7512 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007513 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007514 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7515 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7516 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007517 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007518
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007519 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7520 * have the server ID between val_beg and delim, and the original cookie between
7521 * delim+1 and val_end. Otherwise, delim==val_end :
7522 *
7523 * Cookie: NAME=SRV; # in all but prefix modes
7524 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7525 * | || || | |+-> next
7526 * | || || | +--> val_end
7527 * | || || +---------> delim
7528 * | || |+------------> val_beg
7529 * | || +-------------> att_end = equal
7530 * | |+-----------------> att_beg
7531 * | +------------------> prev
7532 * +-------------------------> hdr_beg
7533 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007534
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007535 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007536 for (delim = val_beg; delim < val_end; delim++)
7537 if (*delim == COOKIE_DELIM)
7538 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007539 } else {
7540 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007541 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007542 /* Now check if the cookie contains a date field, which would
7543 * appear after a vertical bar ('|') just after the server name
7544 * and before the delimiter.
7545 */
7546 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7547 if (vbar1) {
7548 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007549 * right is the last seen date. It is a base64 encoded
7550 * 30-bit value representing the UNIX date since the
7551 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007552 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007553 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007554 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007555 if (val_end - vbar1 >= 5) {
7556 val = b64tos30(vbar1);
7557 if (val > 0)
7558 txn->cookie_last_date = val << 2;
7559 }
7560 /* look for a second vertical bar */
7561 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7562 if (vbar1 && (val_end - vbar1 > 5)) {
7563 val = b64tos30(vbar1 + 1);
7564 if (val > 0)
7565 txn->cookie_first_date = val << 2;
7566 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007567 }
7568 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007569
Willy Tarreauf64d1412010-10-07 20:06:11 +02007570 /* if the cookie has an expiration date and the proxy wants to check
7571 * it, then we do that now. We first check if the cookie is too old,
7572 * then only if it has expired. We detect strict overflow because the
7573 * time resolution here is not great (4 seconds). Cookies with dates
7574 * in the future are ignored if their offset is beyond one day. This
7575 * allows an admin to fix timezone issues without expiring everyone
7576 * and at the same time avoids keeping unwanted side effects for too
7577 * long.
7578 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007579 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7580 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007581 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007582 txn->flags &= ~TX_CK_MASK;
7583 txn->flags |= TX_CK_OLD;
7584 delim = val_beg; // let's pretend we have not found the cookie
7585 txn->cookie_first_date = 0;
7586 txn->cookie_last_date = 0;
7587 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007588 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7589 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007590 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007591 txn->flags &= ~TX_CK_MASK;
7592 txn->flags |= TX_CK_EXPIRED;
7593 delim = val_beg; // let's pretend we have not found the cookie
7594 txn->cookie_first_date = 0;
7595 txn->cookie_last_date = 0;
7596 }
7597
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007598 /* Here, we'll look for the first running server which supports the cookie.
7599 * This allows to share a same cookie between several servers, for example
7600 * to dedicate backup servers to specific servers only.
7601 * However, to prevent clients from sticking to cookie-less backup server
7602 * when they have incidentely learned an empty cookie, we simply ignore
7603 * empty cookies and mark them as invalid.
7604 * The same behaviour is applied when persistence must be ignored.
7605 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007606 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007607 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007608
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007609 while (srv) {
7610 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7611 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007612 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007613 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007614 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007615 /* we found the server and we can use it */
7616 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007617 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007618 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007619 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007620 break;
7621 } else {
7622 /* we found a server, but it's down,
7623 * mark it as such and go on in case
7624 * another one is available.
7625 */
7626 txn->flags &= ~TX_CK_MASK;
7627 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007628 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007629 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007630 srv = srv->next;
7631 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007632
Willy Tarreauf64d1412010-10-07 20:06:11 +02007633 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007634 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007635 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007636 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007637 txn->flags |= TX_CK_UNUSED;
7638 else
7639 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007640 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007641
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007642 /* depending on the cookie mode, we may have to either :
7643 * - delete the complete cookie if we're in insert+indirect mode, so that
7644 * the server never sees it ;
7645 * - remove the server id from the cookie value, and tag the cookie as an
7646 * application cookie so that it does not get accidentely removed later,
7647 * if we're in cookie prefix mode
7648 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007649 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007650 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007651
Willy Tarreau9b28e032012-10-12 23:49:43 +02007652 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007653 val_end += delta;
7654 next += delta;
7655 hdr_end += delta;
7656 hdr_next += delta;
7657 cur_hdr->len += delta;
7658 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007659
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007660 del_from = NULL;
7661 preserve_hdr = 1; /* we want to keep this cookie */
7662 }
7663 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007664 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007665 del_from = prev;
7666 }
7667 } else {
7668 /* This is not our cookie, so we must preserve it. But if we already
7669 * scheduled another cookie for removal, we cannot remove the
7670 * complete header, but we can remove the previous block itself.
7671 */
7672 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007673
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007674 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007675 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007676 if (att_beg >= del_from)
7677 att_beg += delta;
7678 if (att_end >= del_from)
7679 att_end += delta;
7680 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007681 val_end += delta;
7682 next += delta;
7683 hdr_end += delta;
7684 hdr_next += delta;
7685 cur_hdr->len += delta;
7686 http_msg_move_end(&txn->req, delta);
7687 prev = del_from;
7688 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007689 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007690 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007691
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007692 /* continue with next cookie on this header line */
7693 att_beg = next;
7694 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007695
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007696 /* There are no more cookies on this line.
7697 * We may still have one (or several) marked for deletion at the
7698 * end of the line. We must do this now in two ways :
7699 * - if some cookies must be preserved, we only delete from the
7700 * mark to the end of line ;
7701 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007702 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007703 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007704 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007705 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007706 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007707 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007708 cur_hdr->len += delta;
7709 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007710 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007711
7712 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007713 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7714 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007715 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007716 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007717 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007718 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007719 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007720 }
7721
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007722 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007723 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007724 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007725}
7726
7727
Willy Tarreaua15645d2007-03-18 16:22:39 +01007728/* Iterate the same filter through all response headers contained in <rtr>.
7729 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7730 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007731int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007732{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007733 char *cur_ptr, *cur_end, *cur_next;
7734 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007735 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007736 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007737 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007738
7739 last_hdr = 0;
7740
Willy Tarreau9b28e032012-10-12 23:49:43 +02007741 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007742 old_idx = 0;
7743
7744 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007745 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007746 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007747 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007748 (exp->action == ACT_ALLOW ||
7749 exp->action == ACT_DENY))
7750 return 0;
7751
7752 cur_idx = txn->hdr_idx.v[old_idx].next;
7753 if (!cur_idx)
7754 break;
7755
7756 cur_hdr = &txn->hdr_idx.v[cur_idx];
7757 cur_ptr = cur_next;
7758 cur_end = cur_ptr + cur_hdr->len;
7759 cur_next = cur_end + cur_hdr->cr + 1;
7760
7761 /* Now we have one header between cur_ptr and cur_end,
7762 * and the next header starts at cur_next.
7763 */
7764
Willy Tarreau15a53a42015-01-21 13:39:42 +01007765 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007766 switch (exp->action) {
7767 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007768 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007769 last_hdr = 1;
7770 break;
7771
7772 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007773 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007774 last_hdr = 1;
7775 break;
7776
7777 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007778 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7779 if (trash.len < 0)
7780 return -1;
7781
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007782 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007783 /* FIXME: if the user adds a newline in the replacement, the
7784 * index will not be recalculated for now, and the new line
7785 * will not be counted as a new header.
7786 */
7787
7788 cur_end += delta;
7789 cur_next += delta;
7790 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007791 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007792 break;
7793
7794 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007795 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007796 cur_next += delta;
7797
Willy Tarreaufa355d42009-11-29 18:12:29 +01007798 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007799 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7800 txn->hdr_idx.used--;
7801 cur_hdr->len = 0;
7802 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007803 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007804 break;
7805
7806 }
7807 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007808
7809 /* keep the link from this header to next one in case of later
7810 * removal of next header.
7811 */
7812 old_idx = cur_idx;
7813 }
7814 return 0;
7815}
7816
7817
7818/* Apply the filter to the status line in the response buffer <rtr>.
7819 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7820 * or -1 if a replacement resulted in an invalid status line.
7821 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007822int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007823{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007824 char *cur_ptr, *cur_end;
7825 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007826 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007827 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007828
7829
Willy Tarreau3d300592007-03-18 18:34:41 +01007830 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007831 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007832 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007833 (exp->action == ACT_ALLOW ||
7834 exp->action == ACT_DENY))
7835 return 0;
7836 else if (exp->action == ACT_REMOVE)
7837 return 0;
7838
7839 done = 0;
7840
Willy Tarreau9b28e032012-10-12 23:49:43 +02007841 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007842 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007843
7844 /* Now we have the status line between cur_ptr and cur_end */
7845
Willy Tarreau15a53a42015-01-21 13:39:42 +01007846 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007847 switch (exp->action) {
7848 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007849 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007850 done = 1;
7851 break;
7852
7853 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007854 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007855 done = 1;
7856 break;
7857
7858 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007859 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7860 if (trash.len < 0)
7861 return -1;
7862
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007863 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007864 /* FIXME: if the user adds a newline in the replacement, the
7865 * index will not be recalculated for now, and the new line
7866 * will not be counted as a new header.
7867 */
7868
Willy Tarreaufa355d42009-11-29 18:12:29 +01007869 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007870 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007871 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007872 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007873 cur_ptr, cur_end + 1,
7874 NULL, NULL);
7875 if (unlikely(!cur_end))
7876 return -1;
7877
7878 /* we have a full respnse and we know that we have either a CR
7879 * or an LF at <ptr>.
7880 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007881 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007882 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007883 /* there is no point trying this regex on headers */
7884 return 1;
7885 }
7886 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007887 return done;
7888}
7889
7890
7891
7892/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007893 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007894 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7895 * unparsable response.
7896 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007897int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007898{
Willy Tarreau192252e2015-04-04 01:47:55 +02007899 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007900 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007901 struct hdr_exp *exp;
7902
7903 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007904 int ret;
7905
7906 /*
7907 * The interleaving of transformations and verdicts
7908 * makes it difficult to decide to continue or stop
7909 * the evaluation.
7910 */
7911
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007912 if (txn->flags & TX_SVDENY)
7913 break;
7914
Willy Tarreau3d300592007-03-18 18:34:41 +01007915 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007916 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7917 exp->action == ACT_PASS)) {
7918 exp = exp->next;
7919 continue;
7920 }
7921
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007922 /* if this filter had a condition, evaluate it now and skip to
7923 * next filter if the condition does not match.
7924 */
7925 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007926 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007927 ret = acl_pass(ret);
7928 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7929 ret = !ret;
7930 if (!ret)
7931 continue;
7932 }
7933
Willy Tarreaua15645d2007-03-18 16:22:39 +01007934 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007935 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007936 if (unlikely(ret < 0))
7937 return -1;
7938
7939 if (likely(ret == 0)) {
7940 /* The filter did not match the response, it can be
7941 * iterated through all headers.
7942 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007943 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7944 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007945 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007946 }
7947 return 0;
7948}
7949
7950
Willy Tarreaua15645d2007-03-18 16:22:39 +01007951/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007952 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007953 * desirable to call it only when needed. This function is also used when we
7954 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007955 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007956void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007957{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007958 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007959 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01007960 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007961 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007962 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007963 char *hdr_beg, *hdr_end, *hdr_next;
7964 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007965
Willy Tarreaua15645d2007-03-18 16:22:39 +01007966 /* Iterate through the headers.
7967 * we start with the start line.
7968 */
7969 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007970 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007971
7972 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7973 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007974 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007975
7976 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007977 hdr_beg = hdr_next;
7978 hdr_end = hdr_beg + cur_hdr->len;
7979 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007980
Willy Tarreau24581ba2010-08-31 22:39:35 +02007981 /* We have one full header between hdr_beg and hdr_end, and the
7982 * next header starts at hdr_next. We're only interested in
7983 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007984 */
7985
Willy Tarreau24581ba2010-08-31 22:39:35 +02007986 is_cookie2 = 0;
7987 prev = hdr_beg + 10;
7988 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007989 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007990 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7991 if (!val) {
7992 old_idx = cur_idx;
7993 continue;
7994 }
7995 is_cookie2 = 1;
7996 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007997 }
7998
Willy Tarreau24581ba2010-08-31 22:39:35 +02007999 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8000 * <prev> points to the colon.
8001 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008002 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008003
Willy Tarreau24581ba2010-08-31 22:39:35 +02008004 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8005 * check-cache is enabled) and we are not interested in checking
8006 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008007 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02008008 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008009 return;
8010
Willy Tarreau24581ba2010-08-31 22:39:35 +02008011 /* OK so now we know we have to process this response cookie.
8012 * The format of the Set-Cookie header is slightly different
8013 * from the format of the Cookie header in that it does not
8014 * support the comma as a cookie delimiter (thus the header
8015 * cannot be folded) because the Expires attribute described in
8016 * the original Netscape's spec may contain an unquoted date
8017 * with a comma inside. We have to live with this because
8018 * many browsers don't support Max-Age and some browsers don't
8019 * support quoted strings. However the Set-Cookie2 header is
8020 * clean.
8021 *
8022 * We have to keep multiple pointers in order to support cookie
8023 * removal at the beginning, middle or end of header without
8024 * corrupting the header (in case of set-cookie2). A special
8025 * pointer, <scav> points to the beginning of the set-cookie-av
8026 * fields after the first semi-colon. The <next> pointer points
8027 * either to the end of line (set-cookie) or next unquoted comma
8028 * (set-cookie2). All of these headers are valid :
8029 *
8030 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8031 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8032 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8033 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8034 * | | | | | | | | | |
8035 * | | | | | | | | +-> next hdr_end <--+
8036 * | | | | | | | +------------> scav
8037 * | | | | | | +--------------> val_end
8038 * | | | | | +--------------------> val_beg
8039 * | | | | +----------------------> equal
8040 * | | | +------------------------> att_end
8041 * | | +----------------------------> att_beg
8042 * | +------------------------------> prev
8043 * +-----------------------------------------> hdr_beg
8044 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008045
Willy Tarreau24581ba2010-08-31 22:39:35 +02008046 for (; prev < hdr_end; prev = next) {
8047 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008048
Willy Tarreau24581ba2010-08-31 22:39:35 +02008049 /* find att_beg */
8050 att_beg = prev + 1;
8051 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8052 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008053
Willy Tarreau24581ba2010-08-31 22:39:35 +02008054 /* find att_end : this is the first character after the last non
8055 * space before the equal. It may be equal to hdr_end.
8056 */
8057 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008058
Willy Tarreau24581ba2010-08-31 22:39:35 +02008059 while (equal < hdr_end) {
8060 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8061 break;
8062 if (http_is_spht[(unsigned char)*equal++])
8063 continue;
8064 att_end = equal;
8065 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008066
Willy Tarreau24581ba2010-08-31 22:39:35 +02008067 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8068 * is between <att_beg> and <equal>, both may be identical.
8069 */
8070
8071 /* look for end of cookie if there is an equal sign */
8072 if (equal < hdr_end && *equal == '=') {
8073 /* look for the beginning of the value */
8074 val_beg = equal + 1;
8075 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8076 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008077
Willy Tarreau24581ba2010-08-31 22:39:35 +02008078 /* find the end of the value, respecting quotes */
8079 next = find_cookie_value_end(val_beg, hdr_end);
8080
8081 /* make val_end point to the first white space or delimitor after the value */
8082 val_end = next;
8083 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8084 val_end--;
8085 } else {
8086 /* <equal> points to next comma, semi-colon or EOL */
8087 val_beg = val_end = next = equal;
8088 }
8089
8090 if (next < hdr_end) {
8091 /* Set-Cookie2 supports multiple cookies, and <next> points to
8092 * a colon or semi-colon before the end. So skip all attr-value
8093 * pairs and look for the next comma. For Set-Cookie, since
8094 * commas are permitted in values, skip to the end.
8095 */
8096 if (is_cookie2)
8097 next = find_hdr_value_end(next, hdr_end);
8098 else
8099 next = hdr_end;
8100 }
8101
8102 /* Now everything is as on the diagram above */
8103
8104 /* Ignore cookies with no equal sign */
8105 if (equal == val_end)
8106 continue;
8107
8108 /* If there are spaces around the equal sign, we need to
8109 * strip them otherwise we'll get trouble for cookie captures,
8110 * or even for rewrites. Since this happens extremely rarely,
8111 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008112 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008113 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8114 int stripped_before = 0;
8115 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008116
Willy Tarreau24581ba2010-08-31 22:39:35 +02008117 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008118 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008119 equal += stripped_before;
8120 val_beg += stripped_before;
8121 }
8122
8123 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008124 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008125 val_beg += stripped_after;
8126 stripped_before += stripped_after;
8127 }
8128
8129 val_end += stripped_before;
8130 next += stripped_before;
8131 hdr_end += stripped_before;
8132 hdr_next += stripped_before;
8133 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008134 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008135 }
8136
8137 /* First, let's see if we want to capture this cookie. We check
8138 * that we don't already have a server side cookie, because we
8139 * can only capture one. Also as an optimisation, we ignore
8140 * cookies shorter than the declared name.
8141 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008142 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008143 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008144 (val_end - att_beg >= sess->fe->capture_namelen) &&
8145 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008146 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008147 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008148 Alert("HTTP logging : out of memory.\n");
8149 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008150 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008151 if (log_len > sess->fe->capture_len)
8152 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008153 memcpy(txn->srv_cookie, att_beg, log_len);
8154 txn->srv_cookie[log_len] = 0;
8155 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008156 }
8157
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008158 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008159 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008160 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008161 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8162 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008163 /* assume passive cookie by default */
8164 txn->flags &= ~TX_SCK_MASK;
8165 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008166
8167 /* If the cookie is in insert mode on a known server, we'll delete
8168 * this occurrence because we'll insert another one later.
8169 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008170 * a direct access.
8171 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008172 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008173 /* The "preserve" flag was set, we don't want to touch the
8174 * server's cookie.
8175 */
8176 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008177 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008178 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008179 /* this cookie must be deleted */
8180 if (*prev == ':' && next == hdr_end) {
8181 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008182 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008183 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8184 txn->hdr_idx.used--;
8185 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008186 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008187 hdr_next += delta;
8188 http_msg_move_end(&txn->rsp, delta);
8189 /* note: while both invalid now, <next> and <hdr_end>
8190 * are still equal, so the for() will stop as expected.
8191 */
8192 } else {
8193 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008194 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008195 next = prev;
8196 hdr_end += delta;
8197 hdr_next += delta;
8198 cur_hdr->len += delta;
8199 http_msg_move_end(&txn->rsp, delta);
8200 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008201 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008202 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008203 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008204 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008205 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008206 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008207 * with this server since we know it.
8208 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008209 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008210 next += delta;
8211 hdr_end += delta;
8212 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008213 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008214 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008215
Willy Tarreauf1348312010-10-07 15:54:11 +02008216 txn->flags &= ~TX_SCK_MASK;
8217 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008218 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008219 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008220 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008221 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008222 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008223 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008224 next += delta;
8225 hdr_end += delta;
8226 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008227 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008228 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008229
Willy Tarreau827aee92011-03-10 16:55:02 +01008230 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008231 txn->flags &= ~TX_SCK_MASK;
8232 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008233 }
8234 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008235 /* that's done for this cookie, check the next one on the same
8236 * line when next != hdr_end (only if is_cookie2).
8237 */
8238 }
8239 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008240 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008241 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008242}
8243
8244
Willy Tarreaua15645d2007-03-18 16:22:39 +01008245/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008246 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008247 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008248void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008249{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008250 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008251 char *p1, *p2;
8252
8253 char *cur_ptr, *cur_end, *cur_next;
8254 int cur_idx;
8255
Willy Tarreau5df51872007-11-25 16:20:08 +01008256 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008257 return;
8258
8259 /* Iterate through the headers.
8260 * we start with the start line.
8261 */
8262 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008263 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008264
8265 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8266 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008267 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008268
8269 cur_hdr = &txn->hdr_idx.v[cur_idx];
8270 cur_ptr = cur_next;
8271 cur_end = cur_ptr + cur_hdr->len;
8272 cur_next = cur_end + cur_hdr->cr + 1;
8273
8274 /* We have one full header between cur_ptr and cur_end, and the
8275 * next header starts at cur_next. We're only interested in
8276 * "Cookie:" headers.
8277 */
8278
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008279 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8280 if (val) {
8281 if ((cur_end - (cur_ptr + val) >= 8) &&
8282 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8283 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8284 return;
8285 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008286 }
8287
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008288 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8289 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008290 continue;
8291
8292 /* OK, right now we know we have a cache-control header at cur_ptr */
8293
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008294 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008295
8296 if (p1 >= cur_end) /* no more info */
8297 continue;
8298
8299 /* p1 is at the beginning of the value */
8300 p2 = p1;
8301
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008302 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008303 p2++;
8304
8305 /* we have a complete value between p1 and p2 */
8306 if (p2 < cur_end && *p2 == '=') {
8307 /* we have something of the form no-cache="set-cookie" */
8308 if ((cur_end - p1 >= 21) &&
8309 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8310 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008311 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008312 continue;
8313 }
8314
8315 /* OK, so we know that either p2 points to the end of string or to a comma */
8316 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008317 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008318 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8319 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8320 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008321 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008322 return;
8323 }
8324
8325 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008326 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008327 continue;
8328 }
8329 }
8330}
8331
Willy Tarreau58f10d72006-12-04 02:26:12 +01008332
Willy Tarreaub2513902006-12-17 14:52:38 +01008333/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008334 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008335 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008336 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008337 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008338 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008339 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008340 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008341 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008342int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008343{
8344 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008345 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008346 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008347
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008348 if (!uri_auth)
8349 return 0;
8350
Cyril Bonté70be45d2010-10-12 00:14:35 +02008351 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008352 return 0;
8353
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008354 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008355 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008356 return 0;
8357
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008358 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008359 return 0;
8360
Willy Tarreaub2513902006-12-17 14:52:38 +01008361 return 1;
8362}
8363
Willy Tarreau4076a152009-04-02 15:18:36 +02008364/*
8365 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008366 * By default it tries to report the error position as msg->err_pos. However if
8367 * this one is not set, it will then report msg->next, which is the last known
8368 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008369 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008370 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008371void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008372 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008373 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008374{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008375 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008376 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008377 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008378
Willy Tarreau9b28e032012-10-12 23:49:43 +02008379 es->len = MIN(chn->buf->i, sizeof(es->buf));
8380 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008381 len1 = MIN(len1, es->len);
8382 len2 = es->len - len1; /* remaining data if buffer wraps */
8383
Willy Tarreau9b28e032012-10-12 23:49:43 +02008384 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008385 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008386 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008387
Willy Tarreau4076a152009-04-02 15:18:36 +02008388 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008389 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008390 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008391 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008392
Willy Tarreau4076a152009-04-02 15:18:36 +02008393 es->when = date; // user-visible date
8394 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008395 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008396 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008397 if (objt_conn(sess->origin))
8398 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008399 else
8400 memset(&es->src, 0, sizeof(es->src));
8401
Willy Tarreau078272e2010-12-12 12:46:33 +01008402 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008403 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008404 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008405 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008406 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008407 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008408 es->b_out = chn->buf->o;
8409 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008410 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008411 es->m_clen = msg->chunk_len;
8412 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008413}
Willy Tarreaub2513902006-12-17 14:52:38 +01008414
Willy Tarreau294c4732011-12-16 21:35:50 +01008415/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8416 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8417 * performed over the whole headers. Otherwise it must contain a valid header
8418 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8419 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8420 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8421 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008422 * -1. The value fetch stops at commas, so this function is suited for use with
8423 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008424 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008425 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008426unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008427 struct hdr_idx *idx, int occ,
8428 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008429{
Willy Tarreau294c4732011-12-16 21:35:50 +01008430 struct hdr_ctx local_ctx;
8431 char *ptr_hist[MAX_HDR_HISTORY];
8432 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008433 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008434 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008435
Willy Tarreau294c4732011-12-16 21:35:50 +01008436 if (!ctx) {
8437 local_ctx.idx = 0;
8438 ctx = &local_ctx;
8439 }
8440
Willy Tarreaubce70882009-09-07 11:51:47 +02008441 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008442 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008443 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008444 occ--;
8445 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008446 *vptr = ctx->line + ctx->val;
8447 *vlen = ctx->vlen;
8448 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008449 }
8450 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008451 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008452 }
8453
8454 /* negative occurrence, we scan all the list then walk back */
8455 if (-occ > MAX_HDR_HISTORY)
8456 return 0;
8457
Willy Tarreau294c4732011-12-16 21:35:50 +01008458 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008459 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008460 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8461 len_hist[hist_ptr] = ctx->vlen;
8462 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008463 hist_ptr = 0;
8464 found++;
8465 }
8466 if (-occ > found)
8467 return 0;
8468 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008469 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8470 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8471 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008472 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008473 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008474 if (hist_ptr >= MAX_HDR_HISTORY)
8475 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008476 *vptr = ptr_hist[hist_ptr];
8477 *vlen = len_hist[hist_ptr];
8478 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008479}
8480
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008481/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8482 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8483 * performed over the whole headers. Otherwise it must contain a valid header
8484 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8485 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8486 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8487 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8488 * -1. This function differs from http_get_hdr() in that it only returns full
8489 * line header values and does not stop at commas.
8490 * The return value is 0 if nothing was found, or non-zero otherwise.
8491 */
8492unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8493 struct hdr_idx *idx, int occ,
8494 struct hdr_ctx *ctx, char **vptr, int *vlen)
8495{
8496 struct hdr_ctx local_ctx;
8497 char *ptr_hist[MAX_HDR_HISTORY];
8498 int len_hist[MAX_HDR_HISTORY];
8499 unsigned int hist_ptr;
8500 int found;
8501
8502 if (!ctx) {
8503 local_ctx.idx = 0;
8504 ctx = &local_ctx;
8505 }
8506
8507 if (occ >= 0) {
8508 /* search from the beginning */
8509 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8510 occ--;
8511 if (occ <= 0) {
8512 *vptr = ctx->line + ctx->val;
8513 *vlen = ctx->vlen;
8514 return 1;
8515 }
8516 }
8517 return 0;
8518 }
8519
8520 /* negative occurrence, we scan all the list then walk back */
8521 if (-occ > MAX_HDR_HISTORY)
8522 return 0;
8523
8524 found = hist_ptr = 0;
8525 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8526 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8527 len_hist[hist_ptr] = ctx->vlen;
8528 if (++hist_ptr >= MAX_HDR_HISTORY)
8529 hist_ptr = 0;
8530 found++;
8531 }
8532 if (-occ > found)
8533 return 0;
8534 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8535 * find occurrence -occ, so we have to check [hist_ptr+occ].
8536 */
8537 hist_ptr += occ;
8538 if (hist_ptr >= MAX_HDR_HISTORY)
8539 hist_ptr -= MAX_HDR_HISTORY;
8540 *vptr = ptr_hist[hist_ptr];
8541 *vlen = len_hist[hist_ptr];
8542 return 1;
8543}
8544
Willy Tarreaubaaee002006-06-26 02:48:02 +02008545/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008546 * Print a debug line with a header. Always stop at the first CR or LF char,
8547 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8548 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008549 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008550void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008551{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008552 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008553 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008554
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008555 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008556 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008557 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008558 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 +02008559
8560 for (max = 0; start + max < end; max++)
8561 if (start[max] == '\r' || start[max] == '\n')
8562 break;
8563
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008564 UBOUND(max, trash.size - trash.len - 3);
8565 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8566 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008567 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008568}
8569
Willy Tarreaueee5b512015-04-03 23:46:31 +02008570
8571/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8572 * The hdr_idx is allocated as well. In case of allocation failure, everything
8573 * allocated is freed and NULL is returned. Otherwise the new transaction is
8574 * assigned to the stream and returned.
8575 */
8576struct http_txn *http_alloc_txn(struct stream *s)
8577{
8578 struct http_txn *txn = s->txn;
8579
8580 if (txn)
8581 return txn;
8582
8583 txn = pool_alloc2(pool2_http_txn);
8584 if (!txn)
8585 return txn;
8586
8587 txn->hdr_idx.size = global.tune.max_http_hdr;
8588 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8589 if (!txn->hdr_idx.v) {
8590 pool_free2(pool2_http_txn, txn);
8591 return NULL;
8592 }
8593
8594 s->txn = txn;
8595 return txn;
8596}
8597
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008598void http_txn_reset_req(struct http_txn *txn)
8599{
8600 txn->req.flags = 0;
8601 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8602 txn->req.next = 0;
8603 txn->req.chunk_len = 0LL;
8604 txn->req.body_len = 0LL;
8605 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8606}
8607
8608void http_txn_reset_res(struct http_txn *txn)
8609{
8610 txn->rsp.flags = 0;
8611 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8612 txn->rsp.next = 0;
8613 txn->rsp.chunk_len = 0LL;
8614 txn->rsp.body_len = 0LL;
8615 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8616}
8617
Willy Tarreau0937bc42009-12-22 15:03:09 +01008618/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008619 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008620 * the required fields are properly allocated and that we only need to (re)init
8621 * them. This should be used before processing any new request.
8622 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008623void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008624{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008625 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008626 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008627
8628 txn->flags = 0;
8629 txn->status = -1;
8630
Willy Tarreauf64d1412010-10-07 20:06:11 +02008631 txn->cookie_first_date = 0;
8632 txn->cookie_last_date = 0;
8633
Willy Tarreaueee5b512015-04-03 23:46:31 +02008634 txn->srv_cookie = NULL;
8635 txn->cli_cookie = NULL;
8636 txn->uri = NULL;
8637
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008638 http_txn_reset_req(txn);
8639 http_txn_reset_res(txn);
8640
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008641 txn->req.chn = &s->req;
8642 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008643
8644 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008645
8646 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8647 if (fe->options2 & PR_O2_REQBUG_OK)
8648 txn->req.err_pos = -1; /* let buggy requests pass */
8649
Willy Tarreau0937bc42009-12-22 15:03:09 +01008650 if (txn->hdr_idx.v)
8651 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008652
8653 vars_init(&s->vars_txn, SCOPE_TXN);
8654 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008655}
8656
8657/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008658void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008659{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008660 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008661 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008662
8663 /* these ones will have been dynamically allocated */
8664 pool_free2(pool2_requri, txn->uri);
8665 pool_free2(pool2_capture, txn->cli_cookie);
8666 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008667 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008668
William Lallemanda73203e2012-03-12 12:48:57 +01008669 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008670 txn->uri = NULL;
8671 txn->srv_cookie = NULL;
8672 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008673
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008674 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008675 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008676 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008677 pool_free2(h->pool, s->req_cap[h->index]);
8678 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008679 }
8680
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008681 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008682 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008683 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008684 pool_free2(h->pool, s->res_cap[h->index]);
8685 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008686 }
8687
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008688 vars_prune(&s->vars_txn, s);
8689 vars_prune(&s->vars_reqres, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008690}
8691
8692/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008693void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008694{
8695 http_end_txn(s);
8696 http_init_txn(s);
8697
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008698 /* reinitialise the current rule list pointer to NULL. We are sure that
8699 * any rulelist match the NULL pointer.
8700 */
8701 s->current_rule_list = NULL;
8702
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008703 s->be = strm_fe(s);
8704 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008705 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008706 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008707 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008708 /* re-init store persistence */
8709 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008710 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008711
Willy Tarreau0937bc42009-12-22 15:03:09 +01008712 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008713
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008714 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008715
Willy Tarreau739cfba2010-01-25 23:11:14 +01008716 /* We must trim any excess data from the response buffer, because we
8717 * may have blocked an invalid response from a server that we don't
8718 * want to accidentely forward once we disable the analysers, nor do
8719 * we want those data to come along with next response. A typical
8720 * example of such data would be from a buggy server responding to
8721 * a HEAD with some data, or sending more than the advertised
8722 * content-length.
8723 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008724 if (unlikely(s->res.buf->i))
8725 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008726
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008727 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008728 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008729
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008730 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008731 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008732
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008733 s->req.rex = TICK_ETERNITY;
8734 s->req.wex = TICK_ETERNITY;
8735 s->req.analyse_exp = TICK_ETERNITY;
8736 s->res.rex = TICK_ETERNITY;
8737 s->res.wex = TICK_ETERNITY;
8738 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008739}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008740
Sasha Pachev218f0642014-06-16 12:05:59 -06008741void free_http_res_rules(struct list *r)
8742{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008743 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008744
8745 list_for_each_entry_safe(pr, tr, r, list) {
8746 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008747 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008748 free(pr);
8749 }
8750}
8751
8752void free_http_req_rules(struct list *r)
8753{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008754 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01008755
8756 list_for_each_entry_safe(pr, tr, r, list) {
8757 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008758 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008759 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008760
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008761 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008762 free(pr);
8763 }
8764}
8765
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008766/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008767struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01008768{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008769 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008770 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008771 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008772 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008773
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008774 rule = (struct act_rule*)calloc(1, sizeof(struct act_rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01008775 if (!rule) {
8776 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008777 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008778 }
8779
CJ Ess108b1dd2015-04-07 12:03:37 -04008780 rule->deny_status = HTTP_ERR_403;
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008781 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008782 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01008783 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02008784 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04008785 int code;
8786 int hc;
8787
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008788 rule->action = ACT_ACTION_DENY;
Willy Tarreauff011f22011-01-06 17:51:27 +01008789 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04008790 if (strcmp(args[cur_arg], "deny_status") == 0) {
8791 cur_arg++;
8792 if (!args[cur_arg]) {
8793 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
8794 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
8795 goto out_err;
8796 }
8797
8798 code = atol(args[cur_arg]);
8799 cur_arg++;
8800 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
8801 if (http_err_codes[hc] == code) {
8802 rule->deny_status = hc;
8803 break;
8804 }
8805 }
8806
8807 if (hc >= HTTP_ERR_SIZE) {
8808 Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n",
8809 file, linenum, code);
8810 }
8811 }
Willy Tarreauccbcc372012-12-27 12:37:57 +01008812 } else if (!strcmp(args[0], "tarpit")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008813 rule->action = ACT_HTTP_REQ_TARPIT;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008814 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008815 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008816 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008817 cur_arg = 1;
8818
8819 while(*args[cur_arg]) {
8820 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008821 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008822 cur_arg+=2;
8823 continue;
8824 } else
8825 break;
8826 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008827 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008828 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008829 cur_arg = 1;
8830
8831 if (!*args[cur_arg] ||
8832 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8833 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8834 file, linenum, args[0]);
8835 goto out_err;
8836 }
8837 rule->arg.nice = atoi(args[cur_arg]);
8838 if (rule->arg.nice < -1024)
8839 rule->arg.nice = -1024;
8840 else if (rule->arg.nice > 1024)
8841 rule->arg.nice = 1024;
8842 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008843 } else if (!strcmp(args[0], "set-tos")) {
8844#ifdef IP_TOS
8845 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008846 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008847 cur_arg = 1;
8848
8849 if (!*args[cur_arg] ||
8850 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8851 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8852 file, linenum, args[0]);
8853 goto out_err;
8854 }
8855
8856 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8857 if (err && *err != '\0') {
8858 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8859 file, linenum, err, args[0]);
8860 goto out_err;
8861 }
8862 cur_arg++;
8863#else
8864 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8865 goto out_err;
8866#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008867 } else if (!strcmp(args[0], "set-mark")) {
8868#ifdef SO_MARK
8869 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008870 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008871 cur_arg = 1;
8872
8873 if (!*args[cur_arg] ||
8874 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8875 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8876 file, linenum, args[0]);
8877 goto out_err;
8878 }
8879
8880 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8881 if (err && *err != '\0') {
8882 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8883 file, linenum, err, args[0]);
8884 goto out_err;
8885 }
8886 cur_arg++;
8887 global.last_checks |= LSTCHK_NETADM;
8888#else
8889 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8890 goto out_err;
8891#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008892 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008893 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008894 cur_arg = 1;
8895
8896 if (!*args[cur_arg] ||
8897 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8898 bad_log_level:
8899 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8900 file, linenum, args[0]);
8901 goto out_err;
8902 }
8903 if (strcmp(args[cur_arg], "silent") == 0)
8904 rule->arg.loglevel = -1;
8905 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8906 goto bad_log_level;
8907 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008908 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008909 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008910 cur_arg = 1;
8911
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008912 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8913 (*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 +01008914 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8915 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008916 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008917 }
8918
8919 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8920 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8921 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008922
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008923 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008924 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008925 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8926 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008927 free(proxy->conf.lfs_file);
8928 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8929 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008930 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02008931 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008932 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008933 cur_arg = 1;
8934
8935 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02008936 (*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 -06008937 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
8938 file, linenum, args[0]);
8939 goto out_err;
8940 }
8941
8942 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8943 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8944 LIST_INIT(&rule->arg.hdr_add.fmt);
8945
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008946 error = NULL;
8947 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
8948 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8949 args[cur_arg + 1], error);
8950 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008951 goto out_err;
8952 }
8953
8954 proxy->conf.args.ctx = ARGC_HRQ;
8955 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
8956 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8957 file, linenum);
8958
8959 free(proxy->conf.lfs_file);
8960 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8961 proxy->conf.lfs_line = proxy->conf.args.line;
8962 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008963 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008964 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008965 cur_arg = 1;
8966
8967 if (!*args[cur_arg] ||
8968 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8969 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8970 file, linenum, args[0]);
8971 goto out_err;
8972 }
8973
8974 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8975 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8976
8977 proxy->conf.args.ctx = ARGC_HRQ;
8978 free(proxy->conf.lfs_file);
8979 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8980 proxy->conf.lfs_line = proxy->conf.args.line;
8981 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02008982 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
8983 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02008984 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02008985 struct sample_expr *expr;
8986 unsigned int where;
8987 char *err = NULL;
8988
8989 cur_arg = 1;
8990 proxy->conf.args.ctx = ARGC_TRK;
8991
8992 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
8993 if (!expr) {
8994 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8995 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
8996 free(err);
8997 goto out_err;
8998 }
8999
9000 where = 0;
9001 if (proxy->cap & PR_CAP_FE)
9002 where |= SMP_VAL_FE_HRQ_HDR;
9003 if (proxy->cap & PR_CAP_BE)
9004 where |= SMP_VAL_BE_HRQ_HDR;
9005
9006 if (!(expr->fetch->val & where)) {
9007 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9008 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9009 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9010 args[cur_arg-1], sample_src_names(expr->fetch->use));
9011 free(expr);
9012 goto out_err;
9013 }
9014
9015 if (strcmp(args[cur_arg], "table") == 0) {
9016 cur_arg++;
9017 if (!args[cur_arg]) {
9018 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9019 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9020 free(expr);
9021 goto out_err;
9022 }
9023 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009024 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02009025 cur_arg++;
9026 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009027 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009028 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009029 } else if (strcmp(args[0], "redirect") == 0) {
9030 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009031 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009032
Willy Tarreaube4653b2015-05-28 15:26:58 +02009033 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009034 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9035 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9036 goto out_err;
9037 }
9038
9039 /* this redirect rule might already contain a parsed condition which
9040 * we'll pass to the http-request rule.
9041 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009042 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009043 rule->arg.redir = redir;
9044 rule->cond = redir->cond;
9045 redir->cond = NULL;
9046 cur_arg = 2;
9047 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009048 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9049 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009050 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009051 /*
9052 * '+ 8' for 'add-acl('
9053 * '- 9' for 'add-acl(' + trailing ')'
9054 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009055 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009056
9057 cur_arg = 1;
9058
9059 if (!*args[cur_arg] ||
9060 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9061 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9062 file, linenum, args[0]);
9063 goto out_err;
9064 }
9065
9066 LIST_INIT(&rule->arg.map.key);
9067 proxy->conf.args.ctx = ARGC_HRQ;
9068 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9069 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9070 file, linenum);
9071 free(proxy->conf.lfs_file);
9072 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9073 proxy->conf.lfs_line = proxy->conf.args.line;
9074 cur_arg += 1;
9075 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9076 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009077 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009078 /*
9079 * '+ 8' for 'del-acl('
9080 * '- 9' for 'del-acl(' + trailing ')'
9081 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009082 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009083
9084 cur_arg = 1;
9085
9086 if (!*args[cur_arg] ||
9087 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9088 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9089 file, linenum, args[0]);
9090 goto out_err;
9091 }
9092
9093 LIST_INIT(&rule->arg.map.key);
9094 proxy->conf.args.ctx = ARGC_HRQ;
9095 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9096 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9097 file, linenum);
9098 free(proxy->conf.lfs_file);
9099 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9100 proxy->conf.lfs_line = proxy->conf.args.line;
9101 cur_arg += 1;
9102 } else if (strncmp(args[0], "del-map", 7) == 0) {
9103 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009104 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009105 /*
9106 * '+ 8' for 'del-map('
9107 * '- 9' for 'del-map(' + trailing ')'
9108 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009109 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009110
9111 cur_arg = 1;
9112
9113 if (!*args[cur_arg] ||
9114 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9115 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9116 file, linenum, args[0]);
9117 goto out_err;
9118 }
9119
9120 LIST_INIT(&rule->arg.map.key);
9121 proxy->conf.args.ctx = ARGC_HRQ;
9122 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9123 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9124 file, linenum);
9125 free(proxy->conf.lfs_file);
9126 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9127 proxy->conf.lfs_line = proxy->conf.args.line;
9128 cur_arg += 1;
9129 } else if (strncmp(args[0], "set-map", 7) == 0) {
9130 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009131 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009132 /*
9133 * '+ 8' for 'set-map('
9134 * '- 9' for 'set-map(' + trailing ')'
9135 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009136 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009137
9138 cur_arg = 1;
9139
9140 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9141 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9142 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9143 file, linenum, args[0]);
9144 goto out_err;
9145 }
9146
9147 LIST_INIT(&rule->arg.map.key);
9148 LIST_INIT(&rule->arg.map.value);
9149 proxy->conf.args.ctx = ARGC_HRQ;
9150
9151 /* key pattern */
9152 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9153 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9154 file, linenum);
9155
9156 /* value pattern */
9157 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9158 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9159 file, linenum);
9160 free(proxy->conf.lfs_file);
9161 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9162 proxy->conf.lfs_line = proxy->conf.args.line;
9163
9164 cur_arg += 2;
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02009165 } else if (strncmp(args[0], "set-src", 7) == 0) {
9166 struct sample_expr *expr;
9167 unsigned int where;
9168 char *err = NULL;
9169
9170 cur_arg = 1;
9171 proxy->conf.args.ctx = ARGC_HRQ;
9172
9173 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9174 if (!expr) {
9175 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9176 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9177 free(err);
9178 goto out_err;
9179 }
9180
9181 where = 0;
9182 if (proxy->cap & PR_CAP_FE)
9183 where |= SMP_VAL_FE_HRQ_HDR;
9184 if (proxy->cap & PR_CAP_BE)
9185 where |= SMP_VAL_BE_HRQ_HDR;
9186
9187 if (!(expr->fetch->val & where)) {
9188 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9189 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9190 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9191 args[cur_arg-1], sample_src_names(expr->fetch->use));
9192 free(expr);
9193 goto out_err;
9194 }
9195
Thierry FOURNIERa002dc92015-07-31 08:50:51 +02009196 rule->arg.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009197 rule->action = ACT_HTTP_REQ_SET_SRC;
William Lallemand73025dd2014-04-24 14:38:37 +02009198 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9199 char *errmsg = NULL;
9200 cur_arg = 1;
9201 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009202 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009203 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009204 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009205 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9206 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9207 free(errmsg);
9208 goto out_err;
9209 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009210 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009211 action_build_list(&http_req_keywords.list, &trash);
9212 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
9213 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
9214 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', "
9215 "'set-src'%s%s, but got '%s'%s.\n",
9216 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009217 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009218 }
9219
9220 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9221 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009222 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009223
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009224 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9225 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9226 file, linenum, args[0], errmsg);
9227 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009228 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009229 }
9230 rule->cond = cond;
9231 }
9232 else if (*args[cur_arg]) {
9233 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9234 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9235 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009236 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009237 }
9238
9239 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009240 out_err:
9241 free(rule);
9242 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009243}
9244
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009245/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009246struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009247{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009248 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009249 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009250 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009251 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009252
9253 rule = calloc(1, sizeof(*rule));
9254 if (!rule) {
9255 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9256 goto out_err;
9257 }
9258
9259 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009260 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009261 cur_arg = 1;
9262 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009263 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009264 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009265 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009266 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009267 cur_arg = 1;
9268
9269 if (!*args[cur_arg] ||
9270 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9271 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9272 file, linenum, args[0]);
9273 goto out_err;
9274 }
9275 rule->arg.nice = atoi(args[cur_arg]);
9276 if (rule->arg.nice < -1024)
9277 rule->arg.nice = -1024;
9278 else if (rule->arg.nice > 1024)
9279 rule->arg.nice = 1024;
9280 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009281 } else if (!strcmp(args[0], "set-tos")) {
9282#ifdef IP_TOS
9283 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009284 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009285 cur_arg = 1;
9286
9287 if (!*args[cur_arg] ||
9288 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9289 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9290 file, linenum, args[0]);
9291 goto out_err;
9292 }
9293
9294 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9295 if (err && *err != '\0') {
9296 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9297 file, linenum, err, args[0]);
9298 goto out_err;
9299 }
9300 cur_arg++;
9301#else
9302 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9303 goto out_err;
9304#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009305 } else if (!strcmp(args[0], "set-mark")) {
9306#ifdef SO_MARK
9307 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009308 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009309 cur_arg = 1;
9310
9311 if (!*args[cur_arg] ||
9312 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9313 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9314 file, linenum, args[0]);
9315 goto out_err;
9316 }
9317
9318 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9319 if (err && *err != '\0') {
9320 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9321 file, linenum, err, args[0]);
9322 goto out_err;
9323 }
9324 cur_arg++;
9325 global.last_checks |= LSTCHK_NETADM;
9326#else
9327 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9328 goto out_err;
9329#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009330 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009331 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009332 cur_arg = 1;
9333
9334 if (!*args[cur_arg] ||
9335 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9336 bad_log_level:
9337 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9338 file, linenum, args[0]);
9339 goto out_err;
9340 }
9341 if (strcmp(args[cur_arg], "silent") == 0)
9342 rule->arg.loglevel = -1;
9343 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9344 goto bad_log_level;
9345 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009346 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009347 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009348 cur_arg = 1;
9349
9350 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9351 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9352 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9353 file, linenum, args[0]);
9354 goto out_err;
9355 }
9356
9357 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9358 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9359 LIST_INIT(&rule->arg.hdr_add.fmt);
9360
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009361 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009362 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009363 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9364 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009365 free(proxy->conf.lfs_file);
9366 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9367 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009368 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009369 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009370 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009371 cur_arg = 1;
9372
9373 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009374 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9375 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009376 file, linenum, args[0]);
9377 goto out_err;
9378 }
9379
9380 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9381 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9382 LIST_INIT(&rule->arg.hdr_add.fmt);
9383
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009384 error = NULL;
9385 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9386 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9387 args[cur_arg + 1], error);
9388 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009389 goto out_err;
9390 }
9391
9392 proxy->conf.args.ctx = ARGC_HRQ;
9393 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9394 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9395 file, linenum);
9396
9397 free(proxy->conf.lfs_file);
9398 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9399 proxy->conf.lfs_line = proxy->conf.args.line;
9400 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009401 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009402 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009403 cur_arg = 1;
9404
9405 if (!*args[cur_arg] ||
9406 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9407 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9408 file, linenum, args[0]);
9409 goto out_err;
9410 }
9411
9412 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9413 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9414
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009415 proxy->conf.args.ctx = ARGC_HRS;
9416 free(proxy->conf.lfs_file);
9417 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9418 proxy->conf.lfs_line = proxy->conf.args.line;
9419 cur_arg += 1;
9420 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9421 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009422 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009423 /*
9424 * '+ 8' for 'add-acl('
9425 * '- 9' for 'add-acl(' + trailing ')'
9426 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009427 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009428
9429 cur_arg = 1;
9430
9431 if (!*args[cur_arg] ||
9432 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9433 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9434 file, linenum, args[0]);
9435 goto out_err;
9436 }
9437
9438 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009439 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009440 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9441 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9442 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009443 free(proxy->conf.lfs_file);
9444 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9445 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009446
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009447 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009448 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9449 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009450 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009451 /*
9452 * '+ 8' for 'del-acl('
9453 * '- 9' for 'del-acl(' + trailing ')'
9454 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009455 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009456
9457 cur_arg = 1;
9458
9459 if (!*args[cur_arg] ||
9460 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9461 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9462 file, linenum, args[0]);
9463 goto out_err;
9464 }
9465
9466 LIST_INIT(&rule->arg.map.key);
9467 proxy->conf.args.ctx = ARGC_HRS;
9468 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9469 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9470 file, linenum);
9471 free(proxy->conf.lfs_file);
9472 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9473 proxy->conf.lfs_line = proxy->conf.args.line;
9474 cur_arg += 1;
9475 } else if (strncmp(args[0], "del-map", 7) == 0) {
9476 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009477 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009478 /*
9479 * '+ 8' for 'del-map('
9480 * '- 9' for 'del-map(' + trailing ')'
9481 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009482 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009483
9484 cur_arg = 1;
9485
9486 if (!*args[cur_arg] ||
9487 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9488 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9489 file, linenum, args[0]);
9490 goto out_err;
9491 }
9492
9493 LIST_INIT(&rule->arg.map.key);
9494 proxy->conf.args.ctx = ARGC_HRS;
9495 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9496 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9497 file, linenum);
9498 free(proxy->conf.lfs_file);
9499 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9500 proxy->conf.lfs_line = proxy->conf.args.line;
9501 cur_arg += 1;
9502 } else if (strncmp(args[0], "set-map", 7) == 0) {
9503 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009504 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009505 /*
9506 * '+ 8' for 'set-map('
9507 * '- 9' for 'set-map(' + trailing ')'
9508 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009509 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009510
9511 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 LIST_INIT(&rule->arg.map.key);
9521 LIST_INIT(&rule->arg.map.value);
9522
9523 proxy->conf.args.ctx = ARGC_HRS;
9524
9525 /* key pattern */
9526 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9527 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9528 file, linenum);
9529
9530 /* value pattern */
9531 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9532 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9533 file, linenum);
9534
9535 free(proxy->conf.lfs_file);
9536 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9537 proxy->conf.lfs_line = proxy->conf.args.line;
9538
9539 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009540 } else if (strcmp(args[0], "redirect") == 0) {
9541 struct redirect_rule *redir;
9542 char *errmsg = NULL;
9543
9544 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9545 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9546 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9547 goto out_err;
9548 }
9549
9550 /* this redirect rule might already contain a parsed condition which
9551 * we'll pass to the http-request rule.
9552 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009553 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009554 rule->arg.redir = redir;
9555 rule->cond = redir->cond;
9556 redir->cond = NULL;
9557 cur_arg = 2;
9558 return rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009559 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9560 char *errmsg = NULL;
9561 cur_arg = 1;
9562 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009563 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009564 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009565 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009566 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9567 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9568 free(errmsg);
9569 goto out_err;
9570 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009571 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009572 action_build_list(&http_res_keywords.list, &trash);
9573 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9574 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
9575 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', "
9576 "'set-src'%s%s, but got '%s'%s.\n",
9577 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009578 goto out_err;
9579 }
9580
9581 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9582 struct acl_cond *cond;
9583 char *errmsg = NULL;
9584
9585 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9586 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9587 file, linenum, args[0], errmsg);
9588 free(errmsg);
9589 goto out_err;
9590 }
9591 rule->cond = cond;
9592 }
9593 else if (*args[cur_arg]) {
9594 Alert("parsing [%s:%d]: 'http-response %s' expects"
9595 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9596 file, linenum, args[0], args[cur_arg]);
9597 goto out_err;
9598 }
9599
9600 return rule;
9601 out_err:
9602 free(rule);
9603 return NULL;
9604}
9605
Willy Tarreau4baae242012-12-27 12:00:31 +01009606/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009607 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009608 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9609 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009610 */
9611struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009612 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009613{
9614 struct redirect_rule *rule;
9615 int cur_arg;
9616 int type = REDIRECT_TYPE_NONE;
9617 int code = 302;
9618 const char *destination = NULL;
9619 const char *cookie = NULL;
9620 int cookie_set = 0;
9621 unsigned int flags = REDIRECT_FLAG_NONE;
9622 struct acl_cond *cond = NULL;
9623
9624 cur_arg = 0;
9625 while (*(args[cur_arg])) {
9626 if (strcmp(args[cur_arg], "location") == 0) {
9627 if (!*args[cur_arg + 1])
9628 goto missing_arg;
9629
9630 type = REDIRECT_TYPE_LOCATION;
9631 cur_arg++;
9632 destination = args[cur_arg];
9633 }
9634 else if (strcmp(args[cur_arg], "prefix") == 0) {
9635 if (!*args[cur_arg + 1])
9636 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009637 type = REDIRECT_TYPE_PREFIX;
9638 cur_arg++;
9639 destination = args[cur_arg];
9640 }
9641 else if (strcmp(args[cur_arg], "scheme") == 0) {
9642 if (!*args[cur_arg + 1])
9643 goto missing_arg;
9644
9645 type = REDIRECT_TYPE_SCHEME;
9646 cur_arg++;
9647 destination = args[cur_arg];
9648 }
9649 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9650 if (!*args[cur_arg + 1])
9651 goto missing_arg;
9652
9653 cur_arg++;
9654 cookie = args[cur_arg];
9655 cookie_set = 1;
9656 }
9657 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9658 if (!*args[cur_arg + 1])
9659 goto missing_arg;
9660
9661 cur_arg++;
9662 cookie = args[cur_arg];
9663 cookie_set = 0;
9664 }
9665 else if (strcmp(args[cur_arg], "code") == 0) {
9666 if (!*args[cur_arg + 1])
9667 goto missing_arg;
9668
9669 cur_arg++;
9670 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009671 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009672 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009673 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009674 args[cur_arg - 1], args[cur_arg]);
9675 return NULL;
9676 }
9677 }
9678 else if (!strcmp(args[cur_arg],"drop-query")) {
9679 flags |= REDIRECT_FLAG_DROP_QS;
9680 }
9681 else if (!strcmp(args[cur_arg],"append-slash")) {
9682 flags |= REDIRECT_FLAG_APPEND_SLASH;
9683 }
9684 else if (strcmp(args[cur_arg], "if") == 0 ||
9685 strcmp(args[cur_arg], "unless") == 0) {
9686 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9687 if (!cond) {
9688 memprintf(errmsg, "error in condition: %s", *errmsg);
9689 return NULL;
9690 }
9691 break;
9692 }
9693 else {
9694 memprintf(errmsg,
9695 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9696 args[cur_arg]);
9697 return NULL;
9698 }
9699 cur_arg++;
9700 }
9701
9702 if (type == REDIRECT_TYPE_NONE) {
9703 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9704 return NULL;
9705 }
9706
Willy Tarreaube4653b2015-05-28 15:26:58 +02009707 if (dir && type != REDIRECT_TYPE_LOCATION) {
9708 memprintf(errmsg, "response only supports redirect type 'location'");
9709 return NULL;
9710 }
9711
Willy Tarreau4baae242012-12-27 12:00:31 +01009712 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9713 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009714 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009715
9716 if (!use_fmt) {
9717 /* old-style static redirect rule */
9718 rule->rdr_str = strdup(destination);
9719 rule->rdr_len = strlen(destination);
9720 }
9721 else {
9722 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009723
9724 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9725 * if prefix == "/", we don't want to add anything, otherwise it
9726 * makes it hard for the user to configure a self-redirection.
9727 */
Godbachd9722032014-12-18 15:44:58 +08009728 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009729 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009730 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009731 dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR
9732 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009733 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009734 free(curproxy->conf.lfs_file);
9735 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9736 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009737 }
9738 }
9739
Willy Tarreau4baae242012-12-27 12:00:31 +01009740 if (cookie) {
9741 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9742 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9743 */
9744 rule->cookie_len = strlen(cookie);
9745 if (cookie_set) {
9746 rule->cookie_str = malloc(rule->cookie_len + 10);
9747 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9748 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9749 rule->cookie_len += 9;
9750 } else {
9751 rule->cookie_str = malloc(rule->cookie_len + 21);
9752 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9753 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9754 rule->cookie_len += 20;
9755 }
9756 }
9757 rule->type = type;
9758 rule->code = code;
9759 rule->flags = flags;
9760 LIST_INIT(&rule->list);
9761 return rule;
9762
9763 missing_arg:
9764 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9765 return NULL;
9766}
9767
Willy Tarreau8797c062007-05-07 00:55:35 +02009768/************************************************************************/
9769/* The code below is dedicated to ACL parsing and matching */
9770/************************************************************************/
9771
9772
Willy Tarreau14174bc2012-04-16 14:34:04 +02009773/* This function ensures that the prerequisites for an L7 fetch are ready,
9774 * which means that a request or response is ready. If some data is missing,
9775 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009776 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9777 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009778 *
9779 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009780 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9781 * decide whether or not an HTTP message is present ;
9782 * 0 if the requested data cannot be fetched or if it is certain that
9783 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009784 * 1 if an HTTP message is ready
9785 */
James Rosewell91a41cb2015-09-18 17:11:16 +01009786int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009787 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009788{
Willy Tarreau192252e2015-04-04 01:47:55 +02009789 struct http_txn *txn;
9790 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009791
Willy Tarreaueee5b512015-04-03 23:46:31 +02009792 /* Note: this function may only be used from places where
9793 * http_init_txn() has already been done, and implies that <s>,
9794 * <txn>, and <hdr_idx.v> are properly set. An extra check protects
9795 * against an eventual mistake in the fetch capability matrix.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009796 */
9797
Willy Tarreau192252e2015-04-04 01:47:55 +02009798 if (!s)
9799 return 0;
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +02009800 if (!s->txn) {
9801 if (unlikely(!http_alloc_txn(s)))
9802 return 0; /* not enough memory */
9803 http_init_txn(s);
9804 }
Willy Tarreau192252e2015-04-04 01:47:55 +02009805 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +02009806 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009807
9808 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009809 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009810
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009811 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009812 /* If the buffer does not leave enough free space at the end,
9813 * we must first realign it.
9814 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009815 if (s->req.buf->p > s->req.buf->data &&
9816 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
9817 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009818
Willy Tarreau14174bc2012-04-16 14:34:04 +02009819 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009820 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009821 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009822
9823 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009824 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009825 http_msg_analyzer(msg, &txn->hdr_idx);
9826
9827 /* Still no valid request ? */
9828 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009829 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009830 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009831 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009832 }
9833 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009834 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009835 return 0;
9836 }
9837
9838 /* OK we just got a valid HTTP request. We have some minor
9839 * preparation to perform so that further checks can rely
9840 * on HTTP tests.
9841 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009842
9843 /* If the request was parsed but was too large, we must absolutely
9844 * return an error so that it is not processed. At the moment this
9845 * cannot happen, but if the parsers are to change in the future,
9846 * we want this check to be maintained.
9847 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009848 if (unlikely(s->req.buf->i + s->req.buf->p >
9849 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009850 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009851 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009852 return 1;
9853 }
9854
Willy Tarreau9b28e032012-10-12 23:49:43 +02009855 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009856 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02009857 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009858
Willy Tarreau506d0502013-07-06 13:29:24 +02009859 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9860 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009861 }
9862
Willy Tarreau506d0502013-07-06 13:29:24 +02009863 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009864 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009865 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009866
9867 /* otherwise everything's ready for the request */
9868 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009869 else {
9870 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009871 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9872 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009873 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009874 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009875 }
9876
9877 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009878 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009879 return 1;
9880}
Willy Tarreau8797c062007-05-07 00:55:35 +02009881
Willy Tarreau8797c062007-05-07 00:55:35 +02009882/* 1. Check on METHOD
9883 * We use the pre-parsed method if it is known, and store its number as an
9884 * integer. If it is unknown, we use the pointer and the length.
9885 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009886static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009887{
9888 int len, meth;
9889
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009890 len = strlen(text);
9891 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009892
9893 pattern->val.i = meth;
9894 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +02009895 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009896 pattern->len = len;
9897 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009898 else {
9899 pattern->ptr.str = NULL;
9900 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009901 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009902 return 1;
9903}
9904
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009905/* This function fetches the method of current HTTP request and stores
9906 * it in the global pattern struct as a chunk. There are two possibilities :
9907 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9908 * in <len> and <ptr> is NULL ;
9909 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9910 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009911 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009912 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009913static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009914smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009915{
9916 int meth;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009917 struct http_txn *txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009918
Willy Tarreau24e32d82012-04-23 23:55:44 +02009919 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009920
Willy Tarreau8797c062007-05-07 00:55:35 +02009921 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009922 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009923 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009924 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009925 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9926 /* ensure the indexes are not affected */
9927 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009928 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009929 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
9930 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009931 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009932 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009933 return 1;
9934}
9935
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009936/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009937static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009938{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009939 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009940 struct pattern_list *lst;
9941 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009942
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009943 list_for_each_entry(lst, &expr->patterns, list) {
9944 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009945
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009946 /* well-known method */
9947 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009948 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009949 return pattern;
9950 else
9951 continue;
9952 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009953
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009954 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009955 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009956 continue;
9957
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009958 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009959 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
9960 (!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 +01009961 return pattern;
9962 }
9963 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009964}
9965
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009966static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009967smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009968{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009969 struct http_txn *txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009970 char *ptr;
9971 int len;
9972
Willy Tarreauc0239e02012-04-16 14:42:55 +02009973 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009974
Willy Tarreau8797c062007-05-07 00:55:35 +02009975 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009976 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009977
9978 while ((len-- > 0) && (*ptr++ != '/'));
9979 if (len <= 0)
9980 return 0;
9981
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009982 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009983 smp->data.u.str.str = ptr;
9984 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009985
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009986 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009987 return 1;
9988}
9989
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009990static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009991smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009992{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009993 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009994 char *ptr;
9995 int len;
9996
Willy Tarreauc0239e02012-04-16 14:42:55 +02009997 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009998
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009999 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010000 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10001 return 0;
10002
Willy Tarreau8797c062007-05-07 00:55:35 +020010003 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010004 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010005
10006 while ((len-- > 0) && (*ptr++ != '/'));
10007 if (len <= 0)
10008 return 0;
10009
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010010 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010011 smp->data.u.str.str = ptr;
10012 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010013
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010014 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010015 return 1;
10016}
10017
10018/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010019static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010020smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010021{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010022 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010023 char *ptr;
10024 int len;
10025
Willy Tarreauc0239e02012-04-16 14:42:55 +020010026 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010027
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010028 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010029 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10030 return 0;
10031
Willy Tarreau8797c062007-05-07 00:55:35 +020010032 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010033 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010034
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010035 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010036 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010037 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010038 return 1;
10039}
10040
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010041/* returns the longest available part of the body. This requires that the body
10042 * has been waited for using http-buffer-request.
10043 */
10044static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010045smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010046{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010047 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010048 struct http_msg *msg;
10049 unsigned long len;
10050 unsigned long block1;
10051 char *body;
10052 struct chunk *temp;
10053
10054 CHECK_HTTP_MESSAGE_FIRST();
10055
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010056 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010057 msg = &txn->req;
10058 else
10059 msg = &txn->rsp;
10060
10061 len = http_body_bytes(msg);
10062 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10063
10064 block1 = len;
10065 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10066 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10067
10068 if (block1 == len) {
10069 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010070 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010071 smp->data.u.str.str = body;
10072 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010073 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10074 }
10075 else {
10076 /* buffer is wrapped, we need to defragment it */
10077 temp = get_trash_chunk();
10078 memcpy(temp->str, body, block1);
10079 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010080 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010081 smp->data.u.str.str = temp->str;
10082 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010083 smp->flags = SMP_F_VOL_TEST;
10084 }
10085 return 1;
10086}
10087
10088
10089/* returns the available length of the body. This requires that the body
10090 * has been waited for using http-buffer-request.
10091 */
10092static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010093smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010094{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010095 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010096 struct http_msg *msg;
10097
10098 CHECK_HTTP_MESSAGE_FIRST();
10099
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010100 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010101 msg = &txn->req;
10102 else
10103 msg = &txn->rsp;
10104
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010105 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010106 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010107
10108 smp->flags = SMP_F_VOL_TEST;
10109 return 1;
10110}
10111
10112
10113/* returns the advertised length of the body, or the advertised size of the
10114 * chunks available in the buffer. This requires that the body has been waited
10115 * for using http-buffer-request.
10116 */
10117static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010118smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010119{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010120 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010121 struct http_msg *msg;
10122
10123 CHECK_HTTP_MESSAGE_FIRST();
10124
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010125 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010126 msg = &txn->req;
10127 else
10128 msg = &txn->rsp;
10129
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010130 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010131 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010132
10133 smp->flags = SMP_F_VOL_TEST;
10134 return 1;
10135}
10136
10137
Willy Tarreau8797c062007-05-07 00:55:35 +020010138/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010139static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010140smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010141{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010142 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010143
Willy Tarreauc0239e02012-04-16 14:42:55 +020010144 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010145
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010146 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010147 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010148 smp->data.u.str.len = txn->req.sl.rq.u_l;
10149 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010150 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010151 return 1;
10152}
10153
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010154static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010155smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010156{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010157 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010158 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010159
Willy Tarreauc0239e02012-04-16 14:42:55 +020010160 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010161
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010162 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010163 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 +020010164 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010165 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010166
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010167 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010168 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010169 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010170 return 1;
10171}
10172
10173static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010174smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010175{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010176 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010177 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010178
Willy Tarreauc0239e02012-04-16 14:42:55 +020010179 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010180
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010181 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010182 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 +020010183 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10184 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010185
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010186 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010187 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010188 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010189 return 1;
10190}
10191
Willy Tarreau185b5c42012-04-26 15:11:51 +020010192/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10193 * Accepts an optional argument of type string containing the header field name,
10194 * and an optional argument of type signed or unsigned integer to request an
10195 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010196 * headers are considered from the first one. It does not stop on commas and
10197 * returns full lines instead (useful for User-Agent or Date for example).
10198 */
10199static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010200smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010201{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010202 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010203 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010204 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010205 int occ = 0;
10206 const char *name_str = NULL;
10207 int name_len = 0;
10208
10209 if (!ctx) {
10210 /* first call */
10211 ctx = &static_hdr_ctx;
10212 ctx->idx = 0;
10213 smp->ctx.a[0] = ctx;
10214 }
10215
10216 if (args) {
10217 if (args[0].type != ARGT_STR)
10218 return 0;
10219 name_str = args[0].data.str.str;
10220 name_len = args[0].data.str.len;
10221
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010222 if (args[1].type == ARGT_SINT)
10223 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010224 }
10225
10226 CHECK_HTTP_MESSAGE_FIRST();
10227
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010228 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010229 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 +020010230
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010231 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10232 /* search for header from the beginning */
10233 ctx->idx = 0;
10234
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010235 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010236 /* no explicit occurrence and single fetch => last header by default */
10237 occ = -1;
10238
10239 if (!occ)
10240 /* prepare to report multiple occurrences for ACL fetches */
10241 smp->flags |= SMP_F_NOT_LAST;
10242
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010243 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010244 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010245 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 +020010246 return 1;
10247
10248 smp->flags &= ~SMP_F_NOT_LAST;
10249 return 0;
10250}
10251
10252/* 6. Check on HTTP header count. The number of occurrences is returned.
10253 * Accepts exactly 1 argument of type string. It does not stop on commas and
10254 * returns full lines instead (useful for User-Agent or Date for example).
10255 */
10256static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010257smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010258{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010259 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010260 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010261 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010262 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010263 const char *name = NULL;
10264 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010265
Willy Tarreau601a4d12015-04-01 19:16:09 +020010266 if (args && args->type == ARGT_STR) {
10267 name = args->data.str.str;
10268 len = args->data.str.len;
10269 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010270
10271 CHECK_HTTP_MESSAGE_FIRST();
10272
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010273 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010274 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 +020010275
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010276 ctx.idx = 0;
10277 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010278 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010279 cnt++;
10280
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010281 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010282 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010283 smp->flags = SMP_F_VOL_HDR;
10284 return 1;
10285}
10286
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010287static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010288smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010289{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010290 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010291 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010292 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010293 struct chunk *temp;
10294 char del = ',';
10295
10296 if (args && args->type == ARGT_STR)
10297 del = *args[0].data.str.str;
10298
10299 CHECK_HTTP_MESSAGE_FIRST();
10300
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010301 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010302 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 +020010303
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010304 temp = get_trash_chunk();
10305
10306 ctx.idx = 0;
10307 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10308 if (temp->len)
10309 temp->str[temp->len++] = del;
10310 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10311 temp->len += ctx.del;
10312 }
10313
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010314 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010315 smp->data.u.str.str = temp->str;
10316 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010317 smp->flags = SMP_F_VOL_HDR;
10318 return 1;
10319}
10320
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010321/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10322 * Accepts an optional argument of type string containing the header field name,
10323 * and an optional argument of type signed or unsigned integer to request an
10324 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010325 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010326 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010327static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010328smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010329{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010330 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010331 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010332 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010333 int occ = 0;
10334 const char *name_str = NULL;
10335 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010336
Willy Tarreaua890d072013-04-02 12:01:06 +020010337 if (!ctx) {
10338 /* first call */
10339 ctx = &static_hdr_ctx;
10340 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010341 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010342 }
10343
Willy Tarreau185b5c42012-04-26 15:11:51 +020010344 if (args) {
10345 if (args[0].type != ARGT_STR)
10346 return 0;
10347 name_str = args[0].data.str.str;
10348 name_len = args[0].data.str.len;
10349
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010350 if (args[1].type == ARGT_SINT)
10351 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010352 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010353
Willy Tarreaue333ec92012-04-16 16:26:40 +020010354 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010355
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010356 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010357 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 +020010358
Willy Tarreau185b5c42012-04-26 15:11:51 +020010359 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010360 /* search for header from the beginning */
10361 ctx->idx = 0;
10362
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010363 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010364 /* no explicit occurrence and single fetch => last header by default */
10365 occ = -1;
10366
10367 if (!occ)
10368 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010369 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010370
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010371 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010372 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010373 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 +020010374 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010375
Willy Tarreau37406352012-04-23 16:16:37 +020010376 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010377 return 0;
10378}
10379
Willy Tarreauc11416f2007-06-17 16:58:38 +020010380/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010381 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010382 */
10383static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010384smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010385{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010386 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010387 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010388 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010389 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010390 const char *name = NULL;
10391 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010392
Willy Tarreau601a4d12015-04-01 19:16:09 +020010393 if (args && args->type == ARGT_STR) {
10394 name = args->data.str.str;
10395 len = args->data.str.len;
10396 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010397
Willy Tarreaue333ec92012-04-16 16:26:40 +020010398 CHECK_HTTP_MESSAGE_FIRST();
10399
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010400 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010401 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 +020010402
Willy Tarreau33a7e692007-06-10 19:45:56 +020010403 ctx.idx = 0;
10404 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010405 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010406 cnt++;
10407
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010408 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010409 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010410 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010411 return 1;
10412}
10413
Willy Tarreau185b5c42012-04-26 15:11:51 +020010414/* Fetch an HTTP header's integer value. The integer value is returned. It
10415 * takes a mandatory argument of type string and an optional one of type int
10416 * to designate a specific occurrence. It returns an unsigned integer, which
10417 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010418 */
10419static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010420smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010421{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010422 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010423
Willy Tarreauf853c462012-04-23 18:53:56 +020010424 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010425 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010426 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010427 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010428
Willy Tarreaud53e2422012-04-16 17:21:11 +020010429 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010430}
10431
Cyril Bonté69fa9922012-10-25 00:01:06 +020010432/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10433 * and an optional one of type int to designate a specific occurrence.
10434 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010435 */
10436static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010437smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010438{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010439 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010440
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010441 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010442 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010443 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010444 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010445 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010446 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010447 if (smp->data.u.str.len < temp->size - 1) {
10448 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10449 temp->str[smp->data.u.str.len] = '\0';
10450 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010451 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010452 break;
10453 }
10454 }
10455 }
10456
Willy Tarreaud53e2422012-04-16 17:21:11 +020010457 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010458 if (!(smp->flags & SMP_F_NOT_LAST))
10459 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010460 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010461 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010462}
10463
Willy Tarreau737b0c12007-06-10 21:28:46 +020010464/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10465 * the first '/' after the possible hostname, and ends before the possible '?'.
10466 */
10467static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010468smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010469{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010470 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010471 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010472
Willy Tarreauc0239e02012-04-16 14:42:55 +020010473 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010474
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010475 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010476 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010477 ptr = http_get_path(txn);
10478 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010479 return 0;
10480
10481 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010482 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010483 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010484
10485 while (ptr < end && *ptr != '?')
10486 ptr++;
10487
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010488 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010489 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010490 return 1;
10491}
10492
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010493/* This produces a concatenation of the first occurrence of the Host header
10494 * followed by the path component if it begins with a slash ('/'). This means
10495 * that '*' will not be added, resulting in exactly the first Host entry.
10496 * If no Host header is found, then the path is returned as-is. The returned
10497 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010498 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010499 */
10500static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010501smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010502{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010503 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010504 char *ptr, *end, *beg;
10505 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010506 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010507
10508 CHECK_HTTP_MESSAGE_FIRST();
10509
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010510 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010511 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010512 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010513 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010514
10515 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010516 temp = get_trash_chunk();
10517 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010518 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010519 smp->data.u.str.str = temp->str;
10520 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010521
10522 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010523 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010524 beg = http_get_path(txn);
10525 if (!beg)
10526 beg = end;
10527
10528 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10529
10530 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010531 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10532 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010533 }
10534
10535 smp->flags = SMP_F_VOL_1ST;
10536 return 1;
10537}
10538
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010539/* This produces a 32-bit hash of the concatenation of the first occurrence of
10540 * the Host header followed by the path component if it begins with a slash ('/').
10541 * This means that '*' will not be added, resulting in exactly the first Host
10542 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010543 * is hashed using the path hash followed by a full avalanche hash and provides a
10544 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010545 * high-traffic sites without having to store whole paths.
10546 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010547int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010548smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010549{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010550 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010551 struct hdr_ctx ctx;
10552 unsigned int hash = 0;
10553 char *ptr, *beg, *end;
10554 int len;
10555
10556 CHECK_HTTP_MESSAGE_FIRST();
10557
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010558 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010559 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010560 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010561 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10562 ptr = ctx.line + ctx.val;
10563 len = ctx.vlen;
10564 while (len--)
10565 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10566 }
10567
10568 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010569 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010570 beg = http_get_path(txn);
10571 if (!beg)
10572 beg = end;
10573
10574 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10575
10576 if (beg < ptr && *beg == '/') {
10577 while (beg < ptr)
10578 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10579 }
10580 hash = full_hash(hash);
10581
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010582 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010583 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010584 smp->flags = SMP_F_VOL_1ST;
10585 return 1;
10586}
10587
Willy Tarreau4a550602012-12-09 14:53:32 +010010588/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010589 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10590 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10591 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010592 * that in environments where IPv6 is insignificant, truncating the output to
10593 * 8 bytes would still work.
10594 */
10595static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010596smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010597{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010598 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010599 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010600
10601 if (!cli_conn)
10602 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010603
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010604 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010605 return 0;
10606
Willy Tarreau47ca5452012-12-23 20:22:19 +010010607 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010608 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010609 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010610
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010611 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010612 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010613 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010614 temp->len += 4;
10615 break;
10616 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010617 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010618 temp->len += 16;
10619 break;
10620 default:
10621 return 0;
10622 }
10623
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010624 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010625 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010626 return 1;
10627}
10628
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010629/* Extracts the query string, which comes after the question mark '?'. If no
10630 * question mark is found, nothing is returned. Otherwise it returns a sample
10631 * of type string carrying the whole query string.
10632 */
10633static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010634smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010635{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010636 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010637 char *ptr, *end;
10638
10639 CHECK_HTTP_MESSAGE_FIRST();
10640
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010641 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010642 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10643 end = ptr + txn->req.sl.rq.u_l;
10644
10645 /* look up the '?' */
10646 do {
10647 if (ptr == end)
10648 return 0;
10649 } while (*ptr++ != '?');
10650
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010651 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010652 smp->data.u.str.str = ptr;
10653 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010654 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10655 return 1;
10656}
10657
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010658static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010659smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010660{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010661 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10662 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10663 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010664
Willy Tarreau24e32d82012-04-23 23:55:44 +020010665 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010666
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010667 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010668 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010669 return 1;
10670}
10671
Willy Tarreau7f18e522010-10-22 20:04:13 +020010672/* return a valid test if the current request is the first one on the connection */
10673static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010674smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010675{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010676 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010677 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010678 return 1;
10679}
10680
Willy Tarreau34db1082012-04-19 17:16:54 +020010681/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010682static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010683smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010684{
10685
Willy Tarreau24e32d82012-04-23 23:55:44 +020010686 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010687 return 0;
10688
Willy Tarreauc0239e02012-04-16 14:42:55 +020010689 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010690
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010691 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010692 return 0;
10693
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010694 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010695 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010696 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010697 return 1;
10698}
Willy Tarreau8797c062007-05-07 00:55:35 +020010699
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010700/* Accepts exactly 1 argument of type userlist */
10701static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010702smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010703{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010704 if (!args || args->type != ARGT_USR)
10705 return 0;
10706
10707 CHECK_HTTP_MESSAGE_FIRST();
10708
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010709 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010710 return 0;
10711
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010712 /* if the user does not belong to the userlist or has a wrong password,
10713 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010714 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010715 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010716 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10717 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010718 return 0;
10719
10720 /* pat_match_auth() will need the user list */
10721 smp->ctx.a[0] = args->data.usr;
10722
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010723 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010724 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010725 smp->data.u.str.str = smp->strm->txn->auth.user;
10726 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010727
10728 return 1;
10729}
10730
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010731/* Try to find the next occurrence of a cookie name in a cookie header value.
10732 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10733 * the cookie value is returned into *value and *value_l, and the function
10734 * returns a pointer to the next pointer to search from if the value was found.
10735 * Otherwise if the cookie was not found, NULL is returned and neither value
10736 * nor value_l are touched. The input <hdr> string should first point to the
10737 * header's value, and the <hdr_end> pointer must point to the first character
10738 * not part of the value. <list> must be non-zero if value may represent a list
10739 * of values (cookie headers). This makes it faster to abort parsing when no
10740 * list is expected.
10741 */
David Carlier4686f792015-09-25 14:10:50 +010010742char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010743extract_cookie_value(char *hdr, const char *hdr_end,
10744 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010745 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010746{
10747 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10748 char *next;
10749
10750 /* we search at least a cookie name followed by an equal, and more
10751 * generally something like this :
10752 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10753 */
10754 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10755 /* Iterate through all cookies on this line */
10756
10757 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10758 att_beg++;
10759
10760 /* find att_end : this is the first character after the last non
10761 * space before the equal. It may be equal to hdr_end.
10762 */
10763 equal = att_end = att_beg;
10764
10765 while (equal < hdr_end) {
10766 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10767 break;
10768 if (http_is_spht[(unsigned char)*equal++])
10769 continue;
10770 att_end = equal;
10771 }
10772
10773 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10774 * is between <att_beg> and <equal>, both may be identical.
10775 */
10776
10777 /* look for end of cookie if there is an equal sign */
10778 if (equal < hdr_end && *equal == '=') {
10779 /* look for the beginning of the value */
10780 val_beg = equal + 1;
10781 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10782 val_beg++;
10783
10784 /* find the end of the value, respecting quotes */
10785 next = find_cookie_value_end(val_beg, hdr_end);
10786
10787 /* make val_end point to the first white space or delimitor after the value */
10788 val_end = next;
10789 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10790 val_end--;
10791 } else {
10792 val_beg = val_end = next = equal;
10793 }
10794
10795 /* We have nothing to do with attributes beginning with '$'. However,
10796 * they will automatically be removed if a header before them is removed,
10797 * since they're supposed to be linked together.
10798 */
10799 if (*att_beg == '$')
10800 continue;
10801
10802 /* Ignore cookies with no equal sign */
10803 if (equal == next)
10804 continue;
10805
10806 /* Now we have the cookie name between att_beg and att_end, and
10807 * its value between val_beg and val_end.
10808 */
10809
10810 if (att_end - att_beg == cookie_name_l &&
10811 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10812 /* let's return this value and indicate where to go on from */
10813 *value = val_beg;
10814 *value_l = val_end - val_beg;
10815 return next + 1;
10816 }
10817
10818 /* Set-Cookie headers only have the name in the first attr=value part */
10819 if (!list)
10820 break;
10821 }
10822
10823 return NULL;
10824}
10825
William Lallemanda43ba4e2014-01-28 18:14:25 +010010826/* Fetch a captured HTTP request header. The index is the position of
10827 * the "capture" option in the configuration file
10828 */
10829static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010830smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010831{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010832 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010833 int idx;
10834
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010835 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010836 return 0;
10837
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010838 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010839
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010840 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 +010010841 return 0;
10842
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010843 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010844 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010845 smp->data.u.str.str = smp->strm->req_cap[idx];
10846 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010847
10848 return 1;
10849}
10850
10851/* Fetch a captured HTTP response header. The index is the position of
10852 * the "capture" option in the configuration file
10853 */
10854static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010855smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010856{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010857 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010858 int idx;
10859
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010860 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010861 return 0;
10862
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010863 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010864
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010865 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 +010010866 return 0;
10867
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010868 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010869 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010870 smp->data.u.str.str = smp->strm->res_cap[idx];
10871 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010872
10873 return 1;
10874}
10875
William Lallemand65ad6e12014-01-31 15:08:02 +010010876/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10877static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010878smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010879{
10880 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010881 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010010882 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010883
Willy Tarreau15e91e12015-04-04 00:52:09 +020010884 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010885 return 0;
10886
William Lallemand96a77852014-02-05 00:30:02 +010010887 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010888
William Lallemand96a77852014-02-05 00:30:02 +010010889 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10890 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010891
William Lallemand96a77852014-02-05 00:30:02 +010010892 temp = get_trash_chunk();
10893 temp->str = txn->uri;
10894 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010895 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010896 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010897 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010898
10899 return 1;
10900
10901}
10902
10903/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10904static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010905smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010906{
10907 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010908 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010010909 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010910
Willy Tarreau15e91e12015-04-04 00:52:09 +020010911 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010912 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010913
William Lallemand65ad6e12014-01-31 15:08:02 +010010914 ptr = txn->uri;
10915
10916 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10917 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010918
William Lallemand65ad6e12014-01-31 15:08:02 +010010919 if (!*ptr)
10920 return 0;
10921
10922 ptr++; /* skip the space */
10923
10924 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010925 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010926 if (!ptr)
10927 return 0;
10928 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10929 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010930
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010931 smp->data.u.str = *temp;
10932 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010933 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010934 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010935
10936 return 1;
10937}
10938
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010939/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10940 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10941 */
10942static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010943smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010944{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010945 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010946
Willy Tarreau15e91e12015-04-04 00:52:09 +020010947 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010948 return 0;
10949
10950 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010951 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010952 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010953 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010954
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010955 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010956 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010957 smp->flags = SMP_F_CONST;
10958 return 1;
10959
10960}
10961
10962/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10963 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10964 */
10965static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010966smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010967{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010968 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010969
Willy Tarreau15e91e12015-04-04 00:52:09 +020010970 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010971 return 0;
10972
10973 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010974 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010975 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010976 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010977
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010978 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010979 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010980 smp->flags = SMP_F_CONST;
10981 return 1;
10982
10983}
10984
William Lallemand65ad6e12014-01-31 15:08:02 +010010985
Willy Tarreaue333ec92012-04-16 16:26:40 +020010986/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010987 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010988 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010989 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010990 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010991 * Accepts exactly 1 argument of type string. If the input options indicate
10992 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010993 * The returned sample is of type CSTR. Can be used to parse cookies in other
10994 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010995 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010996int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010997{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010998 struct http_txn *txn;
10999 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011000 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011001 const struct http_msg *msg;
11002 const char *hdr_name;
11003 int hdr_name_len;
11004 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011005 int occ = 0;
11006 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011007
Willy Tarreau24e32d82012-04-23 23:55:44 +020011008 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011009 return 0;
11010
Willy Tarreaua890d072013-04-02 12:01:06 +020011011 if (!ctx) {
11012 /* first call */
11013 ctx = &static_hdr_ctx;
11014 ctx->idx = 0;
11015 smp->ctx.a[2] = ctx;
11016 }
11017
Willy Tarreaue333ec92012-04-16 16:26:40 +020011018 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011019
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011020 txn = smp->strm->txn;
11021 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011022
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011023 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011024 msg = &txn->req;
11025 hdr_name = "Cookie";
11026 hdr_name_len = 6;
11027 } else {
11028 msg = &txn->rsp;
11029 hdr_name = "Set-Cookie";
11030 hdr_name_len = 10;
11031 }
11032
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011033 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011034 /* no explicit occurrence and single fetch => last cookie by default */
11035 occ = -1;
11036
11037 /* OK so basically here, either we want only one value and it's the
11038 * last one, or we want to iterate over all of them and we fetch the
11039 * next one.
11040 */
11041
Willy Tarreau9b28e032012-10-12 23:49:43 +020011042 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011043 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011044 /* search for the header from the beginning, we must first initialize
11045 * the search parameters.
11046 */
Willy Tarreau37406352012-04-23 16:16:37 +020011047 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011048 ctx->idx = 0;
11049 }
11050
Willy Tarreau28376d62012-04-26 21:26:10 +020011051 smp->flags |= SMP_F_VOL_HDR;
11052
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011053 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011054 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11055 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011056 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11057 goto out;
11058
Willy Tarreau24e32d82012-04-23 23:55:44 +020011059 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011060 continue;
11061
Willy Tarreau37406352012-04-23 16:16:37 +020011062 smp->ctx.a[0] = ctx->line + ctx->val;
11063 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011064 }
11065
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011066 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011067 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011068 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011069 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011070 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011071 &smp->data.u.str.str,
11072 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011073 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011074 found = 1;
11075 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011076 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011077 smp->flags |= SMP_F_NOT_LAST;
11078 return 1;
11079 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011080 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011081 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011082 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011083 /* all cookie headers and values were scanned. If we're looking for the
11084 * last occurrence, we may return it now.
11085 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011086 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011087 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011088 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011089}
11090
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011091/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011092 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011093 * multiple cookies may be parsed on the same line. The returned sample is of
11094 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011095 */
11096static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011097smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011098{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011099 struct http_txn *txn;
11100 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011101 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011102 const struct http_msg *msg;
11103 const char *hdr_name;
11104 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011105 int cnt;
11106 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011107 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011108
Willy Tarreau24e32d82012-04-23 23:55:44 +020011109 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011110 return 0;
11111
Willy Tarreaue333ec92012-04-16 16:26:40 +020011112 CHECK_HTTP_MESSAGE_FIRST();
11113
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011114 txn = smp->strm->txn;
11115 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011116
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011117 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011118 msg = &txn->req;
11119 hdr_name = "Cookie";
11120 hdr_name_len = 6;
11121 } else {
11122 msg = &txn->rsp;
11123 hdr_name = "Set-Cookie";
11124 hdr_name_len = 10;
11125 }
11126
Willy Tarreau9b28e032012-10-12 23:49:43 +020011127 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011128 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011129 ctx.idx = 0;
11130 cnt = 0;
11131
11132 while (1) {
11133 /* Note: val_beg == NULL every time we need to fetch a new header */
11134 if (!val_beg) {
11135 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11136 break;
11137
Willy Tarreau24e32d82012-04-23 23:55:44 +020011138 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011139 continue;
11140
11141 val_beg = ctx.line + ctx.val;
11142 val_end = val_beg + ctx.vlen;
11143 }
11144
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011145 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011146 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011147 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011148 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011149 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011150 &smp->data.u.str.str,
11151 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011152 cnt++;
11153 }
11154 }
11155
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011156 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011157 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011158 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011159 return 1;
11160}
11161
Willy Tarreau51539362012-05-08 12:46:28 +020011162/* Fetch an cookie's integer value. The integer value is returned. It
11163 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11164 */
11165static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011166smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011167{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011168 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011169
11170 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011171 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011172 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011173 }
11174
11175 return ret;
11176}
11177
Willy Tarreau8797c062007-05-07 00:55:35 +020011178/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011179/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011180/************************************************************************/
11181
David Cournapeau16023ee2010-12-23 20:55:41 +090011182/*
11183 * Given a path string and its length, find the position of beginning of the
11184 * query string. Returns NULL if no query string is found in the path.
11185 *
11186 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11187 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011188 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011189 */
bedis4c75cca2012-10-05 08:38:24 +020011190static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011191{
11192 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011193
bedis4c75cca2012-10-05 08:38:24 +020011194 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011195 return p ? p + 1 : NULL;
11196}
11197
bedis4c75cca2012-10-05 08:38:24 +020011198static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011199{
bedis4c75cca2012-10-05 08:38:24 +020011200 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011201}
11202
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011203/* after increasing a pointer value, it can exceed the first buffer
11204 * size. This function transform the value of <ptr> according with
11205 * the expected position. <chunks> is an array of the one or two
11206 * avalaible chunks. The first value is the start of the first chunk,
11207 * the second value if the end+1 of the first chunks. The third value
11208 * is NULL or the start of the second chunk and the fourth value is
11209 * the end+1 of the second chunk. The function returns 1 if does a
11210 * wrap, else returns 0.
11211 */
11212static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11213{
11214 if (*ptr < chunks[1])
11215 return 0;
11216 if (!chunks[2])
11217 return 0;
11218 *ptr = chunks[2] + ( *ptr - chunks[1] );
11219 return 1;
11220}
11221
David Cournapeau16023ee2010-12-23 20:55:41 +090011222/*
11223 * Given a url parameter, find the starting position of the first occurence,
11224 * or NULL if the parameter is not found.
11225 *
11226 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11227 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011228 *
Willy Tarreauf6625822015-12-27 14:51:01 +010011229 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011230 * or the second chunk. The caller must be check the position before using the
11231 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011232 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011233static const char *
11234find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011235 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011236 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011237{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011238 const char *pos, *last, *equal;
11239 const char **bufs = chunks;
11240 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011241
David Cournapeau16023ee2010-12-23 20:55:41 +090011242
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011243 pos = bufs[0];
11244 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010011245 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011246 /* Check the equal. */
11247 equal = pos + url_param_name_l;
11248 if (fix_pointer_if_wrap(chunks, &equal)) {
11249 if (equal >= chunks[3])
11250 return NULL;
11251 } else {
11252 if (equal >= chunks[1])
11253 return NULL;
11254 }
11255 if (*equal == '=') {
11256 if (pos + url_param_name_l > last) {
11257 /* process wrap case, we detect a wrap. In this case, the
11258 * comparison is performed in two parts.
11259 */
11260
11261 /* This is the end, we dont have any other chunk. */
11262 if (bufs != chunks || !bufs[2])
11263 return NULL;
11264
11265 /* Compute the length of each part of the comparison. */
11266 l1 = last - pos;
11267 l2 = url_param_name_l - l1;
11268
11269 /* The second buffer is too short to contain the compared string. */
11270 if (bufs[2] + l2 > bufs[3])
11271 return NULL;
11272
11273 if (memcmp(pos, url_param_name, l1) == 0 &&
11274 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11275 return pos;
11276
11277 /* Perform wrapping and jump the string who fail the comparison. */
11278 bufs += 2;
11279 pos = bufs[0] + l2;
11280 last = bufs[1];
11281
11282 } else {
11283 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011284 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11285 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011286 pos += url_param_name_l + 1;
11287 if (fix_pointer_if_wrap(chunks, &pos))
11288 last = bufs[2];
11289 }
11290 }
11291
11292 while (1) {
11293 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011294 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011295 pos++;
11296 if (pos < last)
11297 break;
11298 /* process buffer wrapping. */
11299 if (bufs != chunks || !bufs[2])
11300 return NULL;
11301 bufs += 2;
11302 pos = bufs[0];
11303 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011304 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011305 pos++;
11306 }
11307 return NULL;
11308}
11309
11310/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011311 * Given a url parameter name and a query string, find the next value.
11312 * An empty url_param_name matches the first available parameter.
11313 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11314 * respectively provide a pointer to the value and its end.
11315 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011316 */
11317static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011318find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011319 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011320 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011321{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011322 const char *arg_start, *qs_end;
11323 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011324
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011325 arg_start = chunks[0];
11326 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011327 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011328 /* Looks for an argument name. */
11329 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011330 url_param_name, url_param_name_l,
11331 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011332 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011333 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011334 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011335 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011336 if (!arg_start)
11337 return 0;
11338
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011339 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011340 while (1) {
11341 /* looks for the first argument. */
11342 value_start = memchr(arg_start, '=', qs_end - arg_start);
11343 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011344 /* Check for wrapping. */
11345 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011346 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011347 chunks[2]) {
11348 arg_start = chunks[2];
11349 qs_end = chunks[3];
11350 continue;
11351 }
11352 return 0;
11353 }
11354 break;
11355 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011356 value_start++;
11357 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011358 else {
11359 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011360 value_start = arg_start + url_param_name_l + 1;
11361
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011362 /* Check for pointer wrapping. */
11363 if (fix_pointer_if_wrap(chunks, &value_start)) {
11364 /* Update the end pointer. */
11365 qs_end = chunks[3];
11366
11367 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011368 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011369 return 0;
11370 }
11371 }
11372
David Cournapeau16023ee2010-12-23 20:55:41 +090011373 value_end = value_start;
11374
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011375 while (1) {
11376 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11377 value_end++;
11378 if (value_end < qs_end)
11379 break;
11380 /* process buffer wrapping. */
11381 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011382 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011383 chunks[2]) {
11384 value_end = chunks[2];
11385 qs_end = chunks[3];
11386 continue;
11387 }
11388 break;
11389 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011390
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011391 *vstart = value_start;
11392 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011393 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011394}
11395
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011396/* This scans a URL-encoded query string. It takes an optionally wrapping
11397 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11398 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11399 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011400 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011401static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011402smp_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 +090011403{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011404 const char *vstart, *vend;
11405 struct chunk *temp;
11406 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011407
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011408 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011409 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011410 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011411 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011412 return 0;
11413
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011414 /* Create sample. If the value is contiguous, return the pointer as CONST,
11415 * if the value is wrapped, copy-it in a buffer.
11416 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011417 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011418 if (chunks[2] &&
11419 vstart >= chunks[0] && vstart <= chunks[1] &&
11420 vend >= chunks[2] && vend <= chunks[3]) {
11421 /* Wrapped case. */
11422 temp = get_trash_chunk();
11423 memcpy(temp->str, vstart, chunks[1] - vstart);
11424 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011425 smp->data.u.str.str = temp->str;
11426 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011427 } else {
11428 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011429 smp->data.u.str.str = (char *)vstart;
11430 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011431 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11432 }
11433
11434 /* Update context, check wrapping. */
11435 chunks[0] = vend;
11436 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11437 chunks[1] = chunks[3];
11438 chunks[2] = NULL;
11439 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011440
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011441 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011442 smp->flags |= SMP_F_NOT_LAST;
11443
David Cournapeau16023ee2010-12-23 20:55:41 +090011444 return 1;
11445}
11446
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011447/* This function iterates over each parameter of the query string. It uses
11448 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011449 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11450 * An optional parameter name is passed in args[0], otherwise any parameter is
11451 * considered. It supports an optional delimiter argument for the beginning of
11452 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011453 */
11454static int
11455smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11456{
11457 struct http_msg *msg;
11458 char delim = '?';
11459 const char *name;
11460 int name_len;
11461
Dragan Dosen26f77e52015-05-25 10:02:11 +020011462 if (!args ||
11463 (args[0].type && args[0].type != ARGT_STR) ||
11464 (args[1].type && args[1].type != ARGT_STR))
11465 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011466
Dragan Dosen26f77e52015-05-25 10:02:11 +020011467 name = "";
11468 name_len = 0;
11469 if (args->type == ARGT_STR) {
11470 name = args->data.str.str;
11471 name_len = args->data.str.len;
11472 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011473
Dragan Dosen26f77e52015-05-25 10:02:11 +020011474 if (args[1].type)
11475 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011476
Dragan Dosen26f77e52015-05-25 10:02:11 +020011477 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011478 CHECK_HTTP_MESSAGE_FIRST();
11479
11480 msg = &smp->strm->txn->req;
11481
11482 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11483 msg->sl.rq.u_l, delim);
11484 if (!smp->ctx.a[0])
11485 return 0;
11486
11487 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011488
11489 /* Assume that the context is filled with NULL pointer
11490 * before the first call.
11491 * smp->ctx.a[2] = NULL;
11492 * smp->ctx.a[3] = NULL;
11493 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011494 }
11495
11496 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11497}
11498
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011499/* This function iterates over each parameter of the body. This requires
11500 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011501 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11502 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11503 * optional second part if the body wraps at the end of the buffer. An optional
11504 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011505 */
11506static int
11507smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11508{
11509 struct http_txn *txn = smp->strm->txn;
11510 struct http_msg *msg;
11511 unsigned long len;
11512 unsigned long block1;
11513 char *body;
11514 const char *name;
11515 int name_len;
11516
11517 if (!args || (args[0].type && args[0].type != ARGT_STR))
11518 return 0;
11519
11520 name = "";
11521 name_len = 0;
11522 if (args[0].type == ARGT_STR) {
11523 name = args[0].data.str.str;
11524 name_len = args[0].data.str.len;
11525 }
11526
11527 if (!smp->ctx.a[0]) { // first call, find the query string
11528 CHECK_HTTP_MESSAGE_FIRST();
11529
11530 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
11531 msg = &txn->req;
11532 else
11533 msg = &txn->rsp;
11534
11535 len = http_body_bytes(msg);
11536 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11537
11538 block1 = len;
11539 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11540 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11541
11542 if (block1 == len) {
11543 /* buffer is not wrapped (or empty) */
11544 smp->ctx.a[0] = body;
11545 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011546
11547 /* Assume that the context is filled with NULL pointer
11548 * before the first call.
11549 * smp->ctx.a[2] = NULL;
11550 * smp->ctx.a[3] = NULL;
11551 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011552 }
11553 else {
11554 /* buffer is wrapped, we need to defragment it */
11555 smp->ctx.a[0] = body;
11556 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011557 smp->ctx.a[2] = msg->chn->buf->data;
11558 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011559 }
11560 }
11561 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11562}
11563
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011564/* Return the signed integer value for the specified url parameter (see url_param
11565 * above).
11566 */
11567static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011568smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011569{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011570 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011571
11572 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011573 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011574 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011575 }
11576
11577 return ret;
11578}
11579
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011580/* This produces a 32-bit hash of the concatenation of the first occurrence of
11581 * the Host header followed by the path component if it begins with a slash ('/').
11582 * This means that '*' will not be added, resulting in exactly the first Host
11583 * entry. If no Host header is found, then the path is used. The resulting value
11584 * is hashed using the url hash followed by a full avalanche hash and provides a
11585 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11586 * high-traffic sites without having to store whole paths.
11587 * this differs from the base32 functions in that it includes the url parameters
11588 * as well as the path
11589 */
11590static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011591smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011592{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011593 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011594 struct hdr_ctx ctx;
11595 unsigned int hash = 0;
11596 char *ptr, *beg, *end;
11597 int len;
11598
11599 CHECK_HTTP_MESSAGE_FIRST();
11600
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011601 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011602 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011603 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011604 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11605 ptr = ctx.line + ctx.val;
11606 len = ctx.vlen;
11607 while (len--)
11608 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11609 }
11610
11611 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011612 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 +000011613 beg = http_get_path(txn);
11614 if (!beg)
11615 beg = end;
11616
11617 for (ptr = beg; ptr < end ; ptr++);
11618
11619 if (beg < ptr && *beg == '/') {
11620 while (beg < ptr)
11621 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11622 }
11623 hash = full_hash(hash);
11624
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011625 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011626 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011627 smp->flags = SMP_F_VOL_1ST;
11628 return 1;
11629}
11630
11631/* This concatenates the source address with the 32-bit hash of the Host and
11632 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11633 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11634 * on the source address length. The URL hash is stored before the address so
11635 * that in environments where IPv6 is insignificant, truncating the output to
11636 * 8 bytes would still work.
11637 */
11638static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011639smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011640{
11641 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011642 struct connection *cli_conn = objt_conn(smp->sess->origin);
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011643 unsigned int hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011644
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011645 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011646 return 0;
11647
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011648 /* The returned hash is a 32 bytes integer. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011649 hash = smp->data.u.sint;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011650
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011651 temp = get_trash_chunk();
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011652 memcpy(temp->str + temp->len, &hash, sizeof(hash));
11653 temp->len += sizeof(hash);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011654
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011655 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011656 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011657 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011658 temp->len += 4;
11659 break;
11660 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011661 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011662 temp->len += 16;
11663 break;
11664 default:
11665 return 0;
11666 }
11667
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011668 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011669 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011670 return 1;
11671}
11672
Willy Tarreau185b5c42012-04-26 15:11:51 +020011673/* This function is used to validate the arguments passed to any "hdr" fetch
11674 * keyword. These keywords support an optional positive or negative occurrence
11675 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11676 * is assumed that the types are already the correct ones. Returns 0 on error,
11677 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11678 * error message in case of error, that the caller is responsible for freeing.
11679 * The initial location must either be freeable or NULL.
11680 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011681int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011682{
11683 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011684 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011685 return 0;
11686 }
11687 return 1;
11688}
11689
Willy Tarreau276fae92013-07-25 14:36:01 +020011690/* takes an UINT value on input supposed to represent the time since EPOCH,
11691 * adds an optional offset found in args[0] and emits a string representing
11692 * the date in RFC-1123/5322 format.
11693 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011694static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011695{
Cyril Bontéf78d8962016-01-22 19:40:28 +010011696 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020011697 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11698 struct chunk *temp;
11699 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011700 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011701 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011702
11703 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011704 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011705 curr_date += args[0].data.sint;
11706
11707 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011708 if (!tm)
11709 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011710
11711 temp = get_trash_chunk();
11712 temp->len = snprintf(temp->str, temp->size - temp->len,
11713 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11714 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11715 tm->tm_hour, tm->tm_min, tm->tm_sec);
11716
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011717 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011718 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011719 return 1;
11720}
11721
Thierry FOURNIERad903512014-04-11 17:51:01 +020011722/* Match language range with language tag. RFC2616 14.4:
11723 *
11724 * A language-range matches a language-tag if it exactly equals
11725 * the tag, or if it exactly equals a prefix of the tag such
11726 * that the first tag character following the prefix is "-".
11727 *
11728 * Return 1 if the strings match, else return 0.
11729 */
11730static inline int language_range_match(const char *range, int range_len,
11731 const char *tag, int tag_len)
11732{
11733 const char *end = range + range_len;
11734 const char *tend = tag + tag_len;
11735 while (range < end) {
11736 if (*range == '-' && tag == tend)
11737 return 1;
11738 if (*range != *tag || tag == tend)
11739 return 0;
11740 range++;
11741 tag++;
11742 }
11743 /* Return true only if the last char of the tag is matched. */
11744 return tag == tend;
11745}
11746
11747/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011748static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011749{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011750 const char *al = smp->data.u.str.str;
11751 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011752 const char *token;
11753 int toklen;
11754 int qvalue;
11755 const char *str;
11756 const char *w;
11757 int best_q = 0;
11758
11759 /* Set the constant to the sample, because the output of the
11760 * function will be peek in the constant configuration string.
11761 */
11762 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011763 smp->data.u.str.size = 0;
11764 smp->data.u.str.str = "";
11765 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011766
11767 /* Parse the accept language */
11768 while (1) {
11769
11770 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011771 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011772 al++;
11773 if (al >= end)
11774 break;
11775
11776 /* Start of the fisrt word. */
11777 token = al;
11778
11779 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011780 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011781 al++;
11782 if (al == token)
11783 goto expect_comma;
11784
11785 /* Length of the token. */
11786 toklen = al - token;
11787 qvalue = 1000;
11788
11789 /* Check if the token exists in the list. If the token not exists,
11790 * jump to the next token.
11791 */
11792 str = args[0].data.str.str;
11793 w = str;
11794 while (1) {
11795 if (*str == ';' || *str == '\0') {
11796 if (language_range_match(token, toklen, w, str-w))
11797 goto look_for_q;
11798 if (*str == '\0')
11799 goto expect_comma;
11800 w = str + 1;
11801 }
11802 str++;
11803 }
11804 goto expect_comma;
11805
11806look_for_q:
11807
11808 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011809 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011810 al++;
11811 if (al >= end)
11812 goto process_value;
11813
11814 /* If ',' is found, process the result */
11815 if (*al == ',')
11816 goto process_value;
11817
11818 /* If the character is different from ';', look
11819 * for the end of the header part in best effort.
11820 */
11821 if (*al != ';')
11822 goto expect_comma;
11823
11824 /* Assumes that the char is ';', now expect "q=". */
11825 al++;
11826
11827 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011828 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011829 al++;
11830 if (al >= end)
11831 goto process_value;
11832
11833 /* Expect 'q'. If no 'q', continue in best effort */
11834 if (*al != 'q')
11835 goto process_value;
11836 al++;
11837
11838 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011839 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011840 al++;
11841 if (al >= end)
11842 goto process_value;
11843
11844 /* Expect '='. If no '=', continue in best effort */
11845 if (*al != '=')
11846 goto process_value;
11847 al++;
11848
11849 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011850 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011851 al++;
11852 if (al >= end)
11853 goto process_value;
11854
11855 /* Parse the q value. */
11856 qvalue = parse_qvalue(al, &al);
11857
11858process_value:
11859
11860 /* If the new q value is the best q value, then store the associated
11861 * language in the response. If qvalue is the biggest value (1000),
11862 * break the process.
11863 */
11864 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011865 smp->data.u.str.str = (char *)w;
11866 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011867 if (qvalue >= 1000)
11868 break;
11869 best_q = qvalue;
11870 }
11871
11872expect_comma:
11873
11874 /* Expect comma or end. If the end is detected, quit the loop. */
11875 while (al < end && *al != ',')
11876 al++;
11877 if (al >= end)
11878 break;
11879
11880 /* Comma is found, jump it and restart the analyzer. */
11881 al++;
11882 }
11883
11884 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011885 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
11886 smp->data.u.str.str = args[1].data.str.str;
11887 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011888 }
11889
11890 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011891 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011892}
11893
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011894/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011895static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011896{
11897 /* If the constant flag is set or if not size is avalaible at
11898 * the end of the buffer, copy the string in other buffer
11899 * before decoding.
11900 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011901 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011902 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011903 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
11904 smp->data.u.str.str = str->str;
11905 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011906 smp->flags &= ~SMP_F_CONST;
11907 }
11908
11909 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011910 smp->data.u.str.str[smp->data.u.str.len] = '\0';
11911 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011912 return 1;
11913}
11914
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011915static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
11916{
11917 struct proxy *fe = strm_fe(smp->strm);
11918 int idx, i;
11919 struct cap_hdr *hdr;
11920 int len;
11921
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011922 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011923 return 0;
11924
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011925 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011926
11927 /* Check the availibity of the capture id. */
11928 if (idx > fe->nb_req_cap - 1)
11929 return 0;
11930
11931 /* Look for the original configuration. */
11932 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
11933 hdr != NULL && i != idx ;
11934 i--, hdr = hdr->next);
11935 if (!hdr)
11936 return 0;
11937
11938 /* check for the memory allocation */
11939 if (smp->strm->req_cap[hdr->index] == NULL)
11940 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
11941 if (smp->strm->req_cap[hdr->index] == NULL)
11942 return 0;
11943
11944 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011945 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011946 if (len > hdr->len)
11947 len = hdr->len;
11948
11949 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011950 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011951 smp->strm->req_cap[idx][len] = '\0';
11952
11953 return 1;
11954}
11955
11956static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
11957{
11958 struct proxy *fe = strm_fe(smp->strm);
11959 int idx, i;
11960 struct cap_hdr *hdr;
11961 int len;
11962
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011963 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011964 return 0;
11965
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011966 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011967
11968 /* Check the availibity of the capture id. */
11969 if (idx > fe->nb_rsp_cap - 1)
11970 return 0;
11971
11972 /* Look for the original configuration. */
11973 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
11974 hdr != NULL && i != idx ;
11975 i--, hdr = hdr->next);
11976 if (!hdr)
11977 return 0;
11978
11979 /* check for the memory allocation */
11980 if (smp->strm->res_cap[hdr->index] == NULL)
11981 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
11982 if (smp->strm->res_cap[hdr->index] == NULL)
11983 return 0;
11984
11985 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011986 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011987 if (len > hdr->len)
11988 len = hdr->len;
11989
11990 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011991 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011992 smp->strm->res_cap[idx][len] = '\0';
11993
11994 return 1;
11995}
11996
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011997/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011998 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011999 * the relevant part of the request line accordingly. Then it updates various
12000 * pointers to the next elements which were moved, and the total buffer length.
12001 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012002 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12003 * error, though this can be revisited when this code is finally exploited.
12004 *
12005 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12006 * query string and 3 to replace uri.
12007 *
12008 * In query string case, the mark question '?' must be set at the start of the
12009 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012010 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012011int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012012 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012013{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012014 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012015 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012016 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012017 int delta;
12018
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012019 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012020 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012021 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012022 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12023
12024 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012025 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012026 txn->req.sl.rq.m_l += delta;
12027 txn->req.sl.rq.u += delta;
12028 txn->req.sl.rq.v += delta;
12029 break;
12030
12031 case 1: // path
12032 cur_ptr = http_get_path(txn);
12033 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012034 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012035
12036 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012037 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 +010012038 cur_end++;
12039
12040 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012041 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012042 txn->req.sl.rq.u_l += delta;
12043 txn->req.sl.rq.v += delta;
12044 break;
12045
12046 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012047 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012048 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012049 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12050 while (cur_ptr < cur_end && *cur_ptr != '?')
12051 cur_ptr++;
12052
12053 /* skip the question mark or indicate that we must insert it
12054 * (but only if the format string is not empty then).
12055 */
12056 if (cur_ptr < cur_end)
12057 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012058 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012059 offset = 0;
12060
12061 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012062 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012063 txn->req.sl.rq.u_l += delta;
12064 txn->req.sl.rq.v += delta;
12065 break;
12066
12067 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012068 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012069 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12070
12071 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012072 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012073 txn->req.sl.rq.u_l += delta;
12074 txn->req.sl.rq.v += delta;
12075 break;
12076
12077 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012078 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012079 }
12080
12081 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012082 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012083 txn->req.sl.rq.l += delta;
12084 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012085 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012086 return 0;
12087}
12088
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012089/* This function replace the HTTP status code and the associated message. The
12090 * variable <status> contains the new status code. This function never fails.
12091 */
12092void http_set_status(unsigned int status, struct stream *s)
12093{
12094 struct http_txn *txn = s->txn;
12095 char *cur_ptr, *cur_end;
12096 int delta;
12097 char *res;
12098 int c_l;
12099 const char *msg;
12100 int msg_len;
12101
12102 chunk_reset(&trash);
12103
12104 res = ultoa_o(status, trash.str, trash.size);
12105 c_l = res - trash.str;
12106
12107 trash.str[c_l] = ' ';
12108 trash.len = c_l + 1;
12109
12110 msg = get_reason(status);
12111 msg_len = strlen(msg);
12112
12113 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
12114 trash.len += msg_len;
12115
12116 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
12117 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
12118
12119 /* commit changes and adjust message */
12120 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
12121
12122 /* adjust res line offsets and lengths */
12123 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
12124 txn->rsp.sl.st.c_l = c_l;
12125 txn->rsp.sl.st.r_l = msg_len;
12126
12127 delta = trash.len - (cur_end - cur_ptr);
12128 txn->rsp.sl.st.l += delta;
12129 txn->hdr_idx.v[0].len += delta;
12130 http_msg_move_end(&txn->rsp, delta);
12131}
12132
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012133/* This function executes one of the set-{method,path,query,uri} actions. It
12134 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012135 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012136 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012137 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12138 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012139 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012140enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012141 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012142{
12143 chunk_reset(&trash);
12144
12145 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012146 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012147 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012148 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012149
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012150 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012151 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012152}
12153
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012154/* This function is just a compliant action wrapper for "set-status". */
12155enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012156 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012157{
12158 http_set_status(rule->arg.status.code, s);
12159 return ACT_RET_CONT;
12160}
12161
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012162/* parse an http-request action among :
12163 * set-method
12164 * set-path
12165 * set-query
12166 * set-uri
12167 *
12168 * All of them accept a single argument of type string representing a log-format.
12169 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12170 * 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 +020012171 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012172 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012173enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12174 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012175{
12176 int cur_arg = *orig_arg;
12177
Thierry FOURNIER42148732015-09-02 17:17:33 +020012178 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012179
12180 switch (args[0][4]) {
12181 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012182 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012183 rule->action_ptr = http_action_set_req_line;
12184 break;
12185 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012186 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012187 rule->action_ptr = http_action_set_req_line;
12188 break;
12189 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012190 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012191 rule->action_ptr = http_action_set_req_line;
12192 break;
12193 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012194 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012195 rule->action_ptr = http_action_set_req_line;
12196 break;
12197 default:
12198 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012199 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012200 }
12201
12202 if (!*args[cur_arg] ||
12203 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12204 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012205 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012206 }
12207
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012208 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012209 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012210 parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012211 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
12212 proxy->conf.args.file, proxy->conf.args.line);
12213
12214 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012215 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012216}
12217
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012218/* parse set-status action:
12219 * This action accepts a single argument of type int representing
12220 * an http status code. It returns ACT_RET_PRS_OK on success,
12221 * ACT_RET_PRS_ERR on error.
12222 */
12223enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12224 struct act_rule *rule, char **err)
12225{
12226 char *error;
12227
Thierry FOURNIER42148732015-09-02 17:17:33 +020012228 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012229 rule->action_ptr = action_http_set_status;
12230
12231 /* Check if an argument is available */
12232 if (!*args[*orig_arg]) {
12233 memprintf(err, "expects exactly 1 argument <status>");
12234 return ACT_RET_PRS_ERR;
12235 }
12236
12237 /* convert status code as integer */
12238 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12239 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12240 memprintf(err, "expects an integer status code between 100 and 999");
12241 return ACT_RET_PRS_ERR;
12242 }
12243
12244 (*orig_arg)++;
12245 return ACT_RET_PRS_OK;
12246}
12247
Willy Tarreaua9083d02015-05-08 15:27:59 +020012248/* This function executes the "capture" action. It executes a fetch expression,
12249 * turns the result into a string and puts it in a capture slot. It always
12250 * returns 1. If an error occurs the action is cancelled, but the rule
12251 * processing continues.
12252 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012253enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012254 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012255{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012256 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012257 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012258 char **cap = s->req_cap;
12259 int len;
12260
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012261 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 +020012262 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012263 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012264
12265 if (cap[h->index] == NULL)
12266 cap[h->index] = pool_alloc2(h->pool);
12267
12268 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012269 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012270
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012271 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012272 if (len > h->len)
12273 len = h->len;
12274
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012275 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012276 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012277 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012278}
12279
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012280/* This function executes the "capture" action and store the result in a
12281 * capture slot if exists. It executes a fetch expression, turns the result
12282 * into a string and puts it in a capture slot. It always returns 1. If an
12283 * error occurs the action is cancelled, but the rule processing continues.
12284 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012285enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012286 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012287{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012288 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012289 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012290 char **cap = s->req_cap;
12291 struct proxy *fe = strm_fe(s);
12292 int len;
12293 int i;
12294
12295 /* Look for the original configuration. */
12296 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012297 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012298 i--, h = h->next);
12299 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012300 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012301
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012302 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 +020012303 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012304 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012305
12306 if (cap[h->index] == NULL)
12307 cap[h->index] = pool_alloc2(h->pool);
12308
12309 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012310 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012311
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012312 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012313 if (len > h->len)
12314 len = h->len;
12315
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012316 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012317 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012318 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012319}
12320
Willy Tarreaua9083d02015-05-08 15:27:59 +020012321/* parse an "http-request capture" action. It takes a single argument which is
12322 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012323 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012324 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012325 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012326enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12327 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012328{
12329 struct sample_expr *expr;
12330 struct cap_hdr *hdr;
12331 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012332 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012333
12334 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12335 if (strcmp(args[cur_arg], "if") == 0 ||
12336 strcmp(args[cur_arg], "unless") == 0)
12337 break;
12338
12339 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012340 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012341 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012342 }
12343
Willy Tarreaua9083d02015-05-08 15:27:59 +020012344 cur_arg = *orig_arg;
12345 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12346 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012347 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012348
12349 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12350 memprintf(err,
12351 "fetch method '%s' extracts information from '%s', none of which is available here",
12352 args[cur_arg-1], sample_src_names(expr->fetch->use));
12353 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012354 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012355 }
12356
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012357 if (!args[cur_arg] || !*args[cur_arg]) {
12358 memprintf(err, "expects 'len or 'id'");
12359 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012360 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012361 }
12362
Willy Tarreaua9083d02015-05-08 15:27:59 +020012363 if (strcmp(args[cur_arg], "len") == 0) {
12364 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012365
12366 if (!(px->cap & PR_CAP_FE)) {
12367 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012368 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012369 }
12370
12371 proxy->conf.args.ctx = ARGC_CAP;
12372
Willy Tarreaua9083d02015-05-08 15:27:59 +020012373 if (!args[cur_arg]) {
12374 memprintf(err, "missing length value");
12375 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012376 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012377 }
12378 /* we copy the table name for now, it will be resolved later */
12379 len = atoi(args[cur_arg]);
12380 if (len <= 0) {
12381 memprintf(err, "length must be > 0");
12382 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012383 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012384 }
12385 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012386
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012387 if (!len) {
12388 memprintf(err, "a positive 'len' argument is mandatory");
12389 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012390 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012391 }
12392
12393 hdr = calloc(sizeof(struct cap_hdr), 1);
12394 hdr->next = px->req_cap;
12395 hdr->name = NULL; /* not a header capture */
12396 hdr->namelen = 0;
12397 hdr->len = len;
12398 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12399 hdr->index = px->nb_req_cap++;
12400
12401 px->req_cap = hdr;
12402 px->to_log |= LW_REQHDR;
12403
Thierry FOURNIER42148732015-09-02 17:17:33 +020012404 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012405 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012406 rule->arg.cap.expr = expr;
12407 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012408 }
12409
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012410 else if (strcmp(args[cur_arg], "id") == 0) {
12411 int id;
12412 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012413
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012414 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012415
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012416 if (!args[cur_arg]) {
12417 memprintf(err, "missing id value");
12418 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012419 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012420 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012421
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012422 id = strtol(args[cur_arg], &error, 10);
12423 if (*error != '\0') {
12424 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12425 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012426 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012427 }
12428 cur_arg++;
12429
12430 proxy->conf.args.ctx = ARGC_CAP;
12431
Thierry FOURNIER42148732015-09-02 17:17:33 +020012432 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012433 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012434 rule->arg.capid.expr = expr;
12435 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012436 }
12437
12438 else {
12439 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12440 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012441 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012442 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012443
12444 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012445 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012446}
12447
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012448/* This function executes the "capture" action and store the result in a
12449 * capture slot if exists. It executes a fetch expression, turns the result
12450 * into a string and puts it in a capture slot. It always returns 1. If an
12451 * error occurs the action is cancelled, but the rule processing continues.
12452 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012453enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012454 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012455{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012456 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012457 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012458 char **cap = s->res_cap;
12459 struct proxy *fe = strm_fe(s);
12460 int len;
12461 int i;
12462
12463 /* Look for the original configuration. */
12464 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012465 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012466 i--, h = h->next);
12467 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012468 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012469
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012470 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 +020012471 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012472 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012473
12474 if (cap[h->index] == NULL)
12475 cap[h->index] = pool_alloc2(h->pool);
12476
12477 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012478 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012479
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012480 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012481 if (len > h->len)
12482 len = h->len;
12483
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012484 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012485 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012486 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012487}
12488
12489/* parse an "http-response capture" action. It takes a single argument which is
12490 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12491 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012492 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012493 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012494enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12495 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012496{
12497 struct sample_expr *expr;
12498 int cur_arg;
12499 int id;
12500 char *error;
12501
12502 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12503 if (strcmp(args[cur_arg], "if") == 0 ||
12504 strcmp(args[cur_arg], "unless") == 0)
12505 break;
12506
12507 if (cur_arg < *orig_arg + 3) {
12508 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012509 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012510 }
12511
12512 cur_arg = *orig_arg;
12513 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12514 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012515 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012516
12517 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12518 memprintf(err,
12519 "fetch method '%s' extracts information from '%s', none of which is available here",
12520 args[cur_arg-1], sample_src_names(expr->fetch->use));
12521 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012522 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012523 }
12524
12525 if (!args[cur_arg] || !*args[cur_arg]) {
12526 memprintf(err, "expects 'len or 'id'");
12527 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012528 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012529 }
12530
12531 if (strcmp(args[cur_arg], "id") != 0) {
12532 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12533 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012534 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012535 }
12536
12537 cur_arg++;
12538
12539 if (!args[cur_arg]) {
12540 memprintf(err, "missing id value");
12541 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012542 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012543 }
12544
12545 id = strtol(args[cur_arg], &error, 10);
12546 if (*error != '\0') {
12547 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12548 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012549 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012550 }
12551 cur_arg++;
12552
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012553 proxy->conf.args.ctx = ARGC_CAP;
12554
Thierry FOURNIER42148732015-09-02 17:17:33 +020012555 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012556 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012557 rule->arg.capid.expr = expr;
12558 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012559
12560 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012561 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012562}
12563
William Lallemand73025dd2014-04-24 14:38:37 +020012564/*
12565 * Return the struct http_req_action_kw associated to a keyword.
12566 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012567struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012568{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012569 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012570}
12571
12572/*
12573 * Return the struct http_res_action_kw associated to a keyword.
12574 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012575struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012576{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012577 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012578}
12579
Willy Tarreau4a568972010-05-12 08:08:50 +020012580/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012581/* All supported ACL keywords must be declared here. */
12582/************************************************************************/
12583
12584/* Note: must not be declared <const> as its list will be overwritten.
12585 * Please take care of keeping this list alphabetically sorted.
12586 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012587static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012588 { "base", "base", PAT_MATCH_STR },
12589 { "base_beg", "base", PAT_MATCH_BEG },
12590 { "base_dir", "base", PAT_MATCH_DIR },
12591 { "base_dom", "base", PAT_MATCH_DOM },
12592 { "base_end", "base", PAT_MATCH_END },
12593 { "base_len", "base", PAT_MATCH_LEN },
12594 { "base_reg", "base", PAT_MATCH_REG },
12595 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012596
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012597 { "cook", "req.cook", PAT_MATCH_STR },
12598 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12599 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12600 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12601 { "cook_end", "req.cook", PAT_MATCH_END },
12602 { "cook_len", "req.cook", PAT_MATCH_LEN },
12603 { "cook_reg", "req.cook", PAT_MATCH_REG },
12604 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012605
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012606 { "hdr", "req.hdr", PAT_MATCH_STR },
12607 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12608 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12609 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12610 { "hdr_end", "req.hdr", PAT_MATCH_END },
12611 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12612 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12613 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012614
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012615 /* these two declarations uses strings with list storage (in place
12616 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12617 * and delete functions are relative to the list management. The parse
12618 * and match method are related to the corresponding fetch methods. This
12619 * is very particular ACL declaration mode.
12620 */
12621 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12622 { "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 +020012623
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012624 { "path", "path", PAT_MATCH_STR },
12625 { "path_beg", "path", PAT_MATCH_BEG },
12626 { "path_dir", "path", PAT_MATCH_DIR },
12627 { "path_dom", "path", PAT_MATCH_DOM },
12628 { "path_end", "path", PAT_MATCH_END },
12629 { "path_len", "path", PAT_MATCH_LEN },
12630 { "path_reg", "path", PAT_MATCH_REG },
12631 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012632
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012633 { "req_ver", "req.ver", PAT_MATCH_STR },
12634 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012635
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012636 { "scook", "res.cook", PAT_MATCH_STR },
12637 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12638 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12639 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12640 { "scook_end", "res.cook", PAT_MATCH_END },
12641 { "scook_len", "res.cook", PAT_MATCH_LEN },
12642 { "scook_reg", "res.cook", PAT_MATCH_REG },
12643 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012644
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012645 { "shdr", "res.hdr", PAT_MATCH_STR },
12646 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12647 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12648 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12649 { "shdr_end", "res.hdr", PAT_MATCH_END },
12650 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12651 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12652 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012653
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012654 { "url", "url", PAT_MATCH_STR },
12655 { "url_beg", "url", PAT_MATCH_BEG },
12656 { "url_dir", "url", PAT_MATCH_DIR },
12657 { "url_dom", "url", PAT_MATCH_DOM },
12658 { "url_end", "url", PAT_MATCH_END },
12659 { "url_len", "url", PAT_MATCH_LEN },
12660 { "url_reg", "url", PAT_MATCH_REG },
12661 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012662
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012663 { "urlp", "urlp", PAT_MATCH_STR },
12664 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12665 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12666 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12667 { "urlp_end", "urlp", PAT_MATCH_END },
12668 { "urlp_len", "urlp", PAT_MATCH_LEN },
12669 { "urlp_reg", "urlp", PAT_MATCH_REG },
12670 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012671
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012672 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012673}};
12674
12675/************************************************************************/
12676/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012677/************************************************************************/
12678/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012679static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012680 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012681 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012682 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12683
Willy Tarreau87b09662015-04-03 00:22:06 +020012684 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012685 { "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 +020012686
12687 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012688 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12689 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12690 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012691
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012692 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12693 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012694
Willy Tarreau409bcde2013-01-08 00:31:00 +010012695 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12696 * are only here to match the ACL's name, are request-only and are used
12697 * for ACL compatibility only.
12698 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012699 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12700 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012701 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12702 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012703
12704 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12705 * only here to match the ACL's name, are request-only and are used for
12706 * ACL compatibility only.
12707 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012708 { "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 +020012709 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012710 { "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 +020012711 { "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 +010012712
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012713 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012714 { "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 +010012715 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012716 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012717 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012718 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012719
12720 /* HTTP protocol on the request path */
12721 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012722 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012723
12724 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012725 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12726 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012727
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012728 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012729 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12730 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012731 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012732
Willy Tarreau18ed2562013-01-14 15:56:36 +010012733 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012734 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12735 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012736
Willy Tarreau18ed2562013-01-14 15:56:36 +010012737 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012738 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012739 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12740 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012741
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012742 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012743 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012744 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012745 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012746 { "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 +010012747 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012748 { "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 +010012749
12750 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012751 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012752 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12753 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012754
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012755 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012756 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012757 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012758 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012759 { "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 +010012760 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012761 { "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 +010012762
Willy Tarreau409bcde2013-01-08 00:31:00 +010012763 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012764 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012765 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12766 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012767 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012768
12769 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012770 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012771 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012772 { "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 +020012773 { "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 +010012774
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012775 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012776 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012777 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012778 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012779 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012780 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020012781 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12782 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012783 { "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 +010012784 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012785}};
12786
Willy Tarreau8797c062007-05-07 00:55:35 +020012787
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012788/************************************************************************/
12789/* All supported converter keywords must be declared here. */
12790/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012791/* Note: must not be declared <const> as its list will be overwritten */
12792static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012793 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020012794 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012795 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
12796 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012797 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012798 { NULL, NULL, 0, 0, 0 },
12799}};
12800
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012801
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012802/************************************************************************/
12803/* All supported http-request action keywords must be declared here. */
12804/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012805struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012806 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020012807 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012808 { "set-method", parse_set_req_line },
12809 { "set-path", parse_set_req_line },
12810 { "set-query", parse_set_req_line },
12811 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012812 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012813 }
12814};
12815
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012816struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012817 .kw = {
12818 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012819 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012820 { NULL, NULL }
12821 }
12822};
12823
Willy Tarreau8797c062007-05-07 00:55:35 +020012824__attribute__((constructor))
12825static void __http_protocol_init(void)
12826{
12827 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012828 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012829 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012830 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012831 http_res_keywords_register(&http_res_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020012832}
12833
12834
Willy Tarreau58f10d72006-12-04 02:26:12 +010012835/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012836 * Local variables:
12837 * c-indent-level: 8
12838 * c-basic-offset: 8
12839 * End:
12840 */