blob: 74cd260c34afe0b078eaa1aae877acf661631190 [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);
Willy Tarreau5c557d12016-03-13 08:17:02 +01001588 len = p - h;
1589 if (!p || len <= 0)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001590 return 0;
1591
Willy Tarreau5c557d12016-03-13 08:17:02 +01001592 chunk_initlen(&auth_method, h, 0, len);
1593 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001594
1595 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1596
1597 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001598 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001599
1600 if (len < 0)
1601 return 0;
1602
1603
1604 get_http_auth_buff[len] = '\0';
1605
1606 p = strchr(get_http_auth_buff, ':');
1607
1608 if (!p)
1609 return 0;
1610
1611 txn->auth.user = get_http_auth_buff;
1612 *p = '\0';
1613 txn->auth.pass = p+1;
1614
1615 txn->auth.method = HTTP_AUTH_BASIC;
1616 return 1;
1617 }
1618
1619 return 0;
1620}
1621
Willy Tarreau58f10d72006-12-04 02:26:12 +01001622
Willy Tarreau8973c702007-01-21 23:58:29 +01001623/*
1624 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001625 * depending on the initial msg->msg_state. The caller is responsible for
1626 * ensuring that the message does not wrap. The function can be preempted
1627 * everywhere when data are missing and recalled at the exact same location
1628 * with no information loss. The message may even be realigned between two
1629 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001630 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001631 * fields. Note that msg->sol will be initialized after completing the first
1632 * state, so that none of the msg pointers has to be initialized prior to the
1633 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001634 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001635void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001636{
Willy Tarreau3770f232013-12-07 00:01:53 +01001637 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001638 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001639 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001640
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001641 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001642 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001643 ptr = buf->p + msg->next;
1644 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001645
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001646 if (unlikely(ptr >= end))
1647 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001648
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001649 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001650 /*
1651 * First, states that are specific to the response only.
1652 * We check them first so that request and headers are
1653 * closer to each other (accessed more often).
1654 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001655 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001656 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001657 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001658 /* we have a start of message, but we have to check
1659 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001660 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001661 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001662 if (unlikely(ptr != buf->p)) {
1663 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001664 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001665 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001666 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001667 }
Willy Tarreau26927362012-05-18 23:22:52 +02001668 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001669 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001670 hdr_idx_init(idx);
1671 state = HTTP_MSG_RPVER;
1672 goto http_msg_rpver;
1673 }
1674
1675 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1676 goto http_msg_invalid;
1677
1678 if (unlikely(*ptr == '\n'))
1679 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1680 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1681 /* stop here */
1682
Willy Tarreau8973c702007-01-21 23:58:29 +01001683 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001684 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001685 EXPECT_LF_HERE(ptr, http_msg_invalid);
1686 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1687 /* stop here */
1688
Willy Tarreau8973c702007-01-21 23:58:29 +01001689 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001690 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001691 case HTTP_MSG_RPVER_SP:
1692 case HTTP_MSG_RPCODE:
1693 case HTTP_MSG_RPCODE_SP:
1694 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001695 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001696 state, ptr, end,
1697 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001698 if (unlikely(!ptr))
1699 return;
1700
1701 /* we have a full response and we know that we have either a CR
1702 * or an LF at <ptr>.
1703 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001704 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1705
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001706 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001707 if (likely(*ptr == '\r'))
1708 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1709 goto http_msg_rpline_end;
1710
Willy Tarreau8973c702007-01-21 23:58:29 +01001711 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001712 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001713 /* msg->sol must point to the first of CR or LF. */
1714 EXPECT_LF_HERE(ptr, http_msg_invalid);
1715 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1716 /* stop here */
1717
1718 /*
1719 * Second, states that are specific to the request only
1720 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001721 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001722 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001723 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001724 /* we have a start of message, but we have to check
1725 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001726 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001727 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001728 if (likely(ptr != buf->p)) {
1729 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001730 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001731 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001732 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001733 }
Willy Tarreau26927362012-05-18 23:22:52 +02001734 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001735 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001736 state = HTTP_MSG_RQMETH;
1737 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001738 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001739
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001740 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1741 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001742
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001743 if (unlikely(*ptr == '\n'))
1744 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1745 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001746 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001747
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001748 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001749 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001750 EXPECT_LF_HERE(ptr, http_msg_invalid);
1751 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001752 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001753
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001754 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001755 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001756 case HTTP_MSG_RQMETH_SP:
1757 case HTTP_MSG_RQURI:
1758 case HTTP_MSG_RQURI_SP:
1759 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001760 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001761 state, ptr, end,
1762 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001763 if (unlikely(!ptr))
1764 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001765
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001766 /* we have a full request and we know that we have either a CR
1767 * or an LF at <ptr>.
1768 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001769 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001770
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001771 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001772 if (likely(*ptr == '\r'))
1773 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001774 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001775
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001776 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001777 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001778 /* check for HTTP/0.9 request : no version information available.
1779 * msg->sol must point to the first of CR or LF.
1780 */
1781 if (unlikely(msg->sl.rq.v_l == 0))
1782 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001783
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001784 EXPECT_LF_HERE(ptr, http_msg_invalid);
1785 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001786 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001787
Willy Tarreau8973c702007-01-21 23:58:29 +01001788 /*
1789 * Common states below
1790 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001791 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001792 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001793 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001794 if (likely(!HTTP_IS_CRLF(*ptr))) {
1795 goto http_msg_hdr_name;
1796 }
1797
1798 if (likely(*ptr == '\r'))
1799 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1800 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001801
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001802 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001803 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001804 /* assumes msg->sol points to the first char */
1805 if (likely(HTTP_IS_TOKEN(*ptr)))
1806 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001807
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001808 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001809 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001810
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001811 if (likely(msg->err_pos < -1) || *ptr == '\n')
1812 goto http_msg_invalid;
1813
1814 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001815 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001816
1817 /* and we still accept this non-token character */
1818 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001819
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001820 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001821 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001822 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001823 if (likely(HTTP_IS_SPHT(*ptr)))
1824 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001825
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001826 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001827 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001828
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001829 if (likely(!HTTP_IS_CRLF(*ptr))) {
1830 goto http_msg_hdr_val;
1831 }
1832
1833 if (likely(*ptr == '\r'))
1834 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1835 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001836
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001837 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001838 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001839 EXPECT_LF_HERE(ptr, http_msg_invalid);
1840 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001841
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001842 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001843 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001844 if (likely(HTTP_IS_SPHT(*ptr))) {
1845 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001846 for (; buf->p + msg->sov < ptr; msg->sov++)
1847 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001848 goto http_msg_hdr_l1_sp;
1849 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001850 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001851 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001852 goto http_msg_complete_header;
1853
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001854 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001855 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001856 /* assumes msg->sol points to the first char, and msg->sov
1857 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001858 */
1859 if (likely(!HTTP_IS_CRLF(*ptr)))
1860 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001861
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001862 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001863 /* Note: we could also copy eol into ->eoh so that we have the
1864 * real header end in case it ends with lots of LWS, but is this
1865 * really needed ?
1866 */
1867 if (likely(*ptr == '\r'))
1868 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1869 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001870
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001871 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001872 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001873 EXPECT_LF_HERE(ptr, http_msg_invalid);
1874 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001875
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001876 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001877 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001878 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1879 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001880 for (; buf->p + msg->eol < ptr; msg->eol++)
1881 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001882 goto http_msg_hdr_val;
1883 }
1884 http_msg_complete_header:
1885 /*
1886 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001887 * Assumes msg->sol points to the first char, msg->sov points
1888 * to the first character of the value and msg->eol to the
1889 * first CR or LF so we know how the line ends. We insert last
1890 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001891 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001892 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001893 idx, idx->tail) < 0))
1894 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001895
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001896 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001897 if (likely(!HTTP_IS_CRLF(*ptr))) {
1898 goto http_msg_hdr_name;
1899 }
1900
1901 if (likely(*ptr == '\r'))
1902 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1903 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001904
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001905 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001906 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001907 /* Assumes msg->sol points to the first of either CR or LF.
1908 * Sets ->sov and ->next to the total header length, ->eoh to
1909 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1910 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001911 EXPECT_LF_HERE(ptr, http_msg_invalid);
1912 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001913 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001914 msg->eoh = msg->sol;
1915 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001916 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001917 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001918 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001919
1920 case HTTP_MSG_ERROR:
1921 /* this may only happen if we call http_msg_analyser() twice with an error */
1922 break;
1923
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001924 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001925#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001926 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1927 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001928#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001929 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001930 }
1931 http_msg_ood:
1932 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001933 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001934 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001935 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001936
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001937 http_msg_invalid:
1938 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001939 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001940 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001941 return;
1942}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001943
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001944/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1945 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1946 * nothing is done and 1 is returned.
1947 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001948static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001949{
1950 int delta;
1951 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001952 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001953
1954 if (msg->sl.rq.v_l != 0)
1955 return 1;
1956
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001957 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1958 if (txn->meth != HTTP_METH_GET)
1959 return 0;
1960
Willy Tarreau9b28e032012-10-12 23:49:43 +02001961 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001962 delta = 0;
1963
1964 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001965 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1966 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001967 }
1968 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001969 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001970 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001971 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001972 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001973 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001974 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001975 NULL, NULL);
1976 if (unlikely(!cur_end))
1977 return 0;
1978
1979 /* we have a full HTTP/1.0 request now and we know that
1980 * we have either a CR or an LF at <ptr>.
1981 */
1982 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1983 return 1;
1984}
1985
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001986/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001987 * and "keep-alive" values. If we already know that some headers may safely
1988 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001989 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1990 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001991 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001992 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1993 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1994 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001995 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001996 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001997void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001998{
Willy Tarreau5b154472009-12-21 20:11:07 +01001999 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002000 const char *hdr_val = "Connection";
2001 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01002002
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002003 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01002004 return;
2005
Willy Tarreau88d349d2010-01-25 12:15:43 +01002006 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2007 hdr_val = "Proxy-Connection";
2008 hdr_len = 16;
2009 }
2010
Willy Tarreau5b154472009-12-21 20:11:07 +01002011 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002012 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002013 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002014 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2015 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002016 if (to_del & 2)
2017 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002018 else
2019 txn->flags |= TX_CON_KAL_SET;
2020 }
2021 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2022 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002023 if (to_del & 1)
2024 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002025 else
2026 txn->flags |= TX_CON_CLO_SET;
2027 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01002028 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
2029 txn->flags |= TX_HDR_CONN_UPG;
2030 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002031 }
2032
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002033 txn->flags |= TX_HDR_CONN_PRS;
2034 return;
2035}
Willy Tarreau5b154472009-12-21 20:11:07 +01002036
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002037/* Apply desired changes on the Connection: header. Values may be removed and/or
2038 * added depending on the <wanted> flags, which are exclusively composed of
2039 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2040 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2041 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002042void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002043{
2044 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002045 const char *hdr_val = "Connection";
2046 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002047
2048 ctx.idx = 0;
2049
Willy Tarreau88d349d2010-01-25 12:15:43 +01002050
2051 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2052 hdr_val = "Proxy-Connection";
2053 hdr_len = 16;
2054 }
2055
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002056 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002057 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002058 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2059 if (wanted & TX_CON_KAL_SET)
2060 txn->flags |= TX_CON_KAL_SET;
2061 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002062 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002063 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002064 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2065 if (wanted & TX_CON_CLO_SET)
2066 txn->flags |= TX_CON_CLO_SET;
2067 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002068 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002069 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002070 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002071
2072 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2073 return;
2074
2075 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2076 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002077 hdr_val = "Connection: close";
2078 hdr_len = 17;
2079 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2080 hdr_val = "Proxy-Connection: close";
2081 hdr_len = 23;
2082 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002083 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002084 }
2085
2086 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2087 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002088 hdr_val = "Connection: keep-alive";
2089 hdr_len = 22;
2090 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2091 hdr_val = "Proxy-Connection: keep-alive";
2092 hdr_len = 28;
2093 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002094 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002095 }
2096 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002097}
2098
Christopher Faulet113f7de2015-12-14 14:52:13 +01002099/* Parse the chunk size at msg->next. Once done, caller should adjust ->next to
2100 * point to the first byte of data after the chunk size, so that we know we can
2101 * forward exactly msg->next bytes. msg->sol contains the exact number of bytes
2102 * forming the chunk size. That way it is always possible to differentiate
2103 * between the start of the body and the start of the data. Return the number
2104 * of byte parsed on success, 0 when some data is missing, <0 on error. Note:
2105 * this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002106 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002107static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002108{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002109 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002110 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002111 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002112 const char *end = buf->data + buf->size;
2113 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01002114 unsigned int chunk = 0;
2115
2116 /* The chunk size is in the following form, though we are only
2117 * interested in the size and CRLF :
2118 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2119 */
2120 while (1) {
2121 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002122 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002123 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002124 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002125 if (c < 0) /* not a hex digit anymore */
2126 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002127 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002128 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002129 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002130 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002131 chunk = (chunk << 4) + c;
2132 }
2133
Willy Tarreaud98cf932009-12-27 22:54:55 +01002134 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002135 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002136 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002137
2138 while (http_is_spht[(unsigned char)*ptr]) {
2139 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002140 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002141 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002142 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002143 }
2144
Willy Tarreaud98cf932009-12-27 22:54:55 +01002145 /* Up to there, we know that at least one byte is present at *ptr. Check
2146 * for the end of chunk size.
2147 */
2148 while (1) {
2149 if (likely(HTTP_IS_CRLF(*ptr))) {
2150 /* we now have a CR or an LF at ptr */
2151 if (likely(*ptr == '\r')) {
2152 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002153 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002154 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002155 return 0;
2156 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002157
Willy Tarreaud98cf932009-12-27 22:54:55 +01002158 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002159 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002160 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002161 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002162 /* done */
2163 break;
2164 }
2165 else if (*ptr == ';') {
2166 /* chunk extension, ends at next CRLF */
2167 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002168 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002169 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002170 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002171
2172 while (!HTTP_IS_CRLF(*ptr)) {
2173 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002174 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002175 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002176 return 0;
2177 }
2178 /* we have a CRLF now, loop above */
2179 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002180 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002181 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002182 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002183 }
2184
Christopher Faulet113f7de2015-12-14 14:52:13 +01002185 /* OK we found our CRLF and now <ptr> points to the next byte, which may
2186 * or may not be present. We save the number of bytes parsed into
2187 * msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002188 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002189 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002190 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002191 msg->sol += buf->size;
Willy Tarreau124d9912011-03-01 20:30:48 +01002192 msg->chunk_len = chunk;
2193 msg->body_len += chunk;
Christopher Faulet113f7de2015-12-14 14:52:13 +01002194 return msg->sol;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002195 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002196 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002197 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002198}
2199
Christopher Faulet113f7de2015-12-14 14:52:13 +01002200/* This function skips trailers in the buffer associated with HTTP message
2201 * <msg>. The first visited position is msg->next. If the end of the trailers is
2202 * found, the function returns >0. So, the caller can automatically schedul it
2203 * to be forwarded, and switch msg->msg_state to HTTP_MSG_DONE. If not enough
2204 * data are available, the function does not change anything except maybe
2205 * msg->sol if it could parse some lines, and returns zero. If a parse error
2206 * is encountered, the function returns < 0 and does not change anything except
2207 * maybe msg->sol. Note that the message must already be in HTTP_MSG_TRAILERS
2208 * state before calling this function, which implies that all non-trailers data
2209 * have already been scheduled for forwarding, and that msg->next exactly
2210 * matches the length of trailers already parsed and not forwarded. It is also
2211 * important to note that this function is designed to be able to parse wrapped
2212 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002213 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002214static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002215{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002216 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002217
Christopher Faulet113f7de2015-12-14 14:52:13 +01002218 /* we have msg->next which points to next line. Look for CRLF. But
2219 * first, we reset msg->sol */
2220 msg->sol = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002221 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002222 const char *p1 = NULL, *p2 = NULL;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002223 const char *start = b_ptr(buf, msg->next + msg->sol);
2224 const char *stop = bi_end(buf);
2225 const char *ptr = start;
2226 int bytes = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002227
2228 /* scan current line and stop at LF or CRLF */
2229 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002230 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002231 return 0;
2232
2233 if (*ptr == '\n') {
2234 if (!p1)
2235 p1 = ptr;
2236 p2 = ptr;
2237 break;
2238 }
2239
2240 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002241 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002242 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002243 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002244 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002245 p1 = ptr;
2246 }
2247
2248 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002249 if (ptr >= buf->data + buf->size)
2250 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002251 }
2252
2253 /* after LF; point to beginning of next line */
2254 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002255 if (p2 >= buf->data + buf->size)
2256 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002257
Christopher Faulet2fb28802015-12-01 10:40:57 +01002258 bytes = p2 - start;
Willy Tarreau638cd022010-01-03 07:42:04 +01002259 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002260 bytes += buf->size;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002261 msg->sol += bytes;
Willy Tarreau638cd022010-01-03 07:42:04 +01002262
Christopher Fauletd7c91962015-04-30 11:48:27 +02002263 /* LF/CRLF at beginning of line => end of trailers at p2.
2264 * Everything was scheduled for forwarding, there's nothing left
Christopher Faulet2fb28802015-12-01 10:40:57 +01002265 * from this message. */
2266 if (p1 == start)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002267 return 1;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002268
Willy Tarreaud98cf932009-12-27 22:54:55 +01002269 /* OK, next line then */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002270 }
2271}
2272
Christopher Faulet113f7de2015-12-14 14:52:13 +01002273/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
2274 * a possible LF alone at the end of a chunk. The caller should adjust msg->next
2275 * in order to include this part into the next forwarding phase. Note that the
2276 * caller must ensure that ->p points to the first byte to parse. It returns
2277 * the number of bytes parsed on success, so the caller can set msg_state to
2278 * HTTP_MSG_CHUNK_SIZE. If not enough data are available, the function does not
2279 * change anything and returns zero. If a parse error is encountered, the
2280 * function returns < 0. Note: this function is designed to parse wrapped CRLF
2281 * at the end of the buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002282 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002283static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002284{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002285 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002286 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002287 int bytes;
2288
2289 /* NB: we'll check data availabilty at the end. It's not a
2290 * problem because whatever we match first will be checked
2291 * against the correct length.
2292 */
2293 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002294 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002295 if (*ptr == '\r') {
2296 bytes++;
2297 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002298 if (ptr >= buf->data + buf->size)
2299 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002300 }
2301
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002302 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002303 return 0;
2304
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002305 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002306 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002307 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002308 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01002309 return bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002310}
Willy Tarreau5b154472009-12-21 20:11:07 +01002311
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002312/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2313 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2314 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2315 * Unparsable qvalues return 1000 as "q=1.000".
2316 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002317int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002318{
2319 int q = 1000;
2320
Willy Tarreau506c69a2014-07-08 00:59:48 +02002321 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002322 goto out;
2323 q = (*qvalue++ - '0') * 1000;
2324
2325 if (*qvalue++ != '.')
2326 goto out;
2327
Willy Tarreau506c69a2014-07-08 00:59:48 +02002328 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002329 goto out;
2330 q += (*qvalue++ - '0') * 100;
2331
Willy Tarreau506c69a2014-07-08 00:59:48 +02002332 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002333 goto out;
2334 q += (*qvalue++ - '0') * 10;
2335
Willy Tarreau506c69a2014-07-08 00:59:48 +02002336 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002337 goto out;
2338 q += (*qvalue++ - '0') * 1;
2339 out:
2340 if (q > 1000)
2341 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002342 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002343 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002344 return q;
2345}
William Lallemand82fe75c2012-10-23 10:25:10 +02002346
Willy Tarreau87b09662015-04-03 00:22:06 +02002347void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002348{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002349 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002350 int tmp = TX_CON_WANT_KAL;
2351
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002352 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2353 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002354 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2355 tmp = TX_CON_WANT_TUN;
2356
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002357 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002358 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2359 tmp = TX_CON_WANT_TUN;
2360 }
2361
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002362 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002363 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2364 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002365 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002366 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2367 tmp = TX_CON_WANT_CLO;
2368 else
2369 tmp = TX_CON_WANT_SCL;
2370 }
2371
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002372 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002373 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2374 tmp = TX_CON_WANT_CLO;
2375
2376 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2377 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2378
2379 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2380 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2381 /* parse the Connection header and possibly clean it */
2382 int to_del = 0;
2383 if ((msg->flags & HTTP_MSGF_VER_11) ||
2384 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002385 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002386 to_del |= 2; /* remove "keep-alive" */
2387 if (!(msg->flags & HTTP_MSGF_VER_11))
2388 to_del |= 1; /* remove "close" */
2389 http_parse_connection_header(txn, msg, to_del);
2390 }
2391
2392 /* check if client or config asks for explicit close in KAL/SCL */
2393 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2394 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2395 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2396 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2397 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002398 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002399 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2400}
William Lallemand82fe75c2012-10-23 10:25:10 +02002401
Willy Tarreaud787e662009-07-07 10:14:51 +02002402/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2403 * processing can continue on next analysers, or zero if it either needs more
2404 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002405 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002406 * when it has nothing left to do, and may remove any analyser when it wants to
2407 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002408 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002409int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002410{
Willy Tarreau59234e92008-11-30 23:51:27 +01002411 /*
2412 * We will parse the partial (or complete) lines.
2413 * We will check the request syntax, and also join multi-line
2414 * headers. An index of all the lines will be elaborated while
2415 * parsing.
2416 *
2417 * For the parsing, we use a 28 states FSM.
2418 *
2419 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002420 * req->buf->p = beginning of request
2421 * req->buf->p + msg->eoh = end of processed headers / start of current one
2422 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002423 * msg->eol = end of current header or line (LF or CRLF)
2424 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002425 *
2426 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02002427 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002428 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2429 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002430 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002431
Willy Tarreau59234e92008-11-30 23:51:27 +01002432 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002433 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002434 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01002435 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002436 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002437
Willy Tarreau87b09662015-04-03 00:22:06 +02002438 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 +01002439 now_ms, __FUNCTION__,
2440 s,
2441 req,
2442 req->rex, req->wex,
2443 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002444 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002445 req->analysers);
2446
Willy Tarreau52a0c602009-08-16 22:45:38 +02002447 /* we're speaking HTTP here, so let's speak HTTP to the client */
2448 s->srv_error = http_return_srv_error;
2449
Willy Tarreau83e3af02009-12-28 17:39:57 +01002450 /* There's a protected area at the end of the buffer for rewriting
2451 * purposes. We don't want to start to parse the request if the
2452 * protected area is affected, because we may have to move processed
2453 * data later, which is much more complicated.
2454 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002455 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002456 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002457 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002458 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002459 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002460 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002461 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002462 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002463 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002464 return 0;
2465 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002466 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2467 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2468 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002469 }
2470
Willy Tarreau065e8332010-01-08 00:30:20 +01002471 /* Note that we have the same problem with the response ; we
2472 * may want to send a redirect, error or anything which requires
2473 * some spare space. So we'll ensure that we have at least
2474 * maxrewrite bytes available in the response buffer before
2475 * processing that one. This will only affect pipelined
2476 * keep-alive requests.
2477 */
2478 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002479 unlikely(!channel_is_rewritable(&s->res) ||
2480 bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) ||
2481 bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) {
2482 if (s->res.buf->o) {
2483 if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002484 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002485 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002486 channel_dont_connect(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002487 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
2488 s->res.flags |= CF_WAKE_WRITE;
2489 s->res.analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002490 return 0;
2491 }
2492 }
2493
Willy Tarreau9b28e032012-10-12 23:49:43 +02002494 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002495 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002496 }
2497
Willy Tarreau59234e92008-11-30 23:51:27 +01002498 /* 1: we might have to print this header in debug mode */
2499 if (unlikely((global.mode & MODE_DEBUG) &&
2500 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002501 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002502 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002503
Willy Tarreau9b28e032012-10-12 23:49:43 +02002504 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002505 /* this is a bit complex : in case of error on the request line,
2506 * we know that rq.l is still zero, so we display only the part
2507 * up to the end of the line (truncated by debug_hdr).
2508 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002509 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002510 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002511
Willy Tarreau59234e92008-11-30 23:51:27 +01002512 sol += hdr_idx_first_pos(&txn->hdr_idx);
2513 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002514
Willy Tarreau59234e92008-11-30 23:51:27 +01002515 while (cur_idx) {
2516 eol = sol + txn->hdr_idx.v[cur_idx].len;
2517 debug_hdr("clihdr", s, sol, eol);
2518 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2519 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002520 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002521 }
2522
Willy Tarreau58f10d72006-12-04 02:26:12 +01002523
Willy Tarreau59234e92008-11-30 23:51:27 +01002524 /*
2525 * Now we quickly check if we have found a full valid request.
2526 * If not so, we check the FD and buffer states before leaving.
2527 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002528 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002529 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02002530 * on a keep-alive stream, if we encounter and error, close, t/o,
2531 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002532 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02002533 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002534 * Last, we may increase some tracked counters' http request errors on
2535 * the cases that are deliberately the client's fault. For instance,
2536 * a timeout or connection reset is not counted as an error. However
2537 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002538 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002539
Willy Tarreau655dce92009-11-08 13:10:58 +01002540 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002541 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002542 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002543 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002544 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002545 stream_inc_http_req_ctr(s);
2546 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002547 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002548 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002549 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002550
Willy Tarreau59234e92008-11-30 23:51:27 +01002551 /* 1: Since we are in header mode, if there's no space
2552 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02002553 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01002554 * must terminate it now.
2555 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002556 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002557 /* FIXME: check if URI is set and return Status
2558 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002559 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002560 stream_inc_http_req_ctr(s);
2561 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002562 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002563 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002564 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002565 goto return_bad_req;
2566 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002567
Willy Tarreau59234e92008-11-30 23:51:27 +01002568 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002569 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002570 if (!(s->flags & SF_ERR_MASK))
2571 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002572
Willy Tarreaufcffa692010-01-10 14:21:19 +01002573 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002574 goto failed_keep_alive;
2575
Willy Tarreau0f228a02015-05-01 15:37:53 +02002576 if (sess->fe->options & PR_O_IGNORE_PRB)
2577 goto failed_keep_alive;
2578
Willy Tarreau59234e92008-11-30 23:51:27 +01002579 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002580 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002581 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002582 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002583 }
2584
Willy Tarreaudc979f22012-12-04 10:39:01 +01002585 txn->status = 400;
Willy Tarreau59234e92008-11-30 23:51:27 +01002586 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002587 http_reply_and_close(s, txn->status, NULL);
Christopher Fauletd7c91962015-04-30 11:48:27 +02002588 req->analysers &= AN_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02002589 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002590 proxy_inc_fe_req_ctr(sess->fe);
2591 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002592 if (sess->listener->counters)
2593 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002594
Willy Tarreaue7dff022015-04-03 01:14:29 +02002595 if (!(s->flags & SF_FINST_MASK))
2596 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002597 return 0;
2598 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002599
Willy Tarreau59234e92008-11-30 23:51:27 +01002600 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002601 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002602 if (!(s->flags & SF_ERR_MASK))
2603 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002604
Willy Tarreaufcffa692010-01-10 14:21:19 +01002605 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002606 goto failed_keep_alive;
2607
Willy Tarreau0f228a02015-05-01 15:37:53 +02002608 if (sess->fe->options & PR_O_IGNORE_PRB)
2609 goto failed_keep_alive;
2610
Willy Tarreau59234e92008-11-30 23:51:27 +01002611 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002612 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002613 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002614 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002615 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002616 txn->status = 408;
Willy Tarreau59234e92008-11-30 23:51:27 +01002617 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002618 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_408));
Christopher Fauletd7c91962015-04-30 11:48:27 +02002619 req->analysers &= AN_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002620
Willy Tarreau87b09662015-04-03 00:22:06 +02002621 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002622 proxy_inc_fe_req_ctr(sess->fe);
2623 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002624 if (sess->listener->counters)
2625 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002626
Willy Tarreaue7dff022015-04-03 01:14:29 +02002627 if (!(s->flags & SF_FINST_MASK))
2628 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002629 return 0;
2630 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002631
Willy Tarreau59234e92008-11-30 23:51:27 +01002632 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002633 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002634 if (!(s->flags & SF_ERR_MASK))
2635 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002636
Willy Tarreaufcffa692010-01-10 14:21:19 +01002637 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002638 goto failed_keep_alive;
2639
Willy Tarreau0f228a02015-05-01 15:37:53 +02002640 if (sess->fe->options & PR_O_IGNORE_PRB)
2641 goto failed_keep_alive;
2642
Willy Tarreau4076a152009-04-02 15:18:36 +02002643 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002644 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002645 txn->status = 400;
Willy Tarreau59234e92008-11-30 23:51:27 +01002646 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002647 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Christopher Fauletd7c91962015-04-30 11:48:27 +02002648 req->analysers &= AN_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02002649 stream_inc_http_err_ctr(s);
2650 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002651 proxy_inc_fe_req_ctr(sess->fe);
2652 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002653 if (sess->listener->counters)
2654 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002655
Willy Tarreaue7dff022015-04-03 01:14:29 +02002656 if (!(s->flags & SF_FINST_MASK))
2657 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002658 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002659 }
2660
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002661 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002662 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002663 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002664#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002665 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
2666 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002667 /* We need more data, we have to re-enable quick-ack in case we
2668 * previously disabled it, otherwise we might cause the client
2669 * to delay next data.
2670 */
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002671 setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002672 }
2673#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002674
Willy Tarreaufcffa692010-01-10 14:21:19 +01002675 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2676 /* If the client starts to talk, let's fall back to
2677 * request timeout processing.
2678 */
2679 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002680 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002681 }
2682
Willy Tarreau59234e92008-11-30 23:51:27 +01002683 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002684 if (!tick_isset(req->analyse_exp)) {
2685 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2686 (txn->flags & TX_WAIT_NEXT_RQ) &&
2687 tick_isset(s->be->timeout.httpka))
2688 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2689 else
2690 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2691 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002692
Willy Tarreau59234e92008-11-30 23:51:27 +01002693 /* we're not ready yet */
2694 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002695
2696 failed_keep_alive:
2697 /* Here we process low-level errors for keep-alive requests. In
2698 * short, if the request is not the first one and it experiences
2699 * a timeout, read error or shutdown, we just silently close so
2700 * that the client can try again.
2701 */
2702 txn->status = 0;
2703 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Fauletd7c91962015-04-30 11:48:27 +02002704 req->analysers &= AN_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002705 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002706 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002707 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002708 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002709 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002710 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002711
Willy Tarreaud787e662009-07-07 10:14:51 +02002712 /* OK now we have a complete HTTP request with indexed headers. Let's
2713 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002714 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002715 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002716 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002717 * byte after the last LF. msg->sov points to the first byte of data.
2718 * msg->eol cannot be trusted because it may have been left uninitialized
2719 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002720 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002721
Willy Tarreau87b09662015-04-03 00:22:06 +02002722 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002723 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002724
Willy Tarreaub16a5742010-01-10 14:46:16 +01002725 if (txn->flags & TX_WAIT_NEXT_RQ) {
2726 /* kill the pending keep-alive timeout */
2727 txn->flags &= ~TX_WAIT_NEXT_RQ;
2728 req->analyse_exp = TICK_ETERNITY;
2729 }
2730
2731
Willy Tarreaud787e662009-07-07 10:14:51 +02002732 /* Maybe we found in invalid header name while we were configured not
2733 * to block on that, so we have to capture it now.
2734 */
2735 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002736 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002737
Willy Tarreau59234e92008-11-30 23:51:27 +01002738 /*
2739 * 1: identify the method
2740 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002741 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002742
2743 /* we can make use of server redirect on GET and HEAD */
2744 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002745 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002746
Willy Tarreau59234e92008-11-30 23:51:27 +01002747 /*
2748 * 2: check if the URI matches the monitor_uri.
2749 * We have to do this for every request which gets in, because
2750 * the monitor-uri is defined by the frontend.
2751 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002752 if (unlikely((sess->fe->monitor_uri_len != 0) &&
2753 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002754 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002755 sess->fe->monitor_uri,
2756 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002757 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002758 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002759 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002760 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002761
Willy Tarreaue7dff022015-04-03 01:14:29 +02002762 s->flags |= SF_MONITOR;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002763 sess->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002764
Willy Tarreau59234e92008-11-30 23:51:27 +01002765 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002766 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002767 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002768
Willy Tarreau59234e92008-11-30 23:51:27 +01002769 ret = acl_pass(ret);
2770 if (cond->pol == ACL_COND_UNLESS)
2771 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002772
Willy Tarreau59234e92008-11-30 23:51:27 +01002773 if (ret) {
2774 /* we fail this request, let's return 503 service unavail */
2775 txn->status = 503;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002776 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_503));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002777 if (!(s->flags & SF_ERR_MASK))
2778 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002779 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002780 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002781 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002782
Willy Tarreau59234e92008-11-30 23:51:27 +01002783 /* nothing to fail, let's reply normaly */
2784 txn->status = 200;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002785 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_200));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002786 if (!(s->flags & SF_ERR_MASK))
2787 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002788 goto return_prx_cond;
2789 }
2790
2791 /*
2792 * 3: Maybe we have to copy the original REQURI for the logs ?
2793 * Note: we cannot log anymore if the request has been
2794 * classified as invalid.
2795 */
2796 if (unlikely(s->logs.logwait & LW_REQ)) {
2797 /* we have a complete HTTP request that we must log */
2798 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2799 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002800
Willy Tarreau59234e92008-11-30 23:51:27 +01002801 if (urilen >= REQURI_LEN)
2802 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002803 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002804 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002805
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002806 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002807 s->do_log(s);
2808 } else {
2809 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002810 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002811 }
Willy Tarreau06619262006-12-17 08:37:22 +01002812
Willy Tarreau91852eb2015-05-01 13:26:00 +02002813 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
2814 * exactly one digit "." one digit. This check may be disabled using
2815 * option accept-invalid-http-request.
2816 */
2817 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
2818 if (msg->sl.rq.v_l != 8) {
2819 msg->err_pos = msg->sl.rq.v;
2820 goto return_bad_req;
2821 }
2822
2823 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
2824 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
2825 req->buf->p[msg->sl.rq.v + 6] != '.' ||
2826 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
2827 msg->err_pos = msg->sl.rq.v + 4;
2828 goto return_bad_req;
2829 }
2830 }
Willy Tarreau13317662015-05-01 13:47:08 +02002831 else {
2832 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
2833 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
2834 goto return_bad_req;
2835 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02002836
Willy Tarreau5b154472009-12-21 20:11:07 +01002837 /* ... and check if the request is HTTP/1.1 or above */
2838 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002839 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2840 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2841 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002842 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002843
2844 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002845 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 +01002846
Willy Tarreau88d349d2010-01-25 12:15:43 +01002847 /* if the frontend has "option http-use-proxy-header", we'll check if
2848 * we have what looks like a proxied connection instead of a connection,
2849 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2850 * Note that this is *not* RFC-compliant, however browsers and proxies
2851 * happen to do that despite being non-standard :-(
2852 * We consider that a request not beginning with either '/' or '*' is
2853 * a proxied connection, which covers both "scheme://location" and
2854 * CONNECT ip:port.
2855 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002856 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002857 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002858 txn->flags |= TX_USE_PX_CONN;
2859
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002860 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002861 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002862
Willy Tarreau59234e92008-11-30 23:51:27 +01002863 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002864 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002865 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002866 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002867
Willy Tarreau557f1992015-05-01 10:05:17 +02002868 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
2869 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002870 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002871 * The length of a message body is determined by one of the following
2872 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02002873 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002874 * 1. Any response to a HEAD request and any response with a 1xx
2875 * (Informational), 204 (No Content), or 304 (Not Modified) status
2876 * code is always terminated by the first empty line after the
2877 * header fields, regardless of the header fields present in the
2878 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002879 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002880 * 2. Any 2xx (Successful) response to a CONNECT request implies that
2881 * the connection will become a tunnel immediately after the empty
2882 * line that concludes the header fields. A client MUST ignore any
2883 * Content-Length or Transfer-Encoding header fields received in
2884 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002885 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002886 * 3. If a Transfer-Encoding header field is present and the chunked
2887 * transfer coding (Section 4.1) is the final encoding, the message
2888 * body length is determined by reading and decoding the chunked
2889 * data until the transfer coding indicates the data is complete.
2890 *
2891 * If a Transfer-Encoding header field is present in a response and
2892 * the chunked transfer coding is not the final encoding, the
2893 * message body length is determined by reading the connection until
2894 * it is closed by the server. If a Transfer-Encoding header field
2895 * is present in a request and the chunked transfer coding is not
2896 * the final encoding, the message body length cannot be determined
2897 * reliably; the server MUST respond with the 400 (Bad Request)
2898 * status code and then close the connection.
2899 *
2900 * If a message is received with both a Transfer-Encoding and a
2901 * Content-Length header field, the Transfer-Encoding overrides the
2902 * Content-Length. Such a message might indicate an attempt to
2903 * perform request smuggling (Section 9.5) or response splitting
2904 * (Section 9.4) and ought to be handled as an error. A sender MUST
2905 * remove the received Content-Length field prior to forwarding such
2906 * a message downstream.
2907 *
2908 * 4. If a message is received without Transfer-Encoding and with
2909 * either multiple Content-Length header fields having differing
2910 * field-values or a single Content-Length header field having an
2911 * invalid value, then the message framing is invalid and the
2912 * recipient MUST treat it as an unrecoverable error. If this is a
2913 * request message, the server MUST respond with a 400 (Bad Request)
2914 * status code and then close the connection. If this is a response
2915 * message received by a proxy, the proxy MUST close the connection
2916 * to the server, discard the received response, and send a 502 (Bad
2917 * Gateway) response to the client. If this is a response message
2918 * received by a user agent, the user agent MUST close the
2919 * connection to the server and discard the received response.
2920 *
2921 * 5. If a valid Content-Length header field is present without
2922 * Transfer-Encoding, its decimal value defines the expected message
2923 * body length in octets. If the sender closes the connection or
2924 * the recipient times out before the indicated number of octets are
2925 * received, the recipient MUST consider the message to be
2926 * incomplete and close the connection.
2927 *
2928 * 6. If this is a request message and none of the above are true, then
2929 * the message body length is zero (no message body is present).
2930 *
2931 * 7. Otherwise, this is a response message without a declared message
2932 * body length, so the message body length is determined by the
2933 * number of octets received prior to the server closing the
2934 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002935 */
2936
Willy Tarreau32b47f42009-10-18 20:55:02 +02002937 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002938 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02002939 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002940 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002941 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2942 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02002943 /* chunked not last, return badreq */
2944 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002945 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002946 }
2947
Willy Tarreau1c913912015-04-30 10:57:51 +02002948 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02002949 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02002950 if (msg->flags & HTTP_MSGF_TE_CHNK) {
2951 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
2952 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2953 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02002954 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002955 signed long long cl;
2956
Willy Tarreauad14f752011-09-02 20:33:27 +02002957 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002958 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002959 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002960 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002961
Willy Tarreauad14f752011-09-02 20:33:27 +02002962 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002963 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002964 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002965 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002966
Willy Tarreauad14f752011-09-02 20:33:27 +02002967 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002968 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002969 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002970 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002971
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002972 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002973 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002974 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002975 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002976
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002977 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002978 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002979 }
2980
Willy Tarreau34dfc602015-05-01 10:09:49 +02002981 /* even bodyless requests have a known length */
2982 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002983
Willy Tarreau179085c2014-04-28 16:48:56 +02002984 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
2985 * only change if both the request and the config reference something else.
2986 * Option httpclose by itself sets tunnel mode where headers are mangled.
2987 * However, if another mode is set, it will affect it (eg: server-close/
2988 * keep-alive + httpclose = close). Note that we avoid to redo the same work
2989 * if FE and BE have the same settings (common). The method consists in
2990 * checking if options changed between the two calls (implying that either
2991 * one is non-null, or one of them is non-null and we are there for the first
2992 * time.
2993 */
2994 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002995 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002996 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02002997
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02002998 /* we may have to wait for the request's body */
2999 if ((s->be->options & PR_O_WREQ_BODY) &&
3000 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
3001 req->analysers |= AN_REQ_HTTP_BODY;
3002
Willy Tarreaud787e662009-07-07 10:14:51 +02003003 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003004 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003005 req->analyse_exp = TICK_ETERNITY;
3006 return 1;
3007
3008 return_bad_req:
3009 /* We centralize bad requests processing here */
3010 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3011 /* we detected a parsing error. We want to archive this request
3012 * in the dedicated proxy area for later troubleshooting.
3013 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003014 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003015 }
3016
3017 txn->req.msg_state = HTTP_MSG_ERROR;
3018 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003019 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003020
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003021 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003022 if (sess->listener->counters)
3023 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003024
3025 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003026 if (!(s->flags & SF_ERR_MASK))
3027 s->flags |= SF_ERR_PRXCOND;
3028 if (!(s->flags & SF_FINST_MASK))
3029 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02003030
Christopher Fauletd7c91962015-04-30 11:48:27 +02003031 req->analysers &= AN_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02003032 req->analyse_exp = TICK_ETERNITY;
3033 return 0;
3034}
3035
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003036
Willy Tarreau347a35d2013-11-22 17:51:09 +01003037/* This function prepares an applet to handle the stats. It can deal with the
3038 * "100-continue" expectation, check that admin rules are met for POST requests,
3039 * and program a response message if something was unexpected. It cannot fail
3040 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003041 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003042 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02003043 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003044 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003045int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003046{
3047 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003048 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02003049 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003050 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003051 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003052 struct uri_auth *uri_auth = s->be->uri_auth;
3053 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003054 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003055
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003056 appctx = si_appctx(si);
3057 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3058 appctx->st1 = appctx->st2 = 0;
3059 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3060 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003061 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003062 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003063
3064 uri = msg->chn->buf->p + msg->sl.rq.u;
3065 lookup = uri + uri_auth->uri_len;
3066
3067 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3068 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003069 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003070 break;
3071 }
3072 }
3073
3074 if (uri_auth->refresh) {
3075 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3076 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003077 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003078 break;
3079 }
3080 }
3081 }
3082
3083 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3084 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003085 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003086 break;
3087 }
3088 }
3089
Willy Tarreau1e62df92016-01-11 18:57:53 +01003090 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
3091 if (memcmp(h, ";typed", 6) == 0) {
3092 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
3093 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3094 break;
3095 }
3096 }
3097
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003098 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3099 if (memcmp(h, ";st=", 4) == 0) {
3100 int i;
3101 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003102 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003103 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3104 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003105 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003106 break;
3107 }
3108 }
3109 break;
3110 }
3111 }
3112
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003113 appctx->ctx.stats.scope_str = 0;
3114 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003115 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3116 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3117 int itx = 0;
3118 const char *h2;
3119 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3120 const char *err;
3121
3122 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3123 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003124 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003125 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3126 itx++;
3127 h++;
3128 }
3129
3130 if (itx > STAT_SCOPE_TXT_MAXLEN)
3131 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003132 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003133
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003134 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003135 memcpy(scope_txt, h2, itx);
3136 scope_txt[itx] = '\0';
3137 err = invalid_char(scope_txt);
3138 if (err) {
3139 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003140 appctx->ctx.stats.scope_str = 0;
3141 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003142 }
3143 break;
3144 }
3145 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003146
3147 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003148 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003149 int ret = 1;
3150
3151 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003152 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 +01003153 ret = acl_pass(ret);
3154 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3155 ret = !ret;
3156 }
3157
3158 if (ret) {
3159 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003160 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003161 break;
3162 }
3163 }
3164
3165 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003166 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003167 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003168 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003169 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
3170 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003171 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003172 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003173 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003174 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3175 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003176 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003177 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003178 else {
3179 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003180 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003181 }
3182
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003183 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003184 return 1;
3185}
3186
Lukas Tribus67db8df2013-06-23 17:37:13 +02003187/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3188 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3189 */
Thierry FOURNIER7fe75e02015-03-16 12:03:44 +01003190void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003191{
3192#ifdef IP_TOS
3193 if (from.ss_family == AF_INET)
3194 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3195#endif
3196#ifdef IPV6_TCLASS
3197 if (from.ss_family == AF_INET6) {
3198 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3199 /* v4-mapped addresses need IP_TOS */
3200 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3201 else
3202 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3203 }
3204#endif
3205}
3206
Willy Tarreau87b09662015-04-03 00:22:06 +02003207int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003208 const char* name, unsigned int name_len,
3209 const char *str, struct my_regex *re,
3210 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003211{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003212 struct hdr_ctx ctx;
3213 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003214 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003215 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3216 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003217 struct chunk *output = get_trash_chunk();
3218
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003219 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003220
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003221 /* Choose the header browsing function. */
3222 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003223 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003224 http_find_hdr_func = http_find_header2;
3225 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003226 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003227 http_find_hdr_func = http_find_full_header2;
3228 break;
3229 default: /* impossible */
3230 return -1;
3231 }
3232
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003233 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3234 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003235 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003236 char *val = ctx.line + ctx.val;
3237 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003238
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003239 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3240 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003241
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003242 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003243 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003244 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003245
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003246 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003247
3248 hdr->len += delta;
3249 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003250
3251 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003252 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003253 }
3254
3255 return 0;
3256}
3257
Willy Tarreau87b09662015-04-03 00:22:06 +02003258static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003259 const char* name, unsigned int name_len,
3260 struct list *fmt, struct my_regex *re,
3261 int action)
3262{
3263 struct chunk *replace = get_trash_chunk();
3264
3265 replace->len = build_logline(s, replace->str, replace->size, fmt);
3266 if (replace->len >= replace->size - 1)
3267 return -1;
3268
3269 return http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
3270}
3271
Willy Tarreau87b09662015-04-03 00:22:06 +02003272/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003273 * transaction <txn>. Returns the verdict of the first rule that prevents
3274 * further processing of the request (auth, deny, ...), and defaults to
3275 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3276 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3277 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003278 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003279enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003280http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003281{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003282 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003283 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003284 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003285 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003286 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003287 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02003288 int act_flags = 0;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003289
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003290 /* If "the current_rule_list" match the executed rule list, we are in
3291 * resume condition. If a resume is needed it is always in the action
3292 * and never in the ACL or converters. In this case, we initialise the
3293 * current rule, and go to the action execution point.
3294 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003295 if (s->current_rule) {
3296 rule = s->current_rule;
3297 s->current_rule = NULL;
3298 if (s->current_rule_list == rules)
3299 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003300 }
3301 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003302
Willy Tarreauff011f22011-01-06 17:51:27 +01003303 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003304
Willy Tarreau96257ec2012-12-27 10:46:37 +01003305 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003306 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003307 int ret;
3308
Willy Tarreau192252e2015-04-04 01:47:55 +02003309 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003310 ret = acl_pass(ret);
3311
Willy Tarreauff011f22011-01-06 17:51:27 +01003312 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003313 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003314
3315 if (!ret) /* condition not matched */
3316 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003317 }
3318
Willy Tarreauacc98002015-09-27 23:34:39 +02003319 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003320resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003321 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003322 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003323 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003324
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003325 case ACT_ACTION_DENY:
CJ Ess108b1dd2015-04-07 12:03:37 -04003326 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003327 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003328
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003329 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01003330 txn->flags |= TX_CLTARPIT;
CJ Ess108b1dd2015-04-07 12:03:37 -04003331 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003332 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003333
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003334 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003335 /* Auth might be performed on regular http-req rules as well as on stats */
3336 auth_realm = rule->arg.auth.realm;
3337 if (!auth_realm) {
3338 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3339 auth_realm = STATS_DEFAULT_REALM;
3340 else
3341 auth_realm = px->id;
3342 }
3343 /* send 401/407 depending on whether we use a proxy or not. We still
3344 * count one error, because normal browsing won't significantly
3345 * increase the counter but brute force attempts will.
3346 */
3347 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3348 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003349 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003350 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003351 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003352
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003353 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003354 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3355 return HTTP_RULE_RES_BADREQ;
3356 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003357
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003358 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003359 s->task->nice = rule->arg.nice;
3360 break;
3361
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003362 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003363 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003364 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003365 break;
3366
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003367 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003368#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003369 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003370 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003371#endif
3372 break;
3373
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003374 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003375 s->logs.level = rule->arg.loglevel;
3376 break;
3377
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003378 case ACT_HTTP_REPLACE_HDR:
3379 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003380 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3381 rule->arg.hdr_add.name_len,
3382 &rule->arg.hdr_add.fmt,
3383 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003384 return HTTP_RULE_RES_BADREQ;
3385 break;
3386
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003387 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003388 ctx.idx = 0;
3389 /* remove all occurrences of the header */
3390 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3391 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3392 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003393 }
Willy Tarreau85603282015-01-21 20:39:27 +01003394 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003395
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003396 case ACT_HTTP_SET_HDR:
3397 case ACT_HTTP_ADD_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003398 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3399 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3400 trash.len = rule->arg.hdr_add.name_len;
3401 trash.str[trash.len++] = ':';
3402 trash.str[trash.len++] = ' ';
3403 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 +01003404
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003405 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003406 /* remove all occurrences of the header */
3407 ctx.idx = 0;
3408 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3409 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3410 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3411 }
3412 }
3413
Willy Tarreau96257ec2012-12-27 10:46:37 +01003414 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3415 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003416
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003417 case ACT_HTTP_DEL_ACL:
3418 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003419 struct pat_ref *ref;
3420 char *key;
3421 int len;
3422
3423 /* collect reference */
3424 ref = pat_ref_lookup(rule->arg.map.ref);
3425 if (!ref)
3426 continue;
3427
3428 /* collect key */
3429 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3430 key = trash.str;
3431 key[len] = '\0';
3432
3433 /* perform update */
3434 /* returned code: 1=ok, 0=ko */
3435 pat_ref_delete(ref, key);
3436
3437 break;
3438 }
3439
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003440 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003441 struct pat_ref *ref;
3442 char *key;
3443 struct chunk *trash_key;
3444 int len;
3445
3446 trash_key = get_trash_chunk();
3447
3448 /* collect reference */
3449 ref = pat_ref_lookup(rule->arg.map.ref);
3450 if (!ref)
3451 continue;
3452
3453 /* collect key */
3454 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3455 key = trash_key->str;
3456 key[len] = '\0';
3457
3458 /* perform update */
3459 /* add entry only if it does not already exist */
3460 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003461 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003462
3463 break;
3464 }
3465
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003466 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003467 struct pat_ref *ref;
3468 char *key, *value;
3469 struct chunk *trash_key, *trash_value;
3470 int len;
3471
3472 trash_key = get_trash_chunk();
3473 trash_value = get_trash_chunk();
3474
3475 /* collect reference */
3476 ref = pat_ref_lookup(rule->arg.map.ref);
3477 if (!ref)
3478 continue;
3479
3480 /* collect key */
3481 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3482 key = trash_key->str;
3483 key[len] = '\0';
3484
3485 /* collect value */
3486 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3487 value = trash_value->str;
3488 value[len] = '\0';
3489
3490 /* perform update */
3491 if (pat_ref_find_elt(ref, key) != NULL)
3492 /* update entry if it exists */
3493 pat_ref_set(ref, key, value, NULL);
3494 else
3495 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003496 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003497
3498 break;
3499 }
William Lallemand73025dd2014-04-24 14:38:37 +02003500
Thierry FOURNIER42148732015-09-02 17:17:33 +02003501 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003502 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3503 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003504
Willy Tarreauacc98002015-09-27 23:34:39 +02003505 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003506 case ACT_RET_ERR:
3507 case ACT_RET_CONT:
3508 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003509 case ACT_RET_STOP:
3510 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003511 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003512 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003513 return HTTP_RULE_RES_YIELD;
3514 }
William Lallemand73025dd2014-04-24 14:38:37 +02003515 break;
3516
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003517 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02003518 /* Note: only the first valid tracking parameter of each
3519 * applies.
3520 */
3521
3522 if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) {
3523 struct stktable *t;
3524 struct stksess *ts;
3525 struct stktable_key *key;
3526 void *ptr;
3527
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02003528 t = rule->arg.trk_ctr.table.t;
3529 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 +02003530
3531 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreau87b09662015-04-03 00:22:06 +02003532 stream_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003533
3534 /* let's count a new HTTP request as it's the first time we do it */
3535 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3536 if (ptr)
3537 stktable_data_cast(ptr, http_req_cnt)++;
3538
3539 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3540 if (ptr)
3541 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3542 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3543
3544 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003545 if (sess->fe != s->be)
Willy Tarreau09448f72014-06-25 18:12:15 +02003546 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3547 }
3548 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003549 break;
3550
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003551 case ACT_HTTP_REQ_SET_SRC:
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003552 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
3553 struct sample *smp;
3554
Thierry FOURNIERa002dc92015-07-31 08:50:51 +02003555 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 +02003556
3557 if (smp) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003558 if (smp->data.type == SMP_T_IPV4) {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003559 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_family = AF_INET;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003560 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003561 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003562 } else if (smp->data.type == SMP_T_IPV6) {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003563 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_family = AF_INET6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003564 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 +02003565 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = 0;
3566 }
3567 }
3568 }
3569 break;
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003570
3571 /* other flags exists, but normaly, they never be matched. */
3572 default:
3573 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003574 }
3575 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003576
3577 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003578 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003579}
3580
Willy Tarreau71241ab2012-12-27 11:30:54 +01003581
Willy Tarreau51d861a2015-05-22 17:30:48 +02003582/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3583 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3584 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3585 * is returned, the process can continue the evaluation of next rule list. If
3586 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3587 * is returned, it means the operation could not be processed and a server error
3588 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3589 * deny rule. If *YIELD is returned, the caller must call again the function
3590 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003591 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003592static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003593http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003594{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003595 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003596 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003597 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003598 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003599 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02003600 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003601
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003602 /* If "the current_rule_list" match the executed rule list, we are in
3603 * resume condition. If a resume is needed it is always in the action
3604 * and never in the ACL or converters. In this case, we initialise the
3605 * current rule, and go to the action execution point.
3606 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003607 if (s->current_rule) {
3608 rule = s->current_rule;
3609 s->current_rule = NULL;
3610 if (s->current_rule_list == rules)
3611 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003612 }
3613 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003614
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003615 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003616
3617 /* check optional condition */
3618 if (rule->cond) {
3619 int ret;
3620
Willy Tarreau192252e2015-04-04 01:47:55 +02003621 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003622 ret = acl_pass(ret);
3623
3624 if (rule->cond->pol == ACL_COND_UNLESS)
3625 ret = !ret;
3626
3627 if (!ret) /* condition not matched */
3628 continue;
3629 }
3630
Willy Tarreauacc98002015-09-27 23:34:39 +02003631 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003632resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003633 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003634 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003635 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003636
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003637 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003638 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003639 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003640
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003641 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003642 s->task->nice = rule->arg.nice;
3643 break;
3644
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003645 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003646 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003647 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003648 break;
3649
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003650 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003651#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003652 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003653 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003654#endif
3655 break;
3656
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003657 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003658 s->logs.level = rule->arg.loglevel;
3659 break;
3660
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003661 case ACT_HTTP_REPLACE_HDR:
3662 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003663 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3664 rule->arg.hdr_add.name_len,
3665 &rule->arg.hdr_add.fmt,
3666 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003667 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003668 break;
3669
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003670 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003671 ctx.idx = 0;
3672 /* remove all occurrences of the header */
3673 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3674 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3675 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3676 }
Willy Tarreau85603282015-01-21 20:39:27 +01003677 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003678
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003679 case ACT_HTTP_SET_HDR:
3680 case ACT_HTTP_ADD_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003681 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3682 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3683 trash.len = rule->arg.hdr_add.name_len;
3684 trash.str[trash.len++] = ':';
3685 trash.str[trash.len++] = ' ';
3686 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 +01003687
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003688 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003689 /* remove all occurrences of the header */
3690 ctx.idx = 0;
3691 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3692 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3693 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3694 }
3695 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003696 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3697 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003698
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003699 case ACT_HTTP_DEL_ACL:
3700 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003701 struct pat_ref *ref;
3702 char *key;
3703 int len;
3704
3705 /* collect reference */
3706 ref = pat_ref_lookup(rule->arg.map.ref);
3707 if (!ref)
3708 continue;
3709
3710 /* collect key */
3711 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3712 key = trash.str;
3713 key[len] = '\0';
3714
3715 /* perform update */
3716 /* returned code: 1=ok, 0=ko */
3717 pat_ref_delete(ref, key);
3718
3719 break;
3720 }
3721
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003722 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003723 struct pat_ref *ref;
3724 char *key;
3725 struct chunk *trash_key;
3726 int len;
3727
3728 trash_key = get_trash_chunk();
3729
3730 /* collect reference */
3731 ref = pat_ref_lookup(rule->arg.map.ref);
3732 if (!ref)
3733 continue;
3734
3735 /* collect key */
3736 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3737 key = trash_key->str;
3738 key[len] = '\0';
3739
3740 /* perform update */
3741 /* check if the entry already exists */
3742 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003743 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003744
3745 break;
3746 }
3747
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003748 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003749 struct pat_ref *ref;
3750 char *key, *value;
3751 struct chunk *trash_key, *trash_value;
3752 int len;
3753
3754 trash_key = get_trash_chunk();
3755 trash_value = get_trash_chunk();
3756
3757 /* collect reference */
3758 ref = pat_ref_lookup(rule->arg.map.ref);
3759 if (!ref)
3760 continue;
3761
3762 /* collect key */
3763 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3764 key = trash_key->str;
3765 key[len] = '\0';
3766
3767 /* collect value */
3768 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3769 value = trash_value->str;
3770 value[len] = '\0';
3771
3772 /* perform update */
3773 if (pat_ref_find_elt(ref, key) != NULL)
3774 /* update entry if it exists */
3775 pat_ref_set(ref, key, value, NULL);
3776 else
3777 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003778 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003779
3780 break;
3781 }
William Lallemand73025dd2014-04-24 14:38:37 +02003782
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003783 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003784 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3785 return HTTP_RULE_RES_BADREQ;
3786 return HTTP_RULE_RES_DONE;
3787
Thierry FOURNIER42148732015-09-02 17:17:33 +02003788 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003789 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3790 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003791
Willy Tarreauacc98002015-09-27 23:34:39 +02003792 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003793 case ACT_RET_ERR:
3794 case ACT_RET_CONT:
3795 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003796 case ACT_RET_STOP:
3797 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003798 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003799 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003800 return HTTP_RULE_RES_YIELD;
3801 }
William Lallemand73025dd2014-04-24 14:38:37 +02003802 break;
3803
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003804 /* other flags exists, but normaly, they never be matched. */
3805 default:
3806 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003807 }
3808 }
3809
3810 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003811 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003812}
3813
3814
Willy Tarreau71241ab2012-12-27 11:30:54 +01003815/* Perform an HTTP redirect based on the information in <rule>. The function
3816 * returns non-zero on success, or zero in case of a, irrecoverable error such
3817 * as too large a request to build a valid response.
3818 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003819static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003820{
Willy Tarreaub329a312015-05-22 16:27:37 +02003821 struct http_msg *req = &txn->req;
3822 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003823 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003824 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003825
3826 /* build redirect message */
3827 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003828 case 308:
3829 msg_fmt = HTTP_308;
3830 break;
3831 case 307:
3832 msg_fmt = HTTP_307;
3833 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003834 case 303:
3835 msg_fmt = HTTP_303;
3836 break;
3837 case 301:
3838 msg_fmt = HTTP_301;
3839 break;
3840 case 302:
3841 default:
3842 msg_fmt = HTTP_302;
3843 break;
3844 }
3845
3846 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3847 return 0;
3848
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003849 location = trash.str + trash.len;
3850
Willy Tarreau71241ab2012-12-27 11:30:54 +01003851 switch(rule->type) {
3852 case REDIRECT_TYPE_SCHEME: {
3853 const char *path;
3854 const char *host;
3855 struct hdr_ctx ctx;
3856 int pathlen;
3857 int hostlen;
3858
3859 host = "";
3860 hostlen = 0;
3861 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02003862 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003863 host = ctx.line + ctx.val;
3864 hostlen = ctx.vlen;
3865 }
3866
3867 path = http_get_path(txn);
3868 /* build message using path */
3869 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003870 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003871 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3872 int qs = 0;
3873 while (qs < pathlen) {
3874 if (path[qs] == '?') {
3875 pathlen = qs;
3876 break;
3877 }
3878 qs++;
3879 }
3880 }
3881 } else {
3882 path = "/";
3883 pathlen = 1;
3884 }
3885
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003886 if (rule->rdr_str) { /* this is an old "redirect" rule */
3887 /* check if we can add scheme + "://" + host + path */
3888 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3889 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003890
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003891 /* add scheme */
3892 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3893 trash.len += rule->rdr_len;
3894 }
3895 else {
3896 /* add scheme with executing log format */
3897 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003898
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003899 /* check if we can add scheme + "://" + host + path */
3900 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3901 return 0;
3902 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003903 /* add "://" */
3904 memcpy(trash.str + trash.len, "://", 3);
3905 trash.len += 3;
3906
3907 /* add host */
3908 memcpy(trash.str + trash.len, host, hostlen);
3909 trash.len += hostlen;
3910
3911 /* add path */
3912 memcpy(trash.str + trash.len, path, pathlen);
3913 trash.len += pathlen;
3914
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003915 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003916 if (trash.len && trash.str[trash.len - 1] != '/' &&
3917 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3918 if (trash.len > trash.size - 5)
3919 return 0;
3920 trash.str[trash.len] = '/';
3921 trash.len++;
3922 }
3923
3924 break;
3925 }
3926 case REDIRECT_TYPE_PREFIX: {
3927 const char *path;
3928 int pathlen;
3929
3930 path = http_get_path(txn);
3931 /* build message using path */
3932 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003933 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003934 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3935 int qs = 0;
3936 while (qs < pathlen) {
3937 if (path[qs] == '?') {
3938 pathlen = qs;
3939 break;
3940 }
3941 qs++;
3942 }
3943 }
3944 } else {
3945 path = "/";
3946 pathlen = 1;
3947 }
3948
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003949 if (rule->rdr_str) { /* this is an old "redirect" rule */
3950 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3951 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003952
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003953 /* add prefix. Note that if prefix == "/", we don't want to
3954 * add anything, otherwise it makes it hard for the user to
3955 * configure a self-redirection.
3956 */
3957 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3958 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3959 trash.len += rule->rdr_len;
3960 }
3961 }
3962 else {
3963 /* add prefix with executing log format */
3964 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3965
3966 /* Check length */
3967 if (trash.len + pathlen > trash.size - 4)
3968 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003969 }
3970
3971 /* add path */
3972 memcpy(trash.str + trash.len, path, pathlen);
3973 trash.len += pathlen;
3974
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003975 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003976 if (trash.len && trash.str[trash.len - 1] != '/' &&
3977 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3978 if (trash.len > trash.size - 5)
3979 return 0;
3980 trash.str[trash.len] = '/';
3981 trash.len++;
3982 }
3983
3984 break;
3985 }
3986 case REDIRECT_TYPE_LOCATION:
3987 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003988 if (rule->rdr_str) { /* this is an old "redirect" rule */
3989 if (trash.len + rule->rdr_len > trash.size - 4)
3990 return 0;
3991
3992 /* add location */
3993 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3994 trash.len += rule->rdr_len;
3995 }
3996 else {
3997 /* add location with executing log format */
3998 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003999
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004000 /* Check left length */
4001 if (trash.len > trash.size - 4)
4002 return 0;
4003 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004004 break;
4005 }
4006
4007 if (rule->cookie_len) {
4008 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
4009 trash.len += 14;
4010 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4011 trash.len += rule->cookie_len;
4012 memcpy(trash.str + trash.len, "\r\n", 2);
4013 trash.len += 2;
4014 }
4015
4016 /* add end of headers and the keep-alive/close status.
4017 * We may choose to set keep-alive if the Location begins
4018 * with a slash, because the client will come back to the
4019 * same server.
4020 */
4021 txn->status = rule->code;
4022 /* let's log the request time */
4023 s->logs.tv_request = now;
4024
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004025 if (*location == '/' &&
Willy Tarreaub329a312015-05-22 16:27:37 +02004026 (req->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau2de8a502015-05-28 17:23:54 +02004027 ((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004028 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4029 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4030 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02004031 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004032 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4033 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4034 trash.len += 30;
4035 } else {
4036 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4037 trash.len += 24;
4038 }
4039 }
4040 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4041 trash.len += 4;
Christopher Faulet3e344292015-11-24 16:24:13 +01004042 FLT_STRM_CB(s, flt_http_reply(s, txn->status, &trash));
Willy Tarreaub329a312015-05-22 16:27:37 +02004043 bo_inject(res->chn, trash.str, trash.len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004044 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02004045 bi_fast_delete(req->chn->buf, req->sov);
4046 req->next -= req->sov;
4047 req->sov = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004048 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_FLT_END);
4049 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->req.analysers & AN_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02004050 req->msg_state = HTTP_MSG_CLOSED;
4051 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02004052 /* Trim any possible response */
4053 res->chn->buf->i = 0;
4054 res->next = res->sov = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004055 } else {
4056 /* keep-alive not possible */
4057 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4058 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4059 trash.len += 29;
4060 } else {
4061 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4062 trash.len += 23;
4063 }
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004064 http_reply_and_close(s, txn->status, &trash);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004065 req->chn->analysers &= AN_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004066 }
4067
Willy Tarreaue7dff022015-04-03 01:14:29 +02004068 if (!(s->flags & SF_ERR_MASK))
4069 s->flags |= SF_ERR_LOCAL;
4070 if (!(s->flags & SF_FINST_MASK))
4071 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004072
4073 return 1;
4074}
4075
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004076/* This stream analyser runs all HTTP request processing which is common to
4077 * frontends and backends, which means blocking ACLs, filters, connection-close,
4078 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004079 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004080 * either needs more data or wants to immediately abort the request (eg: deny,
4081 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004082 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004083int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004084{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004085 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004086 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004087 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004088 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004089 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004090 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02004091
Willy Tarreau655dce92009-11-08 13:10:58 +01004092 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004093 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004094 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004095 }
4096
Willy Tarreau87b09662015-04-03 00:22:06 +02004097 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 +02004098 now_ms, __FUNCTION__,
4099 s,
4100 req,
4101 req->rex, req->wex,
4102 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004103 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004104 req->analysers);
4105
Willy Tarreau65410832014-04-28 21:25:43 +02004106 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004107 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004108
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004109 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004110 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004111 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Willy Tarreau51425942010-02-01 10:40:19 +01004112
Willy Tarreau0b748332014-04-29 00:13:29 +02004113 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004114 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4115 goto return_prx_yield;
4116
Willy Tarreau0b748332014-04-29 00:13:29 +02004117 case HTTP_RULE_RES_CONT:
4118 case HTTP_RULE_RES_STOP: /* nothing to do */
4119 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004120
Willy Tarreau0b748332014-04-29 00:13:29 +02004121 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4122 if (txn->flags & TX_CLTARPIT)
4123 goto tarpit;
4124 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004125
Willy Tarreau0b748332014-04-29 00:13:29 +02004126 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4127 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004128
Willy Tarreau0b748332014-04-29 00:13:29 +02004129 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004130 goto done;
4131
Willy Tarreau0b748332014-04-29 00:13:29 +02004132 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4133 goto return_bad_req;
4134 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004135 }
4136
Willy Tarreau52542592014-04-28 18:33:26 +02004137 /* OK at this stage, we know that the request was accepted according to
4138 * the http-request rules, we can check for the stats. Note that the
4139 * URI is detected *before* the req* rules in order not to be affected
4140 * by a possible reqrep, while they are processed *after* so that a
4141 * reqdeny can still block them. This clearly needs to change in 1.6!
4142 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004143 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004144 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004145 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004146 txn->status = 500;
4147 s->logs.tv_request = now;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004148 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004149
Willy Tarreaue7dff022015-04-03 01:14:29 +02004150 if (!(s->flags & SF_ERR_MASK))
4151 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004152 goto return_prx_cond;
4153 }
4154
4155 /* parse the whole stats request and extract the relevant information */
4156 http_handle_stats(s, req);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004157 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Willy Tarreau0b748332014-04-29 00:13:29 +02004158 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004159
Willy Tarreau0b748332014-04-29 00:13:29 +02004160 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4161 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004162
Willy Tarreau0b748332014-04-29 00:13:29 +02004163 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4164 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004165 }
4166
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004167 /* evaluate the req* rules except reqadd */
4168 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004169 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004170 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004171
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004172 if (txn->flags & TX_CLDENY)
4173 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004174
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004175 if (txn->flags & TX_CLTARPIT)
4176 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004177 }
Willy Tarreau06619262006-12-17 08:37:22 +01004178
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004179 /* add request headers from the rule sets in the same order */
4180 list_for_each_entry(wl, &px->req_add, list) {
4181 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004182 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004183 ret = acl_pass(ret);
4184 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4185 ret = !ret;
4186 if (!ret)
4187 continue;
4188 }
4189
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004190 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004191 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004192 }
4193
Willy Tarreau52542592014-04-28 18:33:26 +02004194
4195 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004196 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004197 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004198 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4199 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004200
Willy Tarreaue7dff022015-04-03 01:14:29 +02004201 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4202 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4203 if (!(s->flags & SF_FINST_MASK))
4204 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004205
Willy Tarreau70730dd2014-04-24 18:06:27 +02004206 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet309c6412015-12-02 09:57:32 +01004207 req->analysers &= (AN_REQ_HTTP_BODY | AN_FLT_HTTP_HDRS | AN_FLT_END);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004208 req->analysers &= ~AN_FLT_XFER_DATA;
4209 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004210 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004211 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004212
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004213 /* check whether we have some ACLs set to redirect this request */
4214 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004215 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004216 int ret;
4217
Willy Tarreau192252e2015-04-04 01:47:55 +02004218 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004219 ret = acl_pass(ret);
4220 if (rule->cond->pol == ACL_COND_UNLESS)
4221 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004222 if (!ret)
4223 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004224 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004225 if (!http_apply_redirect_rule(rule, s, txn))
4226 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004227 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004228 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004229
Willy Tarreau2be39392010-01-03 17:24:51 +01004230 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4231 * If this happens, then the data will not come immediately, so we must
4232 * send all what we have without waiting. Note that due to the small gain
4233 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004234 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004235 * itself once used.
4236 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004237 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004238
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004239 done: /* done with this analyser, continue with next ones that the calling
4240 * points will have set, if any.
4241 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004242 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004243 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4244 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004245 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004246
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004247 tarpit:
4248 /* When a connection is tarpitted, we use the tarpit timeout,
4249 * which may be the same as the connect timeout if unspecified.
4250 * If unset, then set it to zero because we really want it to
4251 * eventually expire. We build the tarpit as an analyser.
4252 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004253 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004254
4255 /* wipe the request out so that we can drop the connection early
4256 * if the client closes first.
4257 */
4258 channel_dont_connect(req);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004259 req->analysers &= AN_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004260 req->analysers |= AN_REQ_HTTP_TARPIT;
4261 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4262 if (!req->analyse_exp)
4263 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004264 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004265 sess->fe->fe_counters.denied_req++;
4266 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004267 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004268 if (sess->listener->counters)
4269 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004270 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004271
4272 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004273 txn->flags |= TX_CLDENY;
CJ Ess108b1dd2015-04-07 12:03:37 -04004274 txn->status = http_err_codes[txn->rule_deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004275 s->logs.tv_request = now;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004276 http_reply_and_close(s, txn->status, http_error_message(s, txn->rule_deny_status));
Willy Tarreau87b09662015-04-03 00:22:06 +02004277 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004278 sess->fe->fe_counters.denied_req++;
4279 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004280 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004281 if (sess->listener->counters)
4282 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004283 goto return_prx_cond;
4284
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004285 return_bad_req:
4286 /* We centralize bad requests processing here */
4287 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4288 /* we detected a parsing error. We want to archive this request
4289 * in the dedicated proxy area for later troubleshooting.
4290 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004291 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004292 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004293
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004294 txn->req.msg_state = HTTP_MSG_ERROR;
4295 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004296 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004297
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004298 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004299 if (sess->listener->counters)
4300 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004301
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004302 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004303 if (!(s->flags & SF_ERR_MASK))
4304 s->flags |= SF_ERR_PRXCOND;
4305 if (!(s->flags & SF_FINST_MASK))
4306 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004307
Christopher Fauletd7c91962015-04-30 11:48:27 +02004308 req->analysers &= AN_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004309 req->analyse_exp = TICK_ETERNITY;
4310 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004311
4312 return_prx_yield:
4313 channel_dont_connect(req);
4314 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004315}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004316
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004317/* This function performs all the processing enabled for the current request.
4318 * It returns 1 if the processing can continue on next analysers, or zero if it
4319 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004320 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004321 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004322int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004323{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004324 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004325 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004326 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004327 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004328
Willy Tarreau655dce92009-11-08 13:10:58 +01004329 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004330 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004331 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004332 return 0;
4333 }
4334
Willy Tarreau87b09662015-04-03 00:22:06 +02004335 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 +02004336 now_ms, __FUNCTION__,
4337 s,
4338 req,
4339 req->rex, req->wex,
4340 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004341 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004342 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004343
Willy Tarreau59234e92008-11-30 23:51:27 +01004344 /*
4345 * Right now, we know that we have processed the entire headers
4346 * and that unwanted requests have been filtered out. We can do
4347 * whatever we want with the remaining request. Also, now we
4348 * may have separate values for ->fe, ->be.
4349 */
Willy Tarreau06619262006-12-17 08:37:22 +01004350
Willy Tarreau59234e92008-11-30 23:51:27 +01004351 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004352 * If HTTP PROXY is set we simply get remote server address parsing
4353 * incoming request. Note that this requires that a connection is
4354 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004355 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004356 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004357 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004358 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004359
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004360 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau973a5422015-08-05 21:47:23 +02004361 if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004362 txn->req.msg_state = HTTP_MSG_ERROR;
4363 txn->status = 500;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004364 req->analysers &= AN_FLT_END;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004365 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004366
Willy Tarreaue7dff022015-04-03 01:14:29 +02004367 if (!(s->flags & SF_ERR_MASK))
4368 s->flags |= SF_ERR_RESOURCE;
4369 if (!(s->flags & SF_FINST_MASK))
4370 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004371
4372 return 0;
4373 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004374
4375 path = http_get_path(txn);
4376 url2sa(req->buf->p + msg->sl.rq.u,
4377 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004378 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004379 /* if the path was found, we have to remove everything between
4380 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4381 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4382 * u_l characters by a single "/".
4383 */
4384 if (path) {
4385 char *cur_ptr = req->buf->p;
4386 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4387 int delta;
4388
4389 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4390 http_msg_move_end(&txn->req, delta);
4391 cur_end += delta;
4392 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4393 goto return_bad_req;
4394 }
4395 else {
4396 char *cur_ptr = req->buf->p;
4397 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4398 int delta;
4399
4400 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4401 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4402 http_msg_move_end(&txn->req, delta);
4403 cur_end += delta;
4404 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4405 goto return_bad_req;
4406 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004407 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004408
Willy Tarreau59234e92008-11-30 23:51:27 +01004409 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004410 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004411 * Note that doing so might move headers in the request, but
4412 * the fields will stay coherent and the URI will not move.
4413 * This should only be performed in the backend.
4414 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02004415 if ((s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004416 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4417 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004418
William Lallemanda73203e2012-03-12 12:48:57 +01004419 /* add unique-id if "header-unique-id" is specified */
4420
Thierry Fournierf4011dd2016-03-29 17:23:51 +02004421 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004422 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4423 goto return_bad_req;
4424 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004425 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004426 }
William Lallemanda73203e2012-03-12 12:48:57 +01004427
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004428 if (sess->fe->header_unique_id && s->unique_id) {
4429 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004430 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004431 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004432 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004433 goto return_bad_req;
4434 }
4435
Cyril Bontéb21570a2009-11-29 20:04:48 +01004436 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004437 * 9: add X-Forwarded-For if either the frontend or the backend
4438 * asks for it.
4439 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004440 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004441 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004442 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4443 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4444 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004445 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004446 /* The header is set to be added only if none is present
4447 * and we found it, so don't do anything.
4448 */
4449 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004450 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004451 /* Add an X-Forwarded-For header unless the source IP is
4452 * in the 'except' network range.
4453 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004454 if ((!sess->fe->except_mask.s_addr ||
4455 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4456 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004457 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004458 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004459 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004460 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004461 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004462 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004463
4464 /* Note: we rely on the backend to get the header name to be used for
4465 * x-forwarded-for, because the header is really meant for the backends.
4466 * However, if the backend did not specify any option, we have to rely
4467 * on the frontend's header name.
4468 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004469 if (s->be->fwdfor_hdr_len) {
4470 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004471 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004472 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004473 len = sess->fe->fwdfor_hdr_len;
4474 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004475 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004476 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 +01004477
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004478 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004479 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004480 }
4481 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004482 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004483 /* FIXME: for the sake of completeness, we should also support
4484 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004485 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004486 int len;
4487 char pn[INET6_ADDRSTRLEN];
4488 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004489 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004490 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004491
Willy Tarreau59234e92008-11-30 23:51:27 +01004492 /* Note: we rely on the backend to get the header name to be used for
4493 * x-forwarded-for, because the header is really meant for the backends.
4494 * However, if the backend did not specify any option, we have to rely
4495 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004496 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004497 if (s->be->fwdfor_hdr_len) {
4498 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004499 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004500 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004501 len = sess->fe->fwdfor_hdr_len;
4502 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004503 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004504 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004505
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004506 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004507 goto return_bad_req;
4508 }
4509 }
4510
4511 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004512 * 10: add X-Original-To if either the frontend or the backend
4513 * asks for it.
4514 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004515 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004516
4517 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004518 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004519 /* Add an X-Original-To header unless the destination IP is
4520 * in the 'except' network range.
4521 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004522 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004523
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004524 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004525 ((!sess->fe->except_mask_to.s_addr ||
4526 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4527 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004528 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004529 (((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 +02004530 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004531 int len;
4532 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004533 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004534
4535 /* Note: we rely on the backend to get the header name to be used for
4536 * x-original-to, because the header is really meant for the backends.
4537 * However, if the backend did not specify any option, we have to rely
4538 * on the frontend's header name.
4539 */
4540 if (s->be->orgto_hdr_len) {
4541 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004542 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004543 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004544 len = sess->fe->orgto_hdr_len;
4545 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004546 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004547 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 +02004548
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004549 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004550 goto return_bad_req;
4551 }
4552 }
4553 }
4554
Willy Tarreau50fc7772012-11-11 22:19:57 +01004555 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4556 * If an "Upgrade" token is found, the header is left untouched in order not to have
4557 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4558 * "Upgrade" is present in the Connection header.
4559 */
4560 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4561 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004562 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004563 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004564 unsigned int want_flags = 0;
4565
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004566 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004567 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004568 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004569 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004570 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004571 want_flags |= TX_CON_CLO_SET;
4572 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004573 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004574 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004575 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004576 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004577 want_flags |= TX_CON_KAL_SET;
4578 }
4579
4580 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004581 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004582 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004583
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004584
Willy Tarreau522d6c02009-12-06 18:49:18 +01004585 /* If we have no server assigned yet and we're balancing on url_param
4586 * with a POST request, we may be interested in checking the body for
4587 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004588 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004589 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004590 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004591 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004592 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004593 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004594 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004595
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004596 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02004597 req->analysers &= ~AN_FLT_XFER_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004598 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004599#ifdef TCP_QUICKACK
4600 /* We expect some data from the client. Unless we know for sure
4601 * we already have a full request, we have to re-enable quick-ack
4602 * in case we previously disabled it, otherwise we might cause
4603 * the client to delay further data.
4604 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004605 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004606 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004607 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004608 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004609 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004610#endif
4611 }
Willy Tarreau03945942009-12-22 16:50:27 +01004612
Willy Tarreau59234e92008-11-30 23:51:27 +01004613 /*************************************************************
4614 * OK, that's finished for the headers. We have done what we *
4615 * could. Let's switch to the DATA state. *
4616 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004617 req->analyse_exp = TICK_ETERNITY;
4618 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004619
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004620 /* if the server closes the connection, we want to immediately react
4621 * and close the socket to save packets and syscalls.
4622 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004623 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004624 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004625
Willy Tarreau59234e92008-11-30 23:51:27 +01004626 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004627 /* OK let's go on with the BODY now */
4628 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004629
Willy Tarreau59234e92008-11-30 23:51:27 +01004630 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004631 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004632 /* we detected a parsing error. We want to archive this request
4633 * in the dedicated proxy area for later troubleshooting.
4634 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004635 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004636 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004637
Willy Tarreau59234e92008-11-30 23:51:27 +01004638 txn->req.msg_state = HTTP_MSG_ERROR;
4639 txn->status = 400;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004640 req->analysers &= AN_FLT_END;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004641 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004642
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004643 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004644 if (sess->listener->counters)
4645 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004646
Willy Tarreaue7dff022015-04-03 01:14:29 +02004647 if (!(s->flags & SF_ERR_MASK))
4648 s->flags |= SF_ERR_PRXCOND;
4649 if (!(s->flags & SF_FINST_MASK))
4650 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004651 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004652}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004653
Willy Tarreau60b85b02008-11-30 23:28:40 +01004654/* This function is an analyser which processes the HTTP tarpit. It always
4655 * returns zero, at the beginning because it prevents any other processing
4656 * from occurring, and at the end because it terminates the request.
4657 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004658int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004659{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004660 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004661
4662 /* This connection is being tarpitted. The CLIENT side has
4663 * already set the connect expiration date to the right
4664 * timeout. We just have to check that the client is still
4665 * there and that the timeout has not expired.
4666 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004667 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004668 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004669 !tick_is_expired(req->analyse_exp, now_ms))
4670 return 0;
4671
4672 /* We will set the queue timer to the time spent, just for
4673 * logging purposes. We fake a 500 server error, so that the
4674 * attacker will not suspect his connection has been tarpitted.
4675 * It will not cause trouble to the logs because we can exclude
4676 * the tarpitted connections by filtering on the 'PT' status flags.
4677 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004678 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4679
4680 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004681 if (!(req->flags & CF_READ_ERROR))
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004682 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004683
Christopher Fauletd7c91962015-04-30 11:48:27 +02004684 req->analysers &= AN_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004685 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004686
Willy Tarreaue7dff022015-04-03 01:14:29 +02004687 if (!(s->flags & SF_ERR_MASK))
4688 s->flags |= SF_ERR_PRXCOND;
4689 if (!(s->flags & SF_FINST_MASK))
4690 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004691 return 0;
4692}
4693
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004694/* This function is an analyser which waits for the HTTP request body. It waits
4695 * for either the buffer to be full, or the full advertised contents to have
4696 * reached the buffer. It must only be called after the standard HTTP request
4697 * processing has occurred, because it expects the request to be parsed and will
4698 * look for the Expect header. It may send a 100-Continue interim response. It
4699 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4700 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4701 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004702 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004703int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004704{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004705 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004706 struct http_txn *txn = s->txn;
4707 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004708
4709 /* We have to parse the HTTP request body to find any required data.
4710 * "balance url_param check_post" should have been the only way to get
4711 * into this. We were brought here after HTTP header analysis, so all
4712 * related structures are ready.
4713 */
4714
Willy Tarreau890988f2014-04-10 11:59:33 +02004715 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4716 /* This is the first call */
4717 if (msg->msg_state < HTTP_MSG_BODY)
4718 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004719
Willy Tarreau890988f2014-04-10 11:59:33 +02004720 if (msg->msg_state < HTTP_MSG_100_SENT) {
4721 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4722 * send an HTTP/1.1 100 Continue intermediate response.
4723 */
4724 if (msg->flags & HTTP_MSGF_VER_11) {
4725 struct hdr_ctx ctx;
4726 ctx.idx = 0;
4727 /* Expect is allowed in 1.1, look for it */
4728 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4729 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004730 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004731 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004732 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004733 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004734 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004735
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004736 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004737 * req->buf->p still points to the beginning of the message. We
4738 * must save the body in msg->next because it survives buffer
4739 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004740 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004741 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004742
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004743 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004744 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4745 else
4746 msg->msg_state = HTTP_MSG_DATA;
4747 }
4748
Willy Tarreau890988f2014-04-10 11:59:33 +02004749 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4750 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004751 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004752 goto missing_data;
4753
4754 /* OK we have everything we need now */
4755 goto http_end;
4756 }
4757
4758 /* OK here we're parsing a chunked-encoded message */
4759
Willy Tarreau522d6c02009-12-06 18:49:18 +01004760 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004761 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004762 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004763 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004764 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004765 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004766
Willy Tarreau115acb92009-12-26 13:56:06 +01004767 if (!ret)
4768 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004769 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004770 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004771 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004772 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01004773 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004774 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01004775 }
4776
Willy Tarreaud98cf932009-12-27 22:54:55 +01004777 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004778 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4779 * for at least a whole chunk or the whole content length bytes after
4780 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004781 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004782 if (msg->msg_state == HTTP_MSG_TRAILERS)
4783 goto http_end;
4784
Willy Tarreaue115b492015-05-01 23:05:14 +02004785 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004786 goto http_end;
4787
4788 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004789 /* we get here if we need to wait for more data. If the buffer is full,
4790 * we have the maximum we can expect.
4791 */
4792 if (buffer_full(req->buf, global.tune.maxrewrite))
4793 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004794
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004795 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004796 txn->status = 408;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004797 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004798
Willy Tarreaue7dff022015-04-03 01:14:29 +02004799 if (!(s->flags & SF_ERR_MASK))
4800 s->flags |= SF_ERR_CLITO;
4801 if (!(s->flags & SF_FINST_MASK))
4802 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004803 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004804 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004805
4806 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004807 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004808 /* Not enough data. We'll re-use the http-request
4809 * timeout here. Ideally, we should set the timeout
4810 * relative to the accept() date. We just set the
4811 * request timeout once at the beginning of the
4812 * request.
4813 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004814 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004815 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004816 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004817 return 0;
4818 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004819
4820 http_end:
4821 /* The situation will not evolve, so let's give up on the analysis. */
4822 s->logs.tv_request = now; /* update the request timer to reflect full request */
4823 req->analysers &= ~an_bit;
4824 req->analyse_exp = TICK_ETERNITY;
4825 return 1;
4826
4827 return_bad_req: /* let's centralize all bad requests */
4828 txn->req.msg_state = HTTP_MSG_ERROR;
4829 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004830 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004831
Willy Tarreaue7dff022015-04-03 01:14:29 +02004832 if (!(s->flags & SF_ERR_MASK))
4833 s->flags |= SF_ERR_PRXCOND;
4834 if (!(s->flags & SF_FINST_MASK))
4835 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004836
Willy Tarreau522d6c02009-12-06 18:49:18 +01004837 return_err_msg:
Christopher Fauletd7c91962015-04-30 11:48:27 +02004838 req->analysers &= AN_FLT_END;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004839 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004840 if (sess->listener->counters)
4841 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004842 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004843}
4844
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004845/* send a server's name with an outgoing request over an established connection.
4846 * Note: this function is designed to be called once the request has been scheduled
4847 * for being forwarded. This is the reason why it rewinds the buffer before
4848 * proceeding.
4849 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004850int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004851
4852 struct hdr_ctx ctx;
4853
Mark Lamourinec2247f02012-01-04 13:02:01 -05004854 char *hdr_name = be->server_id_hdr_name;
4855 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004856 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004857 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004858 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004859
William Lallemandd9e90662012-01-30 17:27:17 +01004860 ctx.idx = 0;
4861
Willy Tarreau211cdec2014-04-17 20:18:08 +02004862 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004863 if (old_o) {
4864 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004865 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004866 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004867 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004868 }
4869
Willy Tarreau9b28e032012-10-12 23:49:43 +02004870 old_i = chn->buf->i;
4871 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 -05004872 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004873 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004874 }
4875
4876 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004877 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004878 memcpy(hdr_val, hdr_name, hdr_name_len);
4879 hdr_val += hdr_name_len;
4880 *hdr_val++ = ':';
4881 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004882 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4883 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004884
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004885 if (old_o) {
4886 /* If this was a forwarded request, we must readjust the amount of
4887 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004888 * variations. Note that the current state is >= HTTP_MSG_BODY,
4889 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004890 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004891 old_o += chn->buf->i - old_i;
4892 b_adv(chn->buf, old_o);
4893 txn->req.next -= old_o;
4894 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004895 }
4896
Mark Lamourinec2247f02012-01-04 13:02:01 -05004897 return 0;
4898}
4899
Willy Tarreau610ecce2010-01-04 21:15:02 +01004900/* Terminate current transaction and prepare a new one. This is very tricky
4901 * right now but it works.
4902 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004903void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004904{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004905 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02004906 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01004907 struct proxy *be = s->be;
Willy Tarreau323a2d92015-08-04 19:00:17 +02004908 struct connection *srv_conn;
4909 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02004910 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01004911
Willy Tarreau610ecce2010-01-04 21:15:02 +01004912 /* FIXME: We need a more portable way of releasing a backend's and a
4913 * server's connections. We need a safer way to reinitialize buffer
4914 * flags. We also need a more accurate method for computing per-request
4915 * data.
4916 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02004917 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004918
Willy Tarreau4213a112013-12-15 10:25:42 +01004919 /* unless we're doing keep-alive, we want to quickly close the connection
4920 * to the server.
4921 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004922 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004923 !si_conn_ready(&s->si[1])) {
4924 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4925 si_shutr(&s->si[1]);
4926 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004927 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004928
Willy Tarreaue7dff022015-04-03 01:14:29 +02004929 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau858b1032015-12-07 17:04:59 +01004930 be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004931 if (unlikely(s->srv_conn))
4932 sess_change_server(s, NULL);
4933 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004934
4935 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02004936 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004937
Willy Tarreaueee5b512015-04-03 23:46:31 +02004938 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004939 int n;
4940
Willy Tarreaueee5b512015-04-03 23:46:31 +02004941 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004942 if (n < 1 || n > 5)
4943 n = 0;
4944
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004945 if (fe->mode == PR_MODE_HTTP) {
4946 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004947 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02004948 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01004949 (be->mode == PR_MODE_HTTP)) {
4950 be->be_counters.p.http.rsp[n]++;
4951 be->be_counters.p.http.cum_req++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004952 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004953 }
4954
4955 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004956 s->logs.bytes_in -= s->req.buf->i;
4957 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004958
4959 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004960 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02004961 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004962 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004963 s->do_log(s);
4964 }
4965
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004966 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02004967 stream_stop_content_counters(s);
4968 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004969
Willy Tarreau610ecce2010-01-04 21:15:02 +01004970 s->logs.accept_date = date; /* user-visible date for logging */
4971 s->logs.tv_accept = now; /* corrected date for internal use */
4972 tv_zero(&s->logs.tv_request);
4973 s->logs.t_queue = -1;
4974 s->logs.t_connect = -1;
4975 s->logs.t_data = -1;
4976 s->logs.t_close = 0;
4977 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4978 s->logs.srv_queue_size = 0; /* we will get this number soon */
4979
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004980 s->logs.bytes_in = s->req.total = s->req.buf->i;
4981 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004982
4983 if (s->pend_pos)
4984 pendconn_free(s->pend_pos);
4985
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004986 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004987 if (s->flags & SF_CURR_SESS) {
4988 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004989 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004990 }
Willy Tarreau858b1032015-12-07 17:04:59 +01004991 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004992 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004993 }
4994
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004995 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004996
Willy Tarreau4213a112013-12-15 10:25:42 +01004997 /* only release our endpoint if we don't intend to reuse the
4998 * connection.
4999 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005000 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005001 !si_conn_ready(&s->si[1])) {
5002 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005003 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005004 }
5005
Willy Tarreau350f4872014-11-28 14:42:25 +01005006 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5007 s->si[1].err_type = SI_ET_NONE;
5008 s->si[1].conn_retries = 0; /* used for logging too */
5009 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005010 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 +01005011 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);
5012 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 +02005013 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5014 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5015 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005016
Willy Tarreaueee5b512015-04-03 23:46:31 +02005017 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005018 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005019 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005020
5021 if (prev_status == 401 || prev_status == 407) {
5022 /* In HTTP keep-alive mode, if we receive a 401, we still have
5023 * a chance of being able to send the visitor again to the same
5024 * server over the same connection. This is required by some
5025 * broken protocols such as NTLM, and anyway whenever there is
5026 * an opportunity for sending the challenge to the proper place,
5027 * it's better to do it (at least it helps with debugging).
5028 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005029 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02005030 if (srv_conn)
5031 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01005032 }
5033
Willy Tarreau53f96852016-02-02 18:50:47 +01005034 /* Never ever allow to reuse a connection from a non-reuse backend */
5035 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
5036 srv_conn->flags |= CO_FL_PRIVATE;
5037
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005038 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005039 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005040
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005041 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005042 s->req.flags |= CF_NEVER_WAIT;
5043 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005044 }
5045
Willy Tarreau610ecce2010-01-04 21:15:02 +01005046 /* if the request buffer is not empty, it means we're
5047 * about to process another request, so send pending
5048 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005049 * Just don't do this if the buffer is close to be full,
5050 * because the request will wait for it to flush a little
5051 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005052 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005053 if (s->req.buf->i) {
5054 if (s->res.buf->o &&
5055 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5056 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5057 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005058 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005059
Willy Tarreau714ea782015-11-25 20:11:11 +01005060 /* we're removing the analysers, we MUST re-enable events detection.
5061 * We don't enable close on the response channel since it's either
5062 * already closed, or in keep-alive with an idle connection handler.
5063 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005064 channel_auto_read(&s->req);
5065 channel_auto_close(&s->req);
5066 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005067
Willy Tarreau1c59bd52015-11-02 20:20:11 +01005068 /* we're in keep-alive with an idle connection, monitor it if not already done */
5069 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02005070 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005071 if (!srv)
5072 si_idle_conn(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01005073 else if (srv_conn->flags & CO_FL_PRIVATE)
Willy Tarreau8dff9982015-08-04 20:45:52 +02005074 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02005075 else if (prev_flags & TX_NOT_FIRST)
5076 /* note: we check the request, not the connection, but
5077 * this is valid for strategies SAFE and AGGR, and in
5078 * case of ALWS, we don't care anyway.
5079 */
5080 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005081 else
5082 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005083 }
Willy Tarreau27375622013-12-17 00:00:28 +01005084
Christopher Faulet3e344292015-11-24 16:24:13 +01005085 if (HAS_FILTERS(s)) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02005086 s->req.analysers &= AN_FLT_END;
5087 s->res.analysers &= AN_FLT_END;
5088 }
Christopher Faulet3e344292015-11-24 16:24:13 +01005089 else {
5090 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
5091 s->res.analysers = 0;
5092 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005093}
5094
5095
5096/* This function updates the request state machine according to the response
5097 * state machine and buffer flags. It returns 1 if it changes anything (flag
5098 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5099 * it is only used to find when a request/response couple is complete. Both
5100 * this function and its equivalent should loop until both return zero. It
5101 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5102 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005103int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005104{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005105 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005106 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005107 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005108 unsigned int old_state = txn->req.msg_state;
5109
Willy Tarreau610ecce2010-01-04 21:15:02 +01005110 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5111 return 0;
5112
5113 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005114 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005115 * We can shut the read side unless we want to abort_on_close,
5116 * or we have a POST request. The issue with POST requests is
5117 * that some browsers still send a CRLF after the request, and
5118 * this CRLF must be read so that it does not remain in the kernel
5119 * buffers, otherwise a close could cause an RST on some systems
5120 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005121 * Note that if we're using keep-alive on the client side, we'd
5122 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005123 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005124 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005125 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005126 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5127 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5128 !(s->be->options & PR_O_ABRT_CLOSE) &&
5129 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005130 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005131
Willy Tarreau40f151a2012-12-20 12:10:09 +01005132 /* if the server closes the connection, we want to immediately react
5133 * and close the socket to save packets and syscalls.
5134 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005135 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005136
Willy Tarreau7f876a12015-11-18 11:59:55 +01005137 /* In any case we've finished parsing the request so we must
5138 * disable Nagle when sending data because 1) we're not going
5139 * to shut this side, and 2) the server is waiting for us to
5140 * send pending data.
5141 */
5142 chn->flags |= CF_NEVER_WAIT;
5143
Willy Tarreau610ecce2010-01-04 21:15:02 +01005144 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5145 goto wait_other_side;
5146
5147 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5148 /* The server has not finished to respond, so we
5149 * don't want to move in order not to upset it.
5150 */
5151 goto wait_other_side;
5152 }
5153
5154 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5155 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005156 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005157 txn->req.msg_state = HTTP_MSG_TUNNEL;
5158 goto wait_other_side;
5159 }
5160
5161 /* When we get here, it means that both the request and the
5162 * response have finished receiving. Depending on the connection
5163 * mode, we'll have to wait for the last bytes to leave in either
5164 * direction, and sometimes for a close to be effective.
5165 */
5166
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005167 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5168 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005169 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5170 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005171 }
5172 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5173 /* Option forceclose is set, or either side wants to close,
5174 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005175 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005176 * once both states are CLOSED.
5177 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005178 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5179 channel_shutr_now(chn);
5180 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005181 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005182 }
5183 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005184 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5185 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005186 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005187 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5188 channel_auto_read(chn);
5189 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005190 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005191 }
5192
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005193 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005194 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005195 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005196
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005197 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005198 txn->req.msg_state = HTTP_MSG_CLOSING;
5199 goto http_msg_closing;
5200 }
5201 else {
5202 txn->req.msg_state = HTTP_MSG_CLOSED;
5203 goto http_msg_closed;
5204 }
5205 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005206 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005207 }
5208
5209 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5210 http_msg_closing:
5211 /* nothing else to forward, just waiting for the output buffer
5212 * to be empty and for the shutw_now to take effect.
5213 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005214 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005215 txn->req.msg_state = HTTP_MSG_CLOSED;
5216 goto http_msg_closed;
5217 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005218 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005219 txn->req.msg_state = HTTP_MSG_ERROR;
5220 goto wait_other_side;
5221 }
5222 }
5223
5224 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5225 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005226 /* see above in MSG_DONE why we only do this in these states */
5227 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5228 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5229 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005230 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005231 goto wait_other_side;
5232 }
5233
5234 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005235 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005236}
5237
5238
5239/* This function updates the response state machine according to the request
5240 * state machine and buffer flags. It returns 1 if it changes anything (flag
5241 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5242 * it is only used to find when a request/response couple is complete. Both
5243 * this function and its equivalent should loop until both return zero. It
5244 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5245 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005246int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005247{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005248 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005249 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005250 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005251 unsigned int old_state = txn->rsp.msg_state;
5252
Willy Tarreau610ecce2010-01-04 21:15:02 +01005253 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5254 return 0;
5255
5256 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5257 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005258 * still monitor the server connection for a possible close
5259 * while the request is being uploaded, so we don't disable
5260 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005261 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005262 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005263
5264 if (txn->req.msg_state == HTTP_MSG_ERROR)
5265 goto wait_other_side;
5266
5267 if (txn->req.msg_state < HTTP_MSG_DONE) {
5268 /* The client seems to still be sending data, probably
5269 * because we got an error response during an upload.
5270 * We have the choice of either breaking the connection
5271 * or letting it pass through. Let's do the later.
5272 */
5273 goto wait_other_side;
5274 }
5275
5276 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5277 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005278 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005279 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005280 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005281 goto wait_other_side;
5282 }
5283
5284 /* When we get here, it means that both the request and the
5285 * response have finished receiving. Depending on the connection
5286 * mode, we'll have to wait for the last bytes to leave in either
5287 * direction, and sometimes for a close to be effective.
5288 */
5289
5290 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5291 /* Server-close mode : shut read and wait for the request
5292 * side to close its output buffer. The caller will detect
5293 * when we're in DONE and the other is in CLOSED and will
5294 * catch that for the final cleanup.
5295 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005296 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5297 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005298 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005299 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5300 /* Option forceclose is set, or either side wants to close,
5301 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005302 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005303 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005304 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005305 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5306 channel_shutr_now(chn);
5307 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005308 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005309 }
5310 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005311 /* The last possible modes are keep-alive and tunnel. Tunnel will
5312 * need to forward remaining data. Keep-alive will need to monitor
5313 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005314 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005315 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005316 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005317 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5318 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005319 }
5320
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005321 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005322 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005323 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005324 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5325 goto http_msg_closing;
5326 }
5327 else {
5328 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5329 goto http_msg_closed;
5330 }
5331 }
5332 goto wait_other_side;
5333 }
5334
5335 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5336 http_msg_closing:
5337 /* nothing else to forward, just waiting for the output buffer
5338 * to be empty and for the shutw_now to take effect.
5339 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005340 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005341 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5342 goto http_msg_closed;
5343 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005344 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005345 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005346 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005347 if (objt_server(s->target))
5348 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005349 goto wait_other_side;
5350 }
5351 }
5352
5353 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5354 http_msg_closed:
5355 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005356 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005357 channel_auto_close(chn);
5358 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005359 goto wait_other_side;
5360 }
5361
5362 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005363 /* We force the response to leave immediately if we're waiting for the
5364 * other side, since there is no pending shutdown to push it out.
5365 */
5366 if (!channel_is_empty(chn))
5367 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005368 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005369}
5370
5371
5372/* Resync the request and response state machines. Return 1 if either state
5373 * changes.
5374 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005375int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005376{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005377 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005378 int old_req_state = txn->req.msg_state;
5379 int old_res_state = txn->rsp.msg_state;
5380
Willy Tarreau610ecce2010-01-04 21:15:02 +01005381 http_sync_req_state(s);
5382 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005383 if (!http_sync_res_state(s))
5384 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005385 if (!http_sync_req_state(s))
5386 break;
5387 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005388
Willy Tarreau610ecce2010-01-04 21:15:02 +01005389 /* OK, both state machines agree on a compatible state.
5390 * There are a few cases we're interested in :
5391 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5392 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5393 * directions, so let's simply disable both analysers.
5394 * - HTTP_MSG_CLOSED on the response only means we must abort the
5395 * request.
5396 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5397 * with server-close mode means we've completed one request and we
5398 * must re-initialize the server connection.
5399 */
5400
5401 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5402 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5403 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5404 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02005405 s->req.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005406 channel_auto_close(&s->req);
5407 channel_auto_read(&s->req);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005408 s->res.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005409 channel_auto_close(&s->res);
5410 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005411 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005412 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005413 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005414 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005415 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02005416 s->res.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005417 channel_auto_close(&s->res);
5418 channel_auto_read(&s->res);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005419 s->req.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005420 channel_abort(&s->req);
5421 channel_auto_close(&s->req);
5422 channel_auto_read(&s->req);
5423 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005424 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005425 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5426 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005427 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005428 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5429 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5430 /* server-close/keep-alive: terminate this transaction,
5431 * possibly killing the server connection and reinitialize
5432 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005433 */
5434 http_end_txn_clean_session(s);
5435 }
5436
Willy Tarreau610ecce2010-01-04 21:15:02 +01005437 return txn->req.msg_state != old_req_state ||
5438 txn->rsp.msg_state != old_res_state;
5439}
5440
Willy Tarreaud98cf932009-12-27 22:54:55 +01005441/* This function is an analyser which forwards request body (including chunk
5442 * sizes if any). It is called as soon as we must forward, even if we forward
5443 * zero byte. The only situation where it must not be called is when we're in
5444 * tunnel mode and we want to forward till the close. It's used both to forward
5445 * remaining data and to resync after end of body. It expects the msg_state to
5446 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005447 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005448 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005449 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005450 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005451int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005452{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005453 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005454 struct http_txn *txn = s->txn;
5455 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01005456 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005457
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005458 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5459 return 0;
5460
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005461 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005462 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005463 /* Output closed while we were sending data. We must abort and
5464 * wake the other side up.
5465 */
5466 msg->msg_state = HTTP_MSG_ERROR;
5467 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005468 return 1;
5469 }
5470
Willy Tarreaud98cf932009-12-27 22:54:55 +01005471 /* Note that we don't have to send 100-continue back because we don't
5472 * need the data to complete our job, and it's up to the server to
5473 * decide whether to return 100, 417 or anything else in return of
5474 * an "Expect: 100-continue" header.
5475 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02005476 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005477 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5478 ? HTTP_MSG_CHUNK_SIZE
5479 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005480
5481 /* TODO/filters: when http-buffer-request option is set or if a
5482 * rule on url_param exists, the first chunk size could be
5483 * already parsed. In that case, msg->next is after the chunk
5484 * size (including the CRLF after the size). So this case should
5485 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005486 }
5487
Willy Tarreau7ba23542014-04-17 21:50:00 +02005488 /* Some post-connect processing might want us to refrain from starting to
5489 * forward data. Currently, the only reason for this is "balance url_param"
5490 * whichs need to parse/process the request after we've enabled forwarding.
5491 */
5492 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005493 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005494 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005495 req->flags |= CF_WAKE_CONNECT;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005496 goto missing_data_or_waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005497 }
5498 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5499 }
5500
Willy Tarreau80a92c02014-03-12 10:41:13 +01005501 /* in most states, we should abort in case of early close */
5502 channel_auto_close(req);
5503
Willy Tarreauefdf0942014-04-24 20:08:57 +02005504 if (req->to_forward) {
5505 /* We can't process the buffer's contents yet */
5506 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005507 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005508 }
5509
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005510 if (msg->msg_state < HTTP_MSG_DONE) {
5511 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5512 ? http_msg_forward_chunked_body(s, msg)
5513 : http_msg_forward_body(s, msg));
5514 if (!ret)
5515 goto missing_data_or_waiting;
5516 if (ret < 0)
5517 goto return_bad_req;
5518 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005519
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005520 /* other states, DONE...TUNNEL */
5521 /* we don't want to forward closes on DONE except in tunnel mode. */
5522 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5523 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005524
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005525 ret = msg->msg_state;
5526 if (http_resync_states(s)) {
5527 /* some state changes occurred, maybe the analyser
5528 * was disabled too. */
5529 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5530 if (req->flags & CF_SHUTW) {
5531 /* request errors are most likely due to the
5532 * server aborting the transfer. */
5533 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005534 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005535 if (msg->err_pos >= 0)
5536 http_capture_bad_message(&sess->fe->invalid_req, s, msg, ret, s->be);
5537 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005538 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005539 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005540 }
5541
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005542 /* If "option abortonclose" is set on the backend, we want to monitor
5543 * the client's connection and forward any shutdown notification to the
5544 * server, which will decide whether to close or to go on processing the
5545 * request. We only do that in tunnel mode, and not in other modes since
5546 * it can be abused to exhaust source ports. */
5547 if (s->be->options & PR_O_ABRT_CLOSE) {
5548 channel_auto_read(req);
5549 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5550 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5551 s->si[1].flags |= SI_FL_NOLINGER;
5552 channel_auto_close(req);
5553 }
5554 else if (s->txn->meth == HTTP_METH_POST) {
5555 /* POST requests may require to read extra CRLF sent by broken
5556 * browsers and which could cause an RST to be sent upon close
5557 * on some systems (eg: Linux). */
5558 channel_auto_read(req);
5559 }
5560 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005561
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005562 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005563 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005564 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005565 if (!(s->flags & SF_ERR_MASK))
5566 s->flags |= SF_ERR_CLICL;
5567 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005568 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005569 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005570 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005571 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005572 }
5573
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005574 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005575 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005576 if (objt_server(s->target))
5577 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005578
5579 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005580 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005581
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005582 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005583 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005584 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005585
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005586 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005587 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005588 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005589 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005590 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005591
Willy Tarreau5c620922011-05-11 19:56:11 +02005592 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005593 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005594 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005595 * modes are already handled by the stream sock layer. We must not do
5596 * this in content-length mode because it could present the MSG_MORE
5597 * flag with the last block of forwarded data, which would cause an
5598 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005599 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005600 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005601 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005602
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005603 return 0;
5604
Willy Tarreaud98cf932009-12-27 22:54:55 +01005605 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005606 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005607 if (sess->listener->counters)
5608 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005609
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005610 return_bad_req_stats_ok:
5611 txn->req.msg_state = HTTP_MSG_ERROR;
5612 if (txn->status) {
5613 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005614 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005615 } else {
5616 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005617 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005618 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005619 req->analysers &= AN_FLT_END;
5620 s->res.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005621
Willy Tarreaue7dff022015-04-03 01:14:29 +02005622 if (!(s->flags & SF_ERR_MASK))
5623 s->flags |= SF_ERR_PRXCOND;
5624 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005625 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005626 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005627 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005628 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005629 }
5630 return 0;
5631
5632 aborted_xfer:
5633 txn->req.msg_state = HTTP_MSG_ERROR;
5634 if (txn->status) {
5635 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005636 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005637 } else {
5638 txn->status = 502;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005639 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005640 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005641 req->analysers &= AN_FLT_END;
5642 s->res.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005643
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005644 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005645 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005646 if (objt_server(s->target))
5647 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005648
Willy Tarreaue7dff022015-04-03 01:14:29 +02005649 if (!(s->flags & SF_ERR_MASK))
5650 s->flags |= SF_ERR_SRVCL;
5651 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005652 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005653 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005654 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005655 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005656 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005657 return 0;
5658}
5659
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005660/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5661 * processing can continue on next analysers, or zero if it either needs more
5662 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005663 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005664 * when it has nothing left to do, and may remove any analyser when it wants to
5665 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005666 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005667int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005668{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005669 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005670 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005671 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005672 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005673 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005674 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005675 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005676
Willy Tarreau87b09662015-04-03 00:22:06 +02005677 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 +02005678 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005679 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005680 rep,
5681 rep->rex, rep->wex,
5682 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005683 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005684 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005685
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005686 /*
5687 * Now parse the partial (or complete) lines.
5688 * We will check the response syntax, and also join multi-line
5689 * headers. An index of all the lines will be elaborated while
5690 * parsing.
5691 *
5692 * For the parsing, we use a 28 states FSM.
5693 *
5694 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005695 * rep->buf->p = beginning of response
5696 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5697 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005698 * msg->eol = end of current header or line (LF or CRLF)
5699 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005700 */
5701
Willy Tarreau628c40c2014-04-24 19:11:26 +02005702 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005703 /* There's a protected area at the end of the buffer for rewriting
5704 * purposes. We don't want to start to parse the request if the
5705 * protected area is affected, because we may have to move processed
5706 * data later, which is much more complicated.
5707 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005708 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005709 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005710 /* some data has still not left the buffer, wake us once that's done */
5711 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5712 goto abort_response;
5713 channel_dont_close(rep);
5714 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005715 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005716 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005717 }
5718
Willy Tarreau379357a2013-06-08 12:55:46 +02005719 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5720 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5721 buffer_slow_realign(rep->buf);
5722
Willy Tarreau9b28e032012-10-12 23:49:43 +02005723 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005724 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005725 }
5726
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005727 /* 1: we might have to print this header in debug mode */
5728 if (unlikely((global.mode & MODE_DEBUG) &&
5729 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005730 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005731 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005732
Willy Tarreau9b28e032012-10-12 23:49:43 +02005733 sol = rep->buf->p;
5734 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005735 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005736
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005737 sol += hdr_idx_first_pos(&txn->hdr_idx);
5738 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005739
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005740 while (cur_idx) {
5741 eol = sol + txn->hdr_idx.v[cur_idx].len;
5742 debug_hdr("srvhdr", s, sol, eol);
5743 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5744 cur_idx = txn->hdr_idx.v[cur_idx].next;
5745 }
5746 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005747
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005748 /*
5749 * Now we quickly check if we have found a full valid response.
5750 * If not so, we check the FD and buffer states before leaving.
5751 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005752 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005753 * responses are checked first.
5754 *
5755 * Depending on whether the client is still there or not, we
5756 * may send an error response back or not. Note that normally
5757 * we should only check for HTTP status there, and check I/O
5758 * errors somewhere else.
5759 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005760
Willy Tarreau655dce92009-11-08 13:10:58 +01005761 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005762 /* Invalid response */
5763 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5764 /* we detected a parsing error. We want to archive this response
5765 * in the dedicated proxy area for later troubleshooting.
5766 */
5767 hdr_response_bad:
5768 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005769 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005770
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005771 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005772 if (objt_server(s->target)) {
5773 objt_server(s->target)->counters.failed_resp++;
5774 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005775 }
Willy Tarreau64648412010-03-05 10:41:54 +01005776 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005777 channel_auto_close(rep);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005778 rep->analysers &= AN_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005779 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005780 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005781 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005782 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005783
Willy Tarreaue7dff022015-04-03 01:14:29 +02005784 if (!(s->flags & SF_ERR_MASK))
5785 s->flags |= SF_ERR_PRXCOND;
5786 if (!(s->flags & SF_FINST_MASK))
5787 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005788
5789 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005790 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005791
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005792 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005793 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005794 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005795 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005796 goto hdr_response_bad;
5797 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005798
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005799 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005800 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005801 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005802 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005803 else if (txn->flags & TX_NOT_FIRST)
5804 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005805
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005806 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005807 if (objt_server(s->target)) {
5808 objt_server(s->target)->counters.failed_resp++;
5809 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005810 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005811
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005812 channel_auto_close(rep);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005813 rep->analysers &= AN_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005814 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005815 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005816 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005817 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005818
Willy Tarreaue7dff022015-04-03 01:14:29 +02005819 if (!(s->flags & SF_ERR_MASK))
5820 s->flags |= SF_ERR_SRVCL;
5821 if (!(s->flags & SF_FINST_MASK))
5822 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005823 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005824 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005825
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005826 /* read timeout : return a 504 to the client. */
5827 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005828 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005829 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005830
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005831 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005832 if (objt_server(s->target)) {
5833 objt_server(s->target)->counters.failed_resp++;
5834 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005835 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005836
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005837 channel_auto_close(rep);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005838 rep->analysers &= AN_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005839 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005840 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005841 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005842 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005843
Willy Tarreaue7dff022015-04-03 01:14:29 +02005844 if (!(s->flags & SF_ERR_MASK))
5845 s->flags |= SF_ERR_SRVTO;
5846 if (!(s->flags & SF_FINST_MASK))
5847 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005848 return 0;
5849 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005850
Willy Tarreauf003d372012-11-26 13:35:37 +01005851 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005852 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005853 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01005854 s->be->be_counters.cli_aborts++;
5855 if (objt_server(s->target))
5856 objt_server(s->target)->counters.cli_aborts++;
5857
Christopher Fauletd7c91962015-04-30 11:48:27 +02005858 rep->analysers &= AN_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01005859 channel_auto_close(rep);
5860
5861 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005862 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005863 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01005864
Willy Tarreaue7dff022015-04-03 01:14:29 +02005865 if (!(s->flags & SF_ERR_MASK))
5866 s->flags |= SF_ERR_CLICL;
5867 if (!(s->flags & SF_FINST_MASK))
5868 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01005869
Willy Tarreau87b09662015-04-03 00:22:06 +02005870 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01005871 return 0;
5872 }
5873
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005874 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005875 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005876 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005877 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005878 else if (txn->flags & TX_NOT_FIRST)
5879 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005880
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005881 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005882 if (objt_server(s->target)) {
5883 objt_server(s->target)->counters.failed_resp++;
5884 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005885 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005886
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005887 channel_auto_close(rep);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005888 rep->analysers &= AN_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005889 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005890 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005891 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005892 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005893
Willy Tarreaue7dff022015-04-03 01:14:29 +02005894 if (!(s->flags & SF_ERR_MASK))
5895 s->flags |= SF_ERR_SRVCL;
5896 if (!(s->flags & SF_FINST_MASK))
5897 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005898 return 0;
5899 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005900
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005901 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005902 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005903 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005904 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005905 else if (txn->flags & TX_NOT_FIRST)
5906 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005907
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005908 s->be->be_counters.failed_resp++;
Christopher Fauletd7c91962015-04-30 11:48:27 +02005909 rep->analysers &= AN_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005910 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005911
Willy Tarreaue7dff022015-04-03 01:14:29 +02005912 if (!(s->flags & SF_ERR_MASK))
5913 s->flags |= SF_ERR_CLICL;
5914 if (!(s->flags & SF_FINST_MASK))
5915 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005916
Willy Tarreau87b09662015-04-03 00:22:06 +02005917 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005918 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005919 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005920
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005921 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005922 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005923 return 0;
5924 }
5925
5926 /* More interesting part now : we know that we have a complete
5927 * response which at least looks like HTTP. We have an indicator
5928 * of each header's length, so we can parse them quickly.
5929 */
5930
5931 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005932 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005933
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005934 /*
5935 * 1: get the status code
5936 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005937 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005938 if (n < 1 || n > 5)
5939 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005940 /* when the client triggers a 4xx from the server, it's most often due
5941 * to a missing object or permission. These events should be tracked
5942 * because if they happen often, it may indicate a brute force or a
5943 * vulnerability scan.
5944 */
5945 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02005946 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02005947
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005948 if (objt_server(s->target))
5949 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005950
Willy Tarreau91852eb2015-05-01 13:26:00 +02005951 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
5952 * exactly one digit "." one digit. This check may be disabled using
5953 * option accept-invalid-http-response.
5954 */
5955 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
5956 if (msg->sl.st.v_l != 8) {
5957 msg->err_pos = 0;
5958 goto hdr_response_bad;
5959 }
5960
5961 if (rep->buf->p[4] != '/' ||
5962 !isdigit((unsigned char)rep->buf->p[5]) ||
5963 rep->buf->p[6] != '.' ||
5964 !isdigit((unsigned char)rep->buf->p[7])) {
5965 msg->err_pos = 4;
5966 goto hdr_response_bad;
5967 }
5968 }
5969
Willy Tarreau5b154472009-12-21 20:11:07 +01005970 /* check if the response is HTTP/1.1 or above */
5971 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005972 ((rep->buf->p[5] > '1') ||
5973 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005974 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005975
5976 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005977 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 +01005978
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005979 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005980 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005981
Willy Tarreau9b28e032012-10-12 23:49:43 +02005982 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005983
Willy Tarreau39650402010-03-15 19:44:39 +01005984 /* Adjust server's health based on status code. Note: status codes 501
5985 * and 505 are triggered on demand by client request, so we must not
5986 * count them as server failures.
5987 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005988 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005989 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005990 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005991 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005992 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005993 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005994
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005995 /*
5996 * 2: check for cacheability.
5997 */
5998
5999 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006000 case 100:
6001 /*
6002 * We may be facing a 100-continue response, in which case this
6003 * is not the right response, and we're waiting for the next one.
6004 * Let's allow this response to go to the client and wait for the
6005 * next one.
6006 */
6007 hdr_idx_init(&txn->hdr_idx);
6008 msg->next -= channel_forward(rep, msg->next);
6009 msg->msg_state = HTTP_MSG_RPBEFORE;
6010 txn->status = 0;
6011 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01006012 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02006013 goto next_one;
6014
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006015 case 200:
6016 case 203:
6017 case 206:
6018 case 300:
6019 case 301:
6020 case 410:
6021 /* RFC2616 @13.4:
6022 * "A response received with a status code of
6023 * 200, 203, 206, 300, 301 or 410 MAY be stored
6024 * by a cache (...) unless a cache-control
6025 * directive prohibits caching."
6026 *
6027 * RFC2616 @9.5: POST method :
6028 * "Responses to this method are not cacheable,
6029 * unless the response includes appropriate
6030 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006031 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006032 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006033 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006034 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6035 break;
6036 default:
6037 break;
6038 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006039
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006040 /*
6041 * 3: we may need to capture headers
6042 */
6043 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006044 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006045 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006046 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006047
Willy Tarreau557f1992015-05-01 10:05:17 +02006048 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6049 * by RFC7230#3.3.3 :
6050 *
6051 * The length of a message body is determined by one of the following
6052 * (in order of precedence):
6053 *
6054 * 1. Any response to a HEAD request and any response with a 1xx
6055 * (Informational), 204 (No Content), or 304 (Not Modified) status
6056 * code is always terminated by the first empty line after the
6057 * header fields, regardless of the header fields present in the
6058 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006059 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006060 * 2. Any 2xx (Successful) response to a CONNECT request implies that
6061 * the connection will become a tunnel immediately after the empty
6062 * line that concludes the header fields. A client MUST ignore any
6063 * Content-Length or Transfer-Encoding header fields received in
6064 * such a message.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006065 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006066 * 3. If a Transfer-Encoding header field is present and the chunked
6067 * transfer coding (Section 4.1) is the final encoding, the message
6068 * body length is determined by reading and decoding the chunked
6069 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006070 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006071 * If a Transfer-Encoding header field is present in a response and
6072 * the chunked transfer coding is not the final encoding, the
6073 * message body length is determined by reading the connection until
6074 * it is closed by the server. If a Transfer-Encoding header field
6075 * is present in a request and the chunked transfer coding is not
6076 * the final encoding, the message body length cannot be determined
6077 * reliably; the server MUST respond with the 400 (Bad Request)
6078 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006079 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006080 * If a message is received with both a Transfer-Encoding and a
6081 * Content-Length header field, the Transfer-Encoding overrides the
6082 * Content-Length. Such a message might indicate an attempt to
6083 * perform request smuggling (Section 9.5) or response splitting
6084 * (Section 9.4) and ought to be handled as an error. A sender MUST
6085 * remove the received Content-Length field prior to forwarding such
6086 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006087 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006088 * 4. If a message is received without Transfer-Encoding and with
6089 * either multiple Content-Length header fields having differing
6090 * field-values or a single Content-Length header field having an
6091 * invalid value, then the message framing is invalid and the
6092 * recipient MUST treat it as an unrecoverable error. If this is a
6093 * request message, the server MUST respond with a 400 (Bad Request)
6094 * status code and then close the connection. If this is a response
6095 * message received by a proxy, the proxy MUST close the connection
6096 * to the server, discard the received response, and send a 502 (Bad
6097 * Gateway) response to the client. If this is a response message
6098 * received by a user agent, the user agent MUST close the
6099 * connection to the server and discard the received response.
6100 *
6101 * 5. If a valid Content-Length header field is present without
6102 * Transfer-Encoding, its decimal value defines the expected message
6103 * body length in octets. If the sender closes the connection or
6104 * the recipient times out before the indicated number of octets are
6105 * received, the recipient MUST consider the message to be
6106 * incomplete and close the connection.
6107 *
6108 * 6. If this is a request message and none of the above are true, then
6109 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006110 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006111 * 7. Otherwise, this is a response message without a declared message
6112 * body length, so the message body length is determined by the
6113 * number of octets received prior to the server closing the
6114 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006115 */
6116
6117 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006118 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006119 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006120 * FIXME: should we parse anyway and return an error on chunked encoding ?
6121 */
6122 if (txn->meth == HTTP_METH_HEAD ||
6123 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006124 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006125 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006126 goto skip_content_length;
6127 }
6128
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006129 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006130 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006131 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006132 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006133 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6134 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006135 /* bad transfer-encoding (chunked followed by something else) */
6136 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006137 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006138 break;
6139 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006140 }
6141
Willy Tarreau1c913912015-04-30 10:57:51 +02006142 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006143 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006144 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006145 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6146 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6147 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006148 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006149 signed long long cl;
6150
Willy Tarreauad14f752011-09-02 20:33:27 +02006151 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006152 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006153 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006154 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006155
Willy Tarreauad14f752011-09-02 20:33:27 +02006156 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006157 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006158 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006159 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006160
Willy Tarreauad14f752011-09-02 20:33:27 +02006161 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006162 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006163 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006164 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006165
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006166 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006167 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006168 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006169 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006170
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006171 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006172 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006173 }
6174
6175skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006176 /* Now we have to check if we need to modify the Connection header.
6177 * This is more difficult on the response than it is on the request,
6178 * because we can have two different HTTP versions and we don't know
6179 * how the client will interprete a response. For instance, let's say
6180 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6181 * HTTP/1.1 response without any header. Maybe it will bound itself to
6182 * HTTP/1.0 because it only knows about it, and will consider the lack
6183 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6184 * the lack of header as a keep-alive. Thus we will use two flags
6185 * indicating how a request MAY be understood by the client. In case
6186 * of multiple possibilities, we'll fix the header to be explicit. If
6187 * ambiguous cases such as both close and keepalive are seen, then we
6188 * will fall back to explicit close. Note that we won't take risks with
6189 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006190 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006191 */
6192
Willy Tarreaudc008c52010-02-01 16:20:08 +01006193 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6194 txn->status == 101)) {
6195 /* Either we've established an explicit tunnel, or we're
6196 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006197 * to understand the next protocols. We have to switch to tunnel
6198 * mode, so that we transfer the request and responses then let
6199 * this protocol pass unmodified. When we later implement specific
6200 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006201 * header which contains information about that protocol for
6202 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006203 */
6204 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6205 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006206 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6207 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006208 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006209 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006210 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006211
Willy Tarreau70dffda2014-01-30 03:07:23 +01006212 /* this situation happens when combining pretend-keepalive with httpclose. */
6213 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006214 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006215 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006216 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6217
Willy Tarreau60466522010-01-18 19:08:45 +01006218 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006219 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006220 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6221 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006222
Willy Tarreau60466522010-01-18 19:08:45 +01006223 /* now adjust header transformations depending on current state */
6224 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6225 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6226 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006227 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006228 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006229 }
Willy Tarreau60466522010-01-18 19:08:45 +01006230 else { /* SCL / KAL */
6231 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006232 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006233 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006234 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006235
Willy Tarreau60466522010-01-18 19:08:45 +01006236 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006237 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006238
Willy Tarreau60466522010-01-18 19:08:45 +01006239 /* Some keep-alive responses are converted to Server-close if
6240 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006241 */
Willy Tarreau60466522010-01-18 19:08:45 +01006242 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6243 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006244 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006245 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006246 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006247 }
6248
Willy Tarreau7959a552013-09-23 16:44:27 +02006249 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006250 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006251
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006252 /* end of job, return OK */
6253 rep->analysers &= ~an_bit;
6254 rep->analyse_exp = TICK_ETERNITY;
6255 channel_auto_close(rep);
6256 return 1;
6257
6258 abort_keep_alive:
6259 /* A keep-alive request to the server failed on a network error.
6260 * The client is required to retry. We need to close without returning
6261 * any other information so that the client retries.
6262 */
6263 txn->status = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +02006264 rep->analysers &= AN_FLT_END;
6265 s->req.analysers &= AN_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006266 channel_auto_close(rep);
6267 s->logs.logwait = 0;
6268 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006269 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006270 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006271 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006272 return 0;
6273}
6274
6275/* This function performs all the processing enabled for the current response.
6276 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006277 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006278 * other functions. It works like process_request (see indications above).
6279 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006280int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006281{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006282 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006283 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006284 struct http_msg *msg = &txn->rsp;
6285 struct proxy *cur_proxy;
6286 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006287 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006288
Willy Tarreau87b09662015-04-03 00:22:06 +02006289 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 +02006290 now_ms, __FUNCTION__,
6291 s,
6292 rep,
6293 rep->rex, rep->wex,
6294 rep->flags,
6295 rep->buf->i,
6296 rep->analysers);
6297
6298 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6299 return 0;
6300
Willy Tarreau70730dd2014-04-24 18:06:27 +02006301 /* The stats applet needs to adjust the Connection header but we don't
6302 * apply any filter there.
6303 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006304 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6305 rep->analysers &= ~an_bit;
6306 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006307 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006308 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006309
Willy Tarreau58975672014-04-24 21:13:57 +02006310 /*
6311 * We will have to evaluate the filters.
6312 * As opposed to version 1.2, now they will be evaluated in the
6313 * filters order and not in the header order. This means that
6314 * each filter has to be validated among all headers.
6315 *
6316 * Filters are tried with ->be first, then with ->fe if it is
6317 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006318 *
6319 * Maybe we are in resume condiion. In this case I choose the
6320 * "struct proxy" which contains the rule list matching the resume
6321 * pointer. If none of theses "struct proxy" match, I initialise
6322 * the process with the first one.
6323 *
6324 * In fact, I check only correspondance betwwen the current list
6325 * pointer and the ->fe rule list. If it doesn't match, I initialize
6326 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006327 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006328 if (s->current_rule_list == &sess->fe->http_res_rules)
6329 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006330 else
6331 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006332 while (1) {
6333 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006334
Willy Tarreau58975672014-04-24 21:13:57 +02006335 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02006336 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006337 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006338
Willy Tarreau51d861a2015-05-22 17:30:48 +02006339 if (ret == HTTP_RULE_RES_BADREQ)
6340 goto return_srv_prx_502;
6341
6342 if (ret == HTTP_RULE_RES_DONE) {
6343 rep->analysers &= ~an_bit;
6344 rep->analyse_exp = TICK_ETERNITY;
6345 return 1;
6346 }
6347 }
6348
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006349 /* we need to be called again. */
6350 if (ret == HTTP_RULE_RES_YIELD) {
6351 channel_dont_close(rep);
6352 return 0;
6353 }
6354
Willy Tarreau58975672014-04-24 21:13:57 +02006355 /* try headers filters */
6356 if (rule_set->rsp_exp != NULL) {
6357 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6358 return_bad_resp:
6359 if (objt_server(s->target)) {
6360 objt_server(s->target)->counters.failed_resp++;
6361 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006362 }
Willy Tarreau58975672014-04-24 21:13:57 +02006363 s->be->be_counters.failed_resp++;
6364 return_srv_prx_502:
Christopher Fauletd7c91962015-04-30 11:48:27 +02006365 rep->analysers &= AN_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02006366 txn->status = 502;
6367 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006368 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006369 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006370 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006371 if (!(s->flags & SF_ERR_MASK))
6372 s->flags |= SF_ERR_PRXCOND;
6373 if (!(s->flags & SF_FINST_MASK))
6374 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006375 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006376 }
Willy Tarreau58975672014-04-24 21:13:57 +02006377 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006378
Willy Tarreau58975672014-04-24 21:13:57 +02006379 /* has the response been denied ? */
6380 if (txn->flags & TX_SVDENY) {
6381 if (objt_server(s->target))
6382 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006383
Willy Tarreau58975672014-04-24 21:13:57 +02006384 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006385 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006386 if (sess->listener->counters)
6387 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006388
Willy Tarreau58975672014-04-24 21:13:57 +02006389 goto return_srv_prx_502;
6390 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006391
Willy Tarreau58975672014-04-24 21:13:57 +02006392 /* add response headers from the rule sets in the same order */
6393 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006394 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006395 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006396 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006397 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006398 ret = acl_pass(ret);
6399 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6400 ret = !ret;
6401 if (!ret)
6402 continue;
6403 }
6404 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6405 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006406 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006407
Willy Tarreau58975672014-04-24 21:13:57 +02006408 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006409 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006410 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006411 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006412 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006413
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006414 /* After this point, this anayzer can't return yield, so we can
6415 * remove the bit corresponding to this analyzer from the list.
6416 *
6417 * Note that the intermediate returns and goto found previously
6418 * reset the analyzers.
6419 */
6420 rep->analysers &= ~an_bit;
6421 rep->analyse_exp = TICK_ETERNITY;
6422
Willy Tarreau58975672014-04-24 21:13:57 +02006423 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006424 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006425 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006426
Willy Tarreau58975672014-04-24 21:13:57 +02006427 /*
6428 * Now check for a server cookie.
6429 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006430 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02006431 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006432
Willy Tarreau58975672014-04-24 21:13:57 +02006433 /*
6434 * Check for cache-control or pragma headers if required.
6435 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006436 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 +02006437 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006438
Willy Tarreau58975672014-04-24 21:13:57 +02006439 /*
6440 * Add server cookie in the response if needed
6441 */
6442 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6443 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006444 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006445 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6446 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6447 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6448 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6449 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006450 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006451 /* the server is known, it's not the one the client requested, or the
6452 * cookie's last seen date needs to be refreshed. We have to
6453 * insert a set-cookie here, except if we want to insert only on POST
6454 * requests and this one isn't. Note that servers which don't have cookies
6455 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006456 */
Willy Tarreau58975672014-04-24 21:13:57 +02006457 if (!objt_server(s->target)->cookie) {
6458 chunk_printf(&trash,
6459 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6460 s->be->cookie_name);
6461 }
6462 else {
6463 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006464
Willy Tarreau58975672014-04-24 21:13:57 +02006465 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6466 /* emit last_date, which is mandatory */
6467 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6468 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6469 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006470
Willy Tarreau58975672014-04-24 21:13:57 +02006471 if (s->be->cookie_maxlife) {
6472 /* emit first_date, which is either the original one or
6473 * the current date.
6474 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006475 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006476 s30tob64(txn->cookie_first_date ?
6477 txn->cookie_first_date >> 2 :
6478 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006479 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006480 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006481 }
Willy Tarreau58975672014-04-24 21:13:57 +02006482 chunk_appendf(&trash, "; path=/");
6483 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006484
Willy Tarreau58975672014-04-24 21:13:57 +02006485 if (s->be->cookie_domain)
6486 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006487
Willy Tarreau58975672014-04-24 21:13:57 +02006488 if (s->be->ck_opts & PR_CK_HTTPONLY)
6489 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006490
Willy Tarreau58975672014-04-24 21:13:57 +02006491 if (s->be->ck_opts & PR_CK_SECURE)
6492 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006493
Willy Tarreau58975672014-04-24 21:13:57 +02006494 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6495 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006496
Willy Tarreau58975672014-04-24 21:13:57 +02006497 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006498 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006499 /* the server did not change, only the date was updated */
6500 txn->flags |= TX_SCK_UPDATED;
6501 else
6502 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006503
Willy Tarreau58975672014-04-24 21:13:57 +02006504 /* Here, we will tell an eventual cache on the client side that we don't
6505 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6506 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6507 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006508 */
Willy Tarreau58975672014-04-24 21:13:57 +02006509 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006510
Willy Tarreau58975672014-04-24 21:13:57 +02006511 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006512
Willy Tarreau58975672014-04-24 21:13:57 +02006513 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6514 "Cache-control: private", 22) < 0))
6515 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006516 }
Willy Tarreau58975672014-04-24 21:13:57 +02006517 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006518
Willy Tarreau58975672014-04-24 21:13:57 +02006519 /*
6520 * Check if result will be cacheable with a cookie.
6521 * We'll block the response if security checks have caught
6522 * nasty things such as a cacheable cookie.
6523 */
6524 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6525 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6526 (s->be->options & PR_O_CHK_CACHE)) {
6527 /* we're in presence of a cacheable response containing
6528 * a set-cookie header. We'll block it as requested by
6529 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006530 */
Willy Tarreau58975672014-04-24 21:13:57 +02006531 if (objt_server(s->target))
6532 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006533
Willy Tarreau58975672014-04-24 21:13:57 +02006534 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006535 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006536 if (sess->listener->counters)
6537 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006538
Willy Tarreau58975672014-04-24 21:13:57 +02006539 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6540 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6541 send_log(s->be, LOG_ALERT,
6542 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6543 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6544 goto return_srv_prx_502;
6545 }
Willy Tarreau03945942009-12-22 16:50:27 +01006546
Willy Tarreau70730dd2014-04-24 18:06:27 +02006547 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006548 /*
6549 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6550 * If an "Upgrade" token is found, the header is left untouched in order
6551 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006552 * if anything but "Upgrade" is present in the Connection header. We don't
6553 * want to touch any 101 response either since it's switching to another
6554 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006555 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006556 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006557 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006558 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006559 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6560 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006561
Willy Tarreau58975672014-04-24 21:13:57 +02006562 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6563 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6564 /* we want a keep-alive response here. Keep-alive header
6565 * required if either side is not 1.1.
6566 */
6567 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6568 want_flags |= TX_CON_KAL_SET;
6569 }
6570 else {
6571 /* we want a close response here. Close header required if
6572 * the server is 1.1, regardless of the client.
6573 */
6574 if (msg->flags & HTTP_MSGF_VER_11)
6575 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006576 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006577
Willy Tarreau58975672014-04-24 21:13:57 +02006578 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6579 http_change_connection_header(txn, msg, want_flags);
6580 }
6581
6582 skip_header_mangling:
Christopher Faulet3e344292015-11-24 16:24:13 +01006583 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_FILTERS(s) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006584 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
6585 rep->analysers &= ~AN_FLT_XFER_DATA;
Willy Tarreau58975672014-04-24 21:13:57 +02006586 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Christopher Fauletd7c91962015-04-30 11:48:27 +02006587 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006588
Willy Tarreau58975672014-04-24 21:13:57 +02006589 /* if the user wants to log as soon as possible, without counting
6590 * bytes from the server, then this is the right moment. We have
6591 * to temporarily assign bytes_out to log what we currently have.
6592 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006593 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006594 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6595 s->logs.bytes_out = txn->rsp.eoh;
6596 s->do_log(s);
6597 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006598 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006599 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006600}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006601
Willy Tarreaud98cf932009-12-27 22:54:55 +01006602/* This function is an analyser which forwards response body (including chunk
6603 * sizes if any). It is called as soon as we must forward, even if we forward
6604 * zero byte. The only situation where it must not be called is when we're in
6605 * tunnel mode and we want to forward till the close. It's used both to forward
6606 * remaining data and to resync after end of body. It expects the msg_state to
6607 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006608 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006609 *
6610 * It is capable of compressing response data both in content-length mode and
6611 * in chunked mode. The state machines follows different flows depending on
6612 * whether content-length and chunked modes are used, since there are no
6613 * trailers in content-length :
6614 *
6615 * chk-mode cl-mode
6616 * ,----- BODY -----.
6617 * / \
6618 * V size > 0 V chk-mode
6619 * .--> SIZE -------------> DATA -------------> CRLF
6620 * | | size == 0 | last byte |
6621 * | v final crlf v inspected |
6622 * | TRAILERS -----------> DONE |
6623 * | |
6624 * `----------------------------------------------'
6625 *
6626 * Compression only happens in the DATA state, and must be flushed in final
6627 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6628 * is performed at once on final states for all bytes parsed, or when leaving
6629 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006630 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006631int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006632{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006633 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006634 struct http_txn *txn = s->txn;
6635 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01006636 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006637
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006638 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6639 return 0;
6640
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006641 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006642 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006643 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006644 /* Output closed while we were sending data. We must abort and
6645 * wake the other side up.
6646 */
6647 msg->msg_state = HTTP_MSG_ERROR;
6648 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006649 return 1;
6650 }
6651
Willy Tarreau4fe41902010-06-07 22:27:41 +02006652 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006653 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006654
Christopher Fauletd7c91962015-04-30 11:48:27 +02006655 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006656 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
6657 ? HTTP_MSG_CHUNK_SIZE
6658 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006659 }
6660
Willy Tarreauefdf0942014-04-24 20:08:57 +02006661 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006662 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02006663 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006664 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006665 }
6666
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006667 if (msg->msg_state < HTTP_MSG_DONE) {
6668 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6669 ? http_msg_forward_chunked_body(s, msg)
6670 : http_msg_forward_body(s, msg));
6671 if (!ret)
6672 goto missing_data_or_waiting;
6673 if (ret < 0)
6674 goto return_bad_res;
6675 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006676
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006677 /* other states, DONE...TUNNEL */
6678 /* for keep-alive we don't want to forward closes on DONE */
6679 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6680 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6681 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006682
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006683 ret = msg->msg_state;
6684 if (http_resync_states(s)) {
6685 /* some state changes occurred, maybe the analyser was disabled
6686 * too. */
6687 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6688 if (res->flags & CF_SHUTW) {
6689 /* response errors are most likely due to the
6690 * client aborting the transfer. */
6691 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006692 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006693 if (msg->err_pos >= 0)
6694 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, strm_fe(s));
6695 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006696 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006697 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006698 }
6699
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006700 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006701 if (res->flags & CF_SHUTW)
6702 goto aborted_xfer;
6703
6704 /* stop waiting for data if the input is closed before the end. If the
6705 * client side was already closed, it means that the client has aborted,
6706 * so we don't want to count this as a server abort. Otherwise it's a
6707 * server abort.
6708 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006709 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006710 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006711 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006712 /* If we have some pending data, we continue the processing */
6713 if (!buffer_pending(res->buf)) {
6714 if (!(s->flags & SF_ERR_MASK))
6715 s->flags |= SF_ERR_SRVCL;
6716 s->be->be_counters.srv_aborts++;
6717 if (objt_server(s->target))
6718 objt_server(s->target)->counters.srv_aborts++;
6719 goto return_bad_res_stats_ok;
6720 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006721 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006722
Willy Tarreau40dba092010-03-04 18:14:51 +01006723 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006724 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006725 goto return_bad_res;
6726
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006727 /* When TE: chunked is used, we need to get there again to parse
6728 * remaining chunks even if the server has closed, so we don't want to
6729 * set CF_DONTCLOSE. Similarly, if the body length is undefined, if
6730 * keep-alive is set on the client side or if there are filters
6731 * registered on the stream, we don't want to forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006732 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006733 if ((msg->flags & HTTP_MSGF_TE_CHNK) || !msg->body_len ||
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006734 HAS_FILTERS(s) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006735 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6736 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006737 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006738
Willy Tarreau5c620922011-05-11 19:56:11 +02006739 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006740 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006741 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006742 * modes are already handled by the stream sock layer. We must not do
6743 * this in content-length mode because it could present the MSG_MORE
6744 * flag with the last block of forwarded data, which would cause an
6745 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006746 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006747 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006748 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006749
Willy Tarreau87b09662015-04-03 00:22:06 +02006750 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006751 return 0;
6752
Willy Tarreau40dba092010-03-04 18:14:51 +01006753 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006754 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006755 if (objt_server(s->target))
6756 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006757
6758 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006759 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006760 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006761 http_reply_and_close(s, txn->status, NULL);
Christopher Fauletd7c91962015-04-30 11:48:27 +02006762 res->analysers &= AN_FLT_END;
6763 s->req.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006764 if (objt_server(s->target))
6765 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006766
Willy Tarreaue7dff022015-04-03 01:14:29 +02006767 if (!(s->flags & SF_ERR_MASK))
6768 s->flags |= SF_ERR_PRXCOND;
6769 if (!(s->flags & SF_FINST_MASK))
6770 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006771 return 0;
6772
6773 aborted_xfer:
6774 txn->rsp.msg_state = HTTP_MSG_ERROR;
6775 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006776 http_reply_and_close(s, txn->status, NULL);
Christopher Fauletd7c91962015-04-30 11:48:27 +02006777 res->analysers &= AN_FLT_END;
6778 s->req.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006779
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006780 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006781 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006782 if (objt_server(s->target))
6783 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006784
Willy Tarreaue7dff022015-04-03 01:14:29 +02006785 if (!(s->flags & SF_ERR_MASK))
6786 s->flags |= SF_ERR_CLICL;
6787 if (!(s->flags & SF_FINST_MASK))
6788 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006789 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006790}
6791
6792
6793static inline int
6794http_msg_forward_body(struct stream *s, struct http_msg *msg)
6795{
6796 struct channel *chn = msg->chn;
6797 int ret;
6798
6799 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
6800
6801 if (msg->msg_state == HTTP_MSG_ENDING)
6802 goto ending;
6803
6804 /* Neither content-length, nor transfer-encoding was found, so we must
6805 * read the body until the server connection is closed. In that case, we
6806 * eat data as they come. Of course, this happens for response only. */
6807 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6808 unsigned long long len = (chn->buf->i - msg->next);
6809 msg->chunk_len += len;
6810 msg->body_len += len;
6811 }
Christopher Fauletda02e172015-12-04 09:25:05 +01006812 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6813 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6814 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006815 msg->next += ret;
6816 msg->chunk_len -= ret;
6817 if (msg->chunk_len) {
6818 /* input empty or output full */
6819 if (chn->buf->i > msg->next)
6820 chn->flags |= CF_WAKE_WRITE;
6821 goto missing_data_or_waiting;
6822 }
6823
6824 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && !(chn->flags & CF_SHUTR)) {
6825 /* The server still sending data */
6826 goto missing_data_or_waiting;
6827 }
6828 msg->msg_state = HTTP_MSG_ENDING;
6829
6830 ending:
6831 /* we may have some pending data starting at res->buf->p such as a last
6832 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006833 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6834 /* default_ret */ msg->next,
6835 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006836 b_adv(chn->buf, ret);
6837 msg->next -= ret;
6838 if (msg->next)
6839 goto missing_data_or_waiting;
6840
Christopher Fauletda02e172015-12-04 09:25:05 +01006841 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
6842 /* default_ret */ 1,
6843 /* on_error */ goto error,
6844 /* on_wait */ goto waiting);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006845 msg->msg_state = HTTP_MSG_DONE;
6846 return 1;
6847
6848 missing_data_or_waiting:
6849 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006850 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6851 /* default_ret */ msg->next,
6852 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006853 b_adv(chn->buf, ret);
6854 msg->next -= ret;
6855 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6856 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006857 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006858 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
6859 waiting:
6860 return 0;
6861 error:
6862 return -1;
6863}
6864
6865static inline int
6866http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
6867{
6868 struct channel *chn = msg->chn;
6869 int ret;
6870
6871 /* Here we have the guarantee to be in one of the following state:
6872 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
6873 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
6874
6875 switch_states:
6876 switch (msg->msg_state) {
6877 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01006878 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6879 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6880 /* on_error */ goto error);
6881 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006882 msg->chunk_len -= ret;
6883 if (msg->chunk_len) {
6884 /* input empty or output full */
6885 if (chn->buf->i > msg->next)
6886 chn->flags |= CF_WAKE_WRITE;
6887 goto missing_data_or_waiting;
6888 }
6889
6890 /* nothing left to forward for this chunk*/
6891 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
6892 /* fall through for HTTP_MSG_CHUNK_CRLF */
6893
6894 case HTTP_MSG_CHUNK_CRLF:
6895 /* we want the CRLF after the data */
6896 ret = http_skip_chunk_crlf(msg);
6897 if (ret == 0)
6898 goto missing_data_or_waiting;
6899 if (ret < 0)
6900 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01006901 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006902 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6903 /* fall through for HTTP_MSG_CHUNK_SIZE */
6904
6905 case HTTP_MSG_CHUNK_SIZE:
6906 /* read the chunk size and assign it to ->chunk_len,
6907 * then set ->next to point to the body and switch to
6908 * DATA or TRAILERS state.
6909 */
6910 ret = http_parse_chunk_size(msg);
6911 if (ret == 0)
6912 goto missing_data_or_waiting;
6913 if (ret < 0)
6914 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01006915 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006916 if (msg->chunk_len) {
6917 msg->msg_state = HTTP_MSG_DATA;
6918 goto switch_states;
6919 }
6920 msg->msg_state = HTTP_MSG_TRAILERS;
6921 /* fall through for HTTP_MSG_TRAILERS */
6922
6923 case HTTP_MSG_TRAILERS:
6924 ret = http_forward_trailers(msg);
6925 if (ret < 0)
6926 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01006927 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
6928 /* default_ret */ 1,
6929 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006930 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006931 if (!ret)
6932 goto missing_data_or_waiting;
6933 break;
6934
6935 case HTTP_MSG_ENDING:
6936 goto ending;
6937
6938 default:
6939 /* This should no happen in this function */
6940 goto error;
6941 }
6942
6943 msg->msg_state = HTTP_MSG_ENDING;
6944 ending:
6945 /* we may have some pending data starting at res->buf->p such as a last
6946 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006947 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006948 /* default_ret */ msg->next,
6949 /* on_error */ goto error);
6950 b_adv(chn->buf, ret);
6951 msg->next -= ret;
6952 if (msg->next)
6953 goto missing_data_or_waiting;
6954
Christopher Fauletda02e172015-12-04 09:25:05 +01006955 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006956 /* default_ret */ 1,
6957 /* on_error */ goto error,
6958 /* on_wait */ goto waiting);
6959 msg->msg_state = HTTP_MSG_DONE;
6960 return 1;
6961
6962 missing_data_or_waiting:
6963 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006964 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006965 /* default_ret */ msg->next,
6966 /* on_error */ goto error);
6967 b_adv(chn->buf, ret);
6968 msg->next -= ret;
6969 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6970 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006971 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006972 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
6973 waiting:
6974 return 0;
6975
6976 chunk_parsing_error:
6977 if (msg->err_pos >= 0) {
6978 if (chn->flags & CF_ISRESP)
6979 http_capture_bad_message(&s->be->invalid_rep, s, msg,
6980 msg->msg_state, strm_fe(s));
6981 else
6982 http_capture_bad_message(&strm_fe(s)->invalid_req, s,
6983 msg, msg->msg_state, s->be);
6984 }
6985 error:
6986 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006987}
6988
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006989
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006990/* Iterate the same filter through all request headers.
6991 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006992 * Since it can manage the switch to another backend, it updates the per-proxy
6993 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006994 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006995int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006996{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006997 char *cur_ptr, *cur_end, *cur_next;
6998 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006999 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007000 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007001 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007002
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007003 last_hdr = 0;
7004
Willy Tarreau9b28e032012-10-12 23:49:43 +02007005 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007006 old_idx = 0;
7007
7008 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007009 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007010 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007011 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007012 (exp->action == ACT_ALLOW ||
7013 exp->action == ACT_DENY ||
7014 exp->action == ACT_TARPIT))
7015 return 0;
7016
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007017 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007018 if (!cur_idx)
7019 break;
7020
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007021 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007022 cur_ptr = cur_next;
7023 cur_end = cur_ptr + cur_hdr->len;
7024 cur_next = cur_end + cur_hdr->cr + 1;
7025
7026 /* Now we have one header between cur_ptr and cur_end,
7027 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007028 */
7029
Willy Tarreau15a53a42015-01-21 13:39:42 +01007030 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007031 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007032 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007033 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007034 last_hdr = 1;
7035 break;
7036
7037 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007038 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007039 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007040 break;
7041
7042 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007043 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007044 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007045 break;
7046
7047 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007048 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7049 if (trash.len < 0)
7050 return -1;
7051
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007052 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007053 /* FIXME: if the user adds a newline in the replacement, the
7054 * index will not be recalculated for now, and the new line
7055 * will not be counted as a new header.
7056 */
7057
7058 cur_end += delta;
7059 cur_next += delta;
7060 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007061 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007062 break;
7063
7064 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007065 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007066 cur_next += delta;
7067
Willy Tarreaufa355d42009-11-29 18:12:29 +01007068 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007069 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7070 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007071 cur_hdr->len = 0;
7072 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007073 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007074 break;
7075
7076 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007077 }
7078
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007079 /* keep the link from this header to next one in case of later
7080 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007081 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007082 old_idx = cur_idx;
7083 }
7084 return 0;
7085}
7086
7087
7088/* Apply the filter to the request line.
7089 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7090 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007091 * Since it can manage the switch to another backend, it updates the per-proxy
7092 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007093 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007094int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007095{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007096 char *cur_ptr, *cur_end;
7097 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007098 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007099 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007100
Willy Tarreau3d300592007-03-18 18:34:41 +01007101 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007102 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007103 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007104 (exp->action == ACT_ALLOW ||
7105 exp->action == ACT_DENY ||
7106 exp->action == ACT_TARPIT))
7107 return 0;
7108 else if (exp->action == ACT_REMOVE)
7109 return 0;
7110
7111 done = 0;
7112
Willy Tarreau9b28e032012-10-12 23:49:43 +02007113 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007114 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007115
7116 /* Now we have the request line between cur_ptr and cur_end */
7117
Willy Tarreau15a53a42015-01-21 13:39:42 +01007118 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007119 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007120 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007121 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007122 done = 1;
7123 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007124
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007125 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007126 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007127 done = 1;
7128 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007129
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007130 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007131 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007132 done = 1;
7133 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007134
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007135 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007136 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7137 if (trash.len < 0)
7138 return -1;
7139
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007140 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007141 /* FIXME: if the user adds a newline in the replacement, the
7142 * index will not be recalculated for now, and the new line
7143 * will not be counted as a new header.
7144 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007145
Willy Tarreaufa355d42009-11-29 18:12:29 +01007146 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007147 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007148 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007149 HTTP_MSG_RQMETH,
7150 cur_ptr, cur_end + 1,
7151 NULL, NULL);
7152 if (unlikely(!cur_end))
7153 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007154
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007155 /* we have a full request and we know that we have either a CR
7156 * or an LF at <ptr>.
7157 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007158 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7159 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007160 /* there is no point trying this regex on headers */
7161 return 1;
7162 }
7163 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007164 return done;
7165}
Willy Tarreau97de6242006-12-27 17:18:38 +01007166
Willy Tarreau58f10d72006-12-04 02:26:12 +01007167
Willy Tarreau58f10d72006-12-04 02:26:12 +01007168
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007169/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007170 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007171 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007172 * unparsable request. Since it can manage the switch to another backend, it
7173 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007174 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007175int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007176{
Willy Tarreau192252e2015-04-04 01:47:55 +02007177 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007178 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007179 struct hdr_exp *exp;
7180
7181 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007182 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007183
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007184 /*
7185 * The interleaving of transformations and verdicts
7186 * makes it difficult to decide to continue or stop
7187 * the evaluation.
7188 */
7189
Willy Tarreau6c123b12010-01-28 20:22:06 +01007190 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7191 break;
7192
Willy Tarreau3d300592007-03-18 18:34:41 +01007193 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007194 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007195 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007196 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007197
7198 /* if this filter had a condition, evaluate it now and skip to
7199 * next filter if the condition does not match.
7200 */
7201 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007202 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007203 ret = acl_pass(ret);
7204 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7205 ret = !ret;
7206
7207 if (!ret)
7208 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007209 }
7210
7211 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007212 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007213 if (unlikely(ret < 0))
7214 return -1;
7215
7216 if (likely(ret == 0)) {
7217 /* The filter did not match the request, it can be
7218 * iterated through all headers.
7219 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007220 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7221 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007222 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007223 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007224 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007225}
7226
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007227
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007228/* Find the end of a cookie value contained between <s> and <e>. It works the
7229 * same way as with headers above except that the semi-colon also ends a token.
7230 * See RFC2965 for more information. Note that it requires a valid header to
7231 * return a valid result.
7232 */
7233char *find_cookie_value_end(char *s, const char *e)
7234{
7235 int quoted, qdpair;
7236
7237 quoted = qdpair = 0;
7238 for (; s < e; s++) {
7239 if (qdpair) qdpair = 0;
7240 else if (quoted) {
7241 if (*s == '\\') qdpair = 1;
7242 else if (*s == '"') quoted = 0;
7243 }
7244 else if (*s == '"') quoted = 1;
7245 else if (*s == ',' || *s == ';') return s;
7246 }
7247 return s;
7248}
7249
7250/* Delete a value in a header between delimiters <from> and <next> in buffer
7251 * <buf>. The number of characters displaced is returned, and the pointer to
7252 * the first delimiter is updated if required. The function tries as much as
7253 * possible to respect the following principles :
7254 * - replace <from> delimiter by the <next> one unless <from> points to a
7255 * colon, in which case <next> is simply removed
7256 * - set exactly one space character after the new first delimiter, unless
7257 * there are not enough characters in the block being moved to do so.
7258 * - remove unneeded spaces before the previous delimiter and after the new
7259 * one.
7260 *
7261 * It is the caller's responsibility to ensure that :
7262 * - <from> points to a valid delimiter or the colon ;
7263 * - <next> points to a valid delimiter or the final CR/LF ;
7264 * - there are non-space chars before <from> ;
7265 * - there is a CR/LF at or after <next>.
7266 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007267int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007268{
7269 char *prev = *from;
7270
7271 if (*prev == ':') {
7272 /* We're removing the first value, preserve the colon and add a
7273 * space if possible.
7274 */
7275 if (!http_is_crlf[(unsigned char)*next])
7276 next++;
7277 prev++;
7278 if (prev < next)
7279 *prev++ = ' ';
7280
7281 while (http_is_spht[(unsigned char)*next])
7282 next++;
7283 } else {
7284 /* Remove useless spaces before the old delimiter. */
7285 while (http_is_spht[(unsigned char)*(prev-1)])
7286 prev--;
7287 *from = prev;
7288
7289 /* copy the delimiter and if possible a space if we're
7290 * not at the end of the line.
7291 */
7292 if (!http_is_crlf[(unsigned char)*next]) {
7293 *prev++ = *next++;
7294 if (prev + 1 < next)
7295 *prev++ = ' ';
7296 while (http_is_spht[(unsigned char)*next])
7297 next++;
7298 }
7299 }
7300 return buffer_replace2(buf, prev, next, NULL, 0);
7301}
7302
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007303/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007304 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007305 * desirable to call it only when needed. This code is quite complex because
7306 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7307 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007308 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007309void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007310{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007311 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007312 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007313 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007314 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007315 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7316 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007317
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007318 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007319 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007320 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007321
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007322 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007323 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007324 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007325
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007326 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007327 hdr_beg = hdr_next;
7328 hdr_end = hdr_beg + cur_hdr->len;
7329 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007330
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007331 /* We have one full header between hdr_beg and hdr_end, and the
7332 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007333 * "Cookie:" headers.
7334 */
7335
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007336 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007337 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007338 old_idx = cur_idx;
7339 continue;
7340 }
7341
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007342 del_from = NULL; /* nothing to be deleted */
7343 preserve_hdr = 0; /* assume we may kill the whole header */
7344
Willy Tarreau58f10d72006-12-04 02:26:12 +01007345 /* Now look for cookies. Conforming to RFC2109, we have to support
7346 * attributes whose name begin with a '$', and associate them with
7347 * the right cookie, if we want to delete this cookie.
7348 * So there are 3 cases for each cookie read :
7349 * 1) it's a special attribute, beginning with a '$' : ignore it.
7350 * 2) it's a server id cookie that we *MAY* want to delete : save
7351 * some pointers on it (last semi-colon, beginning of cookie...)
7352 * 3) it's an application cookie : we *MAY* have to delete a previous
7353 * "special" cookie.
7354 * At the end of loop, if a "special" cookie remains, we may have to
7355 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007356 * *MUST* delete it.
7357 *
7358 * Note: RFC2965 is unclear about the processing of spaces around
7359 * the equal sign in the ATTR=VALUE form. A careful inspection of
7360 * the RFC explicitly allows spaces before it, and not within the
7361 * tokens (attrs or values). An inspection of RFC2109 allows that
7362 * too but section 10.1.3 lets one think that spaces may be allowed
7363 * after the equal sign too, resulting in some (rare) buggy
7364 * implementations trying to do that. So let's do what servers do.
7365 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7366 * allowed quoted strings in values, with any possible character
7367 * after a backslash, including control chars and delimitors, which
7368 * causes parsing to become ambiguous. Browsers also allow spaces
7369 * within values even without quotes.
7370 *
7371 * We have to keep multiple pointers in order to support cookie
7372 * removal at the beginning, middle or end of header without
7373 * corrupting the header. All of these headers are valid :
7374 *
7375 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7376 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7377 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7378 * | | | | | | | | |
7379 * | | | | | | | | hdr_end <--+
7380 * | | | | | | | +--> next
7381 * | | | | | | +----> val_end
7382 * | | | | | +-----------> val_beg
7383 * | | | | +--------------> equal
7384 * | | | +----------------> att_end
7385 * | | +---------------------> att_beg
7386 * | +--------------------------> prev
7387 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007388 */
7389
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007390 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7391 /* Iterate through all cookies on this line */
7392
7393 /* find att_beg */
7394 att_beg = prev + 1;
7395 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7396 att_beg++;
7397
7398 /* find att_end : this is the first character after the last non
7399 * space before the equal. It may be equal to hdr_end.
7400 */
7401 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007402
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007403 while (equal < hdr_end) {
7404 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007405 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007406 if (http_is_spht[(unsigned char)*equal++])
7407 continue;
7408 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007409 }
7410
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007411 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7412 * is between <att_beg> and <equal>, both may be identical.
7413 */
7414
7415 /* look for end of cookie if there is an equal sign */
7416 if (equal < hdr_end && *equal == '=') {
7417 /* look for the beginning of the value */
7418 val_beg = equal + 1;
7419 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7420 val_beg++;
7421
7422 /* find the end of the value, respecting quotes */
7423 next = find_cookie_value_end(val_beg, hdr_end);
7424
7425 /* make val_end point to the first white space or delimitor after the value */
7426 val_end = next;
7427 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7428 val_end--;
7429 } else {
7430 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007431 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007432
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007433 /* We have nothing to do with attributes beginning with '$'. However,
7434 * they will automatically be removed if a header before them is removed,
7435 * since they're supposed to be linked together.
7436 */
7437 if (*att_beg == '$')
7438 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007439
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007440 /* Ignore cookies with no equal sign */
7441 if (equal == next) {
7442 /* This is not our cookie, so we must preserve it. But if we already
7443 * scheduled another cookie for removal, we cannot remove the
7444 * complete header, but we can remove the previous block itself.
7445 */
7446 preserve_hdr = 1;
7447 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007448 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007449 val_end += delta;
7450 next += delta;
7451 hdr_end += delta;
7452 hdr_next += delta;
7453 cur_hdr->len += delta;
7454 http_msg_move_end(&txn->req, delta);
7455 prev = del_from;
7456 del_from = NULL;
7457 }
7458 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007459 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007460
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007461 /* if there are spaces around the equal sign, we need to
7462 * strip them otherwise we'll get trouble for cookie captures,
7463 * or even for rewrites. Since this happens extremely rarely,
7464 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007465 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007466 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7467 int stripped_before = 0;
7468 int stripped_after = 0;
7469
7470 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007471 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007472 equal += stripped_before;
7473 val_beg += stripped_before;
7474 }
7475
7476 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007477 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007478 val_beg += stripped_after;
7479 stripped_before += stripped_after;
7480 }
7481
7482 val_end += stripped_before;
7483 next += stripped_before;
7484 hdr_end += stripped_before;
7485 hdr_next += stripped_before;
7486 cur_hdr->len += stripped_before;
7487 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007488 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007489 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007490
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007491 /* First, let's see if we want to capture this cookie. We check
7492 * that we don't already have a client side cookie, because we
7493 * can only capture one. Also as an optimisation, we ignore
7494 * cookies shorter than the declared name.
7495 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007496 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7497 (val_end - att_beg >= sess->fe->capture_namelen) &&
7498 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007499 int log_len = val_end - att_beg;
7500
7501 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7502 Alert("HTTP logging : out of memory.\n");
7503 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007504 if (log_len > sess->fe->capture_len)
7505 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007506 memcpy(txn->cli_cookie, att_beg, log_len);
7507 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007508 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007509 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007510
Willy Tarreaubca99692010-10-06 19:25:55 +02007511 /* Persistence cookies in passive, rewrite or insert mode have the
7512 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007513 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007514 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007515 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007516 * For cookies in prefix mode, the form is :
7517 *
7518 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007519 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007520 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7521 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7522 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007523 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007524
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007525 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7526 * have the server ID between val_beg and delim, and the original cookie between
7527 * delim+1 and val_end. Otherwise, delim==val_end :
7528 *
7529 * Cookie: NAME=SRV; # in all but prefix modes
7530 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7531 * | || || | |+-> next
7532 * | || || | +--> val_end
7533 * | || || +---------> delim
7534 * | || |+------------> val_beg
7535 * | || +-------------> att_end = equal
7536 * | |+-----------------> att_beg
7537 * | +------------------> prev
7538 * +-------------------------> hdr_beg
7539 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007540
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007541 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007542 for (delim = val_beg; delim < val_end; delim++)
7543 if (*delim == COOKIE_DELIM)
7544 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007545 } else {
7546 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007547 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007548 /* Now check if the cookie contains a date field, which would
7549 * appear after a vertical bar ('|') just after the server name
7550 * and before the delimiter.
7551 */
7552 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7553 if (vbar1) {
7554 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007555 * right is the last seen date. It is a base64 encoded
7556 * 30-bit value representing the UNIX date since the
7557 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007558 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007559 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007560 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007561 if (val_end - vbar1 >= 5) {
7562 val = b64tos30(vbar1);
7563 if (val > 0)
7564 txn->cookie_last_date = val << 2;
7565 }
7566 /* look for a second vertical bar */
7567 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7568 if (vbar1 && (val_end - vbar1 > 5)) {
7569 val = b64tos30(vbar1 + 1);
7570 if (val > 0)
7571 txn->cookie_first_date = val << 2;
7572 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007573 }
7574 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007575
Willy Tarreauf64d1412010-10-07 20:06:11 +02007576 /* if the cookie has an expiration date and the proxy wants to check
7577 * it, then we do that now. We first check if the cookie is too old,
7578 * then only if it has expired. We detect strict overflow because the
7579 * time resolution here is not great (4 seconds). Cookies with dates
7580 * in the future are ignored if their offset is beyond one day. This
7581 * allows an admin to fix timezone issues without expiring everyone
7582 * and at the same time avoids keeping unwanted side effects for too
7583 * long.
7584 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007585 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7586 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007587 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007588 txn->flags &= ~TX_CK_MASK;
7589 txn->flags |= TX_CK_OLD;
7590 delim = val_beg; // let's pretend we have not found the cookie
7591 txn->cookie_first_date = 0;
7592 txn->cookie_last_date = 0;
7593 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007594 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7595 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007596 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007597 txn->flags &= ~TX_CK_MASK;
7598 txn->flags |= TX_CK_EXPIRED;
7599 delim = val_beg; // let's pretend we have not found the cookie
7600 txn->cookie_first_date = 0;
7601 txn->cookie_last_date = 0;
7602 }
7603
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007604 /* Here, we'll look for the first running server which supports the cookie.
7605 * This allows to share a same cookie between several servers, for example
7606 * to dedicate backup servers to specific servers only.
7607 * However, to prevent clients from sticking to cookie-less backup server
7608 * when they have incidentely learned an empty cookie, we simply ignore
7609 * empty cookies and mark them as invalid.
7610 * The same behaviour is applied when persistence must be ignored.
7611 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007612 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007613 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007614
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007615 while (srv) {
7616 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7617 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007618 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007619 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007620 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007621 /* we found the server and we can use it */
7622 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007623 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007624 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007625 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007626 break;
7627 } else {
7628 /* we found a server, but it's down,
7629 * mark it as such and go on in case
7630 * another one is available.
7631 */
7632 txn->flags &= ~TX_CK_MASK;
7633 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007634 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007635 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007636 srv = srv->next;
7637 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007638
Willy Tarreauf64d1412010-10-07 20:06:11 +02007639 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007640 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007641 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007642 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007643 txn->flags |= TX_CK_UNUSED;
7644 else
7645 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007646 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007647
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007648 /* depending on the cookie mode, we may have to either :
7649 * - delete the complete cookie if we're in insert+indirect mode, so that
7650 * the server never sees it ;
7651 * - remove the server id from the cookie value, and tag the cookie as an
7652 * application cookie so that it does not get accidentely removed later,
7653 * if we're in cookie prefix mode
7654 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007655 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007656 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007657
Willy Tarreau9b28e032012-10-12 23:49:43 +02007658 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007659 val_end += delta;
7660 next += delta;
7661 hdr_end += delta;
7662 hdr_next += delta;
7663 cur_hdr->len += delta;
7664 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007665
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007666 del_from = NULL;
7667 preserve_hdr = 1; /* we want to keep this cookie */
7668 }
7669 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007670 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007671 del_from = prev;
7672 }
7673 } else {
7674 /* This is not our cookie, so we must preserve it. But if we already
7675 * scheduled another cookie for removal, we cannot remove the
7676 * complete header, but we can remove the previous block itself.
7677 */
7678 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007679
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007680 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007681 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007682 if (att_beg >= del_from)
7683 att_beg += delta;
7684 if (att_end >= del_from)
7685 att_end += delta;
7686 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007687 val_end += delta;
7688 next += delta;
7689 hdr_end += delta;
7690 hdr_next += delta;
7691 cur_hdr->len += delta;
7692 http_msg_move_end(&txn->req, delta);
7693 prev = del_from;
7694 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007695 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007696 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007697
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007698 /* continue with next cookie on this header line */
7699 att_beg = next;
7700 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007701
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007702 /* There are no more cookies on this line.
7703 * We may still have one (or several) marked for deletion at the
7704 * end of the line. We must do this now in two ways :
7705 * - if some cookies must be preserved, we only delete from the
7706 * mark to the end of line ;
7707 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007708 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007709 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007710 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007711 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007712 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007713 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007714 cur_hdr->len += delta;
7715 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007716 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007717
7718 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007719 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7720 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007721 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007722 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007723 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007724 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007725 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007726 }
7727
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007728 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007729 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007730 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007731}
7732
7733
Willy Tarreaua15645d2007-03-18 16:22:39 +01007734/* Iterate the same filter through all response headers contained in <rtr>.
7735 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7736 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007737int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007738{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007739 char *cur_ptr, *cur_end, *cur_next;
7740 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007741 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007742 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007743 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007744
7745 last_hdr = 0;
7746
Willy Tarreau9b28e032012-10-12 23:49:43 +02007747 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007748 old_idx = 0;
7749
7750 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007751 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007752 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007753 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007754 (exp->action == ACT_ALLOW ||
7755 exp->action == ACT_DENY))
7756 return 0;
7757
7758 cur_idx = txn->hdr_idx.v[old_idx].next;
7759 if (!cur_idx)
7760 break;
7761
7762 cur_hdr = &txn->hdr_idx.v[cur_idx];
7763 cur_ptr = cur_next;
7764 cur_end = cur_ptr + cur_hdr->len;
7765 cur_next = cur_end + cur_hdr->cr + 1;
7766
7767 /* Now we have one header between cur_ptr and cur_end,
7768 * and the next header starts at cur_next.
7769 */
7770
Willy Tarreau15a53a42015-01-21 13:39:42 +01007771 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007772 switch (exp->action) {
7773 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007774 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007775 last_hdr = 1;
7776 break;
7777
7778 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007779 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007780 last_hdr = 1;
7781 break;
7782
7783 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007784 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7785 if (trash.len < 0)
7786 return -1;
7787
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007788 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007789 /* FIXME: if the user adds a newline in the replacement, the
7790 * index will not be recalculated for now, and the new line
7791 * will not be counted as a new header.
7792 */
7793
7794 cur_end += delta;
7795 cur_next += delta;
7796 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007797 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007798 break;
7799
7800 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007801 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007802 cur_next += delta;
7803
Willy Tarreaufa355d42009-11-29 18:12:29 +01007804 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007805 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7806 txn->hdr_idx.used--;
7807 cur_hdr->len = 0;
7808 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007809 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007810 break;
7811
7812 }
7813 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007814
7815 /* keep the link from this header to next one in case of later
7816 * removal of next header.
7817 */
7818 old_idx = cur_idx;
7819 }
7820 return 0;
7821}
7822
7823
7824/* Apply the filter to the status line in the response buffer <rtr>.
7825 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7826 * or -1 if a replacement resulted in an invalid status line.
7827 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007828int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007829{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007830 char *cur_ptr, *cur_end;
7831 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007832 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007833 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007834
7835
Willy Tarreau3d300592007-03-18 18:34:41 +01007836 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007837 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007838 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007839 (exp->action == ACT_ALLOW ||
7840 exp->action == ACT_DENY))
7841 return 0;
7842 else if (exp->action == ACT_REMOVE)
7843 return 0;
7844
7845 done = 0;
7846
Willy Tarreau9b28e032012-10-12 23:49:43 +02007847 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007848 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007849
7850 /* Now we have the status line between cur_ptr and cur_end */
7851
Willy Tarreau15a53a42015-01-21 13:39:42 +01007852 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007853 switch (exp->action) {
7854 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007855 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007856 done = 1;
7857 break;
7858
7859 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007860 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007861 done = 1;
7862 break;
7863
7864 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007865 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7866 if (trash.len < 0)
7867 return -1;
7868
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007869 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007870 /* FIXME: if the user adds a newline in the replacement, the
7871 * index will not be recalculated for now, and the new line
7872 * will not be counted as a new header.
7873 */
7874
Willy Tarreaufa355d42009-11-29 18:12:29 +01007875 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007876 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007877 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007878 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007879 cur_ptr, cur_end + 1,
7880 NULL, NULL);
7881 if (unlikely(!cur_end))
7882 return -1;
7883
7884 /* we have a full respnse and we know that we have either a CR
7885 * or an LF at <ptr>.
7886 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007887 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007888 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007889 /* there is no point trying this regex on headers */
7890 return 1;
7891 }
7892 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007893 return done;
7894}
7895
7896
7897
7898/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007899 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007900 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7901 * unparsable response.
7902 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007903int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007904{
Willy Tarreau192252e2015-04-04 01:47:55 +02007905 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007906 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007907 struct hdr_exp *exp;
7908
7909 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007910 int ret;
7911
7912 /*
7913 * The interleaving of transformations and verdicts
7914 * makes it difficult to decide to continue or stop
7915 * the evaluation.
7916 */
7917
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007918 if (txn->flags & TX_SVDENY)
7919 break;
7920
Willy Tarreau3d300592007-03-18 18:34:41 +01007921 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007922 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7923 exp->action == ACT_PASS)) {
7924 exp = exp->next;
7925 continue;
7926 }
7927
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007928 /* if this filter had a condition, evaluate it now and skip to
7929 * next filter if the condition does not match.
7930 */
7931 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007932 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007933 ret = acl_pass(ret);
7934 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7935 ret = !ret;
7936 if (!ret)
7937 continue;
7938 }
7939
Willy Tarreaua15645d2007-03-18 16:22:39 +01007940 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007941 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007942 if (unlikely(ret < 0))
7943 return -1;
7944
7945 if (likely(ret == 0)) {
7946 /* The filter did not match the response, it can be
7947 * iterated through all headers.
7948 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007949 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7950 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007951 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007952 }
7953 return 0;
7954}
7955
7956
Willy Tarreaua15645d2007-03-18 16:22:39 +01007957/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007958 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007959 * desirable to call it only when needed. This function is also used when we
7960 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007961 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007962void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007963{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007964 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007965 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01007966 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007967 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007968 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007969 char *hdr_beg, *hdr_end, *hdr_next;
7970 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007971
Willy Tarreaua15645d2007-03-18 16:22:39 +01007972 /* Iterate through the headers.
7973 * we start with the start line.
7974 */
7975 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007976 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007977
7978 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7979 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007980 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007981
7982 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007983 hdr_beg = hdr_next;
7984 hdr_end = hdr_beg + cur_hdr->len;
7985 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007986
Willy Tarreau24581ba2010-08-31 22:39:35 +02007987 /* We have one full header between hdr_beg and hdr_end, and the
7988 * next header starts at hdr_next. We're only interested in
7989 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007990 */
7991
Willy Tarreau24581ba2010-08-31 22:39:35 +02007992 is_cookie2 = 0;
7993 prev = hdr_beg + 10;
7994 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007995 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007996 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7997 if (!val) {
7998 old_idx = cur_idx;
7999 continue;
8000 }
8001 is_cookie2 = 1;
8002 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008003 }
8004
Willy Tarreau24581ba2010-08-31 22:39:35 +02008005 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8006 * <prev> points to the colon.
8007 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008008 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008009
Willy Tarreau24581ba2010-08-31 22:39:35 +02008010 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8011 * check-cache is enabled) and we are not interested in checking
8012 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008013 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02008014 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008015 return;
8016
Willy Tarreau24581ba2010-08-31 22:39:35 +02008017 /* OK so now we know we have to process this response cookie.
8018 * The format of the Set-Cookie header is slightly different
8019 * from the format of the Cookie header in that it does not
8020 * support the comma as a cookie delimiter (thus the header
8021 * cannot be folded) because the Expires attribute described in
8022 * the original Netscape's spec may contain an unquoted date
8023 * with a comma inside. We have to live with this because
8024 * many browsers don't support Max-Age and some browsers don't
8025 * support quoted strings. However the Set-Cookie2 header is
8026 * clean.
8027 *
8028 * We have to keep multiple pointers in order to support cookie
8029 * removal at the beginning, middle or end of header without
8030 * corrupting the header (in case of set-cookie2). A special
8031 * pointer, <scav> points to the beginning of the set-cookie-av
8032 * fields after the first semi-colon. The <next> pointer points
8033 * either to the end of line (set-cookie) or next unquoted comma
8034 * (set-cookie2). All of these headers are valid :
8035 *
8036 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8037 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8038 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8039 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8040 * | | | | | | | | | |
8041 * | | | | | | | | +-> next hdr_end <--+
8042 * | | | | | | | +------------> scav
8043 * | | | | | | +--------------> val_end
8044 * | | | | | +--------------------> val_beg
8045 * | | | | +----------------------> equal
8046 * | | | +------------------------> att_end
8047 * | | +----------------------------> att_beg
8048 * | +------------------------------> prev
8049 * +-----------------------------------------> hdr_beg
8050 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008051
Willy Tarreau24581ba2010-08-31 22:39:35 +02008052 for (; prev < hdr_end; prev = next) {
8053 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008054
Willy Tarreau24581ba2010-08-31 22:39:35 +02008055 /* find att_beg */
8056 att_beg = prev + 1;
8057 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8058 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008059
Willy Tarreau24581ba2010-08-31 22:39:35 +02008060 /* find att_end : this is the first character after the last non
8061 * space before the equal. It may be equal to hdr_end.
8062 */
8063 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008064
Willy Tarreau24581ba2010-08-31 22:39:35 +02008065 while (equal < hdr_end) {
8066 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8067 break;
8068 if (http_is_spht[(unsigned char)*equal++])
8069 continue;
8070 att_end = equal;
8071 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008072
Willy Tarreau24581ba2010-08-31 22:39:35 +02008073 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8074 * is between <att_beg> and <equal>, both may be identical.
8075 */
8076
8077 /* look for end of cookie if there is an equal sign */
8078 if (equal < hdr_end && *equal == '=') {
8079 /* look for the beginning of the value */
8080 val_beg = equal + 1;
8081 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8082 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008083
Willy Tarreau24581ba2010-08-31 22:39:35 +02008084 /* find the end of the value, respecting quotes */
8085 next = find_cookie_value_end(val_beg, hdr_end);
8086
8087 /* make val_end point to the first white space or delimitor after the value */
8088 val_end = next;
8089 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8090 val_end--;
8091 } else {
8092 /* <equal> points to next comma, semi-colon or EOL */
8093 val_beg = val_end = next = equal;
8094 }
8095
8096 if (next < hdr_end) {
8097 /* Set-Cookie2 supports multiple cookies, and <next> points to
8098 * a colon or semi-colon before the end. So skip all attr-value
8099 * pairs and look for the next comma. For Set-Cookie, since
8100 * commas are permitted in values, skip to the end.
8101 */
8102 if (is_cookie2)
8103 next = find_hdr_value_end(next, hdr_end);
8104 else
8105 next = hdr_end;
8106 }
8107
8108 /* Now everything is as on the diagram above */
8109
8110 /* Ignore cookies with no equal sign */
8111 if (equal == val_end)
8112 continue;
8113
8114 /* If there are spaces around the equal sign, we need to
8115 * strip them otherwise we'll get trouble for cookie captures,
8116 * or even for rewrites. Since this happens extremely rarely,
8117 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008118 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008119 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8120 int stripped_before = 0;
8121 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008122
Willy Tarreau24581ba2010-08-31 22:39:35 +02008123 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008124 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008125 equal += stripped_before;
8126 val_beg += stripped_before;
8127 }
8128
8129 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008130 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008131 val_beg += stripped_after;
8132 stripped_before += stripped_after;
8133 }
8134
8135 val_end += stripped_before;
8136 next += stripped_before;
8137 hdr_end += stripped_before;
8138 hdr_next += stripped_before;
8139 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008140 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008141 }
8142
8143 /* First, let's see if we want to capture this cookie. We check
8144 * that we don't already have a server side cookie, because we
8145 * can only capture one. Also as an optimisation, we ignore
8146 * cookies shorter than the declared name.
8147 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008148 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008149 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008150 (val_end - att_beg >= sess->fe->capture_namelen) &&
8151 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008152 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008153 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008154 Alert("HTTP logging : out of memory.\n");
8155 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008156 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008157 if (log_len > sess->fe->capture_len)
8158 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008159 memcpy(txn->srv_cookie, att_beg, log_len);
8160 txn->srv_cookie[log_len] = 0;
8161 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008162 }
8163
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008164 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008165 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008166 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008167 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8168 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008169 /* assume passive cookie by default */
8170 txn->flags &= ~TX_SCK_MASK;
8171 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008172
8173 /* If the cookie is in insert mode on a known server, we'll delete
8174 * this occurrence because we'll insert another one later.
8175 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008176 * a direct access.
8177 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008178 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008179 /* The "preserve" flag was set, we don't want to touch the
8180 * server's cookie.
8181 */
8182 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008183 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008184 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008185 /* this cookie must be deleted */
8186 if (*prev == ':' && next == hdr_end) {
8187 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008188 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008189 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8190 txn->hdr_idx.used--;
8191 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008192 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008193 hdr_next += delta;
8194 http_msg_move_end(&txn->rsp, delta);
8195 /* note: while both invalid now, <next> and <hdr_end>
8196 * are still equal, so the for() will stop as expected.
8197 */
8198 } else {
8199 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008200 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008201 next = prev;
8202 hdr_end += delta;
8203 hdr_next += delta;
8204 cur_hdr->len += delta;
8205 http_msg_move_end(&txn->rsp, delta);
8206 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008207 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008208 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008209 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008210 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008211 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008212 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008213 * with this server since we know it.
8214 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008215 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008216 next += delta;
8217 hdr_end += delta;
8218 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008219 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008220 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008221
Willy Tarreauf1348312010-10-07 15:54:11 +02008222 txn->flags &= ~TX_SCK_MASK;
8223 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008224 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008225 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008226 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008227 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008228 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008229 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008230 next += delta;
8231 hdr_end += delta;
8232 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008233 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008234 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008235
Willy Tarreau827aee92011-03-10 16:55:02 +01008236 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008237 txn->flags &= ~TX_SCK_MASK;
8238 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008239 }
8240 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008241 /* that's done for this cookie, check the next one on the same
8242 * line when next != hdr_end (only if is_cookie2).
8243 */
8244 }
8245 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008246 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008247 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008248}
8249
8250
Willy Tarreaua15645d2007-03-18 16:22:39 +01008251/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008252 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008253 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008254void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008255{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008256 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008257 char *p1, *p2;
8258
8259 char *cur_ptr, *cur_end, *cur_next;
8260 int cur_idx;
8261
Willy Tarreau5df51872007-11-25 16:20:08 +01008262 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008263 return;
8264
8265 /* Iterate through the headers.
8266 * we start with the start line.
8267 */
8268 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008269 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008270
8271 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8272 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008273 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008274
8275 cur_hdr = &txn->hdr_idx.v[cur_idx];
8276 cur_ptr = cur_next;
8277 cur_end = cur_ptr + cur_hdr->len;
8278 cur_next = cur_end + cur_hdr->cr + 1;
8279
8280 /* We have one full header between cur_ptr and cur_end, and the
8281 * next header starts at cur_next. We're only interested in
8282 * "Cookie:" headers.
8283 */
8284
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008285 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8286 if (val) {
8287 if ((cur_end - (cur_ptr + val) >= 8) &&
8288 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8289 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8290 return;
8291 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008292 }
8293
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008294 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8295 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008296 continue;
8297
8298 /* OK, right now we know we have a cache-control header at cur_ptr */
8299
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008300 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008301
8302 if (p1 >= cur_end) /* no more info */
8303 continue;
8304
8305 /* p1 is at the beginning of the value */
8306 p2 = p1;
8307
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008308 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008309 p2++;
8310
8311 /* we have a complete value between p1 and p2 */
8312 if (p2 < cur_end && *p2 == '=') {
8313 /* we have something of the form no-cache="set-cookie" */
8314 if ((cur_end - p1 >= 21) &&
8315 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8316 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008317 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008318 continue;
8319 }
8320
8321 /* OK, so we know that either p2 points to the end of string or to a comma */
8322 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008323 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008324 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8325 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8326 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008327 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008328 return;
8329 }
8330
8331 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008332 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008333 continue;
8334 }
8335 }
8336}
8337
Willy Tarreau58f10d72006-12-04 02:26:12 +01008338
Willy Tarreaub2513902006-12-17 14:52:38 +01008339/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008340 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008341 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008342 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008343 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008344 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008345 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008346 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008347 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008348int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008349{
8350 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008351 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008352 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008353
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008354 if (!uri_auth)
8355 return 0;
8356
Cyril Bonté70be45d2010-10-12 00:14:35 +02008357 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008358 return 0;
8359
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008360 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008361 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008362 return 0;
8363
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008364 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008365 return 0;
8366
Willy Tarreaub2513902006-12-17 14:52:38 +01008367 return 1;
8368}
8369
Willy Tarreau4076a152009-04-02 15:18:36 +02008370/*
8371 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008372 * By default it tries to report the error position as msg->err_pos. However if
8373 * this one is not set, it will then report msg->next, which is the last known
8374 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008375 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008376 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008377void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008378 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008379 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008380{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008381 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008382 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008383 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008384
Willy Tarreauf3764b72016-03-31 13:45:10 +02008385 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02008386 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008387 len1 = MIN(len1, es->len);
8388 len2 = es->len - len1; /* remaining data if buffer wraps */
8389
Willy Tarreauf3764b72016-03-31 13:45:10 +02008390 if (!es->buf)
8391 es->buf = malloc(global.tune.bufsize);
8392
8393 if (es->buf) {
8394 memcpy(es->buf, chn->buf->p, len1);
8395 if (len2)
8396 memcpy(es->buf + len1, chn->buf->data, len2);
8397 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008398
Willy Tarreau4076a152009-04-02 15:18:36 +02008399 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008400 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008401 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008402 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008403
Willy Tarreau4076a152009-04-02 15:18:36 +02008404 es->when = date; // user-visible date
8405 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008406 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008407 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008408 if (objt_conn(sess->origin))
8409 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008410 else
8411 memset(&es->src, 0, sizeof(es->src));
8412
Willy Tarreau078272e2010-12-12 12:46:33 +01008413 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008414 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008415 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008416 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008417 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008418 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008419 es->b_out = chn->buf->o;
8420 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008421 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008422 es->m_clen = msg->chunk_len;
8423 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008424}
Willy Tarreaub2513902006-12-17 14:52:38 +01008425
Willy Tarreau294c4732011-12-16 21:35:50 +01008426/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8427 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8428 * performed over the whole headers. Otherwise it must contain a valid header
8429 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8430 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8431 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8432 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008433 * -1. The value fetch stops at commas, so this function is suited for use with
8434 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008435 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008436 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008437unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008438 struct hdr_idx *idx, int occ,
8439 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008440{
Willy Tarreau294c4732011-12-16 21:35:50 +01008441 struct hdr_ctx local_ctx;
8442 char *ptr_hist[MAX_HDR_HISTORY];
8443 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008444 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008445 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008446
Willy Tarreau294c4732011-12-16 21:35:50 +01008447 if (!ctx) {
8448 local_ctx.idx = 0;
8449 ctx = &local_ctx;
8450 }
8451
Willy Tarreaubce70882009-09-07 11:51:47 +02008452 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008453 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008454 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008455 occ--;
8456 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008457 *vptr = ctx->line + ctx->val;
8458 *vlen = ctx->vlen;
8459 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008460 }
8461 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008462 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008463 }
8464
8465 /* negative occurrence, we scan all the list then walk back */
8466 if (-occ > MAX_HDR_HISTORY)
8467 return 0;
8468
Willy Tarreau294c4732011-12-16 21:35:50 +01008469 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008470 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008471 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8472 len_hist[hist_ptr] = ctx->vlen;
8473 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008474 hist_ptr = 0;
8475 found++;
8476 }
8477 if (-occ > found)
8478 return 0;
8479 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008480 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8481 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8482 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008483 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008484 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008485 if (hist_ptr >= MAX_HDR_HISTORY)
8486 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008487 *vptr = ptr_hist[hist_ptr];
8488 *vlen = len_hist[hist_ptr];
8489 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008490}
8491
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008492/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8493 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8494 * performed over the whole headers. Otherwise it must contain a valid header
8495 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8496 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8497 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8498 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8499 * -1. This function differs from http_get_hdr() in that it only returns full
8500 * line header values and does not stop at commas.
8501 * The return value is 0 if nothing was found, or non-zero otherwise.
8502 */
8503unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8504 struct hdr_idx *idx, int occ,
8505 struct hdr_ctx *ctx, char **vptr, int *vlen)
8506{
8507 struct hdr_ctx local_ctx;
8508 char *ptr_hist[MAX_HDR_HISTORY];
8509 int len_hist[MAX_HDR_HISTORY];
8510 unsigned int hist_ptr;
8511 int found;
8512
8513 if (!ctx) {
8514 local_ctx.idx = 0;
8515 ctx = &local_ctx;
8516 }
8517
8518 if (occ >= 0) {
8519 /* search from the beginning */
8520 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8521 occ--;
8522 if (occ <= 0) {
8523 *vptr = ctx->line + ctx->val;
8524 *vlen = ctx->vlen;
8525 return 1;
8526 }
8527 }
8528 return 0;
8529 }
8530
8531 /* negative occurrence, we scan all the list then walk back */
8532 if (-occ > MAX_HDR_HISTORY)
8533 return 0;
8534
8535 found = hist_ptr = 0;
8536 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8537 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8538 len_hist[hist_ptr] = ctx->vlen;
8539 if (++hist_ptr >= MAX_HDR_HISTORY)
8540 hist_ptr = 0;
8541 found++;
8542 }
8543 if (-occ > found)
8544 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008545
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008546 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008547 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8548 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8549 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008550 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008551 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008552 if (hist_ptr >= MAX_HDR_HISTORY)
8553 hist_ptr -= MAX_HDR_HISTORY;
8554 *vptr = ptr_hist[hist_ptr];
8555 *vlen = len_hist[hist_ptr];
8556 return 1;
8557}
8558
Willy Tarreaubaaee002006-06-26 02:48:02 +02008559/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008560 * Print a debug line with a header. Always stop at the first CR or LF char,
8561 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8562 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008563 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008564void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008565{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008566 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008567 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008568
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008569 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008570 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008571 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008572 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 +02008573
8574 for (max = 0; start + max < end; max++)
8575 if (start[max] == '\r' || start[max] == '\n')
8576 break;
8577
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008578 UBOUND(max, trash.size - trash.len - 3);
8579 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8580 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008581 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008582}
8583
Willy Tarreaueee5b512015-04-03 23:46:31 +02008584
8585/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8586 * The hdr_idx is allocated as well. In case of allocation failure, everything
8587 * allocated is freed and NULL is returned. Otherwise the new transaction is
8588 * assigned to the stream and returned.
8589 */
8590struct http_txn *http_alloc_txn(struct stream *s)
8591{
8592 struct http_txn *txn = s->txn;
8593
8594 if (txn)
8595 return txn;
8596
8597 txn = pool_alloc2(pool2_http_txn);
8598 if (!txn)
8599 return txn;
8600
8601 txn->hdr_idx.size = global.tune.max_http_hdr;
8602 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8603 if (!txn->hdr_idx.v) {
8604 pool_free2(pool2_http_txn, txn);
8605 return NULL;
8606 }
8607
8608 s->txn = txn;
8609 return txn;
8610}
8611
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008612void http_txn_reset_req(struct http_txn *txn)
8613{
8614 txn->req.flags = 0;
8615 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8616 txn->req.next = 0;
8617 txn->req.chunk_len = 0LL;
8618 txn->req.body_len = 0LL;
8619 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8620}
8621
8622void http_txn_reset_res(struct http_txn *txn)
8623{
8624 txn->rsp.flags = 0;
8625 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8626 txn->rsp.next = 0;
8627 txn->rsp.chunk_len = 0LL;
8628 txn->rsp.body_len = 0LL;
8629 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8630}
8631
Willy Tarreau0937bc42009-12-22 15:03:09 +01008632/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008633 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008634 * the required fields are properly allocated and that we only need to (re)init
8635 * them. This should be used before processing any new request.
8636 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008637void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008638{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008639 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008640 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008641
8642 txn->flags = 0;
8643 txn->status = -1;
8644
Willy Tarreauf64d1412010-10-07 20:06:11 +02008645 txn->cookie_first_date = 0;
8646 txn->cookie_last_date = 0;
8647
Willy Tarreaueee5b512015-04-03 23:46:31 +02008648 txn->srv_cookie = NULL;
8649 txn->cli_cookie = NULL;
8650 txn->uri = NULL;
8651
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008652 http_txn_reset_req(txn);
8653 http_txn_reset_res(txn);
8654
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008655 txn->req.chn = &s->req;
8656 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008657
8658 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008659
8660 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8661 if (fe->options2 & PR_O2_REQBUG_OK)
8662 txn->req.err_pos = -1; /* let buggy requests pass */
8663
Willy Tarreau0937bc42009-12-22 15:03:09 +01008664 if (txn->hdr_idx.v)
8665 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008666
8667 vars_init(&s->vars_txn, SCOPE_TXN);
8668 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008669}
8670
8671/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008672void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008673{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008674 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008675 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008676
8677 /* these ones will have been dynamically allocated */
8678 pool_free2(pool2_requri, txn->uri);
8679 pool_free2(pool2_capture, txn->cli_cookie);
8680 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008681 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008682
William Lallemanda73203e2012-03-12 12:48:57 +01008683 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008684 txn->uri = NULL;
8685 txn->srv_cookie = NULL;
8686 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008687
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008688 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008689 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008690 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008691 pool_free2(h->pool, s->req_cap[h->index]);
8692 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008693 }
8694
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008695 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008696 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008697 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008698 pool_free2(h->pool, s->res_cap[h->index]);
8699 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008700 }
8701
Willy Tarreau6204cd92016-03-10 16:33:04 +01008702 vars_prune(&s->vars_txn, s->sess, s);
8703 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008704}
8705
8706/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008707void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008708{
8709 http_end_txn(s);
8710 http_init_txn(s);
8711
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008712 /* reinitialise the current rule list pointer to NULL. We are sure that
8713 * any rulelist match the NULL pointer.
8714 */
8715 s->current_rule_list = NULL;
8716
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008717 s->be = strm_fe(s);
8718 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008719 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008720 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008721 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008722 /* re-init store persistence */
8723 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008724 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008725
Willy Tarreau0937bc42009-12-22 15:03:09 +01008726 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008727
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008728 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008729
Willy Tarreau739cfba2010-01-25 23:11:14 +01008730 /* We must trim any excess data from the response buffer, because we
8731 * may have blocked an invalid response from a server that we don't
8732 * want to accidentely forward once we disable the analysers, nor do
8733 * we want those data to come along with next response. A typical
8734 * example of such data would be from a buggy server responding to
8735 * a HEAD with some data, or sending more than the advertised
8736 * content-length.
8737 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008738 if (unlikely(s->res.buf->i))
8739 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008740
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008741 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008742 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008743
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008744 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008745 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008746
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008747 s->req.rex = TICK_ETERNITY;
8748 s->req.wex = TICK_ETERNITY;
8749 s->req.analyse_exp = TICK_ETERNITY;
8750 s->res.rex = TICK_ETERNITY;
8751 s->res.wex = TICK_ETERNITY;
8752 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008753}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008754
Sasha Pachev218f0642014-06-16 12:05:59 -06008755void free_http_res_rules(struct list *r)
8756{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008757 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008758
8759 list_for_each_entry_safe(pr, tr, r, list) {
8760 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008761 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008762 free(pr);
8763 }
8764}
8765
8766void free_http_req_rules(struct list *r)
8767{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008768 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01008769
8770 list_for_each_entry_safe(pr, tr, r, list) {
8771 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008772 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008773 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008774
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008775 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008776 free(pr);
8777 }
8778}
8779
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008780/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008781struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01008782{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008783 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008784 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008785 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008786 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008787
Vincent Bernat02779b62016-04-03 13:48:43 +02008788 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01008789 if (!rule) {
8790 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008791 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008792 }
8793
CJ Ess108b1dd2015-04-07 12:03:37 -04008794 rule->deny_status = HTTP_ERR_403;
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008795 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008796 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01008797 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02008798 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04008799 int code;
8800 int hc;
8801
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008802 rule->action = ACT_ACTION_DENY;
Willy Tarreauff011f22011-01-06 17:51:27 +01008803 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04008804 if (strcmp(args[cur_arg], "deny_status") == 0) {
8805 cur_arg++;
8806 if (!args[cur_arg]) {
8807 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
8808 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
8809 goto out_err;
8810 }
8811
8812 code = atol(args[cur_arg]);
8813 cur_arg++;
8814 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
8815 if (http_err_codes[hc] == code) {
8816 rule->deny_status = hc;
8817 break;
8818 }
8819 }
8820
8821 if (hc >= HTTP_ERR_SIZE) {
8822 Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n",
8823 file, linenum, code);
8824 }
8825 }
Willy Tarreauccbcc372012-12-27 12:37:57 +01008826 } else if (!strcmp(args[0], "tarpit")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008827 rule->action = ACT_HTTP_REQ_TARPIT;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008828 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008829 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008830 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008831 cur_arg = 1;
8832
8833 while(*args[cur_arg]) {
8834 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008835 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008836 cur_arg+=2;
8837 continue;
8838 } else
8839 break;
8840 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008841 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008842 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008843 cur_arg = 1;
8844
8845 if (!*args[cur_arg] ||
8846 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8847 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8848 file, linenum, args[0]);
8849 goto out_err;
8850 }
8851 rule->arg.nice = atoi(args[cur_arg]);
8852 if (rule->arg.nice < -1024)
8853 rule->arg.nice = -1024;
8854 else if (rule->arg.nice > 1024)
8855 rule->arg.nice = 1024;
8856 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008857 } else if (!strcmp(args[0], "set-tos")) {
8858#ifdef IP_TOS
8859 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008860 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008861 cur_arg = 1;
8862
8863 if (!*args[cur_arg] ||
8864 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8865 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8866 file, linenum, args[0]);
8867 goto out_err;
8868 }
8869
8870 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8871 if (err && *err != '\0') {
8872 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8873 file, linenum, err, args[0]);
8874 goto out_err;
8875 }
8876 cur_arg++;
8877#else
8878 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8879 goto out_err;
8880#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008881 } else if (!strcmp(args[0], "set-mark")) {
8882#ifdef SO_MARK
8883 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008884 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008885 cur_arg = 1;
8886
8887 if (!*args[cur_arg] ||
8888 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8889 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8890 file, linenum, args[0]);
8891 goto out_err;
8892 }
8893
8894 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8895 if (err && *err != '\0') {
8896 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8897 file, linenum, err, args[0]);
8898 goto out_err;
8899 }
8900 cur_arg++;
8901 global.last_checks |= LSTCHK_NETADM;
8902#else
8903 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8904 goto out_err;
8905#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008906 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008907 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008908 cur_arg = 1;
8909
8910 if (!*args[cur_arg] ||
8911 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8912 bad_log_level:
8913 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8914 file, linenum, args[0]);
8915 goto out_err;
8916 }
8917 if (strcmp(args[cur_arg], "silent") == 0)
8918 rule->arg.loglevel = -1;
8919 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8920 goto bad_log_level;
8921 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008922 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008923 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008924 cur_arg = 1;
8925
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008926 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8927 (*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 +01008928 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8929 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008930 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008931 }
8932
8933 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8934 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8935 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008936
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008937 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008938 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008939 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8940 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008941 free(proxy->conf.lfs_file);
8942 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8943 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008944 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02008945 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008946 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008947 cur_arg = 1;
8948
8949 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02008950 (*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 -06008951 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
8952 file, linenum, args[0]);
8953 goto out_err;
8954 }
8955
8956 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8957 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8958 LIST_INIT(&rule->arg.hdr_add.fmt);
8959
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008960 error = NULL;
8961 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
8962 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8963 args[cur_arg + 1], error);
8964 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008965 goto out_err;
8966 }
8967
8968 proxy->conf.args.ctx = ARGC_HRQ;
8969 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
8970 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8971 file, linenum);
8972
8973 free(proxy->conf.lfs_file);
8974 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8975 proxy->conf.lfs_line = proxy->conf.args.line;
8976 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008977 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008978 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008979 cur_arg = 1;
8980
8981 if (!*args[cur_arg] ||
8982 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8983 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8984 file, linenum, args[0]);
8985 goto out_err;
8986 }
8987
8988 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8989 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8990
8991 proxy->conf.args.ctx = ARGC_HRQ;
8992 free(proxy->conf.lfs_file);
8993 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8994 proxy->conf.lfs_line = proxy->conf.args.line;
8995 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02008996 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
8997 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02008998 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02008999 struct sample_expr *expr;
9000 unsigned int where;
9001 char *err = NULL;
9002
9003 cur_arg = 1;
9004 proxy->conf.args.ctx = ARGC_TRK;
9005
9006 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9007 if (!expr) {
9008 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9009 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9010 free(err);
9011 goto out_err;
9012 }
9013
9014 where = 0;
9015 if (proxy->cap & PR_CAP_FE)
9016 where |= SMP_VAL_FE_HRQ_HDR;
9017 if (proxy->cap & PR_CAP_BE)
9018 where |= SMP_VAL_BE_HRQ_HDR;
9019
9020 if (!(expr->fetch->val & where)) {
9021 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9022 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9023 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9024 args[cur_arg-1], sample_src_names(expr->fetch->use));
9025 free(expr);
9026 goto out_err;
9027 }
9028
9029 if (strcmp(args[cur_arg], "table") == 0) {
9030 cur_arg++;
9031 if (!args[cur_arg]) {
9032 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9033 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9034 free(expr);
9035 goto out_err;
9036 }
9037 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009038 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02009039 cur_arg++;
9040 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009041 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009042 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009043 } else if (strcmp(args[0], "redirect") == 0) {
9044 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009045 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009046
Willy Tarreaube4653b2015-05-28 15:26:58 +02009047 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009048 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9049 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9050 goto out_err;
9051 }
9052
9053 /* this redirect rule might already contain a parsed condition which
9054 * we'll pass to the http-request rule.
9055 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009056 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009057 rule->arg.redir = redir;
9058 rule->cond = redir->cond;
9059 redir->cond = NULL;
9060 cur_arg = 2;
9061 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009062 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9063 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009064 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009065 /*
9066 * '+ 8' for 'add-acl('
9067 * '- 9' for 'add-acl(' + trailing ')'
9068 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009069 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009070
9071 cur_arg = 1;
9072
9073 if (!*args[cur_arg] ||
9074 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9075 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9076 file, linenum, args[0]);
9077 goto out_err;
9078 }
9079
9080 LIST_INIT(&rule->arg.map.key);
9081 proxy->conf.args.ctx = ARGC_HRQ;
9082 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9083 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9084 file, linenum);
9085 free(proxy->conf.lfs_file);
9086 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9087 proxy->conf.lfs_line = proxy->conf.args.line;
9088 cur_arg += 1;
9089 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9090 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009091 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009092 /*
9093 * '+ 8' for 'del-acl('
9094 * '- 9' for 'del-acl(' + trailing ')'
9095 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009096 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009097
9098 cur_arg = 1;
9099
9100 if (!*args[cur_arg] ||
9101 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9102 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9103 file, linenum, args[0]);
9104 goto out_err;
9105 }
9106
9107 LIST_INIT(&rule->arg.map.key);
9108 proxy->conf.args.ctx = ARGC_HRQ;
9109 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9110 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9111 file, linenum);
9112 free(proxy->conf.lfs_file);
9113 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9114 proxy->conf.lfs_line = proxy->conf.args.line;
9115 cur_arg += 1;
9116 } else if (strncmp(args[0], "del-map", 7) == 0) {
9117 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009118 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009119 /*
9120 * '+ 8' for 'del-map('
9121 * '- 9' for 'del-map(' + trailing ')'
9122 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009123 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009124
9125 cur_arg = 1;
9126
9127 if (!*args[cur_arg] ||
9128 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9129 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9130 file, linenum, args[0]);
9131 goto out_err;
9132 }
9133
9134 LIST_INIT(&rule->arg.map.key);
9135 proxy->conf.args.ctx = ARGC_HRQ;
9136 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9137 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9138 file, linenum);
9139 free(proxy->conf.lfs_file);
9140 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9141 proxy->conf.lfs_line = proxy->conf.args.line;
9142 cur_arg += 1;
9143 } else if (strncmp(args[0], "set-map", 7) == 0) {
9144 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009145 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009146 /*
9147 * '+ 8' for 'set-map('
9148 * '- 9' for 'set-map(' + trailing ')'
9149 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009150 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009151
9152 cur_arg = 1;
9153
9154 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9155 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9156 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9157 file, linenum, args[0]);
9158 goto out_err;
9159 }
9160
9161 LIST_INIT(&rule->arg.map.key);
9162 LIST_INIT(&rule->arg.map.value);
9163 proxy->conf.args.ctx = ARGC_HRQ;
9164
9165 /* key pattern */
9166 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9167 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9168 file, linenum);
9169
9170 /* value pattern */
9171 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9172 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9173 file, linenum);
9174 free(proxy->conf.lfs_file);
9175 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9176 proxy->conf.lfs_line = proxy->conf.args.line;
9177
9178 cur_arg += 2;
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02009179 } else if (strncmp(args[0], "set-src", 7) == 0) {
9180 struct sample_expr *expr;
9181 unsigned int where;
9182 char *err = NULL;
9183
9184 cur_arg = 1;
9185 proxy->conf.args.ctx = ARGC_HRQ;
9186
9187 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9188 if (!expr) {
9189 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9190 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9191 free(err);
9192 goto out_err;
9193 }
9194
9195 where = 0;
9196 if (proxy->cap & PR_CAP_FE)
9197 where |= SMP_VAL_FE_HRQ_HDR;
9198 if (proxy->cap & PR_CAP_BE)
9199 where |= SMP_VAL_BE_HRQ_HDR;
9200
9201 if (!(expr->fetch->val & where)) {
9202 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9203 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9204 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9205 args[cur_arg-1], sample_src_names(expr->fetch->use));
9206 free(expr);
9207 goto out_err;
9208 }
9209
Thierry FOURNIERa002dc92015-07-31 08:50:51 +02009210 rule->arg.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009211 rule->action = ACT_HTTP_REQ_SET_SRC;
William Lallemand73025dd2014-04-24 14:38:37 +02009212 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9213 char *errmsg = NULL;
9214 cur_arg = 1;
9215 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009216 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009217 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009218 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009219 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9220 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9221 free(errmsg);
9222 goto out_err;
9223 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009224 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009225 action_build_list(&http_req_keywords.list, &trash);
9226 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
9227 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
9228 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', "
9229 "'set-src'%s%s, but got '%s'%s.\n",
9230 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009231 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009232 }
9233
9234 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9235 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009236 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009237
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009238 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9239 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9240 file, linenum, args[0], errmsg);
9241 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009242 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009243 }
9244 rule->cond = cond;
9245 }
9246 else if (*args[cur_arg]) {
9247 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9248 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9249 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009250 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009251 }
9252
9253 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009254 out_err:
9255 free(rule);
9256 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009257}
9258
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009259/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009260struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009261{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009262 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009263 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009264 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009265 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009266
9267 rule = calloc(1, sizeof(*rule));
9268 if (!rule) {
9269 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9270 goto out_err;
9271 }
9272
9273 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009274 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009275 cur_arg = 1;
9276 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009277 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009278 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009279 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009280 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009281 cur_arg = 1;
9282
9283 if (!*args[cur_arg] ||
9284 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9285 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9286 file, linenum, args[0]);
9287 goto out_err;
9288 }
9289 rule->arg.nice = atoi(args[cur_arg]);
9290 if (rule->arg.nice < -1024)
9291 rule->arg.nice = -1024;
9292 else if (rule->arg.nice > 1024)
9293 rule->arg.nice = 1024;
9294 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009295 } else if (!strcmp(args[0], "set-tos")) {
9296#ifdef IP_TOS
9297 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009298 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009299 cur_arg = 1;
9300
9301 if (!*args[cur_arg] ||
9302 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9303 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9304 file, linenum, args[0]);
9305 goto out_err;
9306 }
9307
9308 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9309 if (err && *err != '\0') {
9310 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9311 file, linenum, err, args[0]);
9312 goto out_err;
9313 }
9314 cur_arg++;
9315#else
9316 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9317 goto out_err;
9318#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009319 } else if (!strcmp(args[0], "set-mark")) {
9320#ifdef SO_MARK
9321 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009322 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009323 cur_arg = 1;
9324
9325 if (!*args[cur_arg] ||
9326 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9327 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9328 file, linenum, args[0]);
9329 goto out_err;
9330 }
9331
9332 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9333 if (err && *err != '\0') {
9334 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9335 file, linenum, err, args[0]);
9336 goto out_err;
9337 }
9338 cur_arg++;
9339 global.last_checks |= LSTCHK_NETADM;
9340#else
9341 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9342 goto out_err;
9343#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009344 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009345 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009346 cur_arg = 1;
9347
9348 if (!*args[cur_arg] ||
9349 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9350 bad_log_level:
9351 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9352 file, linenum, args[0]);
9353 goto out_err;
9354 }
9355 if (strcmp(args[cur_arg], "silent") == 0)
9356 rule->arg.loglevel = -1;
9357 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9358 goto bad_log_level;
9359 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009360 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009361 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009362 cur_arg = 1;
9363
9364 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9365 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9366 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9367 file, linenum, args[0]);
9368 goto out_err;
9369 }
9370
9371 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9372 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9373 LIST_INIT(&rule->arg.hdr_add.fmt);
9374
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009375 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009376 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009377 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9378 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009379 free(proxy->conf.lfs_file);
9380 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9381 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009382 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009383 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009384 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009385 cur_arg = 1;
9386
9387 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009388 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9389 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009390 file, linenum, args[0]);
9391 goto out_err;
9392 }
9393
9394 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9395 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9396 LIST_INIT(&rule->arg.hdr_add.fmt);
9397
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009398 error = NULL;
9399 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9400 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9401 args[cur_arg + 1], error);
9402 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009403 goto out_err;
9404 }
9405
9406 proxy->conf.args.ctx = ARGC_HRQ;
9407 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9408 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9409 file, linenum);
9410
9411 free(proxy->conf.lfs_file);
9412 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9413 proxy->conf.lfs_line = proxy->conf.args.line;
9414 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009415 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009416 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009417 cur_arg = 1;
9418
9419 if (!*args[cur_arg] ||
9420 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9421 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9422 file, linenum, args[0]);
9423 goto out_err;
9424 }
9425
9426 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9427 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9428
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009429 proxy->conf.args.ctx = ARGC_HRS;
9430 free(proxy->conf.lfs_file);
9431 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9432 proxy->conf.lfs_line = proxy->conf.args.line;
9433 cur_arg += 1;
9434 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9435 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009436 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009437 /*
9438 * '+ 8' for 'add-acl('
9439 * '- 9' for 'add-acl(' + trailing ')'
9440 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009441 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009442
9443 cur_arg = 1;
9444
9445 if (!*args[cur_arg] ||
9446 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9447 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9448 file, linenum, args[0]);
9449 goto out_err;
9450 }
9451
9452 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009453 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009454 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9455 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9456 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009457 free(proxy->conf.lfs_file);
9458 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9459 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009460
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009461 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009462 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9463 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009464 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009465 /*
9466 * '+ 8' for 'del-acl('
9467 * '- 9' for 'del-acl(' + trailing ')'
9468 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009469 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009470
9471 cur_arg = 1;
9472
9473 if (!*args[cur_arg] ||
9474 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9475 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9476 file, linenum, args[0]);
9477 goto out_err;
9478 }
9479
9480 LIST_INIT(&rule->arg.map.key);
9481 proxy->conf.args.ctx = ARGC_HRS;
9482 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9483 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9484 file, linenum);
9485 free(proxy->conf.lfs_file);
9486 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9487 proxy->conf.lfs_line = proxy->conf.args.line;
9488 cur_arg += 1;
9489 } else if (strncmp(args[0], "del-map", 7) == 0) {
9490 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009491 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009492 /*
9493 * '+ 8' for 'del-map('
9494 * '- 9' for 'del-map(' + trailing ')'
9495 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009496 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009497
9498 cur_arg = 1;
9499
9500 if (!*args[cur_arg] ||
9501 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9502 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9503 file, linenum, args[0]);
9504 goto out_err;
9505 }
9506
9507 LIST_INIT(&rule->arg.map.key);
9508 proxy->conf.args.ctx = ARGC_HRS;
9509 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9510 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9511 file, linenum);
9512 free(proxy->conf.lfs_file);
9513 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9514 proxy->conf.lfs_line = proxy->conf.args.line;
9515 cur_arg += 1;
9516 } else if (strncmp(args[0], "set-map", 7) == 0) {
9517 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009518 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009519 /*
9520 * '+ 8' for 'set-map('
9521 * '- 9' for 'set-map(' + trailing ')'
9522 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009523 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009524
9525 cur_arg = 1;
9526
9527 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9528 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9529 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9530 file, linenum, args[0]);
9531 goto out_err;
9532 }
9533
9534 LIST_INIT(&rule->arg.map.key);
9535 LIST_INIT(&rule->arg.map.value);
9536
9537 proxy->conf.args.ctx = ARGC_HRS;
9538
9539 /* key pattern */
9540 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9541 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9542 file, linenum);
9543
9544 /* value pattern */
9545 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9546 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9547 file, linenum);
9548
9549 free(proxy->conf.lfs_file);
9550 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9551 proxy->conf.lfs_line = proxy->conf.args.line;
9552
9553 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009554 } else if (strcmp(args[0], "redirect") == 0) {
9555 struct redirect_rule *redir;
9556 char *errmsg = NULL;
9557
9558 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9559 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9560 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9561 goto out_err;
9562 }
9563
9564 /* this redirect rule might already contain a parsed condition which
9565 * we'll pass to the http-request rule.
9566 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009567 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009568 rule->arg.redir = redir;
9569 rule->cond = redir->cond;
9570 redir->cond = NULL;
9571 cur_arg = 2;
9572 return rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009573 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9574 char *errmsg = NULL;
9575 cur_arg = 1;
9576 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009577 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009578 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009579 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009580 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9581 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9582 free(errmsg);
9583 goto out_err;
9584 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009585 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009586 action_build_list(&http_res_keywords.list, &trash);
9587 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9588 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
9589 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', "
9590 "'set-src'%s%s, but got '%s'%s.\n",
9591 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009592 goto out_err;
9593 }
9594
9595 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9596 struct acl_cond *cond;
9597 char *errmsg = NULL;
9598
9599 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9600 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9601 file, linenum, args[0], errmsg);
9602 free(errmsg);
9603 goto out_err;
9604 }
9605 rule->cond = cond;
9606 }
9607 else if (*args[cur_arg]) {
9608 Alert("parsing [%s:%d]: 'http-response %s' expects"
9609 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9610 file, linenum, args[0], args[cur_arg]);
9611 goto out_err;
9612 }
9613
9614 return rule;
9615 out_err:
9616 free(rule);
9617 return NULL;
9618}
9619
Willy Tarreau4baae242012-12-27 12:00:31 +01009620/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009621 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009622 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9623 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009624 */
9625struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009626 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009627{
9628 struct redirect_rule *rule;
9629 int cur_arg;
9630 int type = REDIRECT_TYPE_NONE;
9631 int code = 302;
9632 const char *destination = NULL;
9633 const char *cookie = NULL;
9634 int cookie_set = 0;
9635 unsigned int flags = REDIRECT_FLAG_NONE;
9636 struct acl_cond *cond = NULL;
9637
9638 cur_arg = 0;
9639 while (*(args[cur_arg])) {
9640 if (strcmp(args[cur_arg], "location") == 0) {
9641 if (!*args[cur_arg + 1])
9642 goto missing_arg;
9643
9644 type = REDIRECT_TYPE_LOCATION;
9645 cur_arg++;
9646 destination = args[cur_arg];
9647 }
9648 else if (strcmp(args[cur_arg], "prefix") == 0) {
9649 if (!*args[cur_arg + 1])
9650 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009651 type = REDIRECT_TYPE_PREFIX;
9652 cur_arg++;
9653 destination = args[cur_arg];
9654 }
9655 else if (strcmp(args[cur_arg], "scheme") == 0) {
9656 if (!*args[cur_arg + 1])
9657 goto missing_arg;
9658
9659 type = REDIRECT_TYPE_SCHEME;
9660 cur_arg++;
9661 destination = args[cur_arg];
9662 }
9663 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9664 if (!*args[cur_arg + 1])
9665 goto missing_arg;
9666
9667 cur_arg++;
9668 cookie = args[cur_arg];
9669 cookie_set = 1;
9670 }
9671 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9672 if (!*args[cur_arg + 1])
9673 goto missing_arg;
9674
9675 cur_arg++;
9676 cookie = args[cur_arg];
9677 cookie_set = 0;
9678 }
9679 else if (strcmp(args[cur_arg], "code") == 0) {
9680 if (!*args[cur_arg + 1])
9681 goto missing_arg;
9682
9683 cur_arg++;
9684 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009685 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009686 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009687 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009688 args[cur_arg - 1], args[cur_arg]);
9689 return NULL;
9690 }
9691 }
9692 else if (!strcmp(args[cur_arg],"drop-query")) {
9693 flags |= REDIRECT_FLAG_DROP_QS;
9694 }
9695 else if (!strcmp(args[cur_arg],"append-slash")) {
9696 flags |= REDIRECT_FLAG_APPEND_SLASH;
9697 }
9698 else if (strcmp(args[cur_arg], "if") == 0 ||
9699 strcmp(args[cur_arg], "unless") == 0) {
9700 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9701 if (!cond) {
9702 memprintf(errmsg, "error in condition: %s", *errmsg);
9703 return NULL;
9704 }
9705 break;
9706 }
9707 else {
9708 memprintf(errmsg,
9709 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9710 args[cur_arg]);
9711 return NULL;
9712 }
9713 cur_arg++;
9714 }
9715
9716 if (type == REDIRECT_TYPE_NONE) {
9717 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9718 return NULL;
9719 }
9720
Willy Tarreaube4653b2015-05-28 15:26:58 +02009721 if (dir && type != REDIRECT_TYPE_LOCATION) {
9722 memprintf(errmsg, "response only supports redirect type 'location'");
9723 return NULL;
9724 }
9725
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02009726 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +01009727 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009728 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009729
9730 if (!use_fmt) {
9731 /* old-style static redirect rule */
9732 rule->rdr_str = strdup(destination);
9733 rule->rdr_len = strlen(destination);
9734 }
9735 else {
9736 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009737
9738 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9739 * if prefix == "/", we don't want to add anything, otherwise it
9740 * makes it hard for the user to configure a self-redirection.
9741 */
Godbachd9722032014-12-18 15:44:58 +08009742 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009743 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009744 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009745 dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR
9746 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009747 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009748 free(curproxy->conf.lfs_file);
9749 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9750 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009751 }
9752 }
9753
Willy Tarreau4baae242012-12-27 12:00:31 +01009754 if (cookie) {
9755 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9756 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9757 */
9758 rule->cookie_len = strlen(cookie);
9759 if (cookie_set) {
9760 rule->cookie_str = malloc(rule->cookie_len + 10);
9761 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9762 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9763 rule->cookie_len += 9;
9764 } else {
9765 rule->cookie_str = malloc(rule->cookie_len + 21);
9766 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9767 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9768 rule->cookie_len += 20;
9769 }
9770 }
9771 rule->type = type;
9772 rule->code = code;
9773 rule->flags = flags;
9774 LIST_INIT(&rule->list);
9775 return rule;
9776
9777 missing_arg:
9778 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9779 return NULL;
9780}
9781
Willy Tarreau8797c062007-05-07 00:55:35 +02009782/************************************************************************/
9783/* The code below is dedicated to ACL parsing and matching */
9784/************************************************************************/
9785
9786
Willy Tarreau14174bc2012-04-16 14:34:04 +02009787/* This function ensures that the prerequisites for an L7 fetch are ready,
9788 * which means that a request or response is ready. If some data is missing,
9789 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009790 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9791 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009792 *
9793 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009794 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9795 * decide whether or not an HTTP message is present ;
9796 * 0 if the requested data cannot be fetched or if it is certain that
9797 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009798 * 1 if an HTTP message is ready
9799 */
James Rosewell91a41cb2015-09-18 17:11:16 +01009800int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009801 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009802{
Willy Tarreau192252e2015-04-04 01:47:55 +02009803 struct http_txn *txn;
9804 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009805
Willy Tarreaube508f12016-03-10 11:47:01 +01009806 /* Note: it is possible that <s> is NULL when called before stream
9807 * initialization (eg: tcp-request connection), so this function is the
9808 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009809 */
Willy Tarreau192252e2015-04-04 01:47:55 +02009810 if (!s)
9811 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01009812
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +02009813 if (!s->txn) {
9814 if (unlikely(!http_alloc_txn(s)))
9815 return 0; /* not enough memory */
9816 http_init_txn(s);
9817 }
Willy Tarreau192252e2015-04-04 01:47:55 +02009818 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +02009819 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009820
9821 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009822 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009823
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009824 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009825 /* If the buffer does not leave enough free space at the end,
9826 * we must first realign it.
9827 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009828 if (s->req.buf->p > s->req.buf->data &&
9829 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
9830 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009831
Willy Tarreau14174bc2012-04-16 14:34:04 +02009832 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009833 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009834 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009835
9836 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009837 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009838 http_msg_analyzer(msg, &txn->hdr_idx);
9839
9840 /* Still no valid request ? */
9841 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009842 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009843 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009844 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009845 }
9846 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009847 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009848 return 0;
9849 }
9850
9851 /* OK we just got a valid HTTP request. We have some minor
9852 * preparation to perform so that further checks can rely
9853 * on HTTP tests.
9854 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009855
9856 /* If the request was parsed but was too large, we must absolutely
9857 * return an error so that it is not processed. At the moment this
9858 * cannot happen, but if the parsers are to change in the future,
9859 * we want this check to be maintained.
9860 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009861 if (unlikely(s->req.buf->i + s->req.buf->p >
9862 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009863 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009864 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009865 return 1;
9866 }
9867
Willy Tarreau9b28e032012-10-12 23:49:43 +02009868 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009869 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02009870 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009871
Willy Tarreau506d0502013-07-06 13:29:24 +02009872 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9873 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009874 }
9875
Willy Tarreau506d0502013-07-06 13:29:24 +02009876 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009877 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009878 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009879
9880 /* otherwise everything's ready for the request */
9881 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009882 else {
9883 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009884 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9885 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009886 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009887 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009888 }
9889
9890 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009891 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009892 return 1;
9893}
Willy Tarreau8797c062007-05-07 00:55:35 +02009894
Willy Tarreau8797c062007-05-07 00:55:35 +02009895/* 1. Check on METHOD
9896 * We use the pre-parsed method if it is known, and store its number as an
9897 * integer. If it is unknown, we use the pointer and the length.
9898 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009899static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009900{
9901 int len, meth;
9902
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009903 len = strlen(text);
9904 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009905
9906 pattern->val.i = meth;
9907 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +02009908 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009909 pattern->len = len;
9910 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009911 else {
9912 pattern->ptr.str = NULL;
9913 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009914 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009915 return 1;
9916}
9917
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009918/* This function fetches the method of current HTTP request and stores
9919 * it in the global pattern struct as a chunk. There are two possibilities :
9920 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9921 * in <len> and <ptr> is NULL ;
9922 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9923 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009924 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009925 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009926static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009927smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009928{
9929 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +01009930 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009931
Willy Tarreau24e32d82012-04-23 23:55:44 +02009932 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009933
Willy Tarreaube508f12016-03-10 11:47:01 +01009934 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009935 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009936 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009937 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009938 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009939 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9940 /* ensure the indexes are not affected */
9941 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009942 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009943 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
9944 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009945 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009946 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009947 return 1;
9948}
9949
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009950/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009951static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009952{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009953 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009954 struct pattern_list *lst;
9955 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009956
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009957 list_for_each_entry(lst, &expr->patterns, list) {
9958 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009959
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009960 /* well-known method */
9961 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009962 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009963 return pattern;
9964 else
9965 continue;
9966 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009967
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009968 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009969 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009970 continue;
9971
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009972 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009973 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
9974 (!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 +01009975 return pattern;
9976 }
9977 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009978}
9979
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009980static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009981smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009982{
Willy Tarreaube508f12016-03-10 11:47:01 +01009983 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009984 char *ptr;
9985 int len;
9986
Willy Tarreauc0239e02012-04-16 14:42:55 +02009987 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009988
Willy Tarreaube508f12016-03-10 11:47:01 +01009989 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009990 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009991 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009992
9993 while ((len-- > 0) && (*ptr++ != '/'));
9994 if (len <= 0)
9995 return 0;
9996
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009997 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009998 smp->data.u.str.str = ptr;
9999 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010000
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010001 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010002 return 1;
10003}
10004
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010005static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010006smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010007{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010008 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010009 char *ptr;
10010 int len;
10011
Willy Tarreauc0239e02012-04-16 14:42:55 +020010012 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010013
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010014 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010015 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10016 return 0;
10017
Willy Tarreau8797c062007-05-07 00:55:35 +020010018 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010019 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010020
10021 while ((len-- > 0) && (*ptr++ != '/'));
10022 if (len <= 0)
10023 return 0;
10024
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010025 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010026 smp->data.u.str.str = ptr;
10027 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010028
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010029 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010030 return 1;
10031}
10032
10033/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010034static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010035smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010036{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010037 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010038 char *ptr;
10039 int len;
10040
Willy Tarreauc0239e02012-04-16 14:42:55 +020010041 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010042
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010043 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010044 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10045 return 0;
10046
Willy Tarreau8797c062007-05-07 00:55:35 +020010047 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010048 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010049
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010050 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010051 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010052 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010053 return 1;
10054}
10055
Thierry Fournierf4011dd2016-03-29 17:23:51 +020010056static int
10057smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
10058{
10059 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
10060 return 0;
10061
10062 if (!smp->strm->unique_id) {
10063 if ((smp->strm->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
10064 return 0;
10065 smp->strm->unique_id[0] = '\0';
10066 }
10067 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
10068 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
10069
10070 smp->data.type = SMP_T_STR;
10071 smp->data.u.str.str = smp->strm->unique_id;
10072 smp->flags = SMP_F_CONST;
10073 return 1;
10074}
10075
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010076/* returns the longest available part of the body. This requires that the body
10077 * has been waited for using http-buffer-request.
10078 */
10079static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010080smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010081{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010082 struct http_msg *msg;
10083 unsigned long len;
10084 unsigned long block1;
10085 char *body;
10086 struct chunk *temp;
10087
10088 CHECK_HTTP_MESSAGE_FIRST();
10089
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010090 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010091 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010092 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010093 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010094
10095 len = http_body_bytes(msg);
10096 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10097
10098 block1 = len;
10099 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10100 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10101
10102 if (block1 == len) {
10103 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010104 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010105 smp->data.u.str.str = body;
10106 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010107 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10108 }
10109 else {
10110 /* buffer is wrapped, we need to defragment it */
10111 temp = get_trash_chunk();
10112 memcpy(temp->str, body, block1);
10113 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010114 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010115 smp->data.u.str.str = temp->str;
10116 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010117 smp->flags = SMP_F_VOL_TEST;
10118 }
10119 return 1;
10120}
10121
10122
10123/* returns the available length of the body. This requires that the body
10124 * has been waited for using http-buffer-request.
10125 */
10126static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010127smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010128{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010129 struct http_msg *msg;
10130
10131 CHECK_HTTP_MESSAGE_FIRST();
10132
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010133 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010134 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010135 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010136 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010137
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010138 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010139 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010140
10141 smp->flags = SMP_F_VOL_TEST;
10142 return 1;
10143}
10144
10145
10146/* returns the advertised length of the body, or the advertised size of the
10147 * chunks available in the buffer. This requires that the body has been waited
10148 * for using http-buffer-request.
10149 */
10150static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010151smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010152{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010153 struct http_msg *msg;
10154
10155 CHECK_HTTP_MESSAGE_FIRST();
10156
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010157 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010158 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010159 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010160 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010161
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010162 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010163 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010164
10165 smp->flags = SMP_F_VOL_TEST;
10166 return 1;
10167}
10168
10169
Willy Tarreau8797c062007-05-07 00:55:35 +020010170/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010171static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010172smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010173{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010174 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010175
Willy Tarreauc0239e02012-04-16 14:42:55 +020010176 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010177
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010178 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010179 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010180 smp->data.u.str.len = txn->req.sl.rq.u_l;
10181 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010182 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010183 return 1;
10184}
10185
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010186static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010187smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010188{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010189 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010190 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010191
Willy Tarreauc0239e02012-04-16 14:42:55 +020010192 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010193
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010194 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010195 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 +020010196 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010197 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010198
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010199 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010200 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010201 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010202 return 1;
10203}
10204
10205static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010206smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010207{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010208 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010209 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010210
Willy Tarreauc0239e02012-04-16 14:42:55 +020010211 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010212
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010213 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010214 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 +020010215 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10216 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010217
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010218 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010219 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010220 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010221 return 1;
10222}
10223
Willy Tarreau185b5c42012-04-26 15:11:51 +020010224/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10225 * Accepts an optional argument of type string containing the header field name,
10226 * and an optional argument of type signed or unsigned integer to request an
10227 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010228 * headers are considered from the first one. It does not stop on commas and
10229 * returns full lines instead (useful for User-Agent or Date for example).
10230 */
10231static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010232smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010233{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010234 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010235 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010236 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010237 int occ = 0;
10238 const char *name_str = NULL;
10239 int name_len = 0;
10240
10241 if (!ctx) {
10242 /* first call */
10243 ctx = &static_hdr_ctx;
10244 ctx->idx = 0;
10245 smp->ctx.a[0] = ctx;
10246 }
10247
10248 if (args) {
10249 if (args[0].type != ARGT_STR)
10250 return 0;
10251 name_str = args[0].data.str.str;
10252 name_len = args[0].data.str.len;
10253
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010254 if (args[1].type == ARGT_SINT)
10255 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010256 }
10257
10258 CHECK_HTTP_MESSAGE_FIRST();
10259
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010260 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010261 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 +020010262
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010263 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10264 /* search for header from the beginning */
10265 ctx->idx = 0;
10266
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010267 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010268 /* no explicit occurrence and single fetch => last header by default */
10269 occ = -1;
10270
10271 if (!occ)
10272 /* prepare to report multiple occurrences for ACL fetches */
10273 smp->flags |= SMP_F_NOT_LAST;
10274
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010275 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010276 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010277 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 +020010278 return 1;
10279
10280 smp->flags &= ~SMP_F_NOT_LAST;
10281 return 0;
10282}
10283
10284/* 6. Check on HTTP header count. The number of occurrences is returned.
10285 * Accepts exactly 1 argument of type string. It does not stop on commas and
10286 * returns full lines instead (useful for User-Agent or Date for example).
10287 */
10288static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010289smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010290{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010291 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010292 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010293 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010294 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010295 const char *name = NULL;
10296 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010297
Willy Tarreau601a4d12015-04-01 19:16:09 +020010298 if (args && args->type == ARGT_STR) {
10299 name = args->data.str.str;
10300 len = args->data.str.len;
10301 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010302
10303 CHECK_HTTP_MESSAGE_FIRST();
10304
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010305 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010306 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 +020010307
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010308 ctx.idx = 0;
10309 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010310 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010311 cnt++;
10312
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010313 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010314 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010315 smp->flags = SMP_F_VOL_HDR;
10316 return 1;
10317}
10318
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010319static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010320smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010321{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010322 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010323 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010324 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010325 struct chunk *temp;
10326 char del = ',';
10327
10328 if (args && args->type == ARGT_STR)
10329 del = *args[0].data.str.str;
10330
10331 CHECK_HTTP_MESSAGE_FIRST();
10332
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010333 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010334 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 +020010335
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010336 temp = get_trash_chunk();
10337
10338 ctx.idx = 0;
10339 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10340 if (temp->len)
10341 temp->str[temp->len++] = del;
10342 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10343 temp->len += ctx.del;
10344 }
10345
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010346 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010347 smp->data.u.str.str = temp->str;
10348 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010349 smp->flags = SMP_F_VOL_HDR;
10350 return 1;
10351}
10352
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010353/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10354 * Accepts an optional argument of type string containing the header field name,
10355 * and an optional argument of type signed or unsigned integer to request an
10356 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010357 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010358 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010359static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010360smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010361{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010362 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010363 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010364 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010365 int occ = 0;
10366 const char *name_str = NULL;
10367 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010368
Willy Tarreaua890d072013-04-02 12:01:06 +020010369 if (!ctx) {
10370 /* first call */
10371 ctx = &static_hdr_ctx;
10372 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010373 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010374 }
10375
Willy Tarreau185b5c42012-04-26 15:11:51 +020010376 if (args) {
10377 if (args[0].type != ARGT_STR)
10378 return 0;
10379 name_str = args[0].data.str.str;
10380 name_len = args[0].data.str.len;
10381
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010382 if (args[1].type == ARGT_SINT)
10383 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010384 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010385
Willy Tarreaue333ec92012-04-16 16:26:40 +020010386 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010387
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010388 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010389 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 +020010390
Willy Tarreau185b5c42012-04-26 15:11:51 +020010391 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010392 /* search for header from the beginning */
10393 ctx->idx = 0;
10394
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010395 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010396 /* no explicit occurrence and single fetch => last header by default */
10397 occ = -1;
10398
10399 if (!occ)
10400 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010401 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010402
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010403 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010404 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010405 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 +020010406 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010407
Willy Tarreau37406352012-04-23 16:16:37 +020010408 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010409 return 0;
10410}
10411
Willy Tarreauc11416f2007-06-17 16:58:38 +020010412/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010413 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010414 */
10415static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010416smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010417{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010418 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010419 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010420 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010421 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010422 const char *name = NULL;
10423 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010424
Willy Tarreau601a4d12015-04-01 19:16:09 +020010425 if (args && args->type == ARGT_STR) {
10426 name = args->data.str.str;
10427 len = args->data.str.len;
10428 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010429
Willy Tarreaue333ec92012-04-16 16:26:40 +020010430 CHECK_HTTP_MESSAGE_FIRST();
10431
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010432 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010433 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 +020010434
Willy Tarreau33a7e692007-06-10 19:45:56 +020010435 ctx.idx = 0;
10436 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010437 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010438 cnt++;
10439
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010440 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010441 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010442 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010443 return 1;
10444}
10445
Willy Tarreau185b5c42012-04-26 15:11:51 +020010446/* Fetch an HTTP header's integer value. The integer value is returned. It
10447 * takes a mandatory argument of type string and an optional one of type int
10448 * to designate a specific occurrence. It returns an unsigned integer, which
10449 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010450 */
10451static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010452smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010453{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010454 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010455
Willy Tarreauf853c462012-04-23 18:53:56 +020010456 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010457 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010458 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010459 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010460
Willy Tarreaud53e2422012-04-16 17:21:11 +020010461 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010462}
10463
Cyril Bonté69fa9922012-10-25 00:01:06 +020010464/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10465 * and an optional one of type int to designate a specific occurrence.
10466 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010467 */
10468static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010469smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010470{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010471 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010472
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010473 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010474 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010475 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010476 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010477 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010478 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010479 if (smp->data.u.str.len < temp->size - 1) {
10480 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10481 temp->str[smp->data.u.str.len] = '\0';
10482 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010483 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010484 break;
10485 }
10486 }
10487 }
10488
Willy Tarreaud53e2422012-04-16 17:21:11 +020010489 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010490 if (!(smp->flags & SMP_F_NOT_LAST))
10491 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010492 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010493 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010494}
10495
Willy Tarreau737b0c12007-06-10 21:28:46 +020010496/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10497 * the first '/' after the possible hostname, and ends before the possible '?'.
10498 */
10499static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010500smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010501{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010502 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010503 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010504
Willy Tarreauc0239e02012-04-16 14:42:55 +020010505 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010506
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010507 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010508 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010509 ptr = http_get_path(txn);
10510 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010511 return 0;
10512
10513 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010514 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010515 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010516
10517 while (ptr < end && *ptr != '?')
10518 ptr++;
10519
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010520 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010521 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010522 return 1;
10523}
10524
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010525/* This produces a concatenation of the first occurrence of the Host header
10526 * followed by the path component if it begins with a slash ('/'). This means
10527 * that '*' will not be added, resulting in exactly the first Host entry.
10528 * If no Host header is found, then the path is returned as-is. The returned
10529 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010530 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010531 */
10532static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010533smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010534{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010535 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010536 char *ptr, *end, *beg;
10537 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010538 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010539
10540 CHECK_HTTP_MESSAGE_FIRST();
10541
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010542 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010543 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010544 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010545 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010546
10547 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010548 temp = get_trash_chunk();
10549 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010550 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010551 smp->data.u.str.str = temp->str;
10552 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010553
10554 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010555 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010556 beg = http_get_path(txn);
10557 if (!beg)
10558 beg = end;
10559
10560 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10561
10562 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010563 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10564 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010565 }
10566
10567 smp->flags = SMP_F_VOL_1ST;
10568 return 1;
10569}
10570
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010571/* This produces a 32-bit hash of the concatenation of the first occurrence of
10572 * the Host header followed by the path component if it begins with a slash ('/').
10573 * This means that '*' will not be added, resulting in exactly the first Host
10574 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010575 * is hashed using the path hash followed by a full avalanche hash and provides a
10576 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010577 * high-traffic sites without having to store whole paths.
10578 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010579int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010580smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010581{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010582 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010583 struct hdr_ctx ctx;
10584 unsigned int hash = 0;
10585 char *ptr, *beg, *end;
10586 int len;
10587
10588 CHECK_HTTP_MESSAGE_FIRST();
10589
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010590 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010591 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010592 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010593 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10594 ptr = ctx.line + ctx.val;
10595 len = ctx.vlen;
10596 while (len--)
10597 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10598 }
10599
10600 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010601 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010602 beg = http_get_path(txn);
10603 if (!beg)
10604 beg = end;
10605
10606 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10607
10608 if (beg < ptr && *beg == '/') {
10609 while (beg < ptr)
10610 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10611 }
10612 hash = full_hash(hash);
10613
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010614 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010615 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010616 smp->flags = SMP_F_VOL_1ST;
10617 return 1;
10618}
10619
Willy Tarreau4a550602012-12-09 14:53:32 +010010620/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010621 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10622 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10623 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010624 * that in environments where IPv6 is insignificant, truncating the output to
10625 * 8 bytes would still work.
10626 */
10627static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010628smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010629{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010630 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010631 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010632
10633 if (!cli_conn)
10634 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010635
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010636 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010637 return 0;
10638
Willy Tarreau47ca5452012-12-23 20:22:19 +010010639 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010640 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010641 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010642
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010643 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010644 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010645 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010646 temp->len += 4;
10647 break;
10648 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010649 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010650 temp->len += 16;
10651 break;
10652 default:
10653 return 0;
10654 }
10655
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010656 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010657 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010658 return 1;
10659}
10660
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010661/* Extracts the query string, which comes after the question mark '?'. If no
10662 * question mark is found, nothing is returned. Otherwise it returns a sample
10663 * of type string carrying the whole query string.
10664 */
10665static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010666smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010667{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010668 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010669 char *ptr, *end;
10670
10671 CHECK_HTTP_MESSAGE_FIRST();
10672
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010673 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010674 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10675 end = ptr + txn->req.sl.rq.u_l;
10676
10677 /* look up the '?' */
10678 do {
10679 if (ptr == end)
10680 return 0;
10681 } while (*ptr++ != '?');
10682
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010683 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010684 smp->data.u.str.str = ptr;
10685 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010686 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10687 return 1;
10688}
10689
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010690static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010691smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010692{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010693 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10694 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10695 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010696
Willy Tarreau24e32d82012-04-23 23:55:44 +020010697 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010698
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010699 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010700 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010701 return 1;
10702}
10703
Willy Tarreau7f18e522010-10-22 20:04:13 +020010704/* return a valid test if the current request is the first one on the connection */
10705static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010706smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010707{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010708 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010709 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010710 return 1;
10711}
10712
Willy Tarreau34db1082012-04-19 17:16:54 +020010713/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010714static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010715smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010716{
10717
Willy Tarreau24e32d82012-04-23 23:55:44 +020010718 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010719 return 0;
10720
Willy Tarreauc0239e02012-04-16 14:42:55 +020010721 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010722
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010723 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010724 return 0;
10725
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010726 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010727 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010728 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010729 return 1;
10730}
Willy Tarreau8797c062007-05-07 00:55:35 +020010731
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010732/* Accepts exactly 1 argument of type userlist */
10733static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010734smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010735{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010736 if (!args || args->type != ARGT_USR)
10737 return 0;
10738
10739 CHECK_HTTP_MESSAGE_FIRST();
10740
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010741 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010742 return 0;
10743
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010744 /* if the user does not belong to the userlist or has a wrong password,
10745 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010746 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010747 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010748 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10749 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010750 return 0;
10751
10752 /* pat_match_auth() will need the user list */
10753 smp->ctx.a[0] = args->data.usr;
10754
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010755 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010756 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010757 smp->data.u.str.str = smp->strm->txn->auth.user;
10758 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010759
10760 return 1;
10761}
10762
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010763/* Try to find the next occurrence of a cookie name in a cookie header value.
10764 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10765 * the cookie value is returned into *value and *value_l, and the function
10766 * returns a pointer to the next pointer to search from if the value was found.
10767 * Otherwise if the cookie was not found, NULL is returned and neither value
10768 * nor value_l are touched. The input <hdr> string should first point to the
10769 * header's value, and the <hdr_end> pointer must point to the first character
10770 * not part of the value. <list> must be non-zero if value may represent a list
10771 * of values (cookie headers). This makes it faster to abort parsing when no
10772 * list is expected.
10773 */
David Carlier4686f792015-09-25 14:10:50 +010010774char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010775extract_cookie_value(char *hdr, const char *hdr_end,
10776 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010777 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010778{
10779 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10780 char *next;
10781
10782 /* we search at least a cookie name followed by an equal, and more
10783 * generally something like this :
10784 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10785 */
10786 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10787 /* Iterate through all cookies on this line */
10788
10789 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10790 att_beg++;
10791
10792 /* find att_end : this is the first character after the last non
10793 * space before the equal. It may be equal to hdr_end.
10794 */
10795 equal = att_end = att_beg;
10796
10797 while (equal < hdr_end) {
10798 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10799 break;
10800 if (http_is_spht[(unsigned char)*equal++])
10801 continue;
10802 att_end = equal;
10803 }
10804
10805 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10806 * is between <att_beg> and <equal>, both may be identical.
10807 */
10808
10809 /* look for end of cookie if there is an equal sign */
10810 if (equal < hdr_end && *equal == '=') {
10811 /* look for the beginning of the value */
10812 val_beg = equal + 1;
10813 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10814 val_beg++;
10815
10816 /* find the end of the value, respecting quotes */
10817 next = find_cookie_value_end(val_beg, hdr_end);
10818
10819 /* make val_end point to the first white space or delimitor after the value */
10820 val_end = next;
10821 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10822 val_end--;
10823 } else {
10824 val_beg = val_end = next = equal;
10825 }
10826
10827 /* We have nothing to do with attributes beginning with '$'. However,
10828 * they will automatically be removed if a header before them is removed,
10829 * since they're supposed to be linked together.
10830 */
10831 if (*att_beg == '$')
10832 continue;
10833
10834 /* Ignore cookies with no equal sign */
10835 if (equal == next)
10836 continue;
10837
10838 /* Now we have the cookie name between att_beg and att_end, and
10839 * its value between val_beg and val_end.
10840 */
10841
10842 if (att_end - att_beg == cookie_name_l &&
10843 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10844 /* let's return this value and indicate where to go on from */
10845 *value = val_beg;
10846 *value_l = val_end - val_beg;
10847 return next + 1;
10848 }
10849
10850 /* Set-Cookie headers only have the name in the first attr=value part */
10851 if (!list)
10852 break;
10853 }
10854
10855 return NULL;
10856}
10857
William Lallemanda43ba4e2014-01-28 18:14:25 +010010858/* Fetch a captured HTTP request header. The index is the position of
10859 * the "capture" option in the configuration file
10860 */
10861static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010862smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010863{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010864 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010865 int idx;
10866
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010867 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010868 return 0;
10869
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010870 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010871
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010872 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 +010010873 return 0;
10874
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010875 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010876 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010877 smp->data.u.str.str = smp->strm->req_cap[idx];
10878 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010879
10880 return 1;
10881}
10882
10883/* Fetch a captured HTTP response header. The index is the position of
10884 * the "capture" option in the configuration file
10885 */
10886static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010887smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010888{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010889 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010890 int idx;
10891
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010892 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010893 return 0;
10894
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010895 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010896
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010897 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 +010010898 return 0;
10899
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010900 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010901 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010902 smp->data.u.str.str = smp->strm->res_cap[idx];
10903 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010904
10905 return 1;
10906}
10907
William Lallemand65ad6e12014-01-31 15:08:02 +010010908/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10909static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010910smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010911{
10912 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010913 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010010914 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010915
Willy Tarreau15e91e12015-04-04 00:52:09 +020010916 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010917 return 0;
10918
William Lallemand96a77852014-02-05 00:30:02 +010010919 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010920
William Lallemand96a77852014-02-05 00:30:02 +010010921 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10922 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010923
William Lallemand96a77852014-02-05 00:30:02 +010010924 temp = get_trash_chunk();
10925 temp->str = txn->uri;
10926 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010927 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010928 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010929 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010930
10931 return 1;
10932
10933}
10934
10935/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10936static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010937smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010938{
10939 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010940 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010010941 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010942
Willy Tarreau15e91e12015-04-04 00:52:09 +020010943 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010944 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010945
William Lallemand65ad6e12014-01-31 15:08:02 +010010946 ptr = txn->uri;
10947
10948 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10949 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010950
William Lallemand65ad6e12014-01-31 15:08:02 +010010951 if (!*ptr)
10952 return 0;
10953
10954 ptr++; /* skip the space */
10955
10956 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010957 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010958 if (!ptr)
10959 return 0;
10960 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10961 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010962
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010963 smp->data.u.str = *temp;
10964 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010965 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010966 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010967
10968 return 1;
10969}
10970
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010971/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10972 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10973 */
10974static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010975smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010976{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010977 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010978
Willy Tarreau15e91e12015-04-04 00:52:09 +020010979 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010980 return 0;
10981
10982 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010983 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010984 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010985 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010986
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010987 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010988 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010989 smp->flags = SMP_F_CONST;
10990 return 1;
10991
10992}
10993
10994/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10995 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10996 */
10997static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010998smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010999{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011000 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011001
Willy Tarreau15e91e12015-04-04 00:52:09 +020011002 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011003 return 0;
11004
11005 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011006 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011007 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011008 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011009
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011010 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011011 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011012 smp->flags = SMP_F_CONST;
11013 return 1;
11014
11015}
11016
William Lallemand65ad6e12014-01-31 15:08:02 +010011017
Willy Tarreaue333ec92012-04-16 16:26:40 +020011018/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011019 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011020 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011021 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011022 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011023 * Accepts exactly 1 argument of type string. If the input options indicate
11024 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011025 * The returned sample is of type CSTR. Can be used to parse cookies in other
11026 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011027 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011028int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011029{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011030 struct http_txn *txn;
11031 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011032 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011033 const struct http_msg *msg;
11034 const char *hdr_name;
11035 int hdr_name_len;
11036 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011037 int occ = 0;
11038 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011039
Willy Tarreau24e32d82012-04-23 23:55:44 +020011040 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011041 return 0;
11042
Willy Tarreaua890d072013-04-02 12:01:06 +020011043 if (!ctx) {
11044 /* first call */
11045 ctx = &static_hdr_ctx;
11046 ctx->idx = 0;
11047 smp->ctx.a[2] = ctx;
11048 }
11049
Willy Tarreaue333ec92012-04-16 16:26:40 +020011050 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011051
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011052 txn = smp->strm->txn;
11053 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011054
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011055 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011056 msg = &txn->req;
11057 hdr_name = "Cookie";
11058 hdr_name_len = 6;
11059 } else {
11060 msg = &txn->rsp;
11061 hdr_name = "Set-Cookie";
11062 hdr_name_len = 10;
11063 }
11064
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011065 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011066 /* no explicit occurrence and single fetch => last cookie by default */
11067 occ = -1;
11068
11069 /* OK so basically here, either we want only one value and it's the
11070 * last one, or we want to iterate over all of them and we fetch the
11071 * next one.
11072 */
11073
Willy Tarreau9b28e032012-10-12 23:49:43 +020011074 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011075 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011076 /* search for the header from the beginning, we must first initialize
11077 * the search parameters.
11078 */
Willy Tarreau37406352012-04-23 16:16:37 +020011079 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011080 ctx->idx = 0;
11081 }
11082
Willy Tarreau28376d62012-04-26 21:26:10 +020011083 smp->flags |= SMP_F_VOL_HDR;
11084
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011085 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011086 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11087 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011088 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11089 goto out;
11090
Willy Tarreau24e32d82012-04-23 23:55:44 +020011091 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011092 continue;
11093
Willy Tarreau37406352012-04-23 16:16:37 +020011094 smp->ctx.a[0] = ctx->line + ctx->val;
11095 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011096 }
11097
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011098 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011099 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011100 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011101 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011102 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011103 &smp->data.u.str.str,
11104 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011105 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011106 found = 1;
11107 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011108 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011109 smp->flags |= SMP_F_NOT_LAST;
11110 return 1;
11111 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011112 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011113 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011114 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011115 /* all cookie headers and values were scanned. If we're looking for the
11116 * last occurrence, we may return it now.
11117 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011118 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011119 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011120 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011121}
11122
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011123/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011124 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011125 * multiple cookies may be parsed on the same line. The returned sample is of
11126 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011127 */
11128static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011129smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011130{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011131 struct http_txn *txn;
11132 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011133 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011134 const struct http_msg *msg;
11135 const char *hdr_name;
11136 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011137 int cnt;
11138 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011139 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011140
Willy Tarreau24e32d82012-04-23 23:55:44 +020011141 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011142 return 0;
11143
Willy Tarreaue333ec92012-04-16 16:26:40 +020011144 CHECK_HTTP_MESSAGE_FIRST();
11145
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011146 txn = smp->strm->txn;
11147 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011148
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011149 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011150 msg = &txn->req;
11151 hdr_name = "Cookie";
11152 hdr_name_len = 6;
11153 } else {
11154 msg = &txn->rsp;
11155 hdr_name = "Set-Cookie";
11156 hdr_name_len = 10;
11157 }
11158
Willy Tarreau9b28e032012-10-12 23:49:43 +020011159 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011160 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011161 ctx.idx = 0;
11162 cnt = 0;
11163
11164 while (1) {
11165 /* Note: val_beg == NULL every time we need to fetch a new header */
11166 if (!val_beg) {
11167 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11168 break;
11169
Willy Tarreau24e32d82012-04-23 23:55:44 +020011170 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011171 continue;
11172
11173 val_beg = ctx.line + ctx.val;
11174 val_end = val_beg + ctx.vlen;
11175 }
11176
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011177 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011178 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011179 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011180 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011181 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011182 &smp->data.u.str.str,
11183 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011184 cnt++;
11185 }
11186 }
11187
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011188 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011189 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011190 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011191 return 1;
11192}
11193
Willy Tarreau51539362012-05-08 12:46:28 +020011194/* Fetch an cookie's integer value. The integer value is returned. It
11195 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11196 */
11197static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011198smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011199{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011200 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011201
11202 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011203 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011204 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011205 }
11206
11207 return ret;
11208}
11209
Willy Tarreau8797c062007-05-07 00:55:35 +020011210/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011211/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011212/************************************************************************/
11213
David Cournapeau16023ee2010-12-23 20:55:41 +090011214/*
11215 * Given a path string and its length, find the position of beginning of the
11216 * query string. Returns NULL if no query string is found in the path.
11217 *
11218 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11219 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011220 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011221 */
bedis4c75cca2012-10-05 08:38:24 +020011222static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011223{
11224 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011225
bedis4c75cca2012-10-05 08:38:24 +020011226 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011227 return p ? p + 1 : NULL;
11228}
11229
bedis4c75cca2012-10-05 08:38:24 +020011230static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011231{
bedis4c75cca2012-10-05 08:38:24 +020011232 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011233}
11234
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011235/* after increasing a pointer value, it can exceed the first buffer
11236 * size. This function transform the value of <ptr> according with
11237 * the expected position. <chunks> is an array of the one or two
11238 * avalaible chunks. The first value is the start of the first chunk,
11239 * the second value if the end+1 of the first chunks. The third value
11240 * is NULL or the start of the second chunk and the fourth value is
11241 * the end+1 of the second chunk. The function returns 1 if does a
11242 * wrap, else returns 0.
11243 */
11244static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11245{
11246 if (*ptr < chunks[1])
11247 return 0;
11248 if (!chunks[2])
11249 return 0;
11250 *ptr = chunks[2] + ( *ptr - chunks[1] );
11251 return 1;
11252}
11253
David Cournapeau16023ee2010-12-23 20:55:41 +090011254/*
11255 * Given a url parameter, find the starting position of the first occurence,
11256 * or NULL if the parameter is not found.
11257 *
11258 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11259 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011260 *
Willy Tarreauf6625822015-12-27 14:51:01 +010011261 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011262 * or the second chunk. The caller must be check the position before using the
11263 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011264 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011265static const char *
11266find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011267 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011268 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011269{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011270 const char *pos, *last, *equal;
11271 const char **bufs = chunks;
11272 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011273
David Cournapeau16023ee2010-12-23 20:55:41 +090011274
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011275 pos = bufs[0];
11276 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010011277 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011278 /* Check the equal. */
11279 equal = pos + url_param_name_l;
11280 if (fix_pointer_if_wrap(chunks, &equal)) {
11281 if (equal >= chunks[3])
11282 return NULL;
11283 } else {
11284 if (equal >= chunks[1])
11285 return NULL;
11286 }
11287 if (*equal == '=') {
11288 if (pos + url_param_name_l > last) {
11289 /* process wrap case, we detect a wrap. In this case, the
11290 * comparison is performed in two parts.
11291 */
11292
11293 /* This is the end, we dont have any other chunk. */
11294 if (bufs != chunks || !bufs[2])
11295 return NULL;
11296
11297 /* Compute the length of each part of the comparison. */
11298 l1 = last - pos;
11299 l2 = url_param_name_l - l1;
11300
11301 /* The second buffer is too short to contain the compared string. */
11302 if (bufs[2] + l2 > bufs[3])
11303 return NULL;
11304
11305 if (memcmp(pos, url_param_name, l1) == 0 &&
11306 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11307 return pos;
11308
11309 /* Perform wrapping and jump the string who fail the comparison. */
11310 bufs += 2;
11311 pos = bufs[0] + l2;
11312 last = bufs[1];
11313
11314 } else {
11315 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011316 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11317 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011318 pos += url_param_name_l + 1;
11319 if (fix_pointer_if_wrap(chunks, &pos))
11320 last = bufs[2];
11321 }
11322 }
11323
11324 while (1) {
11325 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011326 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011327 pos++;
11328 if (pos < last)
11329 break;
11330 /* process buffer wrapping. */
11331 if (bufs != chunks || !bufs[2])
11332 return NULL;
11333 bufs += 2;
11334 pos = bufs[0];
11335 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011336 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011337 pos++;
11338 }
11339 return NULL;
11340}
11341
11342/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011343 * Given a url parameter name and a query string, find the next value.
11344 * An empty url_param_name matches the first available parameter.
11345 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11346 * respectively provide a pointer to the value and its end.
11347 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011348 */
11349static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011350find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011351 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011352 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011353{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011354 const char *arg_start, *qs_end;
11355 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011356
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011357 arg_start = chunks[0];
11358 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011359 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011360 /* Looks for an argument name. */
11361 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011362 url_param_name, url_param_name_l,
11363 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011364 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011365 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011366 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011367 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011368 if (!arg_start)
11369 return 0;
11370
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011371 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011372 while (1) {
11373 /* looks for the first argument. */
11374 value_start = memchr(arg_start, '=', qs_end - arg_start);
11375 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011376 /* Check for wrapping. */
11377 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011378 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011379 chunks[2]) {
11380 arg_start = chunks[2];
11381 qs_end = chunks[3];
11382 continue;
11383 }
11384 return 0;
11385 }
11386 break;
11387 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011388 value_start++;
11389 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011390 else {
11391 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011392 value_start = arg_start + url_param_name_l + 1;
11393
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011394 /* Check for pointer wrapping. */
11395 if (fix_pointer_if_wrap(chunks, &value_start)) {
11396 /* Update the end pointer. */
11397 qs_end = chunks[3];
11398
11399 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011400 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011401 return 0;
11402 }
11403 }
11404
David Cournapeau16023ee2010-12-23 20:55:41 +090011405 value_end = value_start;
11406
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011407 while (1) {
11408 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11409 value_end++;
11410 if (value_end < qs_end)
11411 break;
11412 /* process buffer wrapping. */
11413 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011414 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011415 chunks[2]) {
11416 value_end = chunks[2];
11417 qs_end = chunks[3];
11418 continue;
11419 }
11420 break;
11421 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011422
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011423 *vstart = value_start;
11424 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011425 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011426}
11427
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011428/* This scans a URL-encoded query string. It takes an optionally wrapping
11429 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11430 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11431 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011432 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011433static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011434smp_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 +090011435{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011436 const char *vstart, *vend;
11437 struct chunk *temp;
11438 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011439
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011440 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011441 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011442 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011443 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011444 return 0;
11445
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011446 /* Create sample. If the value is contiguous, return the pointer as CONST,
11447 * if the value is wrapped, copy-it in a buffer.
11448 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011449 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011450 if (chunks[2] &&
11451 vstart >= chunks[0] && vstart <= chunks[1] &&
11452 vend >= chunks[2] && vend <= chunks[3]) {
11453 /* Wrapped case. */
11454 temp = get_trash_chunk();
11455 memcpy(temp->str, vstart, chunks[1] - vstart);
11456 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011457 smp->data.u.str.str = temp->str;
11458 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011459 } else {
11460 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011461 smp->data.u.str.str = (char *)vstart;
11462 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011463 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11464 }
11465
11466 /* Update context, check wrapping. */
11467 chunks[0] = vend;
11468 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11469 chunks[1] = chunks[3];
11470 chunks[2] = NULL;
11471 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011472
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011473 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011474 smp->flags |= SMP_F_NOT_LAST;
11475
David Cournapeau16023ee2010-12-23 20:55:41 +090011476 return 1;
11477}
11478
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011479/* This function iterates over each parameter of the query string. It uses
11480 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011481 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11482 * An optional parameter name is passed in args[0], otherwise any parameter is
11483 * considered. It supports an optional delimiter argument for the beginning of
11484 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011485 */
11486static int
11487smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11488{
11489 struct http_msg *msg;
11490 char delim = '?';
11491 const char *name;
11492 int name_len;
11493
Dragan Dosen26f77e52015-05-25 10:02:11 +020011494 if (!args ||
11495 (args[0].type && args[0].type != ARGT_STR) ||
11496 (args[1].type && args[1].type != ARGT_STR))
11497 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011498
Dragan Dosen26f77e52015-05-25 10:02:11 +020011499 name = "";
11500 name_len = 0;
11501 if (args->type == ARGT_STR) {
11502 name = args->data.str.str;
11503 name_len = args->data.str.len;
11504 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011505
Dragan Dosen26f77e52015-05-25 10:02:11 +020011506 if (args[1].type)
11507 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011508
Dragan Dosen26f77e52015-05-25 10:02:11 +020011509 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011510 CHECK_HTTP_MESSAGE_FIRST();
11511
11512 msg = &smp->strm->txn->req;
11513
11514 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11515 msg->sl.rq.u_l, delim);
11516 if (!smp->ctx.a[0])
11517 return 0;
11518
11519 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011520
11521 /* Assume that the context is filled with NULL pointer
11522 * before the first call.
11523 * smp->ctx.a[2] = NULL;
11524 * smp->ctx.a[3] = NULL;
11525 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011526 }
11527
11528 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11529}
11530
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011531/* This function iterates over each parameter of the body. This requires
11532 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011533 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11534 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11535 * optional second part if the body wraps at the end of the buffer. An optional
11536 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011537 */
11538static int
11539smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11540{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011541 struct http_msg *msg;
11542 unsigned long len;
11543 unsigned long block1;
11544 char *body;
11545 const char *name;
11546 int name_len;
11547
11548 if (!args || (args[0].type && args[0].type != ARGT_STR))
11549 return 0;
11550
11551 name = "";
11552 name_len = 0;
11553 if (args[0].type == ARGT_STR) {
11554 name = args[0].data.str.str;
11555 name_len = args[0].data.str.len;
11556 }
11557
11558 if (!smp->ctx.a[0]) { // first call, find the query string
11559 CHECK_HTTP_MESSAGE_FIRST();
11560
11561 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011562 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011563 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011564 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011565
11566 len = http_body_bytes(msg);
11567 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11568
11569 block1 = len;
11570 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11571 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11572
11573 if (block1 == len) {
11574 /* buffer is not wrapped (or empty) */
11575 smp->ctx.a[0] = body;
11576 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011577
11578 /* Assume that the context is filled with NULL pointer
11579 * before the first call.
11580 * smp->ctx.a[2] = NULL;
11581 * smp->ctx.a[3] = NULL;
11582 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011583 }
11584 else {
11585 /* buffer is wrapped, we need to defragment it */
11586 smp->ctx.a[0] = body;
11587 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011588 smp->ctx.a[2] = msg->chn->buf->data;
11589 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011590 }
11591 }
11592 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11593}
11594
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011595/* Return the signed integer value for the specified url parameter (see url_param
11596 * above).
11597 */
11598static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011599smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011600{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011601 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011602
11603 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011604 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011605 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011606 }
11607
11608 return ret;
11609}
11610
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011611/* This produces a 32-bit hash of the concatenation of the first occurrence of
11612 * the Host header followed by the path component if it begins with a slash ('/').
11613 * This means that '*' will not be added, resulting in exactly the first Host
11614 * entry. If no Host header is found, then the path is used. The resulting value
11615 * is hashed using the url hash followed by a full avalanche hash and provides a
11616 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11617 * high-traffic sites without having to store whole paths.
11618 * this differs from the base32 functions in that it includes the url parameters
11619 * as well as the path
11620 */
11621static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011622smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011623{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011624 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011625 struct hdr_ctx ctx;
11626 unsigned int hash = 0;
11627 char *ptr, *beg, *end;
11628 int len;
11629
11630 CHECK_HTTP_MESSAGE_FIRST();
11631
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011632 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011633 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011634 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011635 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11636 ptr = ctx.line + ctx.val;
11637 len = ctx.vlen;
11638 while (len--)
11639 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11640 }
11641
11642 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011643 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 +000011644 beg = http_get_path(txn);
11645 if (!beg)
11646 beg = end;
11647
11648 for (ptr = beg; ptr < end ; ptr++);
11649
11650 if (beg < ptr && *beg == '/') {
11651 while (beg < ptr)
11652 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11653 }
11654 hash = full_hash(hash);
11655
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011656 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011657 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011658 smp->flags = SMP_F_VOL_1ST;
11659 return 1;
11660}
11661
11662/* This concatenates the source address with the 32-bit hash of the Host and
11663 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11664 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11665 * on the source address length. The URL hash is stored before the address so
11666 * that in environments where IPv6 is insignificant, truncating the output to
11667 * 8 bytes would still work.
11668 */
11669static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011670smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011671{
11672 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011673 struct connection *cli_conn = objt_conn(smp->sess->origin);
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011674 unsigned int hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011675
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011676 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011677 return 0;
11678
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011679 /* The returned hash is a 32 bytes integer. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011680 hash = smp->data.u.sint;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011681
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011682 temp = get_trash_chunk();
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011683 memcpy(temp->str + temp->len, &hash, sizeof(hash));
11684 temp->len += sizeof(hash);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011685
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011686 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011687 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011688 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011689 temp->len += 4;
11690 break;
11691 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011692 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011693 temp->len += 16;
11694 break;
11695 default:
11696 return 0;
11697 }
11698
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011699 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011700 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011701 return 1;
11702}
11703
Willy Tarreau185b5c42012-04-26 15:11:51 +020011704/* This function is used to validate the arguments passed to any "hdr" fetch
11705 * keyword. These keywords support an optional positive or negative occurrence
11706 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11707 * is assumed that the types are already the correct ones. Returns 0 on error,
11708 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11709 * error message in case of error, that the caller is responsible for freeing.
11710 * The initial location must either be freeable or NULL.
11711 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011712int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011713{
11714 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011715 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011716 return 0;
11717 }
11718 return 1;
11719}
11720
Willy Tarreau276fae92013-07-25 14:36:01 +020011721/* takes an UINT value on input supposed to represent the time since EPOCH,
11722 * adds an optional offset found in args[0] and emits a string representing
11723 * the date in RFC-1123/5322 format.
11724 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011725static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011726{
Cyril Bontéf78d8962016-01-22 19:40:28 +010011727 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020011728 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11729 struct chunk *temp;
11730 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011731 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011732 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011733
11734 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011735 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011736 curr_date += args[0].data.sint;
11737
11738 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011739 if (!tm)
11740 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011741
11742 temp = get_trash_chunk();
11743 temp->len = snprintf(temp->str, temp->size - temp->len,
11744 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11745 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11746 tm->tm_hour, tm->tm_min, tm->tm_sec);
11747
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011748 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011749 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011750 return 1;
11751}
11752
Thierry FOURNIERad903512014-04-11 17:51:01 +020011753/* Match language range with language tag. RFC2616 14.4:
11754 *
11755 * A language-range matches a language-tag if it exactly equals
11756 * the tag, or if it exactly equals a prefix of the tag such
11757 * that the first tag character following the prefix is "-".
11758 *
11759 * Return 1 if the strings match, else return 0.
11760 */
11761static inline int language_range_match(const char *range, int range_len,
11762 const char *tag, int tag_len)
11763{
11764 const char *end = range + range_len;
11765 const char *tend = tag + tag_len;
11766 while (range < end) {
11767 if (*range == '-' && tag == tend)
11768 return 1;
11769 if (*range != *tag || tag == tend)
11770 return 0;
11771 range++;
11772 tag++;
11773 }
11774 /* Return true only if the last char of the tag is matched. */
11775 return tag == tend;
11776}
11777
11778/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011779static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011780{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011781 const char *al = smp->data.u.str.str;
11782 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011783 const char *token;
11784 int toklen;
11785 int qvalue;
11786 const char *str;
11787 const char *w;
11788 int best_q = 0;
11789
11790 /* Set the constant to the sample, because the output of the
11791 * function will be peek in the constant configuration string.
11792 */
11793 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011794 smp->data.u.str.size = 0;
11795 smp->data.u.str.str = "";
11796 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011797
11798 /* Parse the accept language */
11799 while (1) {
11800
11801 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011802 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011803 al++;
11804 if (al >= end)
11805 break;
11806
11807 /* Start of the fisrt word. */
11808 token = al;
11809
11810 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011811 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011812 al++;
11813 if (al == token)
11814 goto expect_comma;
11815
11816 /* Length of the token. */
11817 toklen = al - token;
11818 qvalue = 1000;
11819
11820 /* Check if the token exists in the list. If the token not exists,
11821 * jump to the next token.
11822 */
11823 str = args[0].data.str.str;
11824 w = str;
11825 while (1) {
11826 if (*str == ';' || *str == '\0') {
11827 if (language_range_match(token, toklen, w, str-w))
11828 goto look_for_q;
11829 if (*str == '\0')
11830 goto expect_comma;
11831 w = str + 1;
11832 }
11833 str++;
11834 }
11835 goto expect_comma;
11836
11837look_for_q:
11838
11839 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011840 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011841 al++;
11842 if (al >= end)
11843 goto process_value;
11844
11845 /* If ',' is found, process the result */
11846 if (*al == ',')
11847 goto process_value;
11848
11849 /* If the character is different from ';', look
11850 * for the end of the header part in best effort.
11851 */
11852 if (*al != ';')
11853 goto expect_comma;
11854
11855 /* Assumes that the char is ';', now expect "q=". */
11856 al++;
11857
11858 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011859 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011860 al++;
11861 if (al >= end)
11862 goto process_value;
11863
11864 /* Expect 'q'. If no 'q', continue in best effort */
11865 if (*al != 'q')
11866 goto process_value;
11867 al++;
11868
11869 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011870 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011871 al++;
11872 if (al >= end)
11873 goto process_value;
11874
11875 /* Expect '='. If no '=', continue in best effort */
11876 if (*al != '=')
11877 goto process_value;
11878 al++;
11879
11880 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011881 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011882 al++;
11883 if (al >= end)
11884 goto process_value;
11885
11886 /* Parse the q value. */
11887 qvalue = parse_qvalue(al, &al);
11888
11889process_value:
11890
11891 /* If the new q value is the best q value, then store the associated
11892 * language in the response. If qvalue is the biggest value (1000),
11893 * break the process.
11894 */
11895 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011896 smp->data.u.str.str = (char *)w;
11897 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011898 if (qvalue >= 1000)
11899 break;
11900 best_q = qvalue;
11901 }
11902
11903expect_comma:
11904
11905 /* Expect comma or end. If the end is detected, quit the loop. */
11906 while (al < end && *al != ',')
11907 al++;
11908 if (al >= end)
11909 break;
11910
11911 /* Comma is found, jump it and restart the analyzer. */
11912 al++;
11913 }
11914
11915 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011916 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
11917 smp->data.u.str.str = args[1].data.str.str;
11918 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011919 }
11920
11921 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011922 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011923}
11924
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011925/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011926static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011927{
11928 /* If the constant flag is set or if not size is avalaible at
11929 * the end of the buffer, copy the string in other buffer
11930 * before decoding.
11931 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011932 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011933 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011934 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
11935 smp->data.u.str.str = str->str;
11936 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011937 smp->flags &= ~SMP_F_CONST;
11938 }
11939
11940 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011941 smp->data.u.str.str[smp->data.u.str.len] = '\0';
11942 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011943 return 1;
11944}
11945
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011946static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
11947{
11948 struct proxy *fe = strm_fe(smp->strm);
11949 int idx, i;
11950 struct cap_hdr *hdr;
11951 int len;
11952
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011953 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011954 return 0;
11955
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011956 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011957
11958 /* Check the availibity of the capture id. */
11959 if (idx > fe->nb_req_cap - 1)
11960 return 0;
11961
11962 /* Look for the original configuration. */
11963 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
11964 hdr != NULL && i != idx ;
11965 i--, hdr = hdr->next);
11966 if (!hdr)
11967 return 0;
11968
11969 /* check for the memory allocation */
11970 if (smp->strm->req_cap[hdr->index] == NULL)
11971 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
11972 if (smp->strm->req_cap[hdr->index] == NULL)
11973 return 0;
11974
11975 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011976 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011977 if (len > hdr->len)
11978 len = hdr->len;
11979
11980 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011981 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011982 smp->strm->req_cap[idx][len] = '\0';
11983
11984 return 1;
11985}
11986
11987static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
11988{
11989 struct proxy *fe = strm_fe(smp->strm);
11990 int idx, i;
11991 struct cap_hdr *hdr;
11992 int len;
11993
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011994 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011995 return 0;
11996
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011997 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011998
11999 /* Check the availibity of the capture id. */
12000 if (idx > fe->nb_rsp_cap - 1)
12001 return 0;
12002
12003 /* Look for the original configuration. */
12004 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12005 hdr != NULL && i != idx ;
12006 i--, hdr = hdr->next);
12007 if (!hdr)
12008 return 0;
12009
12010 /* check for the memory allocation */
12011 if (smp->strm->res_cap[hdr->index] == NULL)
12012 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
12013 if (smp->strm->res_cap[hdr->index] == NULL)
12014 return 0;
12015
12016 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012017 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012018 if (len > hdr->len)
12019 len = hdr->len;
12020
12021 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012022 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012023 smp->strm->res_cap[idx][len] = '\0';
12024
12025 return 1;
12026}
12027
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012028/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012029 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012030 * the relevant part of the request line accordingly. Then it updates various
12031 * pointers to the next elements which were moved, and the total buffer length.
12032 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012033 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12034 * error, though this can be revisited when this code is finally exploited.
12035 *
12036 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12037 * query string and 3 to replace uri.
12038 *
12039 * In query string case, the mark question '?' must be set at the start of the
12040 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012041 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012042int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012043 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012044{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012045 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012046 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012047 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012048 int delta;
12049
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012050 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012051 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012052 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012053 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12054
12055 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012056 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012057 txn->req.sl.rq.m_l += delta;
12058 txn->req.sl.rq.u += delta;
12059 txn->req.sl.rq.v += delta;
12060 break;
12061
12062 case 1: // path
12063 cur_ptr = http_get_path(txn);
12064 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012065 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012066
12067 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012068 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 +010012069 cur_end++;
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 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012078 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012079 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012080 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12081 while (cur_ptr < cur_end && *cur_ptr != '?')
12082 cur_ptr++;
12083
12084 /* skip the question mark or indicate that we must insert it
12085 * (but only if the format string is not empty then).
12086 */
12087 if (cur_ptr < cur_end)
12088 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012089 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012090 offset = 0;
12091
12092 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012093 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012094 txn->req.sl.rq.u_l += delta;
12095 txn->req.sl.rq.v += delta;
12096 break;
12097
12098 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012099 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012100 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12101
12102 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012103 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012104 txn->req.sl.rq.u_l += delta;
12105 txn->req.sl.rq.v += delta;
12106 break;
12107
12108 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012109 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012110 }
12111
12112 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012113 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012114 txn->req.sl.rq.l += delta;
12115 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012116 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012117 return 0;
12118}
12119
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012120/* This function replace the HTTP status code and the associated message. The
12121 * variable <status> contains the new status code. This function never fails.
12122 */
12123void http_set_status(unsigned int status, struct stream *s)
12124{
12125 struct http_txn *txn = s->txn;
12126 char *cur_ptr, *cur_end;
12127 int delta;
12128 char *res;
12129 int c_l;
12130 const char *msg;
12131 int msg_len;
12132
12133 chunk_reset(&trash);
12134
12135 res = ultoa_o(status, trash.str, trash.size);
12136 c_l = res - trash.str;
12137
12138 trash.str[c_l] = ' ';
12139 trash.len = c_l + 1;
12140
12141 msg = get_reason(status);
12142 msg_len = strlen(msg);
12143
12144 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
12145 trash.len += msg_len;
12146
12147 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
12148 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
12149
12150 /* commit changes and adjust message */
12151 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
12152
12153 /* adjust res line offsets and lengths */
12154 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
12155 txn->rsp.sl.st.c_l = c_l;
12156 txn->rsp.sl.st.r_l = msg_len;
12157
12158 delta = trash.len - (cur_end - cur_ptr);
12159 txn->rsp.sl.st.l += delta;
12160 txn->hdr_idx.v[0].len += delta;
12161 http_msg_move_end(&txn->rsp, delta);
12162}
12163
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012164/* This function executes one of the set-{method,path,query,uri} actions. It
12165 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012166 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012167 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012168 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12169 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012170 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012171enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012172 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012173{
12174 chunk_reset(&trash);
12175
12176 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012177 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012178 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012179 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012180
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012181 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012182 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012183}
12184
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012185/* This function is just a compliant action wrapper for "set-status". */
12186enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012187 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012188{
12189 http_set_status(rule->arg.status.code, s);
12190 return ACT_RET_CONT;
12191}
12192
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012193/* parse an http-request action among :
12194 * set-method
12195 * set-path
12196 * set-query
12197 * set-uri
12198 *
12199 * All of them accept a single argument of type string representing a log-format.
12200 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12201 * 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 +020012202 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012203 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012204enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12205 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012206{
12207 int cur_arg = *orig_arg;
12208
Thierry FOURNIER42148732015-09-02 17:17:33 +020012209 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012210
12211 switch (args[0][4]) {
12212 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012213 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012214 rule->action_ptr = http_action_set_req_line;
12215 break;
12216 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012217 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012218 rule->action_ptr = http_action_set_req_line;
12219 break;
12220 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012221 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012222 rule->action_ptr = http_action_set_req_line;
12223 break;
12224 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012225 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012226 rule->action_ptr = http_action_set_req_line;
12227 break;
12228 default:
12229 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012230 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012231 }
12232
12233 if (!*args[cur_arg] ||
12234 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12235 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012236 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012237 }
12238
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012239 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012240 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012241 parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012242 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
12243 proxy->conf.args.file, proxy->conf.args.line);
12244
12245 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012246 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012247}
12248
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012249/* parse set-status action:
12250 * This action accepts a single argument of type int representing
12251 * an http status code. It returns ACT_RET_PRS_OK on success,
12252 * ACT_RET_PRS_ERR on error.
12253 */
12254enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12255 struct act_rule *rule, char **err)
12256{
12257 char *error;
12258
Thierry FOURNIER42148732015-09-02 17:17:33 +020012259 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012260 rule->action_ptr = action_http_set_status;
12261
12262 /* Check if an argument is available */
12263 if (!*args[*orig_arg]) {
12264 memprintf(err, "expects exactly 1 argument <status>");
12265 return ACT_RET_PRS_ERR;
12266 }
12267
12268 /* convert status code as integer */
12269 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12270 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12271 memprintf(err, "expects an integer status code between 100 and 999");
12272 return ACT_RET_PRS_ERR;
12273 }
12274
12275 (*orig_arg)++;
12276 return ACT_RET_PRS_OK;
12277}
12278
Willy Tarreaua9083d02015-05-08 15:27:59 +020012279/* This function executes the "capture" action. It executes a fetch expression,
12280 * turns the result into a string and puts it in a capture slot. It always
12281 * returns 1. If an error occurs the action is cancelled, but the rule
12282 * processing continues.
12283 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012284enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012285 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012286{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012287 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012288 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012289 char **cap = s->req_cap;
12290 int len;
12291
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012292 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 +020012293 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012294 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012295
12296 if (cap[h->index] == NULL)
12297 cap[h->index] = pool_alloc2(h->pool);
12298
12299 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012300 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012301
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012302 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012303 if (len > h->len)
12304 len = h->len;
12305
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012306 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012307 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012308 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012309}
12310
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012311/* This function executes the "capture" action and store the result in a
12312 * capture slot if exists. It executes a fetch expression, turns the result
12313 * into a string and puts it in a capture slot. It always returns 1. If an
12314 * error occurs the action is cancelled, but the rule processing continues.
12315 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012316enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012317 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012318{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012319 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012320 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012321 char **cap = s->req_cap;
12322 struct proxy *fe = strm_fe(s);
12323 int len;
12324 int i;
12325
12326 /* Look for the original configuration. */
12327 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012328 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012329 i--, h = h->next);
12330 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012331 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012332
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012333 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 +020012334 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012335 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012336
12337 if (cap[h->index] == NULL)
12338 cap[h->index] = pool_alloc2(h->pool);
12339
12340 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012341 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012342
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012343 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012344 if (len > h->len)
12345 len = h->len;
12346
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012347 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012348 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012349 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012350}
12351
Willy Tarreaua9083d02015-05-08 15:27:59 +020012352/* parse an "http-request capture" action. It takes a single argument which is
12353 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012354 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012355 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012356 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012357enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12358 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012359{
12360 struct sample_expr *expr;
12361 struct cap_hdr *hdr;
12362 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012363 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012364
12365 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12366 if (strcmp(args[cur_arg], "if") == 0 ||
12367 strcmp(args[cur_arg], "unless") == 0)
12368 break;
12369
12370 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012371 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012372 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012373 }
12374
Willy Tarreaua9083d02015-05-08 15:27:59 +020012375 cur_arg = *orig_arg;
12376 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12377 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012378 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012379
12380 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12381 memprintf(err,
12382 "fetch method '%s' extracts information from '%s', none of which is available here",
12383 args[cur_arg-1], sample_src_names(expr->fetch->use));
12384 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012385 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012386 }
12387
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012388 if (!args[cur_arg] || !*args[cur_arg]) {
12389 memprintf(err, "expects 'len or 'id'");
12390 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012391 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012392 }
12393
Willy Tarreaua9083d02015-05-08 15:27:59 +020012394 if (strcmp(args[cur_arg], "len") == 0) {
12395 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012396
12397 if (!(px->cap & PR_CAP_FE)) {
12398 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012399 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012400 }
12401
12402 proxy->conf.args.ctx = ARGC_CAP;
12403
Willy Tarreaua9083d02015-05-08 15:27:59 +020012404 if (!args[cur_arg]) {
12405 memprintf(err, "missing length value");
12406 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012407 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012408 }
12409 /* we copy the table name for now, it will be resolved later */
12410 len = atoi(args[cur_arg]);
12411 if (len <= 0) {
12412 memprintf(err, "length must be > 0");
12413 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012414 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012415 }
12416 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012417
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012418 if (!len) {
12419 memprintf(err, "a positive 'len' argument is mandatory");
12420 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012421 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012422 }
12423
Vincent Bernat02779b62016-04-03 13:48:43 +020012424 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012425 hdr->next = px->req_cap;
12426 hdr->name = NULL; /* not a header capture */
12427 hdr->namelen = 0;
12428 hdr->len = len;
12429 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12430 hdr->index = px->nb_req_cap++;
12431
12432 px->req_cap = hdr;
12433 px->to_log |= LW_REQHDR;
12434
Thierry FOURNIER42148732015-09-02 17:17:33 +020012435 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012436 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012437 rule->arg.cap.expr = expr;
12438 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012439 }
12440
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012441 else if (strcmp(args[cur_arg], "id") == 0) {
12442 int id;
12443 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012444
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012445 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012446
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012447 if (!args[cur_arg]) {
12448 memprintf(err, "missing id value");
12449 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012450 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012451 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012452
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012453 id = strtol(args[cur_arg], &error, 10);
12454 if (*error != '\0') {
12455 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12456 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012457 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012458 }
12459 cur_arg++;
12460
12461 proxy->conf.args.ctx = ARGC_CAP;
12462
Thierry FOURNIER42148732015-09-02 17:17:33 +020012463 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012464 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012465 rule->arg.capid.expr = expr;
12466 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012467 }
12468
12469 else {
12470 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12471 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012472 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012473 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012474
12475 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012476 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012477}
12478
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012479/* This function executes the "capture" action and store the result in a
12480 * capture slot if exists. It executes a fetch expression, turns the result
12481 * into a string and puts it in a capture slot. It always returns 1. If an
12482 * error occurs the action is cancelled, but the rule processing continues.
12483 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012484enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012485 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012486{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012487 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012488 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012489 char **cap = s->res_cap;
12490 struct proxy *fe = strm_fe(s);
12491 int len;
12492 int i;
12493
12494 /* Look for the original configuration. */
12495 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012496 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012497 i--, h = h->next);
12498 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012499 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012500
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012501 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 +020012502 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012503 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012504
12505 if (cap[h->index] == NULL)
12506 cap[h->index] = pool_alloc2(h->pool);
12507
12508 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012509 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012510
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012511 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012512 if (len > h->len)
12513 len = h->len;
12514
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012515 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012516 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012517 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012518}
12519
12520/* parse an "http-response capture" action. It takes a single argument which is
12521 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12522 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012523 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012524 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012525enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12526 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012527{
12528 struct sample_expr *expr;
12529 int cur_arg;
12530 int id;
12531 char *error;
12532
12533 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12534 if (strcmp(args[cur_arg], "if") == 0 ||
12535 strcmp(args[cur_arg], "unless") == 0)
12536 break;
12537
12538 if (cur_arg < *orig_arg + 3) {
12539 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012540 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012541 }
12542
12543 cur_arg = *orig_arg;
12544 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12545 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012546 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012547
12548 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12549 memprintf(err,
12550 "fetch method '%s' extracts information from '%s', none of which is available here",
12551 args[cur_arg-1], sample_src_names(expr->fetch->use));
12552 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012553 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012554 }
12555
12556 if (!args[cur_arg] || !*args[cur_arg]) {
12557 memprintf(err, "expects 'len or 'id'");
12558 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012559 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012560 }
12561
12562 if (strcmp(args[cur_arg], "id") != 0) {
12563 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12564 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012565 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012566 }
12567
12568 cur_arg++;
12569
12570 if (!args[cur_arg]) {
12571 memprintf(err, "missing id value");
12572 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012573 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012574 }
12575
12576 id = strtol(args[cur_arg], &error, 10);
12577 if (*error != '\0') {
12578 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12579 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012580 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012581 }
12582 cur_arg++;
12583
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012584 proxy->conf.args.ctx = ARGC_CAP;
12585
Thierry FOURNIER42148732015-09-02 17:17:33 +020012586 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012587 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012588 rule->arg.capid.expr = expr;
12589 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012590
12591 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012592 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012593}
12594
William Lallemand73025dd2014-04-24 14:38:37 +020012595/*
12596 * Return the struct http_req_action_kw associated to a keyword.
12597 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012598struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012599{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012600 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012601}
12602
12603/*
12604 * Return the struct http_res_action_kw associated to a keyword.
12605 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012606struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012607{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012608 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012609}
12610
Willy Tarreau4a568972010-05-12 08:08:50 +020012611/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012612/* All supported ACL keywords must be declared here. */
12613/************************************************************************/
12614
12615/* Note: must not be declared <const> as its list will be overwritten.
12616 * Please take care of keeping this list alphabetically sorted.
12617 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012618static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012619 { "base", "base", PAT_MATCH_STR },
12620 { "base_beg", "base", PAT_MATCH_BEG },
12621 { "base_dir", "base", PAT_MATCH_DIR },
12622 { "base_dom", "base", PAT_MATCH_DOM },
12623 { "base_end", "base", PAT_MATCH_END },
12624 { "base_len", "base", PAT_MATCH_LEN },
12625 { "base_reg", "base", PAT_MATCH_REG },
12626 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012627
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012628 { "cook", "req.cook", PAT_MATCH_STR },
12629 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12630 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12631 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12632 { "cook_end", "req.cook", PAT_MATCH_END },
12633 { "cook_len", "req.cook", PAT_MATCH_LEN },
12634 { "cook_reg", "req.cook", PAT_MATCH_REG },
12635 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012636
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012637 { "hdr", "req.hdr", PAT_MATCH_STR },
12638 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12639 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12640 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12641 { "hdr_end", "req.hdr", PAT_MATCH_END },
12642 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12643 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12644 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012645
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012646 /* these two declarations uses strings with list storage (in place
12647 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12648 * and delete functions are relative to the list management. The parse
12649 * and match method are related to the corresponding fetch methods. This
12650 * is very particular ACL declaration mode.
12651 */
12652 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12653 { "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 +020012654
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012655 { "path", "path", PAT_MATCH_STR },
12656 { "path_beg", "path", PAT_MATCH_BEG },
12657 { "path_dir", "path", PAT_MATCH_DIR },
12658 { "path_dom", "path", PAT_MATCH_DOM },
12659 { "path_end", "path", PAT_MATCH_END },
12660 { "path_len", "path", PAT_MATCH_LEN },
12661 { "path_reg", "path", PAT_MATCH_REG },
12662 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012663
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012664 { "req_ver", "req.ver", PAT_MATCH_STR },
12665 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012666
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012667 { "scook", "res.cook", PAT_MATCH_STR },
12668 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12669 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12670 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12671 { "scook_end", "res.cook", PAT_MATCH_END },
12672 { "scook_len", "res.cook", PAT_MATCH_LEN },
12673 { "scook_reg", "res.cook", PAT_MATCH_REG },
12674 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012675
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012676 { "shdr", "res.hdr", PAT_MATCH_STR },
12677 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12678 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12679 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12680 { "shdr_end", "res.hdr", PAT_MATCH_END },
12681 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12682 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12683 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012684
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012685 { "url", "url", PAT_MATCH_STR },
12686 { "url_beg", "url", PAT_MATCH_BEG },
12687 { "url_dir", "url", PAT_MATCH_DIR },
12688 { "url_dom", "url", PAT_MATCH_DOM },
12689 { "url_end", "url", PAT_MATCH_END },
12690 { "url_len", "url", PAT_MATCH_LEN },
12691 { "url_reg", "url", PAT_MATCH_REG },
12692 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012693
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012694 { "urlp", "urlp", PAT_MATCH_STR },
12695 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12696 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12697 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12698 { "urlp_end", "urlp", PAT_MATCH_END },
12699 { "urlp_len", "urlp", PAT_MATCH_LEN },
12700 { "urlp_reg", "urlp", PAT_MATCH_REG },
12701 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012702
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012703 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012704}};
12705
12706/************************************************************************/
12707/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012708/************************************************************************/
12709/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012710static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012711 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012712 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012713 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12714
Willy Tarreau87b09662015-04-03 00:22:06 +020012715 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012716 { "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 +020012717
12718 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012719 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12720 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12721 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012722
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012723 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12724 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012725
Willy Tarreau409bcde2013-01-08 00:31:00 +010012726 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12727 * are only here to match the ACL's name, are request-only and are used
12728 * for ACL compatibility only.
12729 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012730 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12731 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012732 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12733 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012734
12735 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12736 * only here to match the ACL's name, are request-only and are used for
12737 * ACL compatibility only.
12738 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012739 { "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 +020012740 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012741 { "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 +020012742 { "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 +010012743
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012744 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012745 { "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 +010012746 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012747 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012748 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012749 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012750
12751 /* HTTP protocol on the request path */
12752 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012753 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012754
12755 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012756 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12757 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012758
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012759 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012760 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12761 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012762 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012763
Willy Tarreau18ed2562013-01-14 15:56:36 +010012764 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012765 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12766 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012767
Willy Tarreau18ed2562013-01-14 15:56:36 +010012768 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012769 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012770 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12771 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012772
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012773 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012774 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012775 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012776 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012777 { "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 +010012778 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012779 { "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 +010012780
12781 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012782 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012783 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12784 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012785
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012786 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012787 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012788 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012789 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012790 { "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 +010012791 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012792 { "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 +010012793
Willy Tarreau409bcde2013-01-08 00:31:00 +010012794 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012795 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012796 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12797 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012798 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012799
12800 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012801 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012802 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012803 { "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 +020012804 { "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 +010012805
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012806 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournierf4011dd2016-03-29 17:23:51 +020012807 { "uniqueid", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012808 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012809 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012810 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012811 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012812 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020012813 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12814 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012815 { "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 +010012816 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012817}};
12818
Willy Tarreau8797c062007-05-07 00:55:35 +020012819
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012820/************************************************************************/
12821/* All supported converter keywords must be declared here. */
12822/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012823/* Note: must not be declared <const> as its list will be overwritten */
12824static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012825 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020012826 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012827 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
12828 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012829 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012830 { NULL, NULL, 0, 0, 0 },
12831}};
12832
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012833
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012834/************************************************************************/
12835/* All supported http-request action keywords must be declared here. */
12836/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012837struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012838 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020012839 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012840 { "set-method", parse_set_req_line },
12841 { "set-path", parse_set_req_line },
12842 { "set-query", parse_set_req_line },
12843 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012844 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012845 }
12846};
12847
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012848struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012849 .kw = {
12850 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012851 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012852 { NULL, NULL }
12853 }
12854};
12855
Willy Tarreau8797c062007-05-07 00:55:35 +020012856__attribute__((constructor))
12857static void __http_protocol_init(void)
12858{
12859 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012860 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012861 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012862 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012863 http_res_keywords_register(&http_res_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020012864}
12865
12866
Willy Tarreau58f10d72006-12-04 02:26:12 +010012867/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012868 * Local variables:
12869 * c-indent-level: 8
12870 * c-basic-offset: 8
12871 * End:
12872 */