blob: d32acb0e1f5e2051356d56d861740b5f91658b41 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010028#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020029#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/compat.h>
31#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010032#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020033#include <common/memory.h>
34#include <common/mini-clist.h>
35#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020036#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020037#include <common/time.h>
38#include <common/uri_auth.h>
39#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
41#include <types/capture.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020042#include <types/filters.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreau8797c062007-05-07 00:55:35 +020045#include <proto/acl.h>
Thierry FOURNIER322a1242015-08-19 09:07:47 +020046#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020047#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010048#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020049#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020050#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010051#include <proto/checks.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020052#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020053#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020054#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020055#include <proto/filters.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020056#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020057#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010058#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010059#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020060#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010062#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020063#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020064#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010065#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020066#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010067#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020068#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020069#include <proto/pattern.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020070#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020071
Willy Tarreau522d6c02009-12-06 18:49:18 +010072const char HTTP_100[] =
73 "HTTP/1.1 100 Continue\r\n\r\n";
74
75const struct chunk http_100_chunk = {
76 .str = (char *)&HTTP_100,
77 .len = sizeof(HTTP_100)-1
78};
79
Willy Tarreaua9679ac2010-01-03 17:32:57 +010080/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020081const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010082 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010083 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020084 "Location: "; /* not terminated since it will be concatenated with the URL */
85
Willy Tarreau0f772532006-12-23 20:51:41 +010086const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010087 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010088 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010089 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010090 "Location: "; /* not terminated since it will be concatenated with the URL */
91
92/* same as 302 except that the browser MUST retry with the GET method */
93const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010094 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010095 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010096 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010097 "Location: "; /* not terminated since it will be concatenated with the URL */
98
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040099
100/* same as 302 except that the browser MUST retry with the same method */
101const char *HTTP_307 =
102 "HTTP/1.1 307 Temporary Redirect\r\n"
103 "Cache-Control: no-cache\r\n"
104 "Content-length: 0\r\n"
105 "Location: "; /* not terminated since it will be concatenated with the URL */
106
107/* same as 301 except that the browser MUST retry with the same method */
108const char *HTTP_308 =
109 "HTTP/1.1 308 Permanent Redirect\r\n"
110 "Content-length: 0\r\n"
111 "Location: "; /* not terminated since it will be concatenated with the URL */
112
Willy Tarreaubaaee002006-06-26 02:48:02 +0200113/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
114const char *HTTP_401_fmt =
115 "HTTP/1.0 401 Unauthorized\r\n"
116 "Cache-Control: no-cache\r\n"
117 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200118 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200119 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
120 "\r\n"
121 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
122
Willy Tarreau844a7e72010-01-31 21:46:18 +0100123const char *HTTP_407_fmt =
124 "HTTP/1.0 407 Unauthorized\r\n"
125 "Cache-Control: no-cache\r\n"
126 "Connection: close\r\n"
127 "Content-Type: text/html\r\n"
128 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
129 "\r\n"
Godbach1f1fae62014-12-17 16:32:05 +0800130 "<html><body><h1>407 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
Willy Tarreau844a7e72010-01-31 21:46:18 +0100131
Willy Tarreau0f772532006-12-23 20:51:41 +0100132
133const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200134 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100135 [HTTP_ERR_400] = 400,
136 [HTTP_ERR_403] = 403,
CJ Ess108b1dd2015-04-07 12:03:37 -0400137 [HTTP_ERR_405] = 405,
Willy Tarreau0f772532006-12-23 20:51:41 +0100138 [HTTP_ERR_408] = 408,
CJ Ess108b1dd2015-04-07 12:03:37 -0400139 [HTTP_ERR_429] = 429,
Willy Tarreau0f772532006-12-23 20:51:41 +0100140 [HTTP_ERR_500] = 500,
141 [HTTP_ERR_502] = 502,
142 [HTTP_ERR_503] = 503,
143 [HTTP_ERR_504] = 504,
144};
145
Willy Tarreau80587432006-12-24 17:47:20 +0100146static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200147 [HTTP_ERR_200] =
148 "HTTP/1.0 200 OK\r\n"
149 "Cache-Control: no-cache\r\n"
150 "Connection: close\r\n"
151 "Content-Type: text/html\r\n"
152 "\r\n"
153 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
154
Willy Tarreau0f772532006-12-23 20:51:41 +0100155 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100156 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100157 "Cache-Control: no-cache\r\n"
158 "Connection: close\r\n"
159 "Content-Type: text/html\r\n"
160 "\r\n"
161 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
162
163 [HTTP_ERR_403] =
164 "HTTP/1.0 403 Forbidden\r\n"
165 "Cache-Control: no-cache\r\n"
166 "Connection: close\r\n"
167 "Content-Type: text/html\r\n"
168 "\r\n"
169 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
170
CJ Ess108b1dd2015-04-07 12:03:37 -0400171 [HTTP_ERR_405] =
172 "HTTP/1.0 405 Method Not Allowed\r\n"
173 "Cache-Control: no-cache\r\n"
174 "Connection: close\r\n"
175 "Content-Type: text/html\r\n"
176 "\r\n"
177 "<html><body><h1>405 Method Not Allowed</h1>\nA request was made of a resource using a request method not supported by that resource\n</body></html>\n",
178
Willy Tarreau0f772532006-12-23 20:51:41 +0100179 [HTTP_ERR_408] =
180 "HTTP/1.0 408 Request Time-out\r\n"
181 "Cache-Control: no-cache\r\n"
182 "Connection: close\r\n"
183 "Content-Type: text/html\r\n"
184 "\r\n"
185 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
186
CJ Ess108b1dd2015-04-07 12:03:37 -0400187 [HTTP_ERR_429] =
188 "HTTP/1.0 429 Too Many Requests\r\n"
189 "Cache-Control: no-cache\r\n"
190 "Connection: close\r\n"
191 "Content-Type: text/html\r\n"
192 "\r\n"
193 "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n",
194
Willy Tarreau0f772532006-12-23 20:51:41 +0100195 [HTTP_ERR_500] =
196 "HTTP/1.0 500 Server Error\r\n"
197 "Cache-Control: no-cache\r\n"
198 "Connection: close\r\n"
199 "Content-Type: text/html\r\n"
200 "\r\n"
201 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
202
203 [HTTP_ERR_502] =
204 "HTTP/1.0 502 Bad Gateway\r\n"
205 "Cache-Control: no-cache\r\n"
206 "Connection: close\r\n"
207 "Content-Type: text/html\r\n"
208 "\r\n"
209 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
210
211 [HTTP_ERR_503] =
212 "HTTP/1.0 503 Service Unavailable\r\n"
213 "Cache-Control: no-cache\r\n"
214 "Connection: close\r\n"
215 "Content-Type: text/html\r\n"
216 "\r\n"
217 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
218
219 [HTTP_ERR_504] =
220 "HTTP/1.0 504 Gateway Time-out\r\n"
221 "Cache-Control: no-cache\r\n"
222 "Connection: close\r\n"
223 "Content-Type: text/html\r\n"
224 "\r\n"
225 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
226
227};
228
Cyril Bonté19979e12012-04-04 12:57:21 +0200229/* status codes available for the stats admin page (strictly 4 chars length) */
230const char *stat_status_codes[STAT_STATUS_SIZE] = {
231 [STAT_STATUS_DENY] = "DENY",
232 [STAT_STATUS_DONE] = "DONE",
233 [STAT_STATUS_ERRP] = "ERRP",
234 [STAT_STATUS_EXCD] = "EXCD",
235 [STAT_STATUS_NONE] = "NONE",
236 [STAT_STATUS_PART] = "PART",
237 [STAT_STATUS_UNKN] = "UNKN",
238};
239
240
William Lallemand73025dd2014-04-24 14:38:37 +0200241/* List head of all known action keywords for "http-request" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200242struct action_kw_list http_req_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200243 .list = LIST_HEAD_INIT(http_req_keywords.list)
244};
245
246/* List head of all known action keywords for "http-response" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200247struct action_kw_list http_res_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200248 .list = LIST_HEAD_INIT(http_res_keywords.list)
249};
250
Willy Tarreau80587432006-12-24 17:47:20 +0100251/* We must put the messages here since GCC cannot initialize consts depending
252 * on strlen().
253 */
254struct chunk http_err_chunks[HTTP_ERR_SIZE];
255
Willy Tarreaua890d072013-04-02 12:01:06 +0200256/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
257static struct hdr_ctx static_hdr_ctx;
258
Willy Tarreau42250582007-04-01 01:30:43 +0200259#define FD_SETS_ARE_BITFIELDS
260#ifdef FD_SETS_ARE_BITFIELDS
261/*
262 * This map is used with all the FD_* macros to check whether a particular bit
263 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
264 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
265 * byte should be encoded. Be careful to always pass bytes from 0 to 255
266 * exclusively to the macros.
267 */
268fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
269fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100270fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200271
272#else
273#error "Check if your OS uses bitfields for fd_sets"
274#endif
275
Willy Tarreau87b09662015-04-03 00:22:06 +0200276static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn);
Willy Tarreau0b748332014-04-29 00:13:29 +0200277
David Carlier7365f7d2016-04-04 11:54:42 +0100278static inline int http_msg_forward_body(struct stream *s, struct http_msg *msg);
279static inline int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +0100280
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200281/* This function returns a reason associated with the HTTP status.
282 * This function never fails, a message is always returned.
283 */
284const char *get_reason(unsigned int status)
285{
286 switch (status) {
287 case 100: return "Continue";
288 case 101: return "Switching Protocols";
289 case 102: return "Processing";
290 case 200: return "OK";
291 case 201: return "Created";
292 case 202: return "Accepted";
293 case 203: return "Non-Authoritative Information";
294 case 204: return "No Content";
295 case 205: return "Reset Content";
296 case 206: return "Partial Content";
297 case 207: return "Multi-Status";
298 case 210: return "Content Different";
299 case 226: return "IM Used";
300 case 300: return "Multiple Choices";
301 case 301: return "Moved Permanently";
302 case 302: return "Moved Temporarily";
303 case 303: return "See Other";
304 case 304: return "Not Modified";
305 case 305: return "Use Proxy";
306 case 307: return "Temporary Redirect";
307 case 308: return "Permanent Redirect";
308 case 310: return "Too many Redirects";
309 case 400: return "Bad Request";
310 case 401: return "Unauthorized";
311 case 402: return "Payment Required";
312 case 403: return "Forbidden";
313 case 404: return "Not Found";
314 case 405: return "Method Not Allowed";
315 case 406: return "Not Acceptable";
316 case 407: return "Proxy Authentication Required";
317 case 408: return "Request Time-out";
318 case 409: return "Conflict";
319 case 410: return "Gone";
320 case 411: return "Length Required";
321 case 412: return "Precondition Failed";
322 case 413: return "Request Entity Too Large";
323 case 414: return "Request-URI Too Long";
324 case 415: return "Unsupported Media Type";
325 case 416: return "Requested range unsatisfiable";
326 case 417: return "Expectation failed";
327 case 418: return "I'm a teapot";
328 case 422: return "Unprocessable entity";
329 case 423: return "Locked";
330 case 424: return "Method failure";
331 case 425: return "Unordered Collection";
332 case 426: return "Upgrade Required";
333 case 428: return "Precondition Required";
334 case 429: return "Too Many Requests";
335 case 431: return "Request Header Fields Too Large";
336 case 449: return "Retry With";
337 case 450: return "Blocked by Windows Parental Controls";
338 case 451: return "Unavailable For Legal Reasons";
339 case 456: return "Unrecoverable Error";
340 case 499: return "client has closed connection";
341 case 500: return "Internal Server Error";
342 case 501: return "Not Implemented";
343 case 502: return "Bad Gateway ou Proxy Error";
344 case 503: return "Service Unavailable";
345 case 504: return "Gateway Time-out";
346 case 505: return "HTTP Version not supported";
347 case 506: return "Variant also negociate";
348 case 507: return "Insufficient storage";
349 case 508: return "Loop detected";
350 case 509: return "Bandwidth Limit Exceeded";
351 case 510: return "Not extended";
352 case 511: return "Network authentication required";
353 case 520: return "Web server is returning an unknown error";
354 default:
355 switch (status) {
356 case 100 ... 199: return "Informational";
357 case 200 ... 299: return "Success";
358 case 300 ... 399: return "Redirection";
359 case 400 ... 499: return "Client Error";
360 case 500 ... 599: return "Server Error";
361 default: return "Other";
362 }
363 }
364}
365
Willy Tarreau80587432006-12-24 17:47:20 +0100366void init_proto_http()
367{
Willy Tarreau42250582007-04-01 01:30:43 +0200368 int i;
369 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100370 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200371
Willy Tarreau80587432006-12-24 17:47:20 +0100372 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
373 if (!http_err_msgs[msg]) {
374 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
375 abort();
376 }
377
378 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
379 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
380 }
Willy Tarreau42250582007-04-01 01:30:43 +0200381
382 /* initialize the log header encoding map : '{|}"#' should be encoded with
383 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
384 * URL encoding only requires '"', '#' to be encoded as well as non-
385 * printable characters above.
386 */
387 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
388 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100389 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200390 for (i = 0; i < 32; i++) {
391 FD_SET(i, hdr_encode_map);
392 FD_SET(i, url_encode_map);
393 }
394 for (i = 127; i < 256; i++) {
395 FD_SET(i, hdr_encode_map);
396 FD_SET(i, url_encode_map);
397 }
398
399 tmp = "\"#{|}";
400 while (*tmp) {
401 FD_SET(*tmp, hdr_encode_map);
402 tmp++;
403 }
404
405 tmp = "\"#";
406 while (*tmp) {
407 FD_SET(*tmp, url_encode_map);
408 tmp++;
409 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200410
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100411 /* initialize the http header encoding map. The draft httpbis define the
412 * header content as:
413 *
414 * HTTP-message = start-line
415 * *( header-field CRLF )
416 * CRLF
417 * [ message-body ]
418 * header-field = field-name ":" OWS field-value OWS
419 * field-value = *( field-content / obs-fold )
420 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
421 * obs-fold = CRLF 1*( SP / HTAB )
422 * field-vchar = VCHAR / obs-text
423 * VCHAR = %x21-7E
424 * obs-text = %x80-FF
425 *
426 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
427 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
428 * "obs-fold" is volontary forgotten because haproxy remove this.
429 */
430 memset(http_encode_map, 0, sizeof(http_encode_map));
431 for (i = 0x00; i <= 0x08; i++)
432 FD_SET(i, http_encode_map);
433 for (i = 0x0a; i <= 0x1f; i++)
434 FD_SET(i, http_encode_map);
435 FD_SET(0x7f, http_encode_map);
436
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200437 /* memory allocations */
Willy Tarreau63986c72015-04-03 22:55:33 +0200438 pool2_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200439 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100440 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100441}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200442
Willy Tarreau53b6c742006-12-17 13:37:46 +0100443/*
444 * We have 26 list of methods (1 per first letter), each of which can have
445 * up to 3 entries (2 valid, 1 null).
446 */
447struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100448 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100449 int len;
450 const char text[8];
451};
452
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100453const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100454 ['C' - 'A'] = {
455 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
456 },
457 ['D' - 'A'] = {
458 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
459 },
460 ['G' - 'A'] = {
461 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
462 },
463 ['H' - 'A'] = {
464 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
465 },
Christopher Fauletd57ad642015-07-31 14:26:57 +0200466 ['O' - 'A'] = {
467 [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" },
468 },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100469 ['P' - 'A'] = {
470 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
471 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
472 },
473 ['T' - 'A'] = {
474 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
475 },
476 /* rest is empty like this :
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200477 * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100478 */
479};
480
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100481const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100482 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
483 [HTTP_METH_GET] = { "GET", 3 },
484 [HTTP_METH_HEAD] = { "HEAD", 4 },
485 [HTTP_METH_POST] = { "POST", 4 },
486 [HTTP_METH_PUT] = { "PUT", 3 },
487 [HTTP_METH_DELETE] = { "DELETE", 6 },
488 [HTTP_METH_TRACE] = { "TRACE", 5 },
489 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
490};
491
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100492/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200493 * table than to perform a boolean AND or OR between two tests. Refer to
Willy 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
David Carlier7365f7d2016-04-04 11:54:42 +01001592 if (chunk_initlen(&auth_method, h, 0, len) != 1)
1593 return 0;
1594
Willy Tarreau5c557d12016-03-13 08:17:02 +01001595 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001596
1597 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1598
1599 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001600 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001601
1602 if (len < 0)
1603 return 0;
1604
1605
1606 get_http_auth_buff[len] = '\0';
1607
1608 p = strchr(get_http_auth_buff, ':');
1609
1610 if (!p)
1611 return 0;
1612
1613 txn->auth.user = get_http_auth_buff;
1614 *p = '\0';
1615 txn->auth.pass = p+1;
1616
1617 txn->auth.method = HTTP_AUTH_BASIC;
1618 return 1;
1619 }
1620
1621 return 0;
1622}
1623
Willy Tarreau58f10d72006-12-04 02:26:12 +01001624
Willy Tarreau8973c702007-01-21 23:58:29 +01001625/*
1626 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001627 * depending on the initial msg->msg_state. The caller is responsible for
1628 * ensuring that the message does not wrap. The function can be preempted
1629 * everywhere when data are missing and recalled at the exact same location
1630 * with no information loss. The message may even be realigned between two
1631 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001632 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001633 * fields. Note that msg->sol will be initialized after completing the first
1634 * state, so that none of the msg pointers has to be initialized prior to the
1635 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001636 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001637void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001638{
Willy Tarreau3770f232013-12-07 00:01:53 +01001639 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001640 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001641 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001642
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001643 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001644 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001645 ptr = buf->p + msg->next;
1646 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001647
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001648 if (unlikely(ptr >= end))
1649 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001650
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001651 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001652 /*
1653 * First, states that are specific to the response only.
1654 * We check them first so that request and headers are
1655 * closer to each other (accessed more often).
1656 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001657 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001658 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001659 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001660 /* we have a start of message, but we have to check
1661 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001662 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001663 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001664 if (unlikely(ptr != buf->p)) {
1665 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001666 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001667 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001668 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001669 }
Willy Tarreau26927362012-05-18 23:22:52 +02001670 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001671 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001672 hdr_idx_init(idx);
1673 state = HTTP_MSG_RPVER;
1674 goto http_msg_rpver;
1675 }
1676
1677 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1678 goto http_msg_invalid;
1679
1680 if (unlikely(*ptr == '\n'))
1681 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1682 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1683 /* stop here */
1684
Willy Tarreau8973c702007-01-21 23:58:29 +01001685 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001686 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001687 EXPECT_LF_HERE(ptr, http_msg_invalid);
1688 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1689 /* stop here */
1690
Willy Tarreau8973c702007-01-21 23:58:29 +01001691 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001692 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001693 case HTTP_MSG_RPVER_SP:
1694 case HTTP_MSG_RPCODE:
1695 case HTTP_MSG_RPCODE_SP:
1696 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001697 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001698 state, ptr, end,
1699 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001700 if (unlikely(!ptr))
1701 return;
1702
1703 /* we have a full response and we know that we have either a CR
1704 * or an LF at <ptr>.
1705 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001706 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1707
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001708 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001709 if (likely(*ptr == '\r'))
1710 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1711 goto http_msg_rpline_end;
1712
Willy Tarreau8973c702007-01-21 23:58:29 +01001713 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001714 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001715 /* msg->sol must point to the first of CR or LF. */
1716 EXPECT_LF_HERE(ptr, http_msg_invalid);
1717 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1718 /* stop here */
1719
1720 /*
1721 * Second, states that are specific to the request only
1722 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001723 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001724 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001725 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001726 /* we have a start of message, but we have to check
1727 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001728 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001729 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001730 if (likely(ptr != buf->p)) {
1731 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001732 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001733 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001734 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001735 }
Willy Tarreau26927362012-05-18 23:22:52 +02001736 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001737 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001738 state = HTTP_MSG_RQMETH;
1739 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001740 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001741
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001742 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1743 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001744
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001745 if (unlikely(*ptr == '\n'))
1746 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1747 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001748 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001749
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001750 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001751 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001752 EXPECT_LF_HERE(ptr, http_msg_invalid);
1753 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001754 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001755
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001756 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001757 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001758 case HTTP_MSG_RQMETH_SP:
1759 case HTTP_MSG_RQURI:
1760 case HTTP_MSG_RQURI_SP:
1761 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001762 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001763 state, ptr, end,
1764 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001765 if (unlikely(!ptr))
1766 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001767
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001768 /* we have a full request and we know that we have either a CR
1769 * or an LF at <ptr>.
1770 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001771 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001772
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001773 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001774 if (likely(*ptr == '\r'))
1775 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001776 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001777
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001778 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001779 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001780 /* check for HTTP/0.9 request : no version information available.
1781 * msg->sol must point to the first of CR or LF.
1782 */
1783 if (unlikely(msg->sl.rq.v_l == 0))
1784 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001785
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001786 EXPECT_LF_HERE(ptr, http_msg_invalid);
1787 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001788 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001789
Willy Tarreau8973c702007-01-21 23:58:29 +01001790 /*
1791 * Common states below
1792 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001793 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001794 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001795 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001796 if (likely(!HTTP_IS_CRLF(*ptr))) {
1797 goto http_msg_hdr_name;
1798 }
1799
1800 if (likely(*ptr == '\r'))
1801 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1802 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001803
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001804 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001805 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001806 /* assumes msg->sol points to the first char */
1807 if (likely(HTTP_IS_TOKEN(*ptr)))
1808 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001809
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001810 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001811 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001812
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001813 if (likely(msg->err_pos < -1) || *ptr == '\n')
1814 goto http_msg_invalid;
1815
1816 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001817 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001818
1819 /* and we still accept this non-token character */
1820 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001821
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001822 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001823 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001824 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001825 if (likely(HTTP_IS_SPHT(*ptr)))
1826 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001827
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001828 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001829 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001830
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001831 if (likely(!HTTP_IS_CRLF(*ptr))) {
1832 goto http_msg_hdr_val;
1833 }
1834
1835 if (likely(*ptr == '\r'))
1836 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1837 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001838
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001839 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001840 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001841 EXPECT_LF_HERE(ptr, http_msg_invalid);
1842 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001843
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001844 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001845 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001846 if (likely(HTTP_IS_SPHT(*ptr))) {
1847 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001848 for (; buf->p + msg->sov < ptr; msg->sov++)
1849 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001850 goto http_msg_hdr_l1_sp;
1851 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001852 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001853 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001854 goto http_msg_complete_header;
1855
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001856 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001857 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001858 /* assumes msg->sol points to the first char, and msg->sov
1859 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001860 */
1861 if (likely(!HTTP_IS_CRLF(*ptr)))
1862 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001863
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001864 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001865 /* Note: we could also copy eol into ->eoh so that we have the
1866 * real header end in case it ends with lots of LWS, but is this
1867 * really needed ?
1868 */
1869 if (likely(*ptr == '\r'))
1870 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1871 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001872
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001873 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001874 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001875 EXPECT_LF_HERE(ptr, http_msg_invalid);
1876 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001877
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001878 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001879 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001880 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1881 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001882 for (; buf->p + msg->eol < ptr; msg->eol++)
1883 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001884 goto http_msg_hdr_val;
1885 }
1886 http_msg_complete_header:
1887 /*
1888 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001889 * Assumes msg->sol points to the first char, msg->sov points
1890 * to the first character of the value and msg->eol to the
1891 * first CR or LF so we know how the line ends. We insert last
1892 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001893 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001894 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001895 idx, idx->tail) < 0))
1896 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001897
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001898 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001899 if (likely(!HTTP_IS_CRLF(*ptr))) {
1900 goto http_msg_hdr_name;
1901 }
1902
1903 if (likely(*ptr == '\r'))
1904 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1905 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001906
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001907 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001908 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001909 /* Assumes msg->sol points to the first of either CR or LF.
1910 * Sets ->sov and ->next to the total header length, ->eoh to
1911 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1912 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001913 EXPECT_LF_HERE(ptr, http_msg_invalid);
1914 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001915 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001916 msg->eoh = msg->sol;
1917 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001918 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001919 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001920 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001921
1922 case HTTP_MSG_ERROR:
1923 /* this may only happen if we call http_msg_analyser() twice with an error */
1924 break;
1925
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001926 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001927#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001928 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1929 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001930#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001931 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001932 }
1933 http_msg_ood:
1934 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001935 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001936 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001937 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001938
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001939 http_msg_invalid:
1940 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001941 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001942 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001943 return;
1944}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001945
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001946/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1947 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1948 * nothing is done and 1 is returned.
1949 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001950static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001951{
1952 int delta;
1953 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001954 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001955
1956 if (msg->sl.rq.v_l != 0)
1957 return 1;
1958
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001959 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1960 if (txn->meth != HTTP_METH_GET)
1961 return 0;
1962
Willy Tarreau9b28e032012-10-12 23:49:43 +02001963 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001964 delta = 0;
1965
1966 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001967 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1968 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001969 }
1970 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001971 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001972 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001973 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001974 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001975 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001976 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001977 NULL, NULL);
1978 if (unlikely(!cur_end))
1979 return 0;
1980
1981 /* we have a full HTTP/1.0 request now and we know that
1982 * we have either a CR or an LF at <ptr>.
1983 */
1984 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1985 return 1;
1986}
1987
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001988/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001989 * and "keep-alive" values. If we already know that some headers may safely
1990 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001991 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1992 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001993 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001994 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1995 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1996 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001997 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001998 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001999void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01002000{
Willy Tarreau5b154472009-12-21 20:11:07 +01002001 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002002 const char *hdr_val = "Connection";
2003 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01002004
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002005 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01002006 return;
2007
Willy Tarreau88d349d2010-01-25 12:15:43 +01002008 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2009 hdr_val = "Proxy-Connection";
2010 hdr_len = 16;
2011 }
2012
Willy Tarreau5b154472009-12-21 20:11:07 +01002013 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002014 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002015 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002016 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2017 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002018 if (to_del & 2)
2019 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002020 else
2021 txn->flags |= TX_CON_KAL_SET;
2022 }
2023 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2024 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002025 if (to_del & 1)
2026 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002027 else
2028 txn->flags |= TX_CON_CLO_SET;
2029 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01002030 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
2031 txn->flags |= TX_HDR_CONN_UPG;
2032 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002033 }
2034
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002035 txn->flags |= TX_HDR_CONN_PRS;
2036 return;
2037}
Willy Tarreau5b154472009-12-21 20:11:07 +01002038
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002039/* Apply desired changes on the Connection: header. Values may be removed and/or
2040 * added depending on the <wanted> flags, which are exclusively composed of
2041 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2042 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2043 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002044void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002045{
2046 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002047 const char *hdr_val = "Connection";
2048 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002049
2050 ctx.idx = 0;
2051
Willy Tarreau88d349d2010-01-25 12:15:43 +01002052
2053 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2054 hdr_val = "Proxy-Connection";
2055 hdr_len = 16;
2056 }
2057
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002058 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002059 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002060 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2061 if (wanted & TX_CON_KAL_SET)
2062 txn->flags |= TX_CON_KAL_SET;
2063 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002064 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002065 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002066 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2067 if (wanted & TX_CON_CLO_SET)
2068 txn->flags |= TX_CON_CLO_SET;
2069 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002070 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002071 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002072 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002073
2074 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2075 return;
2076
2077 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2078 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002079 hdr_val = "Connection: close";
2080 hdr_len = 17;
2081 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2082 hdr_val = "Proxy-Connection: close";
2083 hdr_len = 23;
2084 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002085 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002086 }
2087
2088 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2089 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002090 hdr_val = "Connection: keep-alive";
2091 hdr_len = 22;
2092 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2093 hdr_val = "Proxy-Connection: keep-alive";
2094 hdr_len = 28;
2095 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002096 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002097 }
2098 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002099}
2100
Christopher Faulet113f7de2015-12-14 14:52:13 +01002101/* Parse the chunk size at msg->next. Once done, caller should adjust ->next to
2102 * point to the first byte of data after the chunk size, so that we know we can
2103 * forward exactly msg->next bytes. msg->sol contains the exact number of bytes
2104 * forming the chunk size. That way it is always possible to differentiate
2105 * between the start of the body and the start of the data. Return the number
2106 * of byte parsed on success, 0 when some data is missing, <0 on error. Note:
2107 * this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002108 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002109static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002110{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002111 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002112 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002113 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002114 const char *end = buf->data + buf->size;
2115 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01002116 unsigned int chunk = 0;
2117
2118 /* The chunk size is in the following form, though we are only
2119 * interested in the size and CRLF :
2120 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2121 */
2122 while (1) {
2123 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002124 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002125 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002126 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002127 if (c < 0) /* not a hex digit anymore */
2128 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002129 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002130 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002131 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002132 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002133 chunk = (chunk << 4) + c;
2134 }
2135
Willy Tarreaud98cf932009-12-27 22:54:55 +01002136 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002137 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002138 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002139
2140 while (http_is_spht[(unsigned char)*ptr]) {
2141 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002142 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002143 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002144 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002145 }
2146
Willy Tarreaud98cf932009-12-27 22:54:55 +01002147 /* Up to there, we know that at least one byte is present at *ptr. Check
2148 * for the end of chunk size.
2149 */
2150 while (1) {
2151 if (likely(HTTP_IS_CRLF(*ptr))) {
2152 /* we now have a CR or an LF at ptr */
2153 if (likely(*ptr == '\r')) {
2154 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002155 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002156 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002157 return 0;
2158 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002159
Willy Tarreaud98cf932009-12-27 22:54:55 +01002160 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002161 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002162 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002163 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002164 /* done */
2165 break;
2166 }
2167 else if (*ptr == ';') {
2168 /* chunk extension, ends at next CRLF */
2169 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002170 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002171 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002172 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002173
2174 while (!HTTP_IS_CRLF(*ptr)) {
2175 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002176 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002177 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002178 return 0;
2179 }
2180 /* we have a CRLF now, loop above */
2181 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002182 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002183 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002184 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002185 }
2186
Christopher Faulet113f7de2015-12-14 14:52:13 +01002187 /* OK we found our CRLF and now <ptr> points to the next byte, which may
2188 * or may not be present. We save the number of bytes parsed into
2189 * msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002190 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002191 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002192 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002193 msg->sol += buf->size;
Willy Tarreau124d9912011-03-01 20:30:48 +01002194 msg->chunk_len = chunk;
2195 msg->body_len += chunk;
Christopher Faulet113f7de2015-12-14 14:52:13 +01002196 return msg->sol;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002197 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002198 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002199 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002200}
2201
Christopher Faulet113f7de2015-12-14 14:52:13 +01002202/* This function skips trailers in the buffer associated with HTTP message
2203 * <msg>. The first visited position is msg->next. If the end of the trailers is
2204 * found, the function returns >0. So, the caller can automatically schedul it
2205 * to be forwarded, and switch msg->msg_state to HTTP_MSG_DONE. If not enough
2206 * data are available, the function does not change anything except maybe
2207 * msg->sol if it could parse some lines, and returns zero. If a parse error
2208 * is encountered, the function returns < 0 and does not change anything except
2209 * maybe msg->sol. Note that the message must already be in HTTP_MSG_TRAILERS
2210 * state before calling this function, which implies that all non-trailers data
2211 * have already been scheduled for forwarding, and that msg->next exactly
2212 * matches the length of trailers already parsed and not forwarded. It is also
2213 * important to note that this function is designed to be able to parse wrapped
2214 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002215 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002216static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002217{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002218 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002219
Christopher Faulet113f7de2015-12-14 14:52:13 +01002220 /* we have msg->next which points to next line. Look for CRLF. But
2221 * first, we reset msg->sol */
2222 msg->sol = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002223 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002224 const char *p1 = NULL, *p2 = NULL;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002225 const char *start = b_ptr(buf, msg->next + msg->sol);
2226 const char *stop = bi_end(buf);
2227 const char *ptr = start;
2228 int bytes = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002229
2230 /* scan current line and stop at LF or CRLF */
2231 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002232 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002233 return 0;
2234
2235 if (*ptr == '\n') {
2236 if (!p1)
2237 p1 = ptr;
2238 p2 = ptr;
2239 break;
2240 }
2241
2242 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002243 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002244 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002245 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002246 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002247 p1 = ptr;
2248 }
2249
2250 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002251 if (ptr >= buf->data + buf->size)
2252 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002253 }
2254
2255 /* after LF; point to beginning of next line */
2256 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002257 if (p2 >= buf->data + buf->size)
2258 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002259
Christopher Faulet2fb28802015-12-01 10:40:57 +01002260 bytes = p2 - start;
Willy Tarreau638cd022010-01-03 07:42:04 +01002261 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002262 bytes += buf->size;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002263 msg->sol += bytes;
Willy Tarreau638cd022010-01-03 07:42:04 +01002264
Christopher Fauletd7c91962015-04-30 11:48:27 +02002265 /* LF/CRLF at beginning of line => end of trailers at p2.
2266 * Everything was scheduled for forwarding, there's nothing left
Christopher Faulet2fb28802015-12-01 10:40:57 +01002267 * from this message. */
2268 if (p1 == start)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002269 return 1;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002270
Willy Tarreaud98cf932009-12-27 22:54:55 +01002271 /* OK, next line then */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002272 }
2273}
2274
Christopher Faulet113f7de2015-12-14 14:52:13 +01002275/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
2276 * a possible LF alone at the end of a chunk. The caller should adjust msg->next
2277 * in order to include this part into the next forwarding phase. Note that the
2278 * caller must ensure that ->p points to the first byte to parse. It returns
2279 * the number of bytes parsed on success, so the caller can set msg_state to
2280 * HTTP_MSG_CHUNK_SIZE. If not enough data are available, the function does not
2281 * change anything and returns zero. If a parse error is encountered, the
2282 * function returns < 0. Note: this function is designed to parse wrapped CRLF
2283 * at the end of the buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002284 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002285static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002286{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002287 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002288 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002289 int bytes;
2290
2291 /* NB: we'll check data availabilty at the end. It's not a
2292 * problem because whatever we match first will be checked
2293 * against the correct length.
2294 */
2295 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002296 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002297 if (*ptr == '\r') {
2298 bytes++;
2299 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002300 if (ptr >= buf->data + buf->size)
2301 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002302 }
2303
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002304 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002305 return 0;
2306
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002307 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002308 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002309 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002310 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01002311 return bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002312}
Willy Tarreau5b154472009-12-21 20:11:07 +01002313
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002314/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2315 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2316 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2317 * Unparsable qvalues return 1000 as "q=1.000".
2318 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002319int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002320{
2321 int q = 1000;
2322
Willy Tarreau506c69a2014-07-08 00:59:48 +02002323 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002324 goto out;
2325 q = (*qvalue++ - '0') * 1000;
2326
2327 if (*qvalue++ != '.')
2328 goto out;
2329
Willy Tarreau506c69a2014-07-08 00:59:48 +02002330 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002331 goto out;
2332 q += (*qvalue++ - '0') * 100;
2333
Willy Tarreau506c69a2014-07-08 00:59:48 +02002334 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002335 goto out;
2336 q += (*qvalue++ - '0') * 10;
2337
Willy Tarreau506c69a2014-07-08 00:59:48 +02002338 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002339 goto out;
2340 q += (*qvalue++ - '0') * 1;
2341 out:
2342 if (q > 1000)
2343 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002344 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002345 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002346 return q;
2347}
William Lallemand82fe75c2012-10-23 10:25:10 +02002348
Willy Tarreau87b09662015-04-03 00:22:06 +02002349void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002350{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002351 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002352 int tmp = TX_CON_WANT_KAL;
2353
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002354 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2355 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002356 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2357 tmp = TX_CON_WANT_TUN;
2358
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002359 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002360 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2361 tmp = TX_CON_WANT_TUN;
2362 }
2363
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002364 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002365 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2366 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002367 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002368 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2369 tmp = TX_CON_WANT_CLO;
2370 else
2371 tmp = TX_CON_WANT_SCL;
2372 }
2373
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002374 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002375 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2376 tmp = TX_CON_WANT_CLO;
2377
2378 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2379 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2380
2381 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2382 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2383 /* parse the Connection header and possibly clean it */
2384 int to_del = 0;
2385 if ((msg->flags & HTTP_MSGF_VER_11) ||
2386 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002387 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002388 to_del |= 2; /* remove "keep-alive" */
2389 if (!(msg->flags & HTTP_MSGF_VER_11))
2390 to_del |= 1; /* remove "close" */
2391 http_parse_connection_header(txn, msg, to_del);
2392 }
2393
2394 /* check if client or config asks for explicit close in KAL/SCL */
2395 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2396 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2397 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2398 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2399 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002400 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002401 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2402}
William Lallemand82fe75c2012-10-23 10:25:10 +02002403
Willy Tarreaud787e662009-07-07 10:14:51 +02002404/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2405 * processing can continue on next analysers, or zero if it either needs more
2406 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002407 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002408 * when it has nothing left to do, and may remove any analyser when it wants to
2409 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002410 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002411int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002412{
Willy Tarreau59234e92008-11-30 23:51:27 +01002413 /*
2414 * We will parse the partial (or complete) lines.
2415 * We will check the request syntax, and also join multi-line
2416 * headers. An index of all the lines will be elaborated while
2417 * parsing.
2418 *
2419 * For the parsing, we use a 28 states FSM.
2420 *
2421 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002422 * req->buf->p = beginning of request
2423 * req->buf->p + msg->eoh = end of processed headers / start of current one
2424 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002425 * msg->eol = end of current header or line (LF or CRLF)
2426 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002427 *
2428 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02002429 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002430 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2431 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002432 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002433
Willy Tarreau59234e92008-11-30 23:51:27 +01002434 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002435 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002436 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01002437 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002438 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002439
Willy Tarreau87b09662015-04-03 00:22:06 +02002440 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 +01002441 now_ms, __FUNCTION__,
2442 s,
2443 req,
2444 req->rex, req->wex,
2445 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002446 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002447 req->analysers);
2448
Willy Tarreau52a0c602009-08-16 22:45:38 +02002449 /* we're speaking HTTP here, so let's speak HTTP to the client */
2450 s->srv_error = http_return_srv_error;
2451
Willy Tarreau83e3af02009-12-28 17:39:57 +01002452 /* There's a protected area at the end of the buffer for rewriting
2453 * purposes. We don't want to start to parse the request if the
2454 * protected area is affected, because we may have to move processed
2455 * data later, which is much more complicated.
2456 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002457 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002458 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002459 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002460 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002461 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002462 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002463 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002464 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002465 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002466 return 0;
2467 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002468 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2469 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2470 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002471 }
2472
Willy Tarreau065e8332010-01-08 00:30:20 +01002473 /* Note that we have the same problem with the response ; we
2474 * may want to send a redirect, error or anything which requires
2475 * some spare space. So we'll ensure that we have at least
2476 * maxrewrite bytes available in the response buffer before
2477 * processing that one. This will only affect pipelined
2478 * keep-alive requests.
2479 */
2480 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002481 unlikely(!channel_is_rewritable(&s->res) ||
2482 bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) ||
2483 bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) {
2484 if (s->res.buf->o) {
2485 if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002486 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002487 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002488 channel_dont_connect(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002489 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
2490 s->res.flags |= CF_WAKE_WRITE;
2491 s->res.analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002492 return 0;
2493 }
2494 }
2495
Willy Tarreau9b28e032012-10-12 23:49:43 +02002496 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002497 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002498 }
2499
Willy Tarreau59234e92008-11-30 23:51:27 +01002500 /* 1: we might have to print this header in debug mode */
2501 if (unlikely((global.mode & MODE_DEBUG) &&
2502 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002503 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002504 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002505
Willy Tarreau9b28e032012-10-12 23:49:43 +02002506 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002507 /* this is a bit complex : in case of error on the request line,
2508 * we know that rq.l is still zero, so we display only the part
2509 * up to the end of the line (truncated by debug_hdr).
2510 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002511 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002512 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002513
Willy Tarreau59234e92008-11-30 23:51:27 +01002514 sol += hdr_idx_first_pos(&txn->hdr_idx);
2515 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002516
Willy Tarreau59234e92008-11-30 23:51:27 +01002517 while (cur_idx) {
2518 eol = sol + txn->hdr_idx.v[cur_idx].len;
2519 debug_hdr("clihdr", s, sol, eol);
2520 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2521 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002522 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002523 }
2524
Willy Tarreau58f10d72006-12-04 02:26:12 +01002525
Willy Tarreau59234e92008-11-30 23:51:27 +01002526 /*
2527 * Now we quickly check if we have found a full valid request.
2528 * If not so, we check the FD and buffer states before leaving.
2529 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002530 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002531 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02002532 * on a keep-alive stream, if we encounter and error, close, t/o,
2533 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002534 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02002535 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002536 * Last, we may increase some tracked counters' http request errors on
2537 * the cases that are deliberately the client's fault. For instance,
2538 * a timeout or connection reset is not counted as an error. However
2539 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002540 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002541
Willy Tarreau655dce92009-11-08 13:10:58 +01002542 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002543 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002544 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002545 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002546 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002547 stream_inc_http_req_ctr(s);
2548 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002549 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002550 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002551 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002552
Willy Tarreau59234e92008-11-30 23:51:27 +01002553 /* 1: Since we are in header mode, if there's no space
2554 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02002555 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01002556 * must terminate it now.
2557 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002558 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002559 /* FIXME: check if URI is set and return Status
2560 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002561 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002562 stream_inc_http_req_ctr(s);
2563 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002564 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002565 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002566 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002567 goto return_bad_req;
2568 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002569
Willy Tarreau59234e92008-11-30 23:51:27 +01002570 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002571 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002572 if (!(s->flags & SF_ERR_MASK))
2573 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002574
Willy Tarreaufcffa692010-01-10 14:21:19 +01002575 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002576 goto failed_keep_alive;
2577
Willy Tarreau0f228a02015-05-01 15:37:53 +02002578 if (sess->fe->options & PR_O_IGNORE_PRB)
2579 goto failed_keep_alive;
2580
Willy Tarreau59234e92008-11-30 23:51:27 +01002581 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002582 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002583 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002584 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002585 }
2586
Willy Tarreaudc979f22012-12-04 10:39:01 +01002587 txn->status = 400;
Willy Tarreau59234e92008-11-30 23:51:27 +01002588 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002589 http_reply_and_close(s, txn->status, NULL);
Christopher Fauletd7c91962015-04-30 11:48:27 +02002590 req->analysers &= AN_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02002591 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002592 proxy_inc_fe_req_ctr(sess->fe);
2593 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002594 if (sess->listener->counters)
2595 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002596
Willy Tarreaue7dff022015-04-03 01:14:29 +02002597 if (!(s->flags & SF_FINST_MASK))
2598 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002599 return 0;
2600 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002601
Willy Tarreau59234e92008-11-30 23:51:27 +01002602 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002603 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002604 if (!(s->flags & SF_ERR_MASK))
2605 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002606
Willy Tarreaufcffa692010-01-10 14:21:19 +01002607 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002608 goto failed_keep_alive;
2609
Willy Tarreau0f228a02015-05-01 15:37:53 +02002610 if (sess->fe->options & PR_O_IGNORE_PRB)
2611 goto failed_keep_alive;
2612
Willy Tarreau59234e92008-11-30 23:51:27 +01002613 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002614 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002615 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002616 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002617 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002618 txn->status = 408;
Willy Tarreau59234e92008-11-30 23:51:27 +01002619 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002620 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_408));
Christopher Fauletd7c91962015-04-30 11:48:27 +02002621 req->analysers &= AN_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002622
Willy Tarreau87b09662015-04-03 00:22:06 +02002623 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002624 proxy_inc_fe_req_ctr(sess->fe);
2625 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002626 if (sess->listener->counters)
2627 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002628
Willy Tarreaue7dff022015-04-03 01:14:29 +02002629 if (!(s->flags & SF_FINST_MASK))
2630 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002631 return 0;
2632 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002633
Willy Tarreau59234e92008-11-30 23:51:27 +01002634 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002635 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002636 if (!(s->flags & SF_ERR_MASK))
2637 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002638
Willy Tarreaufcffa692010-01-10 14:21:19 +01002639 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002640 goto failed_keep_alive;
2641
Willy Tarreau0f228a02015-05-01 15:37:53 +02002642 if (sess->fe->options & PR_O_IGNORE_PRB)
2643 goto failed_keep_alive;
2644
Willy Tarreau4076a152009-04-02 15:18:36 +02002645 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002646 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002647 txn->status = 400;
Willy Tarreau59234e92008-11-30 23:51:27 +01002648 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002649 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Christopher Fauletd7c91962015-04-30 11:48:27 +02002650 req->analysers &= AN_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02002651 stream_inc_http_err_ctr(s);
2652 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002653 proxy_inc_fe_req_ctr(sess->fe);
2654 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002655 if (sess->listener->counters)
2656 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002657
Willy Tarreaue7dff022015-04-03 01:14:29 +02002658 if (!(s->flags & SF_FINST_MASK))
2659 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002660 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002661 }
2662
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002663 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002664 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002665 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002666#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002667 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
2668 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002669 /* We need more data, we have to re-enable quick-ack in case we
2670 * previously disabled it, otherwise we might cause the client
2671 * to delay next data.
2672 */
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002673 setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002674 }
2675#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002676
Willy Tarreaufcffa692010-01-10 14:21:19 +01002677 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2678 /* If the client starts to talk, let's fall back to
2679 * request timeout processing.
2680 */
2681 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002682 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002683 }
2684
Willy Tarreau59234e92008-11-30 23:51:27 +01002685 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002686 if (!tick_isset(req->analyse_exp)) {
2687 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2688 (txn->flags & TX_WAIT_NEXT_RQ) &&
2689 tick_isset(s->be->timeout.httpka))
2690 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2691 else
2692 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2693 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002694
Willy Tarreau59234e92008-11-30 23:51:27 +01002695 /* we're not ready yet */
2696 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002697
2698 failed_keep_alive:
2699 /* Here we process low-level errors for keep-alive requests. In
2700 * short, if the request is not the first one and it experiences
2701 * a timeout, read error or shutdown, we just silently close so
2702 * that the client can try again.
2703 */
2704 txn->status = 0;
2705 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Fauletd7c91962015-04-30 11:48:27 +02002706 req->analysers &= AN_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002707 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002708 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002709 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002710 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002711 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002712 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002713
Willy Tarreaud787e662009-07-07 10:14:51 +02002714 /* OK now we have a complete HTTP request with indexed headers. Let's
2715 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002716 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002717 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002718 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002719 * byte after the last LF. msg->sov points to the first byte of data.
2720 * msg->eol cannot be trusted because it may have been left uninitialized
2721 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002722 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002723
Willy Tarreau87b09662015-04-03 00:22:06 +02002724 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002725 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002726
Willy Tarreaub16a5742010-01-10 14:46:16 +01002727 if (txn->flags & TX_WAIT_NEXT_RQ) {
2728 /* kill the pending keep-alive timeout */
2729 txn->flags &= ~TX_WAIT_NEXT_RQ;
2730 req->analyse_exp = TICK_ETERNITY;
2731 }
2732
2733
Willy Tarreaud787e662009-07-07 10:14:51 +02002734 /* Maybe we found in invalid header name while we were configured not
2735 * to block on that, so we have to capture it now.
2736 */
2737 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002738 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002739
Willy Tarreau59234e92008-11-30 23:51:27 +01002740 /*
2741 * 1: identify the method
2742 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002743 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002744
2745 /* we can make use of server redirect on GET and HEAD */
2746 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002747 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002748
Willy Tarreau59234e92008-11-30 23:51:27 +01002749 /*
2750 * 2: check if the URI matches the monitor_uri.
2751 * We have to do this for every request which gets in, because
2752 * the monitor-uri is defined by the frontend.
2753 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002754 if (unlikely((sess->fe->monitor_uri_len != 0) &&
2755 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002756 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002757 sess->fe->monitor_uri,
2758 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002759 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002760 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002761 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002762 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002763
Willy Tarreaue7dff022015-04-03 01:14:29 +02002764 s->flags |= SF_MONITOR;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002765 sess->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002766
Willy Tarreau59234e92008-11-30 23:51:27 +01002767 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002768 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002769 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002770
Willy Tarreau59234e92008-11-30 23:51:27 +01002771 ret = acl_pass(ret);
2772 if (cond->pol == ACL_COND_UNLESS)
2773 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002774
Willy Tarreau59234e92008-11-30 23:51:27 +01002775 if (ret) {
2776 /* we fail this request, let's return 503 service unavail */
2777 txn->status = 503;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002778 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_503));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002779 if (!(s->flags & SF_ERR_MASK))
2780 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002781 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002782 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002783 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002784
Willy Tarreau59234e92008-11-30 23:51:27 +01002785 /* nothing to fail, let's reply normaly */
2786 txn->status = 200;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002787 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_200));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002788 if (!(s->flags & SF_ERR_MASK))
2789 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002790 goto return_prx_cond;
2791 }
2792
2793 /*
2794 * 3: Maybe we have to copy the original REQURI for the logs ?
2795 * Note: we cannot log anymore if the request has been
2796 * classified as invalid.
2797 */
2798 if (unlikely(s->logs.logwait & LW_REQ)) {
2799 /* we have a complete HTTP request that we must log */
2800 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2801 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002802
Willy Tarreau59234e92008-11-30 23:51:27 +01002803 if (urilen >= REQURI_LEN)
2804 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002805 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002806 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002807
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002808 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002809 s->do_log(s);
2810 } else {
2811 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002812 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002813 }
Willy Tarreau06619262006-12-17 08:37:22 +01002814
Willy Tarreau91852eb2015-05-01 13:26:00 +02002815 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
2816 * exactly one digit "." one digit. This check may be disabled using
2817 * option accept-invalid-http-request.
2818 */
2819 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
2820 if (msg->sl.rq.v_l != 8) {
2821 msg->err_pos = msg->sl.rq.v;
2822 goto return_bad_req;
2823 }
2824
2825 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
2826 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
2827 req->buf->p[msg->sl.rq.v + 6] != '.' ||
2828 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
2829 msg->err_pos = msg->sl.rq.v + 4;
2830 goto return_bad_req;
2831 }
2832 }
Willy Tarreau13317662015-05-01 13:47:08 +02002833 else {
2834 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
2835 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
2836 goto return_bad_req;
2837 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02002838
Willy Tarreau5b154472009-12-21 20:11:07 +01002839 /* ... and check if the request is HTTP/1.1 or above */
2840 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002841 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2842 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2843 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002844 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002845
2846 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002847 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 +01002848
Willy Tarreau88d349d2010-01-25 12:15:43 +01002849 /* if the frontend has "option http-use-proxy-header", we'll check if
2850 * we have what looks like a proxied connection instead of a connection,
2851 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2852 * Note that this is *not* RFC-compliant, however browsers and proxies
2853 * happen to do that despite being non-standard :-(
2854 * We consider that a request not beginning with either '/' or '*' is
2855 * a proxied connection, which covers both "scheme://location" and
2856 * CONNECT ip:port.
2857 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002858 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002859 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002860 txn->flags |= TX_USE_PX_CONN;
2861
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002862 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002863 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002864
Willy Tarreau59234e92008-11-30 23:51:27 +01002865 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002866 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002867 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002868 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002869
Willy Tarreau557f1992015-05-01 10:05:17 +02002870 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
2871 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002872 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002873 * The length of a message body is determined by one of the following
2874 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02002875 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002876 * 1. Any response to a HEAD request and any response with a 1xx
2877 * (Informational), 204 (No Content), or 304 (Not Modified) status
2878 * code is always terminated by the first empty line after the
2879 * header fields, regardless of the header fields present in the
2880 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002881 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002882 * 2. Any 2xx (Successful) response to a CONNECT request implies that
2883 * the connection will become a tunnel immediately after the empty
2884 * line that concludes the header fields. A client MUST ignore any
2885 * Content-Length or Transfer-Encoding header fields received in
2886 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002887 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002888 * 3. If a Transfer-Encoding header field is present and the chunked
2889 * transfer coding (Section 4.1) is the final encoding, the message
2890 * body length is determined by reading and decoding the chunked
2891 * data until the transfer coding indicates the data is complete.
2892 *
2893 * If a Transfer-Encoding header field is present in a response and
2894 * the chunked transfer coding is not the final encoding, the
2895 * message body length is determined by reading the connection until
2896 * it is closed by the server. If a Transfer-Encoding header field
2897 * is present in a request and the chunked transfer coding is not
2898 * the final encoding, the message body length cannot be determined
2899 * reliably; the server MUST respond with the 400 (Bad Request)
2900 * status code and then close the connection.
2901 *
2902 * If a message is received with both a Transfer-Encoding and a
2903 * Content-Length header field, the Transfer-Encoding overrides the
2904 * Content-Length. Such a message might indicate an attempt to
2905 * perform request smuggling (Section 9.5) or response splitting
2906 * (Section 9.4) and ought to be handled as an error. A sender MUST
2907 * remove the received Content-Length field prior to forwarding such
2908 * a message downstream.
2909 *
2910 * 4. If a message is received without Transfer-Encoding and with
2911 * either multiple Content-Length header fields having differing
2912 * field-values or a single Content-Length header field having an
2913 * invalid value, then the message framing is invalid and the
2914 * recipient MUST treat it as an unrecoverable error. If this is a
2915 * request message, the server MUST respond with a 400 (Bad Request)
2916 * status code and then close the connection. If this is a response
2917 * message received by a proxy, the proxy MUST close the connection
2918 * to the server, discard the received response, and send a 502 (Bad
2919 * Gateway) response to the client. If this is a response message
2920 * received by a user agent, the user agent MUST close the
2921 * connection to the server and discard the received response.
2922 *
2923 * 5. If a valid Content-Length header field is present without
2924 * Transfer-Encoding, its decimal value defines the expected message
2925 * body length in octets. If the sender closes the connection or
2926 * the recipient times out before the indicated number of octets are
2927 * received, the recipient MUST consider the message to be
2928 * incomplete and close the connection.
2929 *
2930 * 6. If this is a request message and none of the above are true, then
2931 * the message body length is zero (no message body is present).
2932 *
2933 * 7. Otherwise, this is a response message without a declared message
2934 * body length, so the message body length is determined by the
2935 * number of octets received prior to the server closing the
2936 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002937 */
2938
Willy Tarreau32b47f42009-10-18 20:55:02 +02002939 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002940 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02002941 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002942 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002943 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2944 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02002945 /* chunked not last, return badreq */
2946 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002947 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002948 }
2949
Willy Tarreau1c913912015-04-30 10:57:51 +02002950 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02002951 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02002952 if (msg->flags & HTTP_MSGF_TE_CHNK) {
2953 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
2954 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2955 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02002956 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002957 signed long long cl;
2958
Willy Tarreauad14f752011-09-02 20:33:27 +02002959 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002960 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002961 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002962 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002963
Willy Tarreauad14f752011-09-02 20:33:27 +02002964 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002965 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002966 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002967 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002968
Willy Tarreauad14f752011-09-02 20:33:27 +02002969 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002970 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002971 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002972 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002973
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002974 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002975 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002976 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002977 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002978
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002979 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002980 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002981 }
2982
Willy Tarreau34dfc602015-05-01 10:09:49 +02002983 /* even bodyless requests have a known length */
2984 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002985
Willy Tarreau179085c2014-04-28 16:48:56 +02002986 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
2987 * only change if both the request and the config reference something else.
2988 * Option httpclose by itself sets tunnel mode where headers are mangled.
2989 * However, if another mode is set, it will affect it (eg: server-close/
2990 * keep-alive + httpclose = close). Note that we avoid to redo the same work
2991 * if FE and BE have the same settings (common). The method consists in
2992 * checking if options changed between the two calls (implying that either
2993 * one is non-null, or one of them is non-null and we are there for the first
2994 * time.
2995 */
2996 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002997 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002998 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02002999
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02003000 /* we may have to wait for the request's body */
3001 if ((s->be->options & PR_O_WREQ_BODY) &&
3002 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
3003 req->analysers |= AN_REQ_HTTP_BODY;
3004
Willy Tarreaud787e662009-07-07 10:14:51 +02003005 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003006 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003007 req->analyse_exp = TICK_ETERNITY;
3008 return 1;
3009
3010 return_bad_req:
3011 /* We centralize bad requests processing here */
3012 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3013 /* we detected a parsing error. We want to archive this request
3014 * in the dedicated proxy area for later troubleshooting.
3015 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003016 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003017 }
3018
3019 txn->req.msg_state = HTTP_MSG_ERROR;
3020 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003021 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003022
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003023 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003024 if (sess->listener->counters)
3025 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003026
3027 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003028 if (!(s->flags & SF_ERR_MASK))
3029 s->flags |= SF_ERR_PRXCOND;
3030 if (!(s->flags & SF_FINST_MASK))
3031 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02003032
Christopher Fauletd7c91962015-04-30 11:48:27 +02003033 req->analysers &= AN_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02003034 req->analyse_exp = TICK_ETERNITY;
3035 return 0;
3036}
3037
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003038
Willy Tarreau347a35d2013-11-22 17:51:09 +01003039/* This function prepares an applet to handle the stats. It can deal with the
3040 * "100-continue" expectation, check that admin rules are met for POST requests,
3041 * and program a response message if something was unexpected. It cannot fail
3042 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003043 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003044 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02003045 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003046 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003047int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003048{
3049 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003050 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02003051 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003052 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003053 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003054 struct uri_auth *uri_auth = s->be->uri_auth;
3055 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003056 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003057
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003058 appctx = si_appctx(si);
3059 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3060 appctx->st1 = appctx->st2 = 0;
3061 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3062 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003063 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003064 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003065
3066 uri = msg->chn->buf->p + msg->sl.rq.u;
3067 lookup = uri + uri_auth->uri_len;
3068
3069 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3070 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003071 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003072 break;
3073 }
3074 }
3075
3076 if (uri_auth->refresh) {
3077 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3078 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003079 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003080 break;
3081 }
3082 }
3083 }
3084
3085 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3086 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003087 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003088 break;
3089 }
3090 }
3091
Willy Tarreau1e62df92016-01-11 18:57:53 +01003092 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
3093 if (memcmp(h, ";typed", 6) == 0) {
3094 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
3095 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3096 break;
3097 }
3098 }
3099
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003100 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3101 if (memcmp(h, ";st=", 4) == 0) {
3102 int i;
3103 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003104 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003105 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3106 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003107 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003108 break;
3109 }
3110 }
3111 break;
3112 }
3113 }
3114
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003115 appctx->ctx.stats.scope_str = 0;
3116 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003117 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3118 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3119 int itx = 0;
3120 const char *h2;
3121 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3122 const char *err;
3123
3124 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3125 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003126 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003127 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3128 itx++;
3129 h++;
3130 }
3131
3132 if (itx > STAT_SCOPE_TXT_MAXLEN)
3133 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003134 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003135
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003136 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003137 memcpy(scope_txt, h2, itx);
3138 scope_txt[itx] = '\0';
3139 err = invalid_char(scope_txt);
3140 if (err) {
3141 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003142 appctx->ctx.stats.scope_str = 0;
3143 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003144 }
3145 break;
3146 }
3147 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003148
3149 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003150 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003151 int ret = 1;
3152
3153 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003154 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 +01003155 ret = acl_pass(ret);
3156 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3157 ret = !ret;
3158 }
3159
3160 if (ret) {
3161 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003162 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003163 break;
3164 }
3165 }
3166
3167 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003168 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003169 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003170 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003171 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
3172 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003173 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003174 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003175 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003176 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3177 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003178 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003179 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003180 else {
3181 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003182 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003183 }
3184
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003185 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003186 return 1;
3187}
3188
Lukas Tribus67db8df2013-06-23 17:37:13 +02003189/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3190 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3191 */
Vincent Bernat6e615892016-05-18 16:17:44 +02003192void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003193{
3194#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02003195 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003196 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3197#endif
3198#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02003199 if (from->ss_family == AF_INET6) {
3200 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02003201 /* v4-mapped addresses need IP_TOS */
3202 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3203 else
3204 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3205 }
3206#endif
3207}
3208
Willy Tarreau87b09662015-04-03 00:22:06 +02003209int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003210 const char* name, unsigned int name_len,
3211 const char *str, struct my_regex *re,
3212 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003213{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003214 struct hdr_ctx ctx;
3215 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003216 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003217 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3218 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003219 struct chunk *output = get_trash_chunk();
3220
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003221 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003222
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003223 /* Choose the header browsing function. */
3224 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003225 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003226 http_find_hdr_func = http_find_header2;
3227 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003228 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003229 http_find_hdr_func = http_find_full_header2;
3230 break;
3231 default: /* impossible */
3232 return -1;
3233 }
3234
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003235 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3236 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003237 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003238 char *val = ctx.line + ctx.val;
3239 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003240
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003241 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3242 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003243
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003244 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003245 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003246 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003247
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003248 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003249
3250 hdr->len += delta;
3251 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003252
3253 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003254 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003255 }
3256
3257 return 0;
3258}
3259
Willy Tarreau87b09662015-04-03 00:22:06 +02003260static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003261 const char* name, unsigned int name_len,
3262 struct list *fmt, struct my_regex *re,
3263 int action)
3264{
3265 struct chunk *replace = get_trash_chunk();
3266
3267 replace->len = build_logline(s, replace->str, replace->size, fmt);
3268 if (replace->len >= replace->size - 1)
3269 return -1;
3270
3271 return http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
3272}
3273
Willy Tarreau87b09662015-04-03 00:22:06 +02003274/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003275 * transaction <txn>. Returns the verdict of the first rule that prevents
3276 * further processing of the request (auth, deny, ...), and defaults to
3277 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3278 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02003279 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
3280 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
3281 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003282 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003283enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02003284http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003285{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003286 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003287 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003288 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003289 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003290 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003291 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02003292 int act_flags = 0;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003293
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003294 /* If "the current_rule_list" match the executed rule list, we are in
3295 * resume condition. If a resume is needed it is always in the action
3296 * and never in the ACL or converters. In this case, we initialise the
3297 * current rule, and go to the action execution point.
3298 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003299 if (s->current_rule) {
3300 rule = s->current_rule;
3301 s->current_rule = NULL;
3302 if (s->current_rule_list == rules)
3303 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003304 }
3305 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003306
Willy Tarreauff011f22011-01-06 17:51:27 +01003307 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003308
Willy Tarreau96257ec2012-12-27 10:46:37 +01003309 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003310 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003311 int ret;
3312
Willy Tarreau192252e2015-04-04 01:47:55 +02003313 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003314 ret = acl_pass(ret);
3315
Willy Tarreauff011f22011-01-06 17:51:27 +01003316 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003317 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003318
3319 if (!ret) /* condition not matched */
3320 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003321 }
3322
Willy Tarreauacc98002015-09-27 23:34:39 +02003323 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003324resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003325 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003326 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003327 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003328
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003329 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02003330 if (deny_status)
3331 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003332 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003333
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003334 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01003335 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003336 if (deny_status)
3337 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003338 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003339
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003340 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003341 /* Auth might be performed on regular http-req rules as well as on stats */
3342 auth_realm = rule->arg.auth.realm;
3343 if (!auth_realm) {
3344 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3345 auth_realm = STATS_DEFAULT_REALM;
3346 else
3347 auth_realm = px->id;
3348 }
3349 /* send 401/407 depending on whether we use a proxy or not. We still
3350 * count one error, because normal browsing won't significantly
3351 * increase the counter but brute force attempts will.
3352 */
3353 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3354 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003355 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003356 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003357 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003358
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003359 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003360 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3361 return HTTP_RULE_RES_BADREQ;
3362 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003363
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003364 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003365 s->task->nice = rule->arg.nice;
3366 break;
3367
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003368 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003369 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Vincent Bernat6e615892016-05-18 16:17:44 +02003370 inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003371 break;
3372
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003373 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003374#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003375 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003376 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003377#endif
3378 break;
3379
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003380 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003381 s->logs.level = rule->arg.loglevel;
3382 break;
3383
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003384 case ACT_HTTP_REPLACE_HDR:
3385 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003386 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3387 rule->arg.hdr_add.name_len,
3388 &rule->arg.hdr_add.fmt,
3389 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003390 return HTTP_RULE_RES_BADREQ;
3391 break;
3392
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003393 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003394 ctx.idx = 0;
3395 /* remove all occurrences of the header */
3396 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3397 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3398 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003399 }
Willy Tarreau85603282015-01-21 20:39:27 +01003400 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003401
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003402 case ACT_HTTP_SET_HDR:
3403 case ACT_HTTP_ADD_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003404 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3405 trash.len = rule->arg.hdr_add.name_len;
3406 trash.str[trash.len++] = ':';
3407 trash.str[trash.len++] = ' ';
3408 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 +01003409
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003410 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003411 /* remove all occurrences of the header */
3412 ctx.idx = 0;
3413 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3414 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3415 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3416 }
3417 }
3418
Willy Tarreau96257ec2012-12-27 10:46:37 +01003419 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3420 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003421
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003422 case ACT_HTTP_DEL_ACL:
3423 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003424 struct pat_ref *ref;
3425 char *key;
3426 int len;
3427
3428 /* collect reference */
3429 ref = pat_ref_lookup(rule->arg.map.ref);
3430 if (!ref)
3431 continue;
3432
3433 /* collect key */
3434 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3435 key = trash.str;
3436 key[len] = '\0';
3437
3438 /* perform update */
3439 /* returned code: 1=ok, 0=ko */
3440 pat_ref_delete(ref, key);
3441
3442 break;
3443 }
3444
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003445 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003446 struct pat_ref *ref;
3447 char *key;
3448 struct chunk *trash_key;
3449 int len;
3450
3451 trash_key = get_trash_chunk();
3452
3453 /* collect reference */
3454 ref = pat_ref_lookup(rule->arg.map.ref);
3455 if (!ref)
3456 continue;
3457
3458 /* collect key */
3459 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3460 key = trash_key->str;
3461 key[len] = '\0';
3462
3463 /* perform update */
3464 /* add entry only if it does not already exist */
3465 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003466 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003467
3468 break;
3469 }
3470
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003471 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003472 struct pat_ref *ref;
3473 char *key, *value;
3474 struct chunk *trash_key, *trash_value;
3475 int len;
3476
3477 trash_key = get_trash_chunk();
3478 trash_value = get_trash_chunk();
3479
3480 /* collect reference */
3481 ref = pat_ref_lookup(rule->arg.map.ref);
3482 if (!ref)
3483 continue;
3484
3485 /* collect key */
3486 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3487 key = trash_key->str;
3488 key[len] = '\0';
3489
3490 /* collect value */
3491 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3492 value = trash_value->str;
3493 value[len] = '\0';
3494
3495 /* perform update */
3496 if (pat_ref_find_elt(ref, key) != NULL)
3497 /* update entry if it exists */
3498 pat_ref_set(ref, key, value, NULL);
3499 else
3500 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003501 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003502
3503 break;
3504 }
William Lallemand73025dd2014-04-24 14:38:37 +02003505
Thierry FOURNIER42148732015-09-02 17:17:33 +02003506 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003507 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3508 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003509
Willy Tarreauacc98002015-09-27 23:34:39 +02003510 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003511 case ACT_RET_ERR:
3512 case ACT_RET_CONT:
3513 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003514 case ACT_RET_STOP:
3515 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003516 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003517 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003518 return HTTP_RULE_RES_YIELD;
3519 }
William Lallemand73025dd2014-04-24 14:38:37 +02003520 break;
3521
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003522 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02003523 /* Note: only the first valid tracking parameter of each
3524 * applies.
3525 */
3526
3527 if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) {
3528 struct stktable *t;
3529 struct stksess *ts;
3530 struct stktable_key *key;
3531 void *ptr;
3532
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02003533 t = rule->arg.trk_ctr.table.t;
3534 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 +02003535
3536 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreau87b09662015-04-03 00:22:06 +02003537 stream_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003538
3539 /* let's count a new HTTP request as it's the first time we do it */
3540 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3541 if (ptr)
3542 stktable_data_cast(ptr, http_req_cnt)++;
3543
3544 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3545 if (ptr)
3546 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3547 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3548
3549 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003550 if (sess->fe != s->be)
Willy Tarreau09448f72014-06-25 18:12:15 +02003551 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3552 }
3553 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003554 break;
3555
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003556 /* other flags exists, but normaly, they never be matched. */
3557 default:
3558 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003559 }
3560 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003561
3562 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003563 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003564}
3565
Willy Tarreau71241ab2012-12-27 11:30:54 +01003566
Willy Tarreau51d861a2015-05-22 17:30:48 +02003567/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3568 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3569 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3570 * is returned, the process can continue the evaluation of next rule list. If
3571 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3572 * is returned, it means the operation could not be processed and a server error
3573 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3574 * deny rule. If *YIELD is returned, the caller must call again the function
3575 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003576 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003577static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003578http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003579{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003580 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003581 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003582 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003583 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003584 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02003585 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003586
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003587 /* If "the current_rule_list" match the executed rule list, we are in
3588 * resume condition. If a resume is needed it is always in the action
3589 * and never in the ACL or converters. In this case, we initialise the
3590 * current rule, and go to the action execution point.
3591 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003592 if (s->current_rule) {
3593 rule = s->current_rule;
3594 s->current_rule = NULL;
3595 if (s->current_rule_list == rules)
3596 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003597 }
3598 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003599
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003600 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003601
3602 /* check optional condition */
3603 if (rule->cond) {
3604 int ret;
3605
Willy Tarreau192252e2015-04-04 01:47:55 +02003606 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003607 ret = acl_pass(ret);
3608
3609 if (rule->cond->pol == ACL_COND_UNLESS)
3610 ret = !ret;
3611
3612 if (!ret) /* condition not matched */
3613 continue;
3614 }
3615
Willy Tarreauacc98002015-09-27 23:34:39 +02003616 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003617resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003618 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003619 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003620 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003621
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003622 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003623 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003624 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003625
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003626 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003627 s->task->nice = rule->arg.nice;
3628 break;
3629
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003630 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003631 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Vincent Bernat6e615892016-05-18 16:17:44 +02003632 inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003633 break;
3634
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003635 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003636#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003637 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003638 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003639#endif
3640 break;
3641
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003642 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003643 s->logs.level = rule->arg.loglevel;
3644 break;
3645
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003646 case ACT_HTTP_REPLACE_HDR:
3647 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003648 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3649 rule->arg.hdr_add.name_len,
3650 &rule->arg.hdr_add.fmt,
3651 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003652 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003653 break;
3654
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003655 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003656 ctx.idx = 0;
3657 /* remove all occurrences of the header */
3658 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3659 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3660 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3661 }
Willy Tarreau85603282015-01-21 20:39:27 +01003662 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003663
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003664 case ACT_HTTP_SET_HDR:
3665 case ACT_HTTP_ADD_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003666 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3667 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3668 trash.len = rule->arg.hdr_add.name_len;
3669 trash.str[trash.len++] = ':';
3670 trash.str[trash.len++] = ' ';
3671 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 +01003672
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003673 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003674 /* remove all occurrences of the header */
3675 ctx.idx = 0;
3676 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3677 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3678 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3679 }
3680 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003681 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3682 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003683
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003684 case ACT_HTTP_DEL_ACL:
3685 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003686 struct pat_ref *ref;
3687 char *key;
3688 int len;
3689
3690 /* collect reference */
3691 ref = pat_ref_lookup(rule->arg.map.ref);
3692 if (!ref)
3693 continue;
3694
3695 /* collect key */
3696 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3697 key = trash.str;
3698 key[len] = '\0';
3699
3700 /* perform update */
3701 /* returned code: 1=ok, 0=ko */
3702 pat_ref_delete(ref, key);
3703
3704 break;
3705 }
3706
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003707 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003708 struct pat_ref *ref;
3709 char *key;
3710 struct chunk *trash_key;
3711 int len;
3712
3713 trash_key = get_trash_chunk();
3714
3715 /* collect reference */
3716 ref = pat_ref_lookup(rule->arg.map.ref);
3717 if (!ref)
3718 continue;
3719
3720 /* collect key */
3721 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3722 key = trash_key->str;
3723 key[len] = '\0';
3724
3725 /* perform update */
3726 /* check if the entry already exists */
3727 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003728 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003729
3730 break;
3731 }
3732
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003733 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003734 struct pat_ref *ref;
3735 char *key, *value;
3736 struct chunk *trash_key, *trash_value;
3737 int len;
3738
3739 trash_key = get_trash_chunk();
3740 trash_value = get_trash_chunk();
3741
3742 /* collect reference */
3743 ref = pat_ref_lookup(rule->arg.map.ref);
3744 if (!ref)
3745 continue;
3746
3747 /* collect key */
3748 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3749 key = trash_key->str;
3750 key[len] = '\0';
3751
3752 /* collect value */
3753 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3754 value = trash_value->str;
3755 value[len] = '\0';
3756
3757 /* perform update */
3758 if (pat_ref_find_elt(ref, key) != NULL)
3759 /* update entry if it exists */
3760 pat_ref_set(ref, key, value, NULL);
3761 else
3762 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003763 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003764
3765 break;
3766 }
William Lallemand73025dd2014-04-24 14:38:37 +02003767
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003768 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003769 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3770 return HTTP_RULE_RES_BADREQ;
3771 return HTTP_RULE_RES_DONE;
3772
Thierry FOURNIER42148732015-09-02 17:17:33 +02003773 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003774 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3775 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003776
Willy Tarreauacc98002015-09-27 23:34:39 +02003777 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003778 case ACT_RET_ERR:
3779 case ACT_RET_CONT:
3780 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003781 case ACT_RET_STOP:
3782 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003783 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003784 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003785 return HTTP_RULE_RES_YIELD;
3786 }
William Lallemand73025dd2014-04-24 14:38:37 +02003787 break;
3788
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003789 /* other flags exists, but normaly, they never be matched. */
3790 default:
3791 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003792 }
3793 }
3794
3795 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003796 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003797}
3798
3799
Willy Tarreau71241ab2012-12-27 11:30:54 +01003800/* Perform an HTTP redirect based on the information in <rule>. The function
3801 * returns non-zero on success, or zero in case of a, irrecoverable error such
3802 * as too large a request to build a valid response.
3803 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003804static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003805{
Willy Tarreaub329a312015-05-22 16:27:37 +02003806 struct http_msg *req = &txn->req;
3807 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003808 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003809 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003810
3811 /* build redirect message */
3812 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003813 case 308:
3814 msg_fmt = HTTP_308;
3815 break;
3816 case 307:
3817 msg_fmt = HTTP_307;
3818 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003819 case 303:
3820 msg_fmt = HTTP_303;
3821 break;
3822 case 301:
3823 msg_fmt = HTTP_301;
3824 break;
3825 case 302:
3826 default:
3827 msg_fmt = HTTP_302;
3828 break;
3829 }
3830
3831 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3832 return 0;
3833
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003834 location = trash.str + trash.len;
3835
Willy Tarreau71241ab2012-12-27 11:30:54 +01003836 switch(rule->type) {
3837 case REDIRECT_TYPE_SCHEME: {
3838 const char *path;
3839 const char *host;
3840 struct hdr_ctx ctx;
3841 int pathlen;
3842 int hostlen;
3843
3844 host = "";
3845 hostlen = 0;
3846 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02003847 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003848 host = ctx.line + ctx.val;
3849 hostlen = ctx.vlen;
3850 }
3851
3852 path = http_get_path(txn);
3853 /* build message using path */
3854 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003855 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003856 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3857 int qs = 0;
3858 while (qs < pathlen) {
3859 if (path[qs] == '?') {
3860 pathlen = qs;
3861 break;
3862 }
3863 qs++;
3864 }
3865 }
3866 } else {
3867 path = "/";
3868 pathlen = 1;
3869 }
3870
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003871 if (rule->rdr_str) { /* this is an old "redirect" rule */
3872 /* check if we can add scheme + "://" + host + path */
3873 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3874 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003875
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003876 /* add scheme */
3877 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3878 trash.len += rule->rdr_len;
3879 }
3880 else {
3881 /* add scheme with executing log format */
3882 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003883
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003884 /* check if we can add scheme + "://" + host + path */
3885 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3886 return 0;
3887 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003888 /* add "://" */
3889 memcpy(trash.str + trash.len, "://", 3);
3890 trash.len += 3;
3891
3892 /* add host */
3893 memcpy(trash.str + trash.len, host, hostlen);
3894 trash.len += hostlen;
3895
3896 /* add path */
3897 memcpy(trash.str + trash.len, path, pathlen);
3898 trash.len += pathlen;
3899
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003900 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003901 if (trash.len && trash.str[trash.len - 1] != '/' &&
3902 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3903 if (trash.len > trash.size - 5)
3904 return 0;
3905 trash.str[trash.len] = '/';
3906 trash.len++;
3907 }
3908
3909 break;
3910 }
3911 case REDIRECT_TYPE_PREFIX: {
3912 const char *path;
3913 int pathlen;
3914
3915 path = http_get_path(txn);
3916 /* build message using path */
3917 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003918 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003919 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3920 int qs = 0;
3921 while (qs < pathlen) {
3922 if (path[qs] == '?') {
3923 pathlen = qs;
3924 break;
3925 }
3926 qs++;
3927 }
3928 }
3929 } else {
3930 path = "/";
3931 pathlen = 1;
3932 }
3933
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003934 if (rule->rdr_str) { /* this is an old "redirect" rule */
3935 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3936 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003937
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003938 /* add prefix. Note that if prefix == "/", we don't want to
3939 * add anything, otherwise it makes it hard for the user to
3940 * configure a self-redirection.
3941 */
3942 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3943 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3944 trash.len += rule->rdr_len;
3945 }
3946 }
3947 else {
3948 /* add prefix with executing log format */
3949 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3950
3951 /* Check length */
3952 if (trash.len + pathlen > trash.size - 4)
3953 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003954 }
3955
3956 /* add path */
3957 memcpy(trash.str + trash.len, path, pathlen);
3958 trash.len += pathlen;
3959
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003960 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003961 if (trash.len && trash.str[trash.len - 1] != '/' &&
3962 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3963 if (trash.len > trash.size - 5)
3964 return 0;
3965 trash.str[trash.len] = '/';
3966 trash.len++;
3967 }
3968
3969 break;
3970 }
3971 case REDIRECT_TYPE_LOCATION:
3972 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003973 if (rule->rdr_str) { /* this is an old "redirect" rule */
3974 if (trash.len + rule->rdr_len > trash.size - 4)
3975 return 0;
3976
3977 /* add location */
3978 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3979 trash.len += rule->rdr_len;
3980 }
3981 else {
3982 /* add location with executing log format */
3983 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003984
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003985 /* Check left length */
3986 if (trash.len > trash.size - 4)
3987 return 0;
3988 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003989 break;
3990 }
3991
3992 if (rule->cookie_len) {
3993 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3994 trash.len += 14;
3995 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3996 trash.len += rule->cookie_len;
3997 memcpy(trash.str + trash.len, "\r\n", 2);
3998 trash.len += 2;
3999 }
4000
4001 /* add end of headers and the keep-alive/close status.
4002 * We may choose to set keep-alive if the Location begins
4003 * with a slash, because the client will come back to the
4004 * same server.
4005 */
4006 txn->status = rule->code;
4007 /* let's log the request time */
4008 s->logs.tv_request = now;
4009
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004010 if (*location == '/' &&
Willy Tarreaub329a312015-05-22 16:27:37 +02004011 (req->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau2de8a502015-05-28 17:23:54 +02004012 ((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004013 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4014 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4015 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02004016 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004017 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4018 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4019 trash.len += 30;
4020 } else {
4021 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4022 trash.len += 24;
4023 }
4024 }
4025 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4026 trash.len += 4;
Christopher Faulet3e344292015-11-24 16:24:13 +01004027 FLT_STRM_CB(s, flt_http_reply(s, txn->status, &trash));
Willy Tarreaub329a312015-05-22 16:27:37 +02004028 bo_inject(res->chn, trash.str, trash.len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004029 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02004030 bi_fast_delete(req->chn->buf, req->sov);
4031 req->next -= req->sov;
4032 req->sov = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004033 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_FLT_END);
4034 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->req.analysers & AN_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02004035 req->msg_state = HTTP_MSG_CLOSED;
4036 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02004037 /* Trim any possible response */
4038 res->chn->buf->i = 0;
4039 res->next = res->sov = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004040 } else {
4041 /* keep-alive not possible */
4042 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4043 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4044 trash.len += 29;
4045 } else {
4046 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4047 trash.len += 23;
4048 }
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004049 http_reply_and_close(s, txn->status, &trash);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004050 req->chn->analysers &= AN_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004051 }
4052
Willy Tarreaue7dff022015-04-03 01:14:29 +02004053 if (!(s->flags & SF_ERR_MASK))
4054 s->flags |= SF_ERR_LOCAL;
4055 if (!(s->flags & SF_FINST_MASK))
4056 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004057
4058 return 1;
4059}
4060
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004061/* This stream analyser runs all HTTP request processing which is common to
4062 * frontends and backends, which means blocking ACLs, filters, connection-close,
4063 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004064 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004065 * either needs more data or wants to immediately abort the request (eg: deny,
4066 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004067 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004068int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004069{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004070 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004071 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004072 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004073 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004074 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004075 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004076 int deny_status = HTTP_ERR_403;
Willy Tarreaud787e662009-07-07 10:14:51 +02004077
Willy Tarreau655dce92009-11-08 13:10:58 +01004078 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004079 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004080 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004081 }
4082
Willy Tarreau87b09662015-04-03 00:22:06 +02004083 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 +02004084 now_ms, __FUNCTION__,
4085 s,
4086 req,
4087 req->rex, req->wex,
4088 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004089 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004090 req->analysers);
4091
Willy Tarreau65410832014-04-28 21:25:43 +02004092 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004093 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004094
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004095 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004096 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02004097 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01004098
Willy Tarreau0b748332014-04-29 00:13:29 +02004099 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004100 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4101 goto return_prx_yield;
4102
Willy Tarreau0b748332014-04-29 00:13:29 +02004103 case HTTP_RULE_RES_CONT:
4104 case HTTP_RULE_RES_STOP: /* nothing to do */
4105 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004106
Willy Tarreau0b748332014-04-29 00:13:29 +02004107 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4108 if (txn->flags & TX_CLTARPIT)
4109 goto tarpit;
4110 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004111
Willy Tarreau0b748332014-04-29 00:13:29 +02004112 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4113 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004114
Willy Tarreau0b748332014-04-29 00:13:29 +02004115 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004116 goto done;
4117
Willy Tarreau0b748332014-04-29 00:13:29 +02004118 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4119 goto return_bad_req;
4120 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004121 }
4122
Willy Tarreau52542592014-04-28 18:33:26 +02004123 /* OK at this stage, we know that the request was accepted according to
4124 * the http-request rules, we can check for the stats. Note that the
4125 * URI is detected *before* the req* rules in order not to be affected
4126 * by a possible reqrep, while they are processed *after* so that a
4127 * reqdeny can still block them. This clearly needs to change in 1.6!
4128 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004129 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004130 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004131 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004132 txn->status = 500;
4133 s->logs.tv_request = now;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004134 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004135
Willy Tarreaue7dff022015-04-03 01:14:29 +02004136 if (!(s->flags & SF_ERR_MASK))
4137 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004138 goto return_prx_cond;
4139 }
4140
4141 /* parse the whole stats request and extract the relevant information */
4142 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02004143 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02004144 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004145
Willy Tarreau0b748332014-04-29 00:13:29 +02004146 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4147 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004148
Willy Tarreau0b748332014-04-29 00:13:29 +02004149 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4150 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004151 }
4152
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004153 /* evaluate the req* rules except reqadd */
4154 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004155 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004156 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004157
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004158 if (txn->flags & TX_CLDENY)
4159 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004160
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004161 if (txn->flags & TX_CLTARPIT)
4162 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004163 }
Willy Tarreau06619262006-12-17 08:37:22 +01004164
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004165 /* add request headers from the rule sets in the same order */
4166 list_for_each_entry(wl, &px->req_add, list) {
4167 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004168 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004169 ret = acl_pass(ret);
4170 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4171 ret = !ret;
4172 if (!ret)
4173 continue;
4174 }
4175
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004176 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004177 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004178 }
4179
Willy Tarreau52542592014-04-28 18:33:26 +02004180
4181 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004182 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004183 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004184 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4185 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004186
Willy Tarreaue7dff022015-04-03 01:14:29 +02004187 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4188 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4189 if (!(s->flags & SF_FINST_MASK))
4190 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004191
Willy Tarreau70730dd2014-04-24 18:06:27 +02004192 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet309c6412015-12-02 09:57:32 +01004193 req->analysers &= (AN_REQ_HTTP_BODY | AN_FLT_HTTP_HDRS | AN_FLT_END);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004194 req->analysers &= ~AN_FLT_XFER_DATA;
4195 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004196 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004197 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004198
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004199 /* check whether we have some ACLs set to redirect this request */
4200 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004201 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004202 int ret;
4203
Willy Tarreau192252e2015-04-04 01:47:55 +02004204 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004205 ret = acl_pass(ret);
4206 if (rule->cond->pol == ACL_COND_UNLESS)
4207 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004208 if (!ret)
4209 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004210 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004211 if (!http_apply_redirect_rule(rule, s, txn))
4212 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004213 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004214 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004215
Willy Tarreau2be39392010-01-03 17:24:51 +01004216 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4217 * If this happens, then the data will not come immediately, so we must
4218 * send all what we have without waiting. Note that due to the small gain
4219 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004220 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004221 * itself once used.
4222 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004223 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004224
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004225 done: /* done with this analyser, continue with next ones that the calling
4226 * points will have set, if any.
4227 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004228 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004229 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4230 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004231 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004232
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004233 tarpit:
4234 /* When a connection is tarpitted, we use the tarpit timeout,
4235 * which may be the same as the connect timeout if unspecified.
4236 * If unset, then set it to zero because we really want it to
4237 * eventually expire. We build the tarpit as an analyser.
4238 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004239 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004240
4241 /* wipe the request out so that we can drop the connection early
4242 * if the client closes first.
4243 */
4244 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004245
4246 /* Allow cookie logging
4247 */
4248 if (s->be->cookie_name || sess->fe->capture_name)
4249 manage_client_side_cookies(s, req);
4250
Christopher Fauletd7c91962015-04-30 11:48:27 +02004251 req->analysers &= AN_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004252 req->analysers |= AN_REQ_HTTP_TARPIT;
4253 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4254 if (!req->analyse_exp)
4255 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004256 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004257 sess->fe->fe_counters.denied_req++;
4258 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004259 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004260 if (sess->listener->counters)
4261 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004262 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004263
4264 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004265
4266 /* Allow cookie logging
4267 */
4268 if (s->be->cookie_name || sess->fe->capture_name)
4269 manage_client_side_cookies(s, req);
4270
Willy Tarreau0b748332014-04-29 00:13:29 +02004271 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004272 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004273 s->logs.tv_request = now;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004274 http_reply_and_close(s, txn->status, http_error_message(s, deny_status));
Willy Tarreau87b09662015-04-03 00:22:06 +02004275 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004276 sess->fe->fe_counters.denied_req++;
4277 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004278 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004279 if (sess->listener->counters)
4280 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004281 goto return_prx_cond;
4282
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004283 return_bad_req:
4284 /* We centralize bad requests processing here */
4285 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4286 /* we detected a parsing error. We want to archive this request
4287 * in the dedicated proxy area for later troubleshooting.
4288 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004289 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004290 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004291
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004292 txn->req.msg_state = HTTP_MSG_ERROR;
4293 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004294 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004295
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004296 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004297 if (sess->listener->counters)
4298 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004299
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004300 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004301 if (!(s->flags & SF_ERR_MASK))
4302 s->flags |= SF_ERR_PRXCOND;
4303 if (!(s->flags & SF_FINST_MASK))
4304 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004305
Christopher Fauletd7c91962015-04-30 11:48:27 +02004306 req->analysers &= AN_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004307 req->analyse_exp = TICK_ETERNITY;
4308 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004309
4310 return_prx_yield:
4311 channel_dont_connect(req);
4312 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004313}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004314
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004315/* This function performs all the processing enabled for the current request.
4316 * It returns 1 if the processing can continue on next analysers, or zero if it
4317 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004318 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004319 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004320int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004321{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004322 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004323 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004324 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004325 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004326
Willy Tarreau655dce92009-11-08 13:10:58 +01004327 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004328 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004329 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004330 return 0;
4331 }
4332
Willy Tarreau87b09662015-04-03 00:22:06 +02004333 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 +02004334 now_ms, __FUNCTION__,
4335 s,
4336 req,
4337 req->rex, req->wex,
4338 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004339 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004340 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004341
Willy Tarreau59234e92008-11-30 23:51:27 +01004342 /*
4343 * Right now, we know that we have processed the entire headers
4344 * and that unwanted requests have been filtered out. We can do
4345 * whatever we want with the remaining request. Also, now we
4346 * may have separate values for ->fe, ->be.
4347 */
Willy Tarreau06619262006-12-17 08:37:22 +01004348
Willy Tarreau59234e92008-11-30 23:51:27 +01004349 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004350 * If HTTP PROXY is set we simply get remote server address parsing
4351 * incoming request. Note that this requires that a connection is
4352 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004353 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004354 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004355 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004356 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004357
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004358 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau973a5422015-08-05 21:47:23 +02004359 if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004360 txn->req.msg_state = HTTP_MSG_ERROR;
4361 txn->status = 500;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004362 req->analysers &= AN_FLT_END;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004363 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004364
Willy Tarreaue7dff022015-04-03 01:14:29 +02004365 if (!(s->flags & SF_ERR_MASK))
4366 s->flags |= SF_ERR_RESOURCE;
4367 if (!(s->flags & SF_FINST_MASK))
4368 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004369
4370 return 0;
4371 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004372
4373 path = http_get_path(txn);
4374 url2sa(req->buf->p + msg->sl.rq.u,
4375 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004376 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004377 /* if the path was found, we have to remove everything between
4378 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4379 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4380 * u_l characters by a single "/".
4381 */
4382 if (path) {
4383 char *cur_ptr = req->buf->p;
4384 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4385 int delta;
4386
4387 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4388 http_msg_move_end(&txn->req, delta);
4389 cur_end += delta;
4390 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4391 goto return_bad_req;
4392 }
4393 else {
4394 char *cur_ptr = req->buf->p;
4395 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4396 int delta;
4397
4398 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4399 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4400 http_msg_move_end(&txn->req, delta);
4401 cur_end += delta;
4402 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4403 goto return_bad_req;
4404 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004405 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004406
Willy Tarreau59234e92008-11-30 23:51:27 +01004407 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004408 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004409 * Note that doing so might move headers in the request, but
4410 * the fields will stay coherent and the URI will not move.
4411 * This should only be performed in the backend.
4412 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004413 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004414 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004415
William Lallemanda73203e2012-03-12 12:48:57 +01004416 /* add unique-id if "header-unique-id" is specified */
4417
Thierry Fournierf4011dd2016-03-29 17:23:51 +02004418 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004419 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4420 goto return_bad_req;
4421 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004422 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004423 }
William Lallemanda73203e2012-03-12 12:48:57 +01004424
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004425 if (sess->fe->header_unique_id && s->unique_id) {
4426 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004427 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004428 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004429 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004430 goto return_bad_req;
4431 }
4432
Cyril Bontéb21570a2009-11-29 20:04:48 +01004433 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004434 * 9: add X-Forwarded-For if either the frontend or the backend
4435 * asks for it.
4436 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004437 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004438 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004439 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4440 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4441 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004442 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004443 /* The header is set to be added only if none is present
4444 * and we found it, so don't do anything.
4445 */
4446 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004447 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004448 /* Add an X-Forwarded-For header unless the source IP is
4449 * in the 'except' network range.
4450 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004451 if ((!sess->fe->except_mask.s_addr ||
4452 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4453 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004454 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004455 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004456 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004457 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004458 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004459 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004460
4461 /* Note: we rely on the backend to get the header name to be used for
4462 * x-forwarded-for, because the header is really meant for the backends.
4463 * However, if the backend did not specify any option, we have to rely
4464 * on the frontend's header name.
4465 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004466 if (s->be->fwdfor_hdr_len) {
4467 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004468 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004469 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004470 len = sess->fe->fwdfor_hdr_len;
4471 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004472 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004473 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 +01004474
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004475 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004476 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004477 }
4478 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004479 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004480 /* FIXME: for the sake of completeness, we should also support
4481 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004482 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004483 int len;
4484 char pn[INET6_ADDRSTRLEN];
4485 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004486 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004487 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004488
Willy Tarreau59234e92008-11-30 23:51:27 +01004489 /* Note: we rely on the backend to get the header name to be used for
4490 * x-forwarded-for, because the header is really meant for the backends.
4491 * However, if the backend did not specify any option, we have to rely
4492 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004493 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004494 if (s->be->fwdfor_hdr_len) {
4495 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004496 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004497 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004498 len = sess->fe->fwdfor_hdr_len;
4499 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004500 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004501 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004502
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004503 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004504 goto return_bad_req;
4505 }
4506 }
4507
4508 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004509 * 10: add X-Original-To if either the frontend or the backend
4510 * asks for it.
4511 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004512 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004513
4514 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004515 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004516 /* Add an X-Original-To header unless the destination IP is
4517 * in the 'except' network range.
4518 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004519 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004520
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004521 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004522 ((!sess->fe->except_mask_to.s_addr ||
4523 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4524 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004525 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004526 (((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 +02004527 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004528 int len;
4529 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004530 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004531
4532 /* Note: we rely on the backend to get the header name to be used for
4533 * x-original-to, because the header is really meant for the backends.
4534 * However, if the backend did not specify any option, we have to rely
4535 * on the frontend's header name.
4536 */
4537 if (s->be->orgto_hdr_len) {
4538 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004539 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004540 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004541 len = sess->fe->orgto_hdr_len;
4542 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004543 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004544 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 +02004545
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004546 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004547 goto return_bad_req;
4548 }
4549 }
4550 }
4551
Willy Tarreau50fc7772012-11-11 22:19:57 +01004552 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4553 * If an "Upgrade" token is found, the header is left untouched in order not to have
4554 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4555 * "Upgrade" is present in the Connection header.
4556 */
4557 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4558 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004559 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004560 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004561 unsigned int want_flags = 0;
4562
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004563 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004564 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004565 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004566 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004567 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004568 want_flags |= TX_CON_CLO_SET;
4569 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004570 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004571 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004572 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004573 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004574 want_flags |= TX_CON_KAL_SET;
4575 }
4576
4577 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004578 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004579 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004580
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004581
Willy Tarreau522d6c02009-12-06 18:49:18 +01004582 /* If we have no server assigned yet and we're balancing on url_param
4583 * with a POST request, we may be interested in checking the body for
4584 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004585 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004586 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004587 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004588 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004589 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004590 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004591 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004592
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004593 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02004594 req->analysers &= ~AN_FLT_XFER_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004595 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004596#ifdef TCP_QUICKACK
4597 /* We expect some data from the client. Unless we know for sure
4598 * we already have a full request, we have to re-enable quick-ack
4599 * in case we previously disabled it, otherwise we might cause
4600 * the client to delay further data.
4601 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004602 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004603 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004604 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004605 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004606 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004607#endif
4608 }
Willy Tarreau03945942009-12-22 16:50:27 +01004609
Willy Tarreau59234e92008-11-30 23:51:27 +01004610 /*************************************************************
4611 * OK, that's finished for the headers. We have done what we *
4612 * could. Let's switch to the DATA state. *
4613 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004614 req->analyse_exp = TICK_ETERNITY;
4615 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004616
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004617 /* if the server closes the connection, we want to immediately react
4618 * and close the socket to save packets and syscalls.
4619 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004620 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004621 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004622
Willy Tarreau59234e92008-11-30 23:51:27 +01004623 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004624 /* OK let's go on with the BODY now */
4625 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004626
Willy Tarreau59234e92008-11-30 23:51:27 +01004627 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004628 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004629 /* we detected a parsing error. We want to archive this request
4630 * in the dedicated proxy area for later troubleshooting.
4631 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004632 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004633 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004634
Willy Tarreau59234e92008-11-30 23:51:27 +01004635 txn->req.msg_state = HTTP_MSG_ERROR;
4636 txn->status = 400;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004637 req->analysers &= AN_FLT_END;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004638 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004639
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004640 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004641 if (sess->listener->counters)
4642 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004643
Willy Tarreaue7dff022015-04-03 01:14:29 +02004644 if (!(s->flags & SF_ERR_MASK))
4645 s->flags |= SF_ERR_PRXCOND;
4646 if (!(s->flags & SF_FINST_MASK))
4647 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004648 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004649}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004650
Willy Tarreau60b85b02008-11-30 23:28:40 +01004651/* This function is an analyser which processes the HTTP tarpit. It always
4652 * returns zero, at the beginning because it prevents any other processing
4653 * from occurring, and at the end because it terminates the request.
4654 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004655int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004656{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004657 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004658
4659 /* This connection is being tarpitted. The CLIENT side has
4660 * already set the connect expiration date to the right
4661 * timeout. We just have to check that the client is still
4662 * there and that the timeout has not expired.
4663 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004664 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004665 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004666 !tick_is_expired(req->analyse_exp, now_ms))
4667 return 0;
4668
4669 /* We will set the queue timer to the time spent, just for
4670 * logging purposes. We fake a 500 server error, so that the
4671 * attacker will not suspect his connection has been tarpitted.
4672 * It will not cause trouble to the logs because we can exclude
4673 * the tarpitted connections by filtering on the 'PT' status flags.
4674 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004675 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4676
4677 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004678 if (!(req->flags & CF_READ_ERROR))
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004679 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004680
Christopher Fauletd7c91962015-04-30 11:48:27 +02004681 req->analysers &= AN_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004682 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004683
Willy Tarreaue7dff022015-04-03 01:14:29 +02004684 if (!(s->flags & SF_ERR_MASK))
4685 s->flags |= SF_ERR_PRXCOND;
4686 if (!(s->flags & SF_FINST_MASK))
4687 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004688 return 0;
4689}
4690
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004691/* This function is an analyser which waits for the HTTP request body. It waits
4692 * for either the buffer to be full, or the full advertised contents to have
4693 * reached the buffer. It must only be called after the standard HTTP request
4694 * processing has occurred, because it expects the request to be parsed and will
4695 * look for the Expect header. It may send a 100-Continue interim response. It
4696 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4697 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4698 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004699 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004700int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004701{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004702 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004703 struct http_txn *txn = s->txn;
4704 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004705
4706 /* We have to parse the HTTP request body to find any required data.
4707 * "balance url_param check_post" should have been the only way to get
4708 * into this. We were brought here after HTTP header analysis, so all
4709 * related structures are ready.
4710 */
4711
Willy Tarreau890988f2014-04-10 11:59:33 +02004712 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4713 /* This is the first call */
4714 if (msg->msg_state < HTTP_MSG_BODY)
4715 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004716
Willy Tarreau890988f2014-04-10 11:59:33 +02004717 if (msg->msg_state < HTTP_MSG_100_SENT) {
4718 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4719 * send an HTTP/1.1 100 Continue intermediate response.
4720 */
4721 if (msg->flags & HTTP_MSGF_VER_11) {
4722 struct hdr_ctx ctx;
4723 ctx.idx = 0;
4724 /* Expect is allowed in 1.1, look for it */
4725 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4726 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004727 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004728 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004729 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004730 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004731 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004732
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004733 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004734 * req->buf->p still points to the beginning of the message. We
4735 * must save the body in msg->next because it survives buffer
4736 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004737 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004738 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004739
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004740 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004741 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4742 else
4743 msg->msg_state = HTTP_MSG_DATA;
4744 }
4745
Willy Tarreau890988f2014-04-10 11:59:33 +02004746 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4747 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004748 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004749 goto missing_data;
4750
4751 /* OK we have everything we need now */
4752 goto http_end;
4753 }
4754
4755 /* OK here we're parsing a chunked-encoded message */
4756
Willy Tarreau522d6c02009-12-06 18:49:18 +01004757 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004758 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004759 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004760 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004761 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004762 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004763
Willy Tarreau115acb92009-12-26 13:56:06 +01004764 if (!ret)
4765 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004766 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004767 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004768 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004769 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01004770 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004771 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01004772 }
4773
Willy Tarreaud98cf932009-12-27 22:54:55 +01004774 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004775 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4776 * for at least a whole chunk or the whole content length bytes after
4777 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004778 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004779 if (msg->msg_state == HTTP_MSG_TRAILERS)
4780 goto http_end;
4781
Willy Tarreaue115b492015-05-01 23:05:14 +02004782 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004783 goto http_end;
4784
4785 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004786 /* we get here if we need to wait for more data. If the buffer is full,
4787 * we have the maximum we can expect.
4788 */
4789 if (buffer_full(req->buf, global.tune.maxrewrite))
4790 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004791
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004792 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004793 txn->status = 408;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004794 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004795
Willy Tarreaue7dff022015-04-03 01:14:29 +02004796 if (!(s->flags & SF_ERR_MASK))
4797 s->flags |= SF_ERR_CLITO;
4798 if (!(s->flags & SF_FINST_MASK))
4799 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004800 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004801 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004802
4803 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004804 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004805 /* Not enough data. We'll re-use the http-request
4806 * timeout here. Ideally, we should set the timeout
4807 * relative to the accept() date. We just set the
4808 * request timeout once at the beginning of the
4809 * request.
4810 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004811 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004812 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004813 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004814 return 0;
4815 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004816
4817 http_end:
4818 /* The situation will not evolve, so let's give up on the analysis. */
4819 s->logs.tv_request = now; /* update the request timer to reflect full request */
4820 req->analysers &= ~an_bit;
4821 req->analyse_exp = TICK_ETERNITY;
4822 return 1;
4823
4824 return_bad_req: /* let's centralize all bad requests */
4825 txn->req.msg_state = HTTP_MSG_ERROR;
4826 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004827 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004828
Willy Tarreaue7dff022015-04-03 01:14:29 +02004829 if (!(s->flags & SF_ERR_MASK))
4830 s->flags |= SF_ERR_PRXCOND;
4831 if (!(s->flags & SF_FINST_MASK))
4832 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004833
Willy Tarreau522d6c02009-12-06 18:49:18 +01004834 return_err_msg:
Christopher Fauletd7c91962015-04-30 11:48:27 +02004835 req->analysers &= AN_FLT_END;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004836 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004837 if (sess->listener->counters)
4838 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004839 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004840}
4841
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004842/* send a server's name with an outgoing request over an established connection.
4843 * Note: this function is designed to be called once the request has been scheduled
4844 * for being forwarded. This is the reason why it rewinds the buffer before
4845 * proceeding.
4846 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004847int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004848
4849 struct hdr_ctx ctx;
4850
Mark Lamourinec2247f02012-01-04 13:02:01 -05004851 char *hdr_name = be->server_id_hdr_name;
4852 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004853 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004854 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004855 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004856
William Lallemandd9e90662012-01-30 17:27:17 +01004857 ctx.idx = 0;
4858
Willy Tarreau211cdec2014-04-17 20:18:08 +02004859 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004860 if (old_o) {
4861 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004862 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004863 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004864 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004865 }
4866
Willy Tarreau9b28e032012-10-12 23:49:43 +02004867 old_i = chn->buf->i;
4868 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 -05004869 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004870 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004871 }
4872
4873 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004874 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004875 memcpy(hdr_val, hdr_name, hdr_name_len);
4876 hdr_val += hdr_name_len;
4877 *hdr_val++ = ':';
4878 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004879 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4880 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004881
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004882 if (old_o) {
4883 /* If this was a forwarded request, we must readjust the amount of
4884 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004885 * variations. Note that the current state is >= HTTP_MSG_BODY,
4886 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004887 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004888 old_o += chn->buf->i - old_i;
4889 b_adv(chn->buf, old_o);
4890 txn->req.next -= old_o;
4891 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004892 }
4893
Mark Lamourinec2247f02012-01-04 13:02:01 -05004894 return 0;
4895}
4896
Willy Tarreau610ecce2010-01-04 21:15:02 +01004897/* Terminate current transaction and prepare a new one. This is very tricky
4898 * right now but it works.
4899 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004900void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004901{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004902 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02004903 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01004904 struct proxy *be = s->be;
Willy Tarreau323a2d92015-08-04 19:00:17 +02004905 struct connection *srv_conn;
4906 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02004907 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01004908
Willy Tarreau610ecce2010-01-04 21:15:02 +01004909 /* FIXME: We need a more portable way of releasing a backend's and a
4910 * server's connections. We need a safer way to reinitialize buffer
4911 * flags. We also need a more accurate method for computing per-request
4912 * data.
4913 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02004914 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004915
Willy Tarreau4213a112013-12-15 10:25:42 +01004916 /* unless we're doing keep-alive, we want to quickly close the connection
4917 * to the server.
4918 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004919 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004920 !si_conn_ready(&s->si[1])) {
4921 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4922 si_shutr(&s->si[1]);
4923 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004924 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004925
Willy Tarreaue7dff022015-04-03 01:14:29 +02004926 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau858b1032015-12-07 17:04:59 +01004927 be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004928 if (unlikely(s->srv_conn))
4929 sess_change_server(s, NULL);
4930 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004931
4932 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02004933 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004934
Willy Tarreaueee5b512015-04-03 23:46:31 +02004935 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004936 int n;
4937
Willy Tarreaueee5b512015-04-03 23:46:31 +02004938 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004939 if (n < 1 || n > 5)
4940 n = 0;
4941
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004942 if (fe->mode == PR_MODE_HTTP) {
4943 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004944 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02004945 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01004946 (be->mode == PR_MODE_HTTP)) {
4947 be->be_counters.p.http.rsp[n]++;
4948 be->be_counters.p.http.cum_req++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004949 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004950 }
4951
4952 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004953 s->logs.bytes_in -= s->req.buf->i;
4954 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004955
4956 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004957 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02004958 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004959 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004960 s->do_log(s);
4961 }
4962
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004963 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02004964 stream_stop_content_counters(s);
4965 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004966
Willy Tarreau610ecce2010-01-04 21:15:02 +01004967 s->logs.accept_date = date; /* user-visible date for logging */
4968 s->logs.tv_accept = now; /* corrected date for internal use */
4969 tv_zero(&s->logs.tv_request);
4970 s->logs.t_queue = -1;
4971 s->logs.t_connect = -1;
4972 s->logs.t_data = -1;
4973 s->logs.t_close = 0;
4974 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4975 s->logs.srv_queue_size = 0; /* we will get this number soon */
4976
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004977 s->logs.bytes_in = s->req.total = s->req.buf->i;
4978 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004979
4980 if (s->pend_pos)
4981 pendconn_free(s->pend_pos);
4982
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004983 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004984 if (s->flags & SF_CURR_SESS) {
4985 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004986 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004987 }
Willy Tarreau858b1032015-12-07 17:04:59 +01004988 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004989 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004990 }
4991
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004992 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004993
Willy Tarreau4213a112013-12-15 10:25:42 +01004994 /* only release our endpoint if we don't intend to reuse the
4995 * connection.
4996 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004997 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004998 !si_conn_ready(&s->si[1])) {
4999 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005000 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005001 }
5002
Willy Tarreau350f4872014-11-28 14:42:25 +01005003 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5004 s->si[1].err_type = SI_ET_NONE;
5005 s->si[1].conn_retries = 0; /* used for logging too */
5006 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005007 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 +01005008 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);
5009 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 +02005010 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5011 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5012 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005013
Willy Tarreaueee5b512015-04-03 23:46:31 +02005014 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005015 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005016 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005017
5018 if (prev_status == 401 || prev_status == 407) {
5019 /* In HTTP keep-alive mode, if we receive a 401, we still have
5020 * a chance of being able to send the visitor again to the same
5021 * server over the same connection. This is required by some
5022 * broken protocols such as NTLM, and anyway whenever there is
5023 * an opportunity for sending the challenge to the proper place,
5024 * it's better to do it (at least it helps with debugging).
5025 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005026 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02005027 if (srv_conn)
5028 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01005029 }
5030
Willy Tarreau53f96852016-02-02 18:50:47 +01005031 /* Never ever allow to reuse a connection from a non-reuse backend */
5032 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
5033 srv_conn->flags |= CO_FL_PRIVATE;
5034
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005035 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005036 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005037
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005038 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005039 s->req.flags |= CF_NEVER_WAIT;
5040 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005041 }
5042
Willy Tarreau610ecce2010-01-04 21:15:02 +01005043 /* if the request buffer is not empty, it means we're
5044 * about to process another request, so send pending
5045 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005046 * Just don't do this if the buffer is close to be full,
5047 * because the request will wait for it to flush a little
5048 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005049 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005050 if (s->req.buf->i) {
5051 if (s->res.buf->o &&
5052 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5053 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5054 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005055 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005056
Willy Tarreau714ea782015-11-25 20:11:11 +01005057 /* we're removing the analysers, we MUST re-enable events detection.
5058 * We don't enable close on the response channel since it's either
5059 * already closed, or in keep-alive with an idle connection handler.
5060 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005061 channel_auto_read(&s->req);
5062 channel_auto_close(&s->req);
5063 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005064
Willy Tarreau1c59bd52015-11-02 20:20:11 +01005065 /* we're in keep-alive with an idle connection, monitor it if not already done */
5066 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02005067 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005068 if (!srv)
5069 si_idle_conn(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01005070 else if (srv_conn->flags & CO_FL_PRIVATE)
Willy Tarreau8dff9982015-08-04 20:45:52 +02005071 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02005072 else if (prev_flags & TX_NOT_FIRST)
5073 /* note: we check the request, not the connection, but
5074 * this is valid for strategies SAFE and AGGR, and in
5075 * case of ALWS, we don't care anyway.
5076 */
5077 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005078 else
5079 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005080 }
Willy Tarreau27375622013-12-17 00:00:28 +01005081
Christopher Faulet3e344292015-11-24 16:24:13 +01005082 if (HAS_FILTERS(s)) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02005083 s->req.analysers &= AN_FLT_END;
5084 s->res.analysers &= AN_FLT_END;
5085 }
Christopher Faulet3e344292015-11-24 16:24:13 +01005086 else {
5087 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
5088 s->res.analysers = 0;
5089 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005090}
5091
5092
5093/* This function updates the request state machine according to the response
5094 * state machine and buffer flags. It returns 1 if it changes anything (flag
5095 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5096 * it is only used to find when a request/response couple is complete. Both
5097 * this function and its equivalent should loop until both return zero. It
5098 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5099 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005100int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005101{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005102 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005103 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005104 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005105 unsigned int old_state = txn->req.msg_state;
5106
Willy Tarreau610ecce2010-01-04 21:15:02 +01005107 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5108 return 0;
5109
5110 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005111 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005112 * We can shut the read side unless we want to abort_on_close,
5113 * or we have a POST request. The issue with POST requests is
5114 * that some browsers still send a CRLF after the request, and
5115 * this CRLF must be read so that it does not remain in the kernel
5116 * buffers, otherwise a close could cause an RST on some systems
5117 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005118 * Note that if we're using keep-alive on the client side, we'd
5119 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005120 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005121 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005122 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005123 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5124 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5125 !(s->be->options & PR_O_ABRT_CLOSE) &&
5126 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005127 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005128
Willy Tarreau40f151a2012-12-20 12:10:09 +01005129 /* if the server closes the connection, we want to immediately react
5130 * and close the socket to save packets and syscalls.
5131 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005132 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005133
Willy Tarreau7f876a12015-11-18 11:59:55 +01005134 /* In any case we've finished parsing the request so we must
5135 * disable Nagle when sending data because 1) we're not going
5136 * to shut this side, and 2) the server is waiting for us to
5137 * send pending data.
5138 */
5139 chn->flags |= CF_NEVER_WAIT;
5140
Willy Tarreau610ecce2010-01-04 21:15:02 +01005141 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5142 goto wait_other_side;
5143
5144 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5145 /* The server has not finished to respond, so we
5146 * don't want to move in order not to upset it.
5147 */
5148 goto wait_other_side;
5149 }
5150
5151 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5152 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005153 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005154 txn->req.msg_state = HTTP_MSG_TUNNEL;
5155 goto wait_other_side;
5156 }
5157
5158 /* When we get here, it means that both the request and the
5159 * response have finished receiving. Depending on the connection
5160 * mode, we'll have to wait for the last bytes to leave in either
5161 * direction, and sometimes for a close to be effective.
5162 */
5163
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005164 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5165 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005166 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5167 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005168 }
5169 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5170 /* Option forceclose is set, or either side wants to close,
5171 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005172 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005173 * once both states are CLOSED.
5174 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005175 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5176 channel_shutr_now(chn);
5177 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005178 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005179 }
5180 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005181 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5182 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005183 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005184 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5185 channel_auto_read(chn);
5186 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005187 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005188 }
5189
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005190 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005191 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005192 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005193
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005194 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005195 txn->req.msg_state = HTTP_MSG_CLOSING;
5196 goto http_msg_closing;
5197 }
5198 else {
5199 txn->req.msg_state = HTTP_MSG_CLOSED;
5200 goto http_msg_closed;
5201 }
5202 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005203 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005204 }
5205
5206 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5207 http_msg_closing:
5208 /* nothing else to forward, just waiting for the output buffer
5209 * to be empty and for the shutw_now to take effect.
5210 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005211 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005212 txn->req.msg_state = HTTP_MSG_CLOSED;
5213 goto http_msg_closed;
5214 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005215 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005216 txn->req.msg_state = HTTP_MSG_ERROR;
5217 goto wait_other_side;
5218 }
5219 }
5220
5221 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5222 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005223 /* see above in MSG_DONE why we only do this in these states */
5224 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5225 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5226 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005227 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005228 goto wait_other_side;
5229 }
5230
5231 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005232 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005233}
5234
5235
5236/* This function updates the response state machine according to the request
5237 * state machine and buffer flags. It returns 1 if it changes anything (flag
5238 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5239 * it is only used to find when a request/response couple is complete. Both
5240 * this function and its equivalent should loop until both return zero. It
5241 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5242 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005243int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005244{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005245 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005246 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005247 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005248 unsigned int old_state = txn->rsp.msg_state;
5249
Willy Tarreau610ecce2010-01-04 21:15:02 +01005250 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5251 return 0;
5252
5253 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5254 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005255 * still monitor the server connection for a possible close
5256 * while the request is being uploaded, so we don't disable
5257 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005258 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005259 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005260
5261 if (txn->req.msg_state == HTTP_MSG_ERROR)
5262 goto wait_other_side;
5263
5264 if (txn->req.msg_state < HTTP_MSG_DONE) {
5265 /* The client seems to still be sending data, probably
5266 * because we got an error response during an upload.
5267 * We have the choice of either breaking the connection
5268 * or letting it pass through. Let's do the later.
5269 */
5270 goto wait_other_side;
5271 }
5272
5273 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5274 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005275 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005276 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005277 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005278 goto wait_other_side;
5279 }
5280
5281 /* When we get here, it means that both the request and the
5282 * response have finished receiving. Depending on the connection
5283 * mode, we'll have to wait for the last bytes to leave in either
5284 * direction, and sometimes for a close to be effective.
5285 */
5286
5287 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5288 /* Server-close mode : shut read and wait for the request
5289 * side to close its output buffer. The caller will detect
5290 * when we're in DONE and the other is in CLOSED and will
5291 * catch that for the final cleanup.
5292 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005293 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5294 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005295 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005296 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5297 /* Option forceclose is set, or either side wants to close,
5298 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005299 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005300 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005301 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005302 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5303 channel_shutr_now(chn);
5304 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005305 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005306 }
5307 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005308 /* The last possible modes are keep-alive and tunnel. Tunnel will
5309 * need to forward remaining data. Keep-alive will need to monitor
5310 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005311 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005312 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005313 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005314 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5315 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005316 }
5317
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005318 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005319 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005320 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005321 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5322 goto http_msg_closing;
5323 }
5324 else {
5325 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5326 goto http_msg_closed;
5327 }
5328 }
5329 goto wait_other_side;
5330 }
5331
5332 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5333 http_msg_closing:
5334 /* nothing else to forward, just waiting for the output buffer
5335 * to be empty and for the shutw_now to take effect.
5336 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005337 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005338 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5339 goto http_msg_closed;
5340 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005341 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005342 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005343 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005344 if (objt_server(s->target))
5345 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005346 goto wait_other_side;
5347 }
5348 }
5349
5350 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5351 http_msg_closed:
5352 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005353 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005354 channel_auto_close(chn);
5355 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005356 goto wait_other_side;
5357 }
5358
5359 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005360 /* We force the response to leave immediately if we're waiting for the
5361 * other side, since there is no pending shutdown to push it out.
5362 */
5363 if (!channel_is_empty(chn))
5364 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005365 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005366}
5367
5368
5369/* Resync the request and response state machines. Return 1 if either state
5370 * changes.
5371 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005372int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005373{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005374 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005375 int old_req_state = txn->req.msg_state;
5376 int old_res_state = txn->rsp.msg_state;
5377
Willy Tarreau610ecce2010-01-04 21:15:02 +01005378 http_sync_req_state(s);
5379 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005380 if (!http_sync_res_state(s))
5381 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005382 if (!http_sync_req_state(s))
5383 break;
5384 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005385
Willy Tarreau610ecce2010-01-04 21:15:02 +01005386 /* OK, both state machines agree on a compatible state.
5387 * There are a few cases we're interested in :
5388 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5389 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5390 * directions, so let's simply disable both analysers.
5391 * - HTTP_MSG_CLOSED on the response only means we must abort the
5392 * request.
5393 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5394 * with server-close mode means we've completed one request and we
5395 * must re-initialize the server connection.
5396 */
5397
5398 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5399 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5400 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5401 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02005402 s->req.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005403 channel_auto_close(&s->req);
5404 channel_auto_read(&s->req);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005405 s->res.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005406 channel_auto_close(&s->res);
5407 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005408 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005409 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005410 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005411 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005412 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02005413 s->res.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005414 channel_auto_close(&s->res);
5415 channel_auto_read(&s->res);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005416 s->req.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005417 channel_abort(&s->req);
5418 channel_auto_close(&s->req);
5419 channel_auto_read(&s->req);
5420 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005421 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005422 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5423 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005424 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005425 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5426 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5427 /* server-close/keep-alive: terminate this transaction,
5428 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005429 * a fresh-new transaction, but only once we're sure there's
5430 * enough room in the request and response buffer to process
5431 * another request. The request buffer must not hold any
5432 * pending output data and the request buffer must not have
5433 * output data occupying the reserve.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005434 */
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005435 if (s->req.buf->o)
5436 s->req.flags |= CF_WAKE_WRITE;
5437 else if (channel_congested(&s->res))
5438 s->res.flags |= CF_WAKE_WRITE;
5439 else
5440 http_end_txn_clean_session(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005441 }
5442
Willy Tarreau610ecce2010-01-04 21:15:02 +01005443 return txn->req.msg_state != old_req_state ||
5444 txn->rsp.msg_state != old_res_state;
5445}
5446
Willy Tarreaud98cf932009-12-27 22:54:55 +01005447/* This function is an analyser which forwards request body (including chunk
5448 * sizes if any). It is called as soon as we must forward, even if we forward
5449 * zero byte. The only situation where it must not be called is when we're in
5450 * tunnel mode and we want to forward till the close. It's used both to forward
5451 * remaining data and to resync after end of body. It expects the msg_state to
5452 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005453 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005454 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005455 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005456 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005457int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005458{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005459 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005460 struct http_txn *txn = s->txn;
5461 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01005462 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005463
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005464 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5465 return 0;
5466
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005467 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005468 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005469 /* Output closed while we were sending data. We must abort and
5470 * wake the other side up.
5471 */
5472 msg->msg_state = HTTP_MSG_ERROR;
5473 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005474 return 1;
5475 }
5476
Willy Tarreaud98cf932009-12-27 22:54:55 +01005477 /* Note that we don't have to send 100-continue back because we don't
5478 * need the data to complete our job, and it's up to the server to
5479 * decide whether to return 100, 417 or anything else in return of
5480 * an "Expect: 100-continue" header.
5481 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02005482 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005483 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5484 ? HTTP_MSG_CHUNK_SIZE
5485 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005486
5487 /* TODO/filters: when http-buffer-request option is set or if a
5488 * rule on url_param exists, the first chunk size could be
5489 * already parsed. In that case, msg->next is after the chunk
5490 * size (including the CRLF after the size). So this case should
5491 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005492 }
5493
Willy Tarreau7ba23542014-04-17 21:50:00 +02005494 /* Some post-connect processing might want us to refrain from starting to
5495 * forward data. Currently, the only reason for this is "balance url_param"
5496 * whichs need to parse/process the request after we've enabled forwarding.
5497 */
5498 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005499 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005500 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005501 req->flags |= CF_WAKE_CONNECT;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005502 goto missing_data_or_waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005503 }
5504 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5505 }
5506
Willy Tarreau80a92c02014-03-12 10:41:13 +01005507 /* in most states, we should abort in case of early close */
5508 channel_auto_close(req);
5509
Willy Tarreauefdf0942014-04-24 20:08:57 +02005510 if (req->to_forward) {
5511 /* We can't process the buffer's contents yet */
5512 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005513 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005514 }
5515
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005516 if (msg->msg_state < HTTP_MSG_DONE) {
5517 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5518 ? http_msg_forward_chunked_body(s, msg)
5519 : http_msg_forward_body(s, msg));
5520 if (!ret)
5521 goto missing_data_or_waiting;
5522 if (ret < 0)
5523 goto return_bad_req;
5524 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005525
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005526 /* other states, DONE...TUNNEL */
5527 /* we don't want to forward closes on DONE except in tunnel mode. */
5528 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5529 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005530
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005531 ret = msg->msg_state;
5532 if (http_resync_states(s)) {
5533 /* some state changes occurred, maybe the analyser
5534 * was disabled too. */
5535 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5536 if (req->flags & CF_SHUTW) {
5537 /* request errors are most likely due to the
5538 * server aborting the transfer. */
5539 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005540 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005541 if (msg->err_pos >= 0)
5542 http_capture_bad_message(&sess->fe->invalid_req, s, msg, ret, s->be);
5543 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005544 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005545 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005546 }
5547
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005548 /* If "option abortonclose" is set on the backend, we want to monitor
5549 * the client's connection and forward any shutdown notification to the
5550 * server, which will decide whether to close or to go on processing the
5551 * request. We only do that in tunnel mode, and not in other modes since
5552 * it can be abused to exhaust source ports. */
5553 if (s->be->options & PR_O_ABRT_CLOSE) {
5554 channel_auto_read(req);
5555 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5556 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5557 s->si[1].flags |= SI_FL_NOLINGER;
5558 channel_auto_close(req);
5559 }
5560 else if (s->txn->meth == HTTP_METH_POST) {
5561 /* POST requests may require to read extra CRLF sent by broken
5562 * browsers and which could cause an RST to be sent upon close
5563 * on some systems (eg: Linux). */
5564 channel_auto_read(req);
5565 }
5566 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005567
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005568 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005569 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005570 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005571 if (!(s->flags & SF_ERR_MASK))
5572 s->flags |= SF_ERR_CLICL;
5573 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005574 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005575 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005576 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005577 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005578 }
5579
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005580 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005581 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005582 if (objt_server(s->target))
5583 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005584
5585 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005586 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005587
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005588 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005589 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005590 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005591
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005592 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005593 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005594 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005595 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005596 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005597
Willy Tarreau5c620922011-05-11 19:56:11 +02005598 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005599 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005600 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005601 * modes are already handled by the stream sock layer. We must not do
5602 * this in content-length mode because it could present the MSG_MORE
5603 * flag with the last block of forwarded data, which would cause an
5604 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005605 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005606 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005607 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005608
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005609 return 0;
5610
Willy Tarreaud98cf932009-12-27 22:54:55 +01005611 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005612 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005613 if (sess->listener->counters)
5614 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005615
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005616 return_bad_req_stats_ok:
5617 txn->req.msg_state = HTTP_MSG_ERROR;
5618 if (txn->status) {
5619 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005620 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005621 } else {
5622 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005623 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005624 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005625 req->analysers &= AN_FLT_END;
5626 s->res.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005627
Willy Tarreaue7dff022015-04-03 01:14:29 +02005628 if (!(s->flags & SF_ERR_MASK))
5629 s->flags |= SF_ERR_PRXCOND;
5630 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005631 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005632 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005633 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005634 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005635 }
5636 return 0;
5637
5638 aborted_xfer:
5639 txn->req.msg_state = HTTP_MSG_ERROR;
5640 if (txn->status) {
5641 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005642 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005643 } else {
5644 txn->status = 502;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005645 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005646 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005647 req->analysers &= AN_FLT_END;
5648 s->res.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005649
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005650 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005651 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005652 if (objt_server(s->target))
5653 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005654
Willy Tarreaue7dff022015-04-03 01:14:29 +02005655 if (!(s->flags & SF_ERR_MASK))
5656 s->flags |= SF_ERR_SRVCL;
5657 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005658 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005659 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005660 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005661 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005662 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005663 return 0;
5664}
5665
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005666/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5667 * processing can continue on next analysers, or zero if it either needs more
5668 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005669 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005670 * when it has nothing left to do, and may remove any analyser when it wants to
5671 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005672 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005673int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005674{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005675 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005676 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005677 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005678 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005679 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005680 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005681 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005682
Willy Tarreau87b09662015-04-03 00:22:06 +02005683 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 +02005684 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005685 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005686 rep,
5687 rep->rex, rep->wex,
5688 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005689 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005690 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005691
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005692 /*
5693 * Now parse the partial (or complete) lines.
5694 * We will check the response syntax, and also join multi-line
5695 * headers. An index of all the lines will be elaborated while
5696 * parsing.
5697 *
5698 * For the parsing, we use a 28 states FSM.
5699 *
5700 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005701 * rep->buf->p = beginning of response
5702 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5703 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005704 * msg->eol = end of current header or line (LF or CRLF)
5705 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005706 */
5707
Willy Tarreau628c40c2014-04-24 19:11:26 +02005708 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005709 /* There's a protected area at the end of the buffer for rewriting
5710 * purposes. We don't want to start to parse the request if the
5711 * protected area is affected, because we may have to move processed
5712 * data later, which is much more complicated.
5713 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005714 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005715 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005716 /* some data has still not left the buffer, wake us once that's done */
5717 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5718 goto abort_response;
5719 channel_dont_close(rep);
5720 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005721 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005722 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005723 }
5724
Willy Tarreau379357a2013-06-08 12:55:46 +02005725 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5726 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5727 buffer_slow_realign(rep->buf);
5728
Willy Tarreau9b28e032012-10-12 23:49:43 +02005729 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005730 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005731 }
5732
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005733 /* 1: we might have to print this header in debug mode */
5734 if (unlikely((global.mode & MODE_DEBUG) &&
5735 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005736 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005737 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005738
Willy Tarreau9b28e032012-10-12 23:49:43 +02005739 sol = rep->buf->p;
5740 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005741 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005742
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005743 sol += hdr_idx_first_pos(&txn->hdr_idx);
5744 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005745
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005746 while (cur_idx) {
5747 eol = sol + txn->hdr_idx.v[cur_idx].len;
5748 debug_hdr("srvhdr", s, sol, eol);
5749 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5750 cur_idx = txn->hdr_idx.v[cur_idx].next;
5751 }
5752 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005753
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005754 /*
5755 * Now we quickly check if we have found a full valid response.
5756 * If not so, we check the FD and buffer states before leaving.
5757 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005758 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005759 * responses are checked first.
5760 *
5761 * Depending on whether the client is still there or not, we
5762 * may send an error response back or not. Note that normally
5763 * we should only check for HTTP status there, and check I/O
5764 * errors somewhere else.
5765 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005766
Willy Tarreau655dce92009-11-08 13:10:58 +01005767 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005768 /* Invalid response */
5769 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5770 /* we detected a parsing error. We want to archive this response
5771 * in the dedicated proxy area for later troubleshooting.
5772 */
5773 hdr_response_bad:
5774 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005775 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005776
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005777 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005778 if (objt_server(s->target)) {
5779 objt_server(s->target)->counters.failed_resp++;
5780 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005781 }
Willy Tarreau64648412010-03-05 10:41:54 +01005782 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005783 channel_auto_close(rep);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005784 rep->analysers &= AN_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005785 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005786 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005787 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005788 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005789
Willy Tarreaue7dff022015-04-03 01:14:29 +02005790 if (!(s->flags & SF_ERR_MASK))
5791 s->flags |= SF_ERR_PRXCOND;
5792 if (!(s->flags & SF_FINST_MASK))
5793 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005794
5795 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005796 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005797
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005798 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005799 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005800 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005801 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005802 goto hdr_response_bad;
5803 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005804
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005805 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005806 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005807 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005808 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005809 else if (txn->flags & TX_NOT_FIRST)
5810 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005811
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005812 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005813 if (objt_server(s->target)) {
5814 objt_server(s->target)->counters.failed_resp++;
5815 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005816 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005817
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005818 channel_auto_close(rep);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005819 rep->analysers &= AN_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005820 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005821 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005822 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005823 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005824
Willy Tarreaue7dff022015-04-03 01:14:29 +02005825 if (!(s->flags & SF_ERR_MASK))
5826 s->flags |= SF_ERR_SRVCL;
5827 if (!(s->flags & SF_FINST_MASK))
5828 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005829 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005830 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005831
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005832 /* read timeout : return a 504 to the client. */
5833 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005834 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005835 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005836
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005837 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005838 if (objt_server(s->target)) {
5839 objt_server(s->target)->counters.failed_resp++;
5840 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005841 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005842
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005843 channel_auto_close(rep);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005844 rep->analysers &= AN_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005845 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005846 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005847 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005848 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005849
Willy Tarreaue7dff022015-04-03 01:14:29 +02005850 if (!(s->flags & SF_ERR_MASK))
5851 s->flags |= SF_ERR_SRVTO;
5852 if (!(s->flags & SF_FINST_MASK))
5853 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005854 return 0;
5855 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005856
Willy Tarreauf003d372012-11-26 13:35:37 +01005857 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005858 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005859 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01005860 s->be->be_counters.cli_aborts++;
5861 if (objt_server(s->target))
5862 objt_server(s->target)->counters.cli_aborts++;
5863
Christopher Fauletd7c91962015-04-30 11:48:27 +02005864 rep->analysers &= AN_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01005865 channel_auto_close(rep);
5866
5867 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005868 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005869 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01005870
Willy Tarreaue7dff022015-04-03 01:14:29 +02005871 if (!(s->flags & SF_ERR_MASK))
5872 s->flags |= SF_ERR_CLICL;
5873 if (!(s->flags & SF_FINST_MASK))
5874 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01005875
Willy Tarreau87b09662015-04-03 00:22:06 +02005876 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01005877 return 0;
5878 }
5879
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005880 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005881 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005882 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005883 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005884 else if (txn->flags & TX_NOT_FIRST)
5885 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005886
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005887 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005888 if (objt_server(s->target)) {
5889 objt_server(s->target)->counters.failed_resp++;
5890 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005891 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005892
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005893 channel_auto_close(rep);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005894 rep->analysers &= AN_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005895 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005896 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005897 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005898 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005899
Willy Tarreaue7dff022015-04-03 01:14:29 +02005900 if (!(s->flags & SF_ERR_MASK))
5901 s->flags |= SF_ERR_SRVCL;
5902 if (!(s->flags & SF_FINST_MASK))
5903 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005904 return 0;
5905 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005906
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005907 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005908 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005909 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005910 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005911 else if (txn->flags & TX_NOT_FIRST)
5912 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005913
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005914 s->be->be_counters.failed_resp++;
Christopher Fauletd7c91962015-04-30 11:48:27 +02005915 rep->analysers &= AN_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005916 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005917
Willy Tarreaue7dff022015-04-03 01:14:29 +02005918 if (!(s->flags & SF_ERR_MASK))
5919 s->flags |= SF_ERR_CLICL;
5920 if (!(s->flags & SF_FINST_MASK))
5921 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005922
Willy Tarreau87b09662015-04-03 00:22:06 +02005923 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005924 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005925 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005926
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005927 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005928 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005929 return 0;
5930 }
5931
5932 /* More interesting part now : we know that we have a complete
5933 * response which at least looks like HTTP. We have an indicator
5934 * of each header's length, so we can parse them quickly.
5935 */
5936
5937 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005938 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005939
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005940 /*
5941 * 1: get the status code
5942 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005943 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005944 if (n < 1 || n > 5)
5945 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005946 /* when the client triggers a 4xx from the server, it's most often due
5947 * to a missing object or permission. These events should be tracked
5948 * because if they happen often, it may indicate a brute force or a
5949 * vulnerability scan.
5950 */
5951 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02005952 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02005953
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005954 if (objt_server(s->target))
5955 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005956
Willy Tarreau91852eb2015-05-01 13:26:00 +02005957 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
5958 * exactly one digit "." one digit. This check may be disabled using
5959 * option accept-invalid-http-response.
5960 */
5961 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
5962 if (msg->sl.st.v_l != 8) {
5963 msg->err_pos = 0;
5964 goto hdr_response_bad;
5965 }
5966
5967 if (rep->buf->p[4] != '/' ||
5968 !isdigit((unsigned char)rep->buf->p[5]) ||
5969 rep->buf->p[6] != '.' ||
5970 !isdigit((unsigned char)rep->buf->p[7])) {
5971 msg->err_pos = 4;
5972 goto hdr_response_bad;
5973 }
5974 }
5975
Willy Tarreau5b154472009-12-21 20:11:07 +01005976 /* check if the response is HTTP/1.1 or above */
5977 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005978 ((rep->buf->p[5] > '1') ||
5979 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005980 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005981
5982 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005983 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 +01005984
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005985 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005986 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005987
Willy Tarreau9b28e032012-10-12 23:49:43 +02005988 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005989
Willy Tarreau39650402010-03-15 19:44:39 +01005990 /* Adjust server's health based on status code. Note: status codes 501
5991 * and 505 are triggered on demand by client request, so we must not
5992 * count them as server failures.
5993 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005994 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005995 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005996 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005997 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005998 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005999 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006000
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006001 /*
6002 * 2: check for cacheability.
6003 */
6004
6005 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006006 case 100:
6007 /*
6008 * We may be facing a 100-continue response, in which case this
6009 * is not the right response, and we're waiting for the next one.
6010 * Let's allow this response to go to the client and wait for the
6011 * next one.
6012 */
6013 hdr_idx_init(&txn->hdr_idx);
6014 msg->next -= channel_forward(rep, msg->next);
6015 msg->msg_state = HTTP_MSG_RPBEFORE;
6016 txn->status = 0;
6017 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01006018 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02006019 goto next_one;
6020
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006021 case 200:
6022 case 203:
6023 case 206:
6024 case 300:
6025 case 301:
6026 case 410:
6027 /* RFC2616 @13.4:
6028 * "A response received with a status code of
6029 * 200, 203, 206, 300, 301 or 410 MAY be stored
6030 * by a cache (...) unless a cache-control
6031 * directive prohibits caching."
6032 *
6033 * RFC2616 @9.5: POST method :
6034 * "Responses to this method are not cacheable,
6035 * unless the response includes appropriate
6036 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006037 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006038 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006039 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006040 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6041 break;
6042 default:
6043 break;
6044 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006045
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006046 /*
6047 * 3: we may need to capture headers
6048 */
6049 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006050 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006051 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006052 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006053
Willy Tarreau557f1992015-05-01 10:05:17 +02006054 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6055 * by RFC7230#3.3.3 :
6056 *
6057 * The length of a message body is determined by one of the following
6058 * (in order of precedence):
6059 *
6060 * 1. Any response to a HEAD request and any response with a 1xx
6061 * (Informational), 204 (No Content), or 304 (Not Modified) status
6062 * code is always terminated by the first empty line after the
6063 * header fields, regardless of the header fields present in the
6064 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006065 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006066 * 2. Any 2xx (Successful) response to a CONNECT request implies that
6067 * the connection will become a tunnel immediately after the empty
6068 * line that concludes the header fields. A client MUST ignore any
6069 * Content-Length or Transfer-Encoding header fields received in
6070 * such a message.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006071 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006072 * 3. If a Transfer-Encoding header field is present and the chunked
6073 * transfer coding (Section 4.1) is the final encoding, the message
6074 * body length is determined by reading and decoding the chunked
6075 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006076 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006077 * If a Transfer-Encoding header field is present in a response and
6078 * the chunked transfer coding is not the final encoding, the
6079 * message body length is determined by reading the connection until
6080 * it is closed by the server. If a Transfer-Encoding header field
6081 * is present in a request and the chunked transfer coding is not
6082 * the final encoding, the message body length cannot be determined
6083 * reliably; the server MUST respond with the 400 (Bad Request)
6084 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006085 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006086 * If a message is received with both a Transfer-Encoding and a
6087 * Content-Length header field, the Transfer-Encoding overrides the
6088 * Content-Length. Such a message might indicate an attempt to
6089 * perform request smuggling (Section 9.5) or response splitting
6090 * (Section 9.4) and ought to be handled as an error. A sender MUST
6091 * remove the received Content-Length field prior to forwarding such
6092 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006093 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006094 * 4. If a message is received without Transfer-Encoding and with
6095 * either multiple Content-Length header fields having differing
6096 * field-values or a single Content-Length header field having an
6097 * invalid value, then the message framing is invalid and the
6098 * recipient MUST treat it as an unrecoverable error. If this is a
6099 * request message, the server MUST respond with a 400 (Bad Request)
6100 * status code and then close the connection. If this is a response
6101 * message received by a proxy, the proxy MUST close the connection
6102 * to the server, discard the received response, and send a 502 (Bad
6103 * Gateway) response to the client. If this is a response message
6104 * received by a user agent, the user agent MUST close the
6105 * connection to the server and discard the received response.
6106 *
6107 * 5. If a valid Content-Length header field is present without
6108 * Transfer-Encoding, its decimal value defines the expected message
6109 * body length in octets. If the sender closes the connection or
6110 * the recipient times out before the indicated number of octets are
6111 * received, the recipient MUST consider the message to be
6112 * incomplete and close the connection.
6113 *
6114 * 6. If this is a request message and none of the above are true, then
6115 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006116 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006117 * 7. Otherwise, this is a response message without a declared message
6118 * body length, so the message body length is determined by the
6119 * number of octets received prior to the server closing the
6120 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006121 */
6122
6123 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006124 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006125 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006126 * FIXME: should we parse anyway and return an error on chunked encoding ?
6127 */
6128 if (txn->meth == HTTP_METH_HEAD ||
6129 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006130 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006131 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006132 goto skip_content_length;
6133 }
6134
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006135 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006136 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006137 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006138 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006139 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6140 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006141 /* bad transfer-encoding (chunked followed by something else) */
6142 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006143 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006144 break;
6145 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006146 }
6147
Willy Tarreau1c913912015-04-30 10:57:51 +02006148 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006149 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006150 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006151 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6152 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6153 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006154 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006155 signed long long cl;
6156
Willy Tarreauad14f752011-09-02 20:33:27 +02006157 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006158 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006159 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006160 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006161
Willy Tarreauad14f752011-09-02 20:33:27 +02006162 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006163 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006164 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006165 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006166
Willy Tarreauad14f752011-09-02 20:33:27 +02006167 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006168 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006169 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006170 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006171
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006172 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006173 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006174 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006175 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006176
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006177 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006178 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006179 }
6180
6181skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006182 /* Now we have to check if we need to modify the Connection header.
6183 * This is more difficult on the response than it is on the request,
6184 * because we can have two different HTTP versions and we don't know
6185 * how the client will interprete a response. For instance, let's say
6186 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6187 * HTTP/1.1 response without any header. Maybe it will bound itself to
6188 * HTTP/1.0 because it only knows about it, and will consider the lack
6189 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6190 * the lack of header as a keep-alive. Thus we will use two flags
6191 * indicating how a request MAY be understood by the client. In case
6192 * of multiple possibilities, we'll fix the header to be explicit. If
6193 * ambiguous cases such as both close and keepalive are seen, then we
6194 * will fall back to explicit close. Note that we won't take risks with
6195 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006196 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006197 */
6198
Willy Tarreaudc008c52010-02-01 16:20:08 +01006199 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6200 txn->status == 101)) {
6201 /* Either we've established an explicit tunnel, or we're
6202 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006203 * to understand the next protocols. We have to switch to tunnel
6204 * mode, so that we transfer the request and responses then let
6205 * this protocol pass unmodified. When we later implement specific
6206 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006207 * header which contains information about that protocol for
6208 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006209 */
6210 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6211 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006212 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6213 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
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 Tarreau60466522010-01-18 19:08:45 +01006216 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006217
Willy Tarreau70dffda2014-01-30 03:07:23 +01006218 /* this situation happens when combining pretend-keepalive with httpclose. */
6219 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006220 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006221 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006222 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6223
Willy Tarreau60466522010-01-18 19:08:45 +01006224 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006225 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006226 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6227 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006228
Willy Tarreau60466522010-01-18 19:08:45 +01006229 /* now adjust header transformations depending on current state */
6230 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6231 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6232 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006233 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006234 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006235 }
Willy Tarreau60466522010-01-18 19:08:45 +01006236 else { /* SCL / KAL */
6237 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006238 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006239 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006240 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006241
Willy Tarreau60466522010-01-18 19:08:45 +01006242 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006243 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006244
Willy Tarreau60466522010-01-18 19:08:45 +01006245 /* Some keep-alive responses are converted to Server-close if
6246 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006247 */
Willy Tarreau60466522010-01-18 19:08:45 +01006248 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6249 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006250 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006251 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006252 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006253 }
6254
Willy Tarreau7959a552013-09-23 16:44:27 +02006255 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006256 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006257
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006258 /* end of job, return OK */
6259 rep->analysers &= ~an_bit;
6260 rep->analyse_exp = TICK_ETERNITY;
6261 channel_auto_close(rep);
6262 return 1;
6263
6264 abort_keep_alive:
6265 /* A keep-alive request to the server failed on a network error.
6266 * The client is required to retry. We need to close without returning
6267 * any other information so that the client retries.
6268 */
6269 txn->status = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +02006270 rep->analysers &= AN_FLT_END;
6271 s->req.analysers &= AN_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006272 channel_auto_close(rep);
6273 s->logs.logwait = 0;
6274 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006275 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006276 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006277 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006278 return 0;
6279}
6280
6281/* This function performs all the processing enabled for the current response.
6282 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006283 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006284 * other functions. It works like process_request (see indications above).
6285 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006286int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006287{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006288 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006289 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006290 struct http_msg *msg = &txn->rsp;
6291 struct proxy *cur_proxy;
6292 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006293 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006294
Willy Tarreau87b09662015-04-03 00:22:06 +02006295 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 +02006296 now_ms, __FUNCTION__,
6297 s,
6298 rep,
6299 rep->rex, rep->wex,
6300 rep->flags,
6301 rep->buf->i,
6302 rep->analysers);
6303
6304 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6305 return 0;
6306
Willy Tarreau70730dd2014-04-24 18:06:27 +02006307 /* The stats applet needs to adjust the Connection header but we don't
6308 * apply any filter there.
6309 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006310 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6311 rep->analysers &= ~an_bit;
6312 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006313 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006314 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006315
Willy Tarreau58975672014-04-24 21:13:57 +02006316 /*
6317 * We will have to evaluate the filters.
6318 * As opposed to version 1.2, now they will be evaluated in the
6319 * filters order and not in the header order. This means that
6320 * each filter has to be validated among all headers.
6321 *
6322 * Filters are tried with ->be first, then with ->fe if it is
6323 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006324 *
6325 * Maybe we are in resume condiion. In this case I choose the
6326 * "struct proxy" which contains the rule list matching the resume
6327 * pointer. If none of theses "struct proxy" match, I initialise
6328 * the process with the first one.
6329 *
6330 * In fact, I check only correspondance betwwen the current list
6331 * pointer and the ->fe rule list. If it doesn't match, I initialize
6332 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006333 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006334 if (s->current_rule_list == &sess->fe->http_res_rules)
6335 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006336 else
6337 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006338 while (1) {
6339 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006340
Willy Tarreau58975672014-04-24 21:13:57 +02006341 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02006342 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006343 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006344
Willy Tarreau51d861a2015-05-22 17:30:48 +02006345 if (ret == HTTP_RULE_RES_BADREQ)
6346 goto return_srv_prx_502;
6347
6348 if (ret == HTTP_RULE_RES_DONE) {
6349 rep->analysers &= ~an_bit;
6350 rep->analyse_exp = TICK_ETERNITY;
6351 return 1;
6352 }
6353 }
6354
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006355 /* we need to be called again. */
6356 if (ret == HTTP_RULE_RES_YIELD) {
6357 channel_dont_close(rep);
6358 return 0;
6359 }
6360
Willy Tarreau58975672014-04-24 21:13:57 +02006361 /* try headers filters */
6362 if (rule_set->rsp_exp != NULL) {
6363 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6364 return_bad_resp:
6365 if (objt_server(s->target)) {
6366 objt_server(s->target)->counters.failed_resp++;
6367 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006368 }
Willy Tarreau58975672014-04-24 21:13:57 +02006369 s->be->be_counters.failed_resp++;
6370 return_srv_prx_502:
Christopher Fauletd7c91962015-04-30 11:48:27 +02006371 rep->analysers &= AN_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02006372 txn->status = 502;
6373 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006374 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006375 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006376 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006377 if (!(s->flags & SF_ERR_MASK))
6378 s->flags |= SF_ERR_PRXCOND;
6379 if (!(s->flags & SF_FINST_MASK))
6380 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006381 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006382 }
Willy Tarreau58975672014-04-24 21:13:57 +02006383 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006384
Willy Tarreau58975672014-04-24 21:13:57 +02006385 /* has the response been denied ? */
6386 if (txn->flags & TX_SVDENY) {
6387 if (objt_server(s->target))
6388 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006389
Willy Tarreau58975672014-04-24 21:13:57 +02006390 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006391 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006392 if (sess->listener->counters)
6393 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006394
Willy Tarreau58975672014-04-24 21:13:57 +02006395 goto return_srv_prx_502;
6396 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006397
Willy Tarreau58975672014-04-24 21:13:57 +02006398 /* add response headers from the rule sets in the same order */
6399 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006400 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006401 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006402 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006403 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006404 ret = acl_pass(ret);
6405 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6406 ret = !ret;
6407 if (!ret)
6408 continue;
6409 }
6410 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6411 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006412 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006413
Willy Tarreau58975672014-04-24 21:13:57 +02006414 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006415 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006416 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006417 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006418 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006419
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006420 /* After this point, this anayzer can't return yield, so we can
6421 * remove the bit corresponding to this analyzer from the list.
6422 *
6423 * Note that the intermediate returns and goto found previously
6424 * reset the analyzers.
6425 */
6426 rep->analysers &= ~an_bit;
6427 rep->analyse_exp = TICK_ETERNITY;
6428
Willy Tarreau58975672014-04-24 21:13:57 +02006429 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006430 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006431 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006432
Willy Tarreau58975672014-04-24 21:13:57 +02006433 /*
6434 * Now check for a server cookie.
6435 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006436 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02006437 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006438
Willy Tarreau58975672014-04-24 21:13:57 +02006439 /*
6440 * Check for cache-control or pragma headers if required.
6441 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006442 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 +02006443 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006444
Willy Tarreau58975672014-04-24 21:13:57 +02006445 /*
6446 * Add server cookie in the response if needed
6447 */
6448 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6449 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006450 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006451 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6452 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6453 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6454 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6455 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006456 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006457 /* the server is known, it's not the one the client requested, or the
6458 * cookie's last seen date needs to be refreshed. We have to
6459 * insert a set-cookie here, except if we want to insert only on POST
6460 * requests and this one isn't. Note that servers which don't have cookies
6461 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006462 */
Willy Tarreau58975672014-04-24 21:13:57 +02006463 if (!objt_server(s->target)->cookie) {
6464 chunk_printf(&trash,
6465 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6466 s->be->cookie_name);
6467 }
6468 else {
6469 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006470
Willy Tarreau58975672014-04-24 21:13:57 +02006471 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6472 /* emit last_date, which is mandatory */
6473 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6474 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6475 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006476
Willy Tarreau58975672014-04-24 21:13:57 +02006477 if (s->be->cookie_maxlife) {
6478 /* emit first_date, which is either the original one or
6479 * the current date.
6480 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006481 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006482 s30tob64(txn->cookie_first_date ?
6483 txn->cookie_first_date >> 2 :
6484 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006485 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006486 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006487 }
Willy Tarreau58975672014-04-24 21:13:57 +02006488 chunk_appendf(&trash, "; path=/");
6489 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006490
Willy Tarreau58975672014-04-24 21:13:57 +02006491 if (s->be->cookie_domain)
6492 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006493
Willy Tarreau58975672014-04-24 21:13:57 +02006494 if (s->be->ck_opts & PR_CK_HTTPONLY)
6495 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006496
Willy Tarreau58975672014-04-24 21:13:57 +02006497 if (s->be->ck_opts & PR_CK_SECURE)
6498 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006499
Willy Tarreau58975672014-04-24 21:13:57 +02006500 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6501 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006502
Willy Tarreau58975672014-04-24 21:13:57 +02006503 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006504 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006505 /* the server did not change, only the date was updated */
6506 txn->flags |= TX_SCK_UPDATED;
6507 else
6508 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006509
Willy Tarreau58975672014-04-24 21:13:57 +02006510 /* Here, we will tell an eventual cache on the client side that we don't
6511 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6512 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6513 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006514 */
Willy Tarreau58975672014-04-24 21:13:57 +02006515 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006516
Willy Tarreau58975672014-04-24 21:13:57 +02006517 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006518
Willy Tarreau58975672014-04-24 21:13:57 +02006519 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6520 "Cache-control: private", 22) < 0))
6521 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006522 }
Willy Tarreau58975672014-04-24 21:13:57 +02006523 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006524
Willy Tarreau58975672014-04-24 21:13:57 +02006525 /*
6526 * Check if result will be cacheable with a cookie.
6527 * We'll block the response if security checks have caught
6528 * nasty things such as a cacheable cookie.
6529 */
6530 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6531 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6532 (s->be->options & PR_O_CHK_CACHE)) {
6533 /* we're in presence of a cacheable response containing
6534 * a set-cookie header. We'll block it as requested by
6535 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006536 */
Willy Tarreau58975672014-04-24 21:13:57 +02006537 if (objt_server(s->target))
6538 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006539
Willy Tarreau58975672014-04-24 21:13:57 +02006540 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006541 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006542 if (sess->listener->counters)
6543 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006544
Willy Tarreau58975672014-04-24 21:13:57 +02006545 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6546 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6547 send_log(s->be, LOG_ALERT,
6548 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6549 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6550 goto return_srv_prx_502;
6551 }
Willy Tarreau03945942009-12-22 16:50:27 +01006552
Willy Tarreau70730dd2014-04-24 18:06:27 +02006553 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006554 /*
6555 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6556 * If an "Upgrade" token is found, the header is left untouched in order
6557 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006558 * if anything but "Upgrade" is present in the Connection header. We don't
6559 * want to touch any 101 response either since it's switching to another
6560 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006561 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006562 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006563 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006564 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006565 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6566 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006567
Willy Tarreau58975672014-04-24 21:13:57 +02006568 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6569 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6570 /* we want a keep-alive response here. Keep-alive header
6571 * required if either side is not 1.1.
6572 */
6573 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6574 want_flags |= TX_CON_KAL_SET;
6575 }
6576 else {
6577 /* we want a close response here. Close header required if
6578 * the server is 1.1, regardless of the client.
6579 */
6580 if (msg->flags & HTTP_MSGF_VER_11)
6581 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006582 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006583
Willy Tarreau58975672014-04-24 21:13:57 +02006584 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6585 http_change_connection_header(txn, msg, want_flags);
6586 }
6587
6588 skip_header_mangling:
Christopher Faulet3e344292015-11-24 16:24:13 +01006589 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_FILTERS(s) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006590 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
6591 rep->analysers &= ~AN_FLT_XFER_DATA;
Willy Tarreau58975672014-04-24 21:13:57 +02006592 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Christopher Fauletd7c91962015-04-30 11:48:27 +02006593 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006594
Willy Tarreau58975672014-04-24 21:13:57 +02006595 /* if the user wants to log as soon as possible, without counting
6596 * bytes from the server, then this is the right moment. We have
6597 * to temporarily assign bytes_out to log what we currently have.
6598 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006599 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006600 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6601 s->logs.bytes_out = txn->rsp.eoh;
6602 s->do_log(s);
6603 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006604 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006605 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006606}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006607
Willy Tarreaud98cf932009-12-27 22:54:55 +01006608/* This function is an analyser which forwards response body (including chunk
6609 * sizes if any). It is called as soon as we must forward, even if we forward
6610 * zero byte. The only situation where it must not be called is when we're in
6611 * tunnel mode and we want to forward till the close. It's used both to forward
6612 * remaining data and to resync after end of body. It expects the msg_state to
6613 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006614 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006615 *
6616 * It is capable of compressing response data both in content-length mode and
6617 * in chunked mode. The state machines follows different flows depending on
6618 * whether content-length and chunked modes are used, since there are no
6619 * trailers in content-length :
6620 *
6621 * chk-mode cl-mode
6622 * ,----- BODY -----.
6623 * / \
6624 * V size > 0 V chk-mode
6625 * .--> SIZE -------------> DATA -------------> CRLF
6626 * | | size == 0 | last byte |
6627 * | v final crlf v inspected |
6628 * | TRAILERS -----------> DONE |
6629 * | |
6630 * `----------------------------------------------'
6631 *
6632 * Compression only happens in the DATA state, and must be flushed in final
6633 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6634 * is performed at once on final states for all bytes parsed, or when leaving
6635 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006636 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006637int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006638{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006639 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006640 struct http_txn *txn = s->txn;
6641 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01006642 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006643
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006644 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6645 return 0;
6646
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006647 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006648 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006649 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006650 /* Output closed while we were sending data. We must abort and
6651 * wake the other side up.
6652 */
6653 msg->msg_state = HTTP_MSG_ERROR;
6654 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006655 return 1;
6656 }
6657
Willy Tarreau4fe41902010-06-07 22:27:41 +02006658 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006659 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006660
Christopher Fauletd7c91962015-04-30 11:48:27 +02006661 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006662 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
6663 ? HTTP_MSG_CHUNK_SIZE
6664 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006665 }
6666
Willy Tarreauefdf0942014-04-24 20:08:57 +02006667 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006668 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02006669 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006670 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006671 }
6672
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006673 if (msg->msg_state < HTTP_MSG_DONE) {
6674 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6675 ? http_msg_forward_chunked_body(s, msg)
6676 : http_msg_forward_body(s, msg));
6677 if (!ret)
6678 goto missing_data_or_waiting;
6679 if (ret < 0)
6680 goto return_bad_res;
6681 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006682
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006683 /* other states, DONE...TUNNEL */
6684 /* for keep-alive we don't want to forward closes on DONE */
6685 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6686 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6687 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006688
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006689 ret = msg->msg_state;
6690 if (http_resync_states(s)) {
6691 /* some state changes occurred, maybe the analyser was disabled
6692 * too. */
6693 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6694 if (res->flags & CF_SHUTW) {
6695 /* response errors are most likely due to the
6696 * client aborting the transfer. */
6697 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006698 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006699 if (msg->err_pos >= 0)
6700 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, strm_fe(s));
6701 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006702 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006703 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006704 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006705 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006706
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006707 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006708 if (res->flags & CF_SHUTW)
6709 goto aborted_xfer;
6710
6711 /* stop waiting for data if the input is closed before the end. If the
6712 * client side was already closed, it means that the client has aborted,
6713 * so we don't want to count this as a server abort. Otherwise it's a
6714 * server abort.
6715 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006716 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006717 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006718 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006719 /* If we have some pending data, we continue the processing */
6720 if (!buffer_pending(res->buf)) {
6721 if (!(s->flags & SF_ERR_MASK))
6722 s->flags |= SF_ERR_SRVCL;
6723 s->be->be_counters.srv_aborts++;
6724 if (objt_server(s->target))
6725 objt_server(s->target)->counters.srv_aborts++;
6726 goto return_bad_res_stats_ok;
6727 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006728 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006729
Willy Tarreau40dba092010-03-04 18:14:51 +01006730 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006731 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006732 goto return_bad_res;
6733
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006734 /* When TE: chunked is used, we need to get there again to parse
6735 * remaining chunks even if the server has closed, so we don't want to
6736 * set CF_DONTCLOSE. Similarly, if the body length is undefined, if
6737 * keep-alive is set on the client side or if there are filters
6738 * registered on the stream, we don't want to forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006739 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006740 if ((msg->flags & HTTP_MSGF_TE_CHNK) || !msg->body_len ||
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006741 HAS_FILTERS(s) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006742 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6743 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006744 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006745
Willy Tarreau5c620922011-05-11 19:56:11 +02006746 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006747 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006748 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006749 * modes are already handled by the stream sock layer. We must not do
6750 * this in content-length mode because it could present the MSG_MORE
6751 * flag with the last block of forwarded data, which would cause an
6752 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006753 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006754 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006755 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006756
Willy Tarreau87b09662015-04-03 00:22:06 +02006757 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006758 return 0;
6759
Willy Tarreau40dba092010-03-04 18:14:51 +01006760 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006761 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006762 if (objt_server(s->target))
6763 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006764
6765 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006766 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006767 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006768 http_reply_and_close(s, txn->status, NULL);
Christopher Fauletd7c91962015-04-30 11:48:27 +02006769 res->analysers &= AN_FLT_END;
6770 s->req.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006771 if (objt_server(s->target))
6772 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006773
Willy Tarreaue7dff022015-04-03 01:14:29 +02006774 if (!(s->flags & SF_ERR_MASK))
6775 s->flags |= SF_ERR_PRXCOND;
6776 if (!(s->flags & SF_FINST_MASK))
6777 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006778 return 0;
6779
6780 aborted_xfer:
6781 txn->rsp.msg_state = HTTP_MSG_ERROR;
6782 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006783 http_reply_and_close(s, txn->status, NULL);
Christopher Fauletd7c91962015-04-30 11:48:27 +02006784 res->analysers &= AN_FLT_END;
6785 s->req.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006786
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006787 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006788 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006789 if (objt_server(s->target))
6790 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006791
Willy Tarreaue7dff022015-04-03 01:14:29 +02006792 if (!(s->flags & SF_ERR_MASK))
6793 s->flags |= SF_ERR_CLICL;
6794 if (!(s->flags & SF_FINST_MASK))
6795 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006796 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006797}
6798
6799
6800static inline int
6801http_msg_forward_body(struct stream *s, struct http_msg *msg)
6802{
6803 struct channel *chn = msg->chn;
6804 int ret;
6805
6806 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
6807
6808 if (msg->msg_state == HTTP_MSG_ENDING)
6809 goto ending;
6810
6811 /* Neither content-length, nor transfer-encoding was found, so we must
6812 * read the body until the server connection is closed. In that case, we
6813 * eat data as they come. Of course, this happens for response only. */
6814 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6815 unsigned long long len = (chn->buf->i - msg->next);
6816 msg->chunk_len += len;
6817 msg->body_len += len;
6818 }
Christopher Fauletda02e172015-12-04 09:25:05 +01006819 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6820 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6821 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006822 msg->next += ret;
6823 msg->chunk_len -= ret;
6824 if (msg->chunk_len) {
6825 /* input empty or output full */
6826 if (chn->buf->i > msg->next)
6827 chn->flags |= CF_WAKE_WRITE;
6828 goto missing_data_or_waiting;
6829 }
6830
6831 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && !(chn->flags & CF_SHUTR)) {
6832 /* The server still sending data */
6833 goto missing_data_or_waiting;
6834 }
6835 msg->msg_state = HTTP_MSG_ENDING;
6836
6837 ending:
6838 /* we may have some pending data starting at res->buf->p such as a last
6839 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006840 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6841 /* default_ret */ msg->next,
6842 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006843 b_adv(chn->buf, ret);
6844 msg->next -= ret;
6845 if (msg->next)
6846 goto missing_data_or_waiting;
6847
Christopher Fauletda02e172015-12-04 09:25:05 +01006848 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
6849 /* default_ret */ 1,
6850 /* on_error */ goto error,
6851 /* on_wait */ goto waiting);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006852 msg->msg_state = HTTP_MSG_DONE;
6853 return 1;
6854
6855 missing_data_or_waiting:
6856 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006857 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6858 /* default_ret */ msg->next,
6859 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006860 b_adv(chn->buf, ret);
6861 msg->next -= ret;
6862 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6863 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006864 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006865 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
6866 waiting:
6867 return 0;
6868 error:
6869 return -1;
6870}
6871
6872static inline int
6873http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
6874{
6875 struct channel *chn = msg->chn;
6876 int ret;
6877
6878 /* Here we have the guarantee to be in one of the following state:
6879 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
6880 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
6881
6882 switch_states:
6883 switch (msg->msg_state) {
6884 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01006885 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6886 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6887 /* on_error */ goto error);
6888 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006889 msg->chunk_len -= ret;
6890 if (msg->chunk_len) {
6891 /* input empty or output full */
6892 if (chn->buf->i > msg->next)
6893 chn->flags |= CF_WAKE_WRITE;
6894 goto missing_data_or_waiting;
6895 }
6896
6897 /* nothing left to forward for this chunk*/
6898 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
6899 /* fall through for HTTP_MSG_CHUNK_CRLF */
6900
6901 case HTTP_MSG_CHUNK_CRLF:
6902 /* we want the CRLF after the data */
6903 ret = http_skip_chunk_crlf(msg);
6904 if (ret == 0)
6905 goto missing_data_or_waiting;
6906 if (ret < 0)
6907 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01006908 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006909 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6910 /* fall through for HTTP_MSG_CHUNK_SIZE */
6911
6912 case HTTP_MSG_CHUNK_SIZE:
6913 /* read the chunk size and assign it to ->chunk_len,
6914 * then set ->next to point to the body and switch to
6915 * DATA or TRAILERS state.
6916 */
6917 ret = http_parse_chunk_size(msg);
6918 if (ret == 0)
6919 goto missing_data_or_waiting;
6920 if (ret < 0)
6921 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01006922 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006923 if (msg->chunk_len) {
6924 msg->msg_state = HTTP_MSG_DATA;
6925 goto switch_states;
6926 }
6927 msg->msg_state = HTTP_MSG_TRAILERS;
6928 /* fall through for HTTP_MSG_TRAILERS */
6929
6930 case HTTP_MSG_TRAILERS:
6931 ret = http_forward_trailers(msg);
6932 if (ret < 0)
6933 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01006934 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
6935 /* default_ret */ 1,
6936 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006937 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006938 if (!ret)
6939 goto missing_data_or_waiting;
6940 break;
6941
6942 case HTTP_MSG_ENDING:
6943 goto ending;
6944
6945 default:
6946 /* This should no happen in this function */
6947 goto error;
6948 }
6949
6950 msg->msg_state = HTTP_MSG_ENDING;
6951 ending:
6952 /* we may have some pending data starting at res->buf->p such as a last
6953 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006954 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006955 /* default_ret */ msg->next,
6956 /* on_error */ goto error);
6957 b_adv(chn->buf, ret);
6958 msg->next -= ret;
6959 if (msg->next)
6960 goto missing_data_or_waiting;
6961
Christopher Fauletda02e172015-12-04 09:25:05 +01006962 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006963 /* default_ret */ 1,
6964 /* on_error */ goto error,
6965 /* on_wait */ goto waiting);
6966 msg->msg_state = HTTP_MSG_DONE;
6967 return 1;
6968
6969 missing_data_or_waiting:
6970 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006971 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006972 /* default_ret */ msg->next,
6973 /* on_error */ goto error);
6974 b_adv(chn->buf, ret);
6975 msg->next -= ret;
6976 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6977 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006978 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006979 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
6980 waiting:
6981 return 0;
6982
6983 chunk_parsing_error:
6984 if (msg->err_pos >= 0) {
6985 if (chn->flags & CF_ISRESP)
6986 http_capture_bad_message(&s->be->invalid_rep, s, msg,
6987 msg->msg_state, strm_fe(s));
6988 else
6989 http_capture_bad_message(&strm_fe(s)->invalid_req, s,
6990 msg, msg->msg_state, s->be);
6991 }
6992 error:
6993 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006994}
6995
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006996
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006997/* Iterate the same filter through all request headers.
6998 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006999 * Since it can manage the switch to another backend, it updates the per-proxy
7000 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007001 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007002int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007003{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007004 char *cur_ptr, *cur_end, *cur_next;
7005 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007006 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007007 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007008 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007009
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007010 last_hdr = 0;
7011
Willy Tarreau9b28e032012-10-12 23:49:43 +02007012 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007013 old_idx = 0;
7014
7015 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007016 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007017 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007018 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007019 (exp->action == ACT_ALLOW ||
7020 exp->action == ACT_DENY ||
7021 exp->action == ACT_TARPIT))
7022 return 0;
7023
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007024 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007025 if (!cur_idx)
7026 break;
7027
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007028 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007029 cur_ptr = cur_next;
7030 cur_end = cur_ptr + cur_hdr->len;
7031 cur_next = cur_end + cur_hdr->cr + 1;
7032
7033 /* Now we have one header between cur_ptr and cur_end,
7034 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007035 */
7036
Willy Tarreau15a53a42015-01-21 13:39:42 +01007037 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007038 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007039 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007040 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007041 last_hdr = 1;
7042 break;
7043
7044 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007045 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007046 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007047 break;
7048
7049 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007050 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007051 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007052 break;
7053
7054 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007055 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7056 if (trash.len < 0)
7057 return -1;
7058
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007059 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007060 /* FIXME: if the user adds a newline in the replacement, the
7061 * index will not be recalculated for now, and the new line
7062 * will not be counted as a new header.
7063 */
7064
7065 cur_end += delta;
7066 cur_next += delta;
7067 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007068 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007069 break;
7070
7071 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007072 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007073 cur_next += delta;
7074
Willy Tarreaufa355d42009-11-29 18:12:29 +01007075 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007076 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7077 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007078 cur_hdr->len = 0;
7079 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007080 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007081 break;
7082
7083 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007084 }
7085
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007086 /* keep the link from this header to next one in case of later
7087 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007088 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007089 old_idx = cur_idx;
7090 }
7091 return 0;
7092}
7093
7094
7095/* Apply the filter to the request line.
7096 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7097 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007098 * Since it can manage the switch to another backend, it updates the per-proxy
7099 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007100 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007101int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007102{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007103 char *cur_ptr, *cur_end;
7104 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007105 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007106 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007107
Willy Tarreau3d300592007-03-18 18:34:41 +01007108 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007109 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007110 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007111 (exp->action == ACT_ALLOW ||
7112 exp->action == ACT_DENY ||
7113 exp->action == ACT_TARPIT))
7114 return 0;
7115 else if (exp->action == ACT_REMOVE)
7116 return 0;
7117
7118 done = 0;
7119
Willy Tarreau9b28e032012-10-12 23:49:43 +02007120 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007121 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007122
7123 /* Now we have the request line between cur_ptr and cur_end */
7124
Willy Tarreau15a53a42015-01-21 13:39:42 +01007125 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007126 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007127 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007128 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007129 done = 1;
7130 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007131
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007132 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007133 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007134 done = 1;
7135 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007136
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007137 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007138 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007139 done = 1;
7140 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007141
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007142 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007143 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7144 if (trash.len < 0)
7145 return -1;
7146
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007147 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007148 /* FIXME: if the user adds a newline in the replacement, the
7149 * index will not be recalculated for now, and the new line
7150 * will not be counted as a new header.
7151 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007152
Willy Tarreaufa355d42009-11-29 18:12:29 +01007153 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007154 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007155 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007156 HTTP_MSG_RQMETH,
7157 cur_ptr, cur_end + 1,
7158 NULL, NULL);
7159 if (unlikely(!cur_end))
7160 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007161
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007162 /* we have a full request and we know that we have either a CR
7163 * or an LF at <ptr>.
7164 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007165 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7166 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007167 /* there is no point trying this regex on headers */
7168 return 1;
7169 }
7170 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007171 return done;
7172}
Willy Tarreau97de6242006-12-27 17:18:38 +01007173
Willy Tarreau58f10d72006-12-04 02:26:12 +01007174
Willy Tarreau58f10d72006-12-04 02:26:12 +01007175
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007176/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007177 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007178 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007179 * unparsable request. Since it can manage the switch to another backend, it
7180 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007181 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007182int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007183{
Willy Tarreau192252e2015-04-04 01:47:55 +02007184 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007185 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007186 struct hdr_exp *exp;
7187
7188 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007189 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007190
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007191 /*
7192 * The interleaving of transformations and verdicts
7193 * makes it difficult to decide to continue or stop
7194 * the evaluation.
7195 */
7196
Willy Tarreau6c123b12010-01-28 20:22:06 +01007197 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7198 break;
7199
Willy Tarreau3d300592007-03-18 18:34:41 +01007200 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007201 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007202 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007203 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007204
7205 /* if this filter had a condition, evaluate it now and skip to
7206 * next filter if the condition does not match.
7207 */
7208 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007209 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007210 ret = acl_pass(ret);
7211 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7212 ret = !ret;
7213
7214 if (!ret)
7215 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007216 }
7217
7218 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007219 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007220 if (unlikely(ret < 0))
7221 return -1;
7222
7223 if (likely(ret == 0)) {
7224 /* The filter did not match the request, it can be
7225 * iterated through all headers.
7226 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007227 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7228 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007229 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007230 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007231 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007232}
7233
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007234
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007235/* Find the end of a cookie value contained between <s> and <e>. It works the
7236 * same way as with headers above except that the semi-colon also ends a token.
7237 * See RFC2965 for more information. Note that it requires a valid header to
7238 * return a valid result.
7239 */
7240char *find_cookie_value_end(char *s, const char *e)
7241{
7242 int quoted, qdpair;
7243
7244 quoted = qdpair = 0;
7245 for (; s < e; s++) {
7246 if (qdpair) qdpair = 0;
7247 else if (quoted) {
7248 if (*s == '\\') qdpair = 1;
7249 else if (*s == '"') quoted = 0;
7250 }
7251 else if (*s == '"') quoted = 1;
7252 else if (*s == ',' || *s == ';') return s;
7253 }
7254 return s;
7255}
7256
7257/* Delete a value in a header between delimiters <from> and <next> in buffer
7258 * <buf>. The number of characters displaced is returned, and the pointer to
7259 * the first delimiter is updated if required. The function tries as much as
7260 * possible to respect the following principles :
7261 * - replace <from> delimiter by the <next> one unless <from> points to a
7262 * colon, in which case <next> is simply removed
7263 * - set exactly one space character after the new first delimiter, unless
7264 * there are not enough characters in the block being moved to do so.
7265 * - remove unneeded spaces before the previous delimiter and after the new
7266 * one.
7267 *
7268 * It is the caller's responsibility to ensure that :
7269 * - <from> points to a valid delimiter or the colon ;
7270 * - <next> points to a valid delimiter or the final CR/LF ;
7271 * - there are non-space chars before <from> ;
7272 * - there is a CR/LF at or after <next>.
7273 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007274int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007275{
7276 char *prev = *from;
7277
7278 if (*prev == ':') {
7279 /* We're removing the first value, preserve the colon and add a
7280 * space if possible.
7281 */
7282 if (!http_is_crlf[(unsigned char)*next])
7283 next++;
7284 prev++;
7285 if (prev < next)
7286 *prev++ = ' ';
7287
7288 while (http_is_spht[(unsigned char)*next])
7289 next++;
7290 } else {
7291 /* Remove useless spaces before the old delimiter. */
7292 while (http_is_spht[(unsigned char)*(prev-1)])
7293 prev--;
7294 *from = prev;
7295
7296 /* copy the delimiter and if possible a space if we're
7297 * not at the end of the line.
7298 */
7299 if (!http_is_crlf[(unsigned char)*next]) {
7300 *prev++ = *next++;
7301 if (prev + 1 < next)
7302 *prev++ = ' ';
7303 while (http_is_spht[(unsigned char)*next])
7304 next++;
7305 }
7306 }
7307 return buffer_replace2(buf, prev, next, NULL, 0);
7308}
7309
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007310/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007311 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007312 * desirable to call it only when needed. This code is quite complex because
7313 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7314 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007315 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007316void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007317{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007318 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007319 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007320 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007321 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007322 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7323 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007324
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007325 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007326 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007327 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007328
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007329 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007330 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007331 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007332
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007333 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007334 hdr_beg = hdr_next;
7335 hdr_end = hdr_beg + cur_hdr->len;
7336 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007337
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007338 /* We have one full header between hdr_beg and hdr_end, and the
7339 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007340 * "Cookie:" headers.
7341 */
7342
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007343 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007344 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007345 old_idx = cur_idx;
7346 continue;
7347 }
7348
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007349 del_from = NULL; /* nothing to be deleted */
7350 preserve_hdr = 0; /* assume we may kill the whole header */
7351
Willy Tarreau58f10d72006-12-04 02:26:12 +01007352 /* Now look for cookies. Conforming to RFC2109, we have to support
7353 * attributes whose name begin with a '$', and associate them with
7354 * the right cookie, if we want to delete this cookie.
7355 * So there are 3 cases for each cookie read :
7356 * 1) it's a special attribute, beginning with a '$' : ignore it.
7357 * 2) it's a server id cookie that we *MAY* want to delete : save
7358 * some pointers on it (last semi-colon, beginning of cookie...)
7359 * 3) it's an application cookie : we *MAY* have to delete a previous
7360 * "special" cookie.
7361 * At the end of loop, if a "special" cookie remains, we may have to
7362 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007363 * *MUST* delete it.
7364 *
7365 * Note: RFC2965 is unclear about the processing of spaces around
7366 * the equal sign in the ATTR=VALUE form. A careful inspection of
7367 * the RFC explicitly allows spaces before it, and not within the
7368 * tokens (attrs or values). An inspection of RFC2109 allows that
7369 * too but section 10.1.3 lets one think that spaces may be allowed
7370 * after the equal sign too, resulting in some (rare) buggy
7371 * implementations trying to do that. So let's do what servers do.
7372 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7373 * allowed quoted strings in values, with any possible character
7374 * after a backslash, including control chars and delimitors, which
7375 * causes parsing to become ambiguous. Browsers also allow spaces
7376 * within values even without quotes.
7377 *
7378 * We have to keep multiple pointers in order to support cookie
7379 * removal at the beginning, middle or end of header without
7380 * corrupting the header. All of these headers are valid :
7381 *
7382 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7383 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7384 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7385 * | | | | | | | | |
7386 * | | | | | | | | hdr_end <--+
7387 * | | | | | | | +--> next
7388 * | | | | | | +----> val_end
7389 * | | | | | +-----------> val_beg
7390 * | | | | +--------------> equal
7391 * | | | +----------------> att_end
7392 * | | +---------------------> att_beg
7393 * | +--------------------------> prev
7394 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007395 */
7396
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007397 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7398 /* Iterate through all cookies on this line */
7399
7400 /* find att_beg */
7401 att_beg = prev + 1;
7402 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7403 att_beg++;
7404
7405 /* find att_end : this is the first character after the last non
7406 * space before the equal. It may be equal to hdr_end.
7407 */
7408 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007409
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007410 while (equal < hdr_end) {
7411 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007412 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007413 if (http_is_spht[(unsigned char)*equal++])
7414 continue;
7415 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007416 }
7417
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007418 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7419 * is between <att_beg> and <equal>, both may be identical.
7420 */
7421
7422 /* look for end of cookie if there is an equal sign */
7423 if (equal < hdr_end && *equal == '=') {
7424 /* look for the beginning of the value */
7425 val_beg = equal + 1;
7426 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7427 val_beg++;
7428
7429 /* find the end of the value, respecting quotes */
7430 next = find_cookie_value_end(val_beg, hdr_end);
7431
7432 /* make val_end point to the first white space or delimitor after the value */
7433 val_end = next;
7434 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7435 val_end--;
7436 } else {
7437 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007438 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007439
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007440 /* We have nothing to do with attributes beginning with '$'. However,
7441 * they will automatically be removed if a header before them is removed,
7442 * since they're supposed to be linked together.
7443 */
7444 if (*att_beg == '$')
7445 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007446
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007447 /* Ignore cookies with no equal sign */
7448 if (equal == next) {
7449 /* This is not our cookie, so we must preserve it. But if we already
7450 * scheduled another cookie for removal, we cannot remove the
7451 * complete header, but we can remove the previous block itself.
7452 */
7453 preserve_hdr = 1;
7454 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007455 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007456 val_end += delta;
7457 next += delta;
7458 hdr_end += delta;
7459 hdr_next += delta;
7460 cur_hdr->len += delta;
7461 http_msg_move_end(&txn->req, delta);
7462 prev = del_from;
7463 del_from = NULL;
7464 }
7465 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007466 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007467
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007468 /* if there are spaces around the equal sign, we need to
7469 * strip them otherwise we'll get trouble for cookie captures,
7470 * or even for rewrites. Since this happens extremely rarely,
7471 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007472 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007473 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7474 int stripped_before = 0;
7475 int stripped_after = 0;
7476
7477 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007478 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007479 equal += stripped_before;
7480 val_beg += stripped_before;
7481 }
7482
7483 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007484 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007485 val_beg += stripped_after;
7486 stripped_before += stripped_after;
7487 }
7488
7489 val_end += stripped_before;
7490 next += stripped_before;
7491 hdr_end += stripped_before;
7492 hdr_next += stripped_before;
7493 cur_hdr->len += stripped_before;
7494 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007495 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007496 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007497
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007498 /* First, let's see if we want to capture this cookie. We check
7499 * that we don't already have a client side cookie, because we
7500 * can only capture one. Also as an optimisation, we ignore
7501 * cookies shorter than the declared name.
7502 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007503 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7504 (val_end - att_beg >= sess->fe->capture_namelen) &&
7505 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007506 int log_len = val_end - att_beg;
7507
7508 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7509 Alert("HTTP logging : out of memory.\n");
7510 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007511 if (log_len > sess->fe->capture_len)
7512 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007513 memcpy(txn->cli_cookie, att_beg, log_len);
7514 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007515 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007516 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007517
Willy Tarreaubca99692010-10-06 19:25:55 +02007518 /* Persistence cookies in passive, rewrite or insert mode have the
7519 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007520 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007521 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007522 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007523 * For cookies in prefix mode, the form is :
7524 *
7525 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007526 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007527 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7528 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7529 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007530 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007531
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007532 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7533 * have the server ID between val_beg and delim, and the original cookie between
7534 * delim+1 and val_end. Otherwise, delim==val_end :
7535 *
7536 * Cookie: NAME=SRV; # in all but prefix modes
7537 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7538 * | || || | |+-> next
7539 * | || || | +--> val_end
7540 * | || || +---------> delim
7541 * | || |+------------> val_beg
7542 * | || +-------------> att_end = equal
7543 * | |+-----------------> att_beg
7544 * | +------------------> prev
7545 * +-------------------------> hdr_beg
7546 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007547
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007548 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007549 for (delim = val_beg; delim < val_end; delim++)
7550 if (*delim == COOKIE_DELIM)
7551 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007552 } else {
7553 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007554 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007555 /* Now check if the cookie contains a date field, which would
7556 * appear after a vertical bar ('|') just after the server name
7557 * and before the delimiter.
7558 */
7559 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7560 if (vbar1) {
7561 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007562 * right is the last seen date. It is a base64 encoded
7563 * 30-bit value representing the UNIX date since the
7564 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007565 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007566 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007567 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007568 if (val_end - vbar1 >= 5) {
7569 val = b64tos30(vbar1);
7570 if (val > 0)
7571 txn->cookie_last_date = val << 2;
7572 }
7573 /* look for a second vertical bar */
7574 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7575 if (vbar1 && (val_end - vbar1 > 5)) {
7576 val = b64tos30(vbar1 + 1);
7577 if (val > 0)
7578 txn->cookie_first_date = val << 2;
7579 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007580 }
7581 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007582
Willy Tarreauf64d1412010-10-07 20:06:11 +02007583 /* if the cookie has an expiration date and the proxy wants to check
7584 * it, then we do that now. We first check if the cookie is too old,
7585 * then only if it has expired. We detect strict overflow because the
7586 * time resolution here is not great (4 seconds). Cookies with dates
7587 * in the future are ignored if their offset is beyond one day. This
7588 * allows an admin to fix timezone issues without expiring everyone
7589 * and at the same time avoids keeping unwanted side effects for too
7590 * long.
7591 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007592 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7593 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007594 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007595 txn->flags &= ~TX_CK_MASK;
7596 txn->flags |= TX_CK_OLD;
7597 delim = val_beg; // let's pretend we have not found the cookie
7598 txn->cookie_first_date = 0;
7599 txn->cookie_last_date = 0;
7600 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007601 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7602 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007603 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007604 txn->flags &= ~TX_CK_MASK;
7605 txn->flags |= TX_CK_EXPIRED;
7606 delim = val_beg; // let's pretend we have not found the cookie
7607 txn->cookie_first_date = 0;
7608 txn->cookie_last_date = 0;
7609 }
7610
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007611 /* Here, we'll look for the first running server which supports the cookie.
7612 * This allows to share a same cookie between several servers, for example
7613 * to dedicate backup servers to specific servers only.
7614 * However, to prevent clients from sticking to cookie-less backup server
7615 * when they have incidentely learned an empty cookie, we simply ignore
7616 * empty cookies and mark them as invalid.
7617 * The same behaviour is applied when persistence must be ignored.
7618 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007619 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007620 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007621
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007622 while (srv) {
7623 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7624 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007625 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007626 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007627 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007628 /* we found the server and we can use it */
7629 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007630 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007631 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007632 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007633 break;
7634 } else {
7635 /* we found a server, but it's down,
7636 * mark it as such and go on in case
7637 * another one is available.
7638 */
7639 txn->flags &= ~TX_CK_MASK;
7640 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007641 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007642 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007643 srv = srv->next;
7644 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007645
Willy Tarreauf64d1412010-10-07 20:06:11 +02007646 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007647 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007648 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007649 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007650 txn->flags |= TX_CK_UNUSED;
7651 else
7652 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007653 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007654
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007655 /* depending on the cookie mode, we may have to either :
7656 * - delete the complete cookie if we're in insert+indirect mode, so that
7657 * the server never sees it ;
7658 * - remove the server id from the cookie value, and tag the cookie as an
7659 * application cookie so that it does not get accidentely removed later,
7660 * if we're in cookie prefix mode
7661 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007662 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007663 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007664
Willy Tarreau9b28e032012-10-12 23:49:43 +02007665 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007666 val_end += delta;
7667 next += delta;
7668 hdr_end += delta;
7669 hdr_next += delta;
7670 cur_hdr->len += delta;
7671 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007672
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007673 del_from = NULL;
7674 preserve_hdr = 1; /* we want to keep this cookie */
7675 }
7676 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007677 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007678 del_from = prev;
7679 }
7680 } else {
7681 /* This is not our cookie, so we must preserve it. But if we already
7682 * scheduled another cookie for removal, we cannot remove the
7683 * complete header, but we can remove the previous block itself.
7684 */
7685 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007686
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007687 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007688 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007689 if (att_beg >= del_from)
7690 att_beg += delta;
7691 if (att_end >= del_from)
7692 att_end += delta;
7693 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007694 val_end += delta;
7695 next += delta;
7696 hdr_end += delta;
7697 hdr_next += delta;
7698 cur_hdr->len += delta;
7699 http_msg_move_end(&txn->req, delta);
7700 prev = del_from;
7701 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007702 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007703 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007704
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007705 /* continue with next cookie on this header line */
7706 att_beg = next;
7707 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007708
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007709 /* There are no more cookies on this line.
7710 * We may still have one (or several) marked for deletion at the
7711 * end of the line. We must do this now in two ways :
7712 * - if some cookies must be preserved, we only delete from the
7713 * mark to the end of line ;
7714 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007715 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007716 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007717 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007718 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007719 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007720 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007721 cur_hdr->len += delta;
7722 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007723 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007724
7725 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007726 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7727 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007728 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007729 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007730 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007731 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007732 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007733 }
7734
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007735 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007736 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007737 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007738}
7739
7740
Willy Tarreaua15645d2007-03-18 16:22:39 +01007741/* Iterate the same filter through all response headers contained in <rtr>.
7742 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7743 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007744int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007745{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007746 char *cur_ptr, *cur_end, *cur_next;
7747 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007748 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007749 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007750 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007751
7752 last_hdr = 0;
7753
Willy Tarreau9b28e032012-10-12 23:49:43 +02007754 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007755 old_idx = 0;
7756
7757 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007758 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007759 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007760 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007761 (exp->action == ACT_ALLOW ||
7762 exp->action == ACT_DENY))
7763 return 0;
7764
7765 cur_idx = txn->hdr_idx.v[old_idx].next;
7766 if (!cur_idx)
7767 break;
7768
7769 cur_hdr = &txn->hdr_idx.v[cur_idx];
7770 cur_ptr = cur_next;
7771 cur_end = cur_ptr + cur_hdr->len;
7772 cur_next = cur_end + cur_hdr->cr + 1;
7773
7774 /* Now we have one header between cur_ptr and cur_end,
7775 * and the next header starts at cur_next.
7776 */
7777
Willy Tarreau15a53a42015-01-21 13:39:42 +01007778 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007779 switch (exp->action) {
7780 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007781 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007782 last_hdr = 1;
7783 break;
7784
7785 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007786 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007787 last_hdr = 1;
7788 break;
7789
7790 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007791 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7792 if (trash.len < 0)
7793 return -1;
7794
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007795 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007796 /* FIXME: if the user adds a newline in the replacement, the
7797 * index will not be recalculated for now, and the new line
7798 * will not be counted as a new header.
7799 */
7800
7801 cur_end += delta;
7802 cur_next += delta;
7803 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007804 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007805 break;
7806
7807 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007808 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007809 cur_next += delta;
7810
Willy Tarreaufa355d42009-11-29 18:12:29 +01007811 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007812 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7813 txn->hdr_idx.used--;
7814 cur_hdr->len = 0;
7815 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007816 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007817 break;
7818
7819 }
7820 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007821
7822 /* keep the link from this header to next one in case of later
7823 * removal of next header.
7824 */
7825 old_idx = cur_idx;
7826 }
7827 return 0;
7828}
7829
7830
7831/* Apply the filter to the status line in the response buffer <rtr>.
7832 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7833 * or -1 if a replacement resulted in an invalid status line.
7834 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007835int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007836{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007837 char *cur_ptr, *cur_end;
7838 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007839 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007840 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007841
7842
Willy Tarreau3d300592007-03-18 18:34:41 +01007843 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007844 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007845 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007846 (exp->action == ACT_ALLOW ||
7847 exp->action == ACT_DENY))
7848 return 0;
7849 else if (exp->action == ACT_REMOVE)
7850 return 0;
7851
7852 done = 0;
7853
Willy Tarreau9b28e032012-10-12 23:49:43 +02007854 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007855 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007856
7857 /* Now we have the status line between cur_ptr and cur_end */
7858
Willy Tarreau15a53a42015-01-21 13:39:42 +01007859 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007860 switch (exp->action) {
7861 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007862 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007863 done = 1;
7864 break;
7865
7866 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007867 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007868 done = 1;
7869 break;
7870
7871 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007872 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7873 if (trash.len < 0)
7874 return -1;
7875
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007876 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007877 /* FIXME: if the user adds a newline in the replacement, the
7878 * index will not be recalculated for now, and the new line
7879 * will not be counted as a new header.
7880 */
7881
Willy Tarreaufa355d42009-11-29 18:12:29 +01007882 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007883 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007884 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007885 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007886 cur_ptr, cur_end + 1,
7887 NULL, NULL);
7888 if (unlikely(!cur_end))
7889 return -1;
7890
7891 /* we have a full respnse and we know that we have either a CR
7892 * or an LF at <ptr>.
7893 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007894 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007895 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007896 /* there is no point trying this regex on headers */
7897 return 1;
7898 }
7899 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007900 return done;
7901}
7902
7903
7904
7905/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007906 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007907 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7908 * unparsable response.
7909 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007910int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007911{
Willy Tarreau192252e2015-04-04 01:47:55 +02007912 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007913 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007914 struct hdr_exp *exp;
7915
7916 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007917 int ret;
7918
7919 /*
7920 * The interleaving of transformations and verdicts
7921 * makes it difficult to decide to continue or stop
7922 * the evaluation.
7923 */
7924
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007925 if (txn->flags & TX_SVDENY)
7926 break;
7927
Willy Tarreau3d300592007-03-18 18:34:41 +01007928 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007929 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7930 exp->action == ACT_PASS)) {
7931 exp = exp->next;
7932 continue;
7933 }
7934
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007935 /* if this filter had a condition, evaluate it now and skip to
7936 * next filter if the condition does not match.
7937 */
7938 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007939 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007940 ret = acl_pass(ret);
7941 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7942 ret = !ret;
7943 if (!ret)
7944 continue;
7945 }
7946
Willy Tarreaua15645d2007-03-18 16:22:39 +01007947 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007948 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007949 if (unlikely(ret < 0))
7950 return -1;
7951
7952 if (likely(ret == 0)) {
7953 /* The filter did not match the response, it can be
7954 * iterated through all headers.
7955 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007956 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7957 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007958 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007959 }
7960 return 0;
7961}
7962
7963
Willy Tarreaua15645d2007-03-18 16:22:39 +01007964/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007965 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007966 * desirable to call it only when needed. This function is also used when we
7967 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007968 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007969void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007970{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007971 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007972 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01007973 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007974 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007975 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007976 char *hdr_beg, *hdr_end, *hdr_next;
7977 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007978
Willy Tarreaua15645d2007-03-18 16:22:39 +01007979 /* Iterate through the headers.
7980 * we start with the start line.
7981 */
7982 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007983 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007984
7985 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7986 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007987 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007988
7989 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007990 hdr_beg = hdr_next;
7991 hdr_end = hdr_beg + cur_hdr->len;
7992 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007993
Willy Tarreau24581ba2010-08-31 22:39:35 +02007994 /* We have one full header between hdr_beg and hdr_end, and the
7995 * next header starts at hdr_next. We're only interested in
7996 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007997 */
7998
Willy Tarreau24581ba2010-08-31 22:39:35 +02007999 is_cookie2 = 0;
8000 prev = hdr_beg + 10;
8001 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008002 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008003 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8004 if (!val) {
8005 old_idx = cur_idx;
8006 continue;
8007 }
8008 is_cookie2 = 1;
8009 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008010 }
8011
Willy Tarreau24581ba2010-08-31 22:39:35 +02008012 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8013 * <prev> points to the colon.
8014 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008015 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008016
Willy Tarreau24581ba2010-08-31 22:39:35 +02008017 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8018 * check-cache is enabled) and we are not interested in checking
8019 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008020 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02008021 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008022 return;
8023
Willy Tarreau24581ba2010-08-31 22:39:35 +02008024 /* OK so now we know we have to process this response cookie.
8025 * The format of the Set-Cookie header is slightly different
8026 * from the format of the Cookie header in that it does not
8027 * support the comma as a cookie delimiter (thus the header
8028 * cannot be folded) because the Expires attribute described in
8029 * the original Netscape's spec may contain an unquoted date
8030 * with a comma inside. We have to live with this because
8031 * many browsers don't support Max-Age and some browsers don't
8032 * support quoted strings. However the Set-Cookie2 header is
8033 * clean.
8034 *
8035 * We have to keep multiple pointers in order to support cookie
8036 * removal at the beginning, middle or end of header without
8037 * corrupting the header (in case of set-cookie2). A special
8038 * pointer, <scav> points to the beginning of the set-cookie-av
8039 * fields after the first semi-colon. The <next> pointer points
8040 * either to the end of line (set-cookie) or next unquoted comma
8041 * (set-cookie2). All of these headers are valid :
8042 *
8043 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8044 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8045 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8046 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8047 * | | | | | | | | | |
8048 * | | | | | | | | +-> next hdr_end <--+
8049 * | | | | | | | +------------> scav
8050 * | | | | | | +--------------> val_end
8051 * | | | | | +--------------------> val_beg
8052 * | | | | +----------------------> equal
8053 * | | | +------------------------> att_end
8054 * | | +----------------------------> att_beg
8055 * | +------------------------------> prev
8056 * +-----------------------------------------> hdr_beg
8057 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008058
Willy Tarreau24581ba2010-08-31 22:39:35 +02008059 for (; prev < hdr_end; prev = next) {
8060 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008061
Willy Tarreau24581ba2010-08-31 22:39:35 +02008062 /* find att_beg */
8063 att_beg = prev + 1;
8064 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8065 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008066
Willy Tarreau24581ba2010-08-31 22:39:35 +02008067 /* find att_end : this is the first character after the last non
8068 * space before the equal. It may be equal to hdr_end.
8069 */
8070 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008071
Willy Tarreau24581ba2010-08-31 22:39:35 +02008072 while (equal < hdr_end) {
8073 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8074 break;
8075 if (http_is_spht[(unsigned char)*equal++])
8076 continue;
8077 att_end = equal;
8078 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008079
Willy Tarreau24581ba2010-08-31 22:39:35 +02008080 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8081 * is between <att_beg> and <equal>, both may be identical.
8082 */
8083
8084 /* look for end of cookie if there is an equal sign */
8085 if (equal < hdr_end && *equal == '=') {
8086 /* look for the beginning of the value */
8087 val_beg = equal + 1;
8088 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8089 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008090
Willy Tarreau24581ba2010-08-31 22:39:35 +02008091 /* find the end of the value, respecting quotes */
8092 next = find_cookie_value_end(val_beg, hdr_end);
8093
8094 /* make val_end point to the first white space or delimitor after the value */
8095 val_end = next;
8096 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8097 val_end--;
8098 } else {
8099 /* <equal> points to next comma, semi-colon or EOL */
8100 val_beg = val_end = next = equal;
8101 }
8102
8103 if (next < hdr_end) {
8104 /* Set-Cookie2 supports multiple cookies, and <next> points to
8105 * a colon or semi-colon before the end. So skip all attr-value
8106 * pairs and look for the next comma. For Set-Cookie, since
8107 * commas are permitted in values, skip to the end.
8108 */
8109 if (is_cookie2)
8110 next = find_hdr_value_end(next, hdr_end);
8111 else
8112 next = hdr_end;
8113 }
8114
8115 /* Now everything is as on the diagram above */
8116
8117 /* Ignore cookies with no equal sign */
8118 if (equal == val_end)
8119 continue;
8120
8121 /* If there are spaces around the equal sign, we need to
8122 * strip them otherwise we'll get trouble for cookie captures,
8123 * or even for rewrites. Since this happens extremely rarely,
8124 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008125 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008126 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8127 int stripped_before = 0;
8128 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008129
Willy Tarreau24581ba2010-08-31 22:39:35 +02008130 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008131 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008132 equal += stripped_before;
8133 val_beg += stripped_before;
8134 }
8135
8136 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008137 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008138 val_beg += stripped_after;
8139 stripped_before += stripped_after;
8140 }
8141
8142 val_end += stripped_before;
8143 next += stripped_before;
8144 hdr_end += stripped_before;
8145 hdr_next += stripped_before;
8146 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008147 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008148 }
8149
8150 /* First, let's see if we want to capture this cookie. We check
8151 * that we don't already have a server side cookie, because we
8152 * can only capture one. Also as an optimisation, we ignore
8153 * cookies shorter than the declared name.
8154 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008155 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008156 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008157 (val_end - att_beg >= sess->fe->capture_namelen) &&
8158 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008159 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008160 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008161 Alert("HTTP logging : out of memory.\n");
8162 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008163 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008164 if (log_len > sess->fe->capture_len)
8165 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008166 memcpy(txn->srv_cookie, att_beg, log_len);
8167 txn->srv_cookie[log_len] = 0;
8168 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008169 }
8170
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008171 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008172 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008173 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008174 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8175 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008176 /* assume passive cookie by default */
8177 txn->flags &= ~TX_SCK_MASK;
8178 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008179
8180 /* If the cookie is in insert mode on a known server, we'll delete
8181 * this occurrence because we'll insert another one later.
8182 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008183 * a direct access.
8184 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008185 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008186 /* The "preserve" flag was set, we don't want to touch the
8187 * server's cookie.
8188 */
8189 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008190 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008191 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008192 /* this cookie must be deleted */
8193 if (*prev == ':' && next == hdr_end) {
8194 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008195 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008196 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8197 txn->hdr_idx.used--;
8198 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008199 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008200 hdr_next += delta;
8201 http_msg_move_end(&txn->rsp, delta);
8202 /* note: while both invalid now, <next> and <hdr_end>
8203 * are still equal, so the for() will stop as expected.
8204 */
8205 } else {
8206 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008207 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008208 next = prev;
8209 hdr_end += delta;
8210 hdr_next += delta;
8211 cur_hdr->len += delta;
8212 http_msg_move_end(&txn->rsp, delta);
8213 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008214 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008215 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008216 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008217 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008218 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008219 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008220 * with this server since we know it.
8221 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008222 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008223 next += delta;
8224 hdr_end += delta;
8225 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008226 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008227 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008228
Willy Tarreauf1348312010-10-07 15:54:11 +02008229 txn->flags &= ~TX_SCK_MASK;
8230 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008231 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008232 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008233 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008234 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008235 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008236 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008237 next += delta;
8238 hdr_end += delta;
8239 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008240 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008241 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008242
Willy Tarreau827aee92011-03-10 16:55:02 +01008243 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008244 txn->flags &= ~TX_SCK_MASK;
8245 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008246 }
8247 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008248 /* that's done for this cookie, check the next one on the same
8249 * line when next != hdr_end (only if is_cookie2).
8250 */
8251 }
8252 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008253 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008254 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008255}
8256
8257
Willy Tarreaua15645d2007-03-18 16:22:39 +01008258/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008259 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008260 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008261void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008262{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008263 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008264 char *p1, *p2;
8265
8266 char *cur_ptr, *cur_end, *cur_next;
8267 int cur_idx;
8268
Willy Tarreau5df51872007-11-25 16:20:08 +01008269 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008270 return;
8271
8272 /* Iterate through the headers.
8273 * we start with the start line.
8274 */
8275 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008276 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008277
8278 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8279 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008280 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008281
8282 cur_hdr = &txn->hdr_idx.v[cur_idx];
8283 cur_ptr = cur_next;
8284 cur_end = cur_ptr + cur_hdr->len;
8285 cur_next = cur_end + cur_hdr->cr + 1;
8286
8287 /* We have one full header between cur_ptr and cur_end, and the
8288 * next header starts at cur_next. We're only interested in
8289 * "Cookie:" headers.
8290 */
8291
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008292 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8293 if (val) {
8294 if ((cur_end - (cur_ptr + val) >= 8) &&
8295 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8296 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8297 return;
8298 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008299 }
8300
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008301 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8302 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008303 continue;
8304
8305 /* OK, right now we know we have a cache-control header at cur_ptr */
8306
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008307 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008308
8309 if (p1 >= cur_end) /* no more info */
8310 continue;
8311
8312 /* p1 is at the beginning of the value */
8313 p2 = p1;
8314
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008315 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008316 p2++;
8317
8318 /* we have a complete value between p1 and p2 */
8319 if (p2 < cur_end && *p2 == '=') {
8320 /* we have something of the form no-cache="set-cookie" */
8321 if ((cur_end - p1 >= 21) &&
8322 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8323 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008324 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008325 continue;
8326 }
8327
8328 /* OK, so we know that either p2 points to the end of string or to a comma */
8329 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008330 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008331 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8332 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8333 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008334 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008335 return;
8336 }
8337
8338 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008339 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008340 continue;
8341 }
8342 }
8343}
8344
Willy Tarreau58f10d72006-12-04 02:26:12 +01008345
Willy Tarreaub2513902006-12-17 14:52:38 +01008346/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008347 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008348 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008349 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008350 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008351 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008352 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008353 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008354 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008355int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008356{
8357 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008358 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008359 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008360
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008361 if (!uri_auth)
8362 return 0;
8363
Cyril Bonté70be45d2010-10-12 00:14:35 +02008364 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008365 return 0;
8366
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008367 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008368 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008369 return 0;
8370
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008371 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008372 return 0;
8373
Willy Tarreaub2513902006-12-17 14:52:38 +01008374 return 1;
8375}
8376
Willy Tarreau4076a152009-04-02 15:18:36 +02008377/*
8378 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008379 * By default it tries to report the error position as msg->err_pos. However if
8380 * this one is not set, it will then report msg->next, which is the last known
8381 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008382 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008383 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008384void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008385 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008386 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008387{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008388 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008389 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008390 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008391
Willy Tarreauf3764b72016-03-31 13:45:10 +02008392 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02008393 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008394 len1 = MIN(len1, es->len);
8395 len2 = es->len - len1; /* remaining data if buffer wraps */
8396
Willy Tarreauf3764b72016-03-31 13:45:10 +02008397 if (!es->buf)
8398 es->buf = malloc(global.tune.bufsize);
8399
8400 if (es->buf) {
8401 memcpy(es->buf, chn->buf->p, len1);
8402 if (len2)
8403 memcpy(es->buf + len1, chn->buf->data, len2);
8404 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008405
Willy Tarreau4076a152009-04-02 15:18:36 +02008406 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008407 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008408 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008409 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008410
Willy Tarreau4076a152009-04-02 15:18:36 +02008411 es->when = date; // user-visible date
8412 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008413 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008414 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008415 if (objt_conn(sess->origin))
8416 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008417 else
8418 memset(&es->src, 0, sizeof(es->src));
8419
Willy Tarreau078272e2010-12-12 12:46:33 +01008420 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008421 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008422 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008423 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008424 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008425 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008426 es->b_out = chn->buf->o;
8427 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008428 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008429 es->m_clen = msg->chunk_len;
8430 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008431}
Willy Tarreaub2513902006-12-17 14:52:38 +01008432
Willy Tarreau294c4732011-12-16 21:35:50 +01008433/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8434 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8435 * performed over the whole headers. Otherwise it must contain a valid header
8436 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8437 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8438 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8439 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008440 * -1. The value fetch stops at commas, so this function is suited for use with
8441 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008442 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008443 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008444unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008445 struct hdr_idx *idx, int occ,
8446 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008447{
Willy Tarreau294c4732011-12-16 21:35:50 +01008448 struct hdr_ctx local_ctx;
8449 char *ptr_hist[MAX_HDR_HISTORY];
8450 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008451 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008452 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008453
Willy Tarreau294c4732011-12-16 21:35:50 +01008454 if (!ctx) {
8455 local_ctx.idx = 0;
8456 ctx = &local_ctx;
8457 }
8458
Willy Tarreaubce70882009-09-07 11:51:47 +02008459 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008460 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008461 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008462 occ--;
8463 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008464 *vptr = ctx->line + ctx->val;
8465 *vlen = ctx->vlen;
8466 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008467 }
8468 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008469 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008470 }
8471
8472 /* negative occurrence, we scan all the list then walk back */
8473 if (-occ > MAX_HDR_HISTORY)
8474 return 0;
8475
Willy Tarreau294c4732011-12-16 21:35:50 +01008476 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008477 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008478 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8479 len_hist[hist_ptr] = ctx->vlen;
8480 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008481 hist_ptr = 0;
8482 found++;
8483 }
8484 if (-occ > found)
8485 return 0;
8486 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008487 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8488 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8489 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008490 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008491 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008492 if (hist_ptr >= MAX_HDR_HISTORY)
8493 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008494 *vptr = ptr_hist[hist_ptr];
8495 *vlen = len_hist[hist_ptr];
8496 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008497}
8498
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008499/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8500 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8501 * performed over the whole headers. Otherwise it must contain a valid header
8502 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8503 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8504 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8505 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8506 * -1. This function differs from http_get_hdr() in that it only returns full
8507 * line header values and does not stop at commas.
8508 * The return value is 0 if nothing was found, or non-zero otherwise.
8509 */
8510unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8511 struct hdr_idx *idx, int occ,
8512 struct hdr_ctx *ctx, char **vptr, int *vlen)
8513{
8514 struct hdr_ctx local_ctx;
8515 char *ptr_hist[MAX_HDR_HISTORY];
8516 int len_hist[MAX_HDR_HISTORY];
8517 unsigned int hist_ptr;
8518 int found;
8519
8520 if (!ctx) {
8521 local_ctx.idx = 0;
8522 ctx = &local_ctx;
8523 }
8524
8525 if (occ >= 0) {
8526 /* search from the beginning */
8527 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8528 occ--;
8529 if (occ <= 0) {
8530 *vptr = ctx->line + ctx->val;
8531 *vlen = ctx->vlen;
8532 return 1;
8533 }
8534 }
8535 return 0;
8536 }
8537
8538 /* negative occurrence, we scan all the list then walk back */
8539 if (-occ > MAX_HDR_HISTORY)
8540 return 0;
8541
8542 found = hist_ptr = 0;
8543 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8544 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8545 len_hist[hist_ptr] = ctx->vlen;
8546 if (++hist_ptr >= MAX_HDR_HISTORY)
8547 hist_ptr = 0;
8548 found++;
8549 }
8550 if (-occ > found)
8551 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008552
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008553 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008554 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8555 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8556 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008557 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008558 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008559 if (hist_ptr >= MAX_HDR_HISTORY)
8560 hist_ptr -= MAX_HDR_HISTORY;
8561 *vptr = ptr_hist[hist_ptr];
8562 *vlen = len_hist[hist_ptr];
8563 return 1;
8564}
8565
Willy Tarreaubaaee002006-06-26 02:48:02 +02008566/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008567 * Print a debug line with a header. Always stop at the first CR or LF char,
8568 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8569 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008570 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008571void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008572{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008573 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008574 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008575
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008576 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008577 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008578 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008579 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 +02008580
8581 for (max = 0; start + max < end; max++)
8582 if (start[max] == '\r' || start[max] == '\n')
8583 break;
8584
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008585 UBOUND(max, trash.size - trash.len - 3);
8586 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8587 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008588 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008589}
8590
Willy Tarreaueee5b512015-04-03 23:46:31 +02008591
8592/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8593 * The hdr_idx is allocated as well. In case of allocation failure, everything
8594 * allocated is freed and NULL is returned. Otherwise the new transaction is
8595 * assigned to the stream and returned.
8596 */
8597struct http_txn *http_alloc_txn(struct stream *s)
8598{
8599 struct http_txn *txn = s->txn;
8600
8601 if (txn)
8602 return txn;
8603
8604 txn = pool_alloc2(pool2_http_txn);
8605 if (!txn)
8606 return txn;
8607
8608 txn->hdr_idx.size = global.tune.max_http_hdr;
8609 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8610 if (!txn->hdr_idx.v) {
8611 pool_free2(pool2_http_txn, txn);
8612 return NULL;
8613 }
8614
8615 s->txn = txn;
8616 return txn;
8617}
8618
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008619void http_txn_reset_req(struct http_txn *txn)
8620{
8621 txn->req.flags = 0;
8622 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8623 txn->req.next = 0;
8624 txn->req.chunk_len = 0LL;
8625 txn->req.body_len = 0LL;
8626 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8627}
8628
8629void http_txn_reset_res(struct http_txn *txn)
8630{
8631 txn->rsp.flags = 0;
8632 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8633 txn->rsp.next = 0;
8634 txn->rsp.chunk_len = 0LL;
8635 txn->rsp.body_len = 0LL;
8636 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8637}
8638
Willy Tarreau0937bc42009-12-22 15:03:09 +01008639/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008640 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008641 * the required fields are properly allocated and that we only need to (re)init
8642 * them. This should be used before processing any new request.
8643 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008644void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008645{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008646 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008647 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008648
8649 txn->flags = 0;
8650 txn->status = -1;
8651
Willy Tarreauf64d1412010-10-07 20:06:11 +02008652 txn->cookie_first_date = 0;
8653 txn->cookie_last_date = 0;
8654
Willy Tarreaueee5b512015-04-03 23:46:31 +02008655 txn->srv_cookie = NULL;
8656 txn->cli_cookie = NULL;
8657 txn->uri = NULL;
8658
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008659 http_txn_reset_req(txn);
8660 http_txn_reset_res(txn);
8661
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008662 txn->req.chn = &s->req;
8663 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008664
8665 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008666
8667 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8668 if (fe->options2 & PR_O2_REQBUG_OK)
8669 txn->req.err_pos = -1; /* let buggy requests pass */
8670
Willy Tarreau0937bc42009-12-22 15:03:09 +01008671 if (txn->hdr_idx.v)
8672 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008673
8674 vars_init(&s->vars_txn, SCOPE_TXN);
8675 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008676}
8677
8678/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008679void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008680{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008681 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008682 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008683
8684 /* these ones will have been dynamically allocated */
8685 pool_free2(pool2_requri, txn->uri);
8686 pool_free2(pool2_capture, txn->cli_cookie);
8687 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008688 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008689
William Lallemanda73203e2012-03-12 12:48:57 +01008690 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008691 txn->uri = NULL;
8692 txn->srv_cookie = NULL;
8693 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008694
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008695 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008696 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008697 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008698 pool_free2(h->pool, s->req_cap[h->index]);
8699 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008700 }
8701
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008702 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008703 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008704 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008705 pool_free2(h->pool, s->res_cap[h->index]);
8706 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008707 }
8708
Willy Tarreau6204cd92016-03-10 16:33:04 +01008709 vars_prune(&s->vars_txn, s->sess, s);
8710 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008711}
8712
8713/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008714void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008715{
8716 http_end_txn(s);
8717 http_init_txn(s);
8718
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008719 /* reinitialise the current rule list pointer to NULL. We are sure that
8720 * any rulelist match the NULL pointer.
8721 */
8722 s->current_rule_list = NULL;
8723
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008724 s->be = strm_fe(s);
8725 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008726 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008727 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008728 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008729 /* re-init store persistence */
8730 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008731 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008732
Willy Tarreau0937bc42009-12-22 15:03:09 +01008733 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008734
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008735 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008736
Willy Tarreau739cfba2010-01-25 23:11:14 +01008737 /* We must trim any excess data from the response buffer, because we
8738 * may have blocked an invalid response from a server that we don't
8739 * want to accidentely forward once we disable the analysers, nor do
8740 * we want those data to come along with next response. A typical
8741 * example of such data would be from a buggy server responding to
8742 * a HEAD with some data, or sending more than the advertised
8743 * content-length.
8744 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008745 if (unlikely(s->res.buf->i))
8746 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008747
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008748 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008749 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008750
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008751 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008752 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008753
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008754 s->req.rex = TICK_ETERNITY;
8755 s->req.wex = TICK_ETERNITY;
8756 s->req.analyse_exp = TICK_ETERNITY;
8757 s->res.rex = TICK_ETERNITY;
8758 s->res.wex = TICK_ETERNITY;
8759 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008760}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008761
Sasha Pachev218f0642014-06-16 12:05:59 -06008762void free_http_res_rules(struct list *r)
8763{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008764 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008765
8766 list_for_each_entry_safe(pr, tr, r, list) {
8767 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008768 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008769 free(pr);
8770 }
8771}
8772
8773void free_http_req_rules(struct list *r)
8774{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008775 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01008776
8777 list_for_each_entry_safe(pr, tr, r, list) {
8778 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008779 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008780 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008781
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008782 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008783 free(pr);
8784 }
8785}
8786
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008787/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008788struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01008789{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008790 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008791 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008792 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008793 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008794
Vincent Bernat02779b62016-04-03 13:48:43 +02008795 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01008796 if (!rule) {
8797 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008798 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008799 }
8800
CJ Ess108b1dd2015-04-07 12:03:37 -04008801 rule->deny_status = HTTP_ERR_403;
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008802 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008803 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01008804 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02008805 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04008806 int code;
8807 int hc;
8808
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008809 rule->action = ACT_ACTION_DENY;
Willy Tarreauff011f22011-01-06 17:51:27 +01008810 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04008811 if (strcmp(args[cur_arg], "deny_status") == 0) {
8812 cur_arg++;
8813 if (!args[cur_arg]) {
8814 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
8815 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
8816 goto out_err;
8817 }
8818
8819 code = atol(args[cur_arg]);
8820 cur_arg++;
8821 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
8822 if (http_err_codes[hc] == code) {
8823 rule->deny_status = hc;
8824 break;
8825 }
8826 }
8827
8828 if (hc >= HTTP_ERR_SIZE) {
8829 Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n",
8830 file, linenum, code);
8831 }
8832 }
Willy Tarreauccbcc372012-12-27 12:37:57 +01008833 } else if (!strcmp(args[0], "tarpit")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008834 rule->action = ACT_HTTP_REQ_TARPIT;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008835 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008836 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008837 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008838 cur_arg = 1;
8839
8840 while(*args[cur_arg]) {
8841 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008842 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008843 cur_arg+=2;
8844 continue;
8845 } else
8846 break;
8847 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008848 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008849 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008850 cur_arg = 1;
8851
8852 if (!*args[cur_arg] ||
8853 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8854 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8855 file, linenum, args[0]);
8856 goto out_err;
8857 }
8858 rule->arg.nice = atoi(args[cur_arg]);
8859 if (rule->arg.nice < -1024)
8860 rule->arg.nice = -1024;
8861 else if (rule->arg.nice > 1024)
8862 rule->arg.nice = 1024;
8863 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008864 } else if (!strcmp(args[0], "set-tos")) {
8865#ifdef IP_TOS
8866 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008867 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008868 cur_arg = 1;
8869
8870 if (!*args[cur_arg] ||
8871 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8872 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8873 file, linenum, args[0]);
8874 goto out_err;
8875 }
8876
8877 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8878 if (err && *err != '\0') {
8879 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8880 file, linenum, err, args[0]);
8881 goto out_err;
8882 }
8883 cur_arg++;
8884#else
8885 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8886 goto out_err;
8887#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008888 } else if (!strcmp(args[0], "set-mark")) {
8889#ifdef SO_MARK
8890 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008891 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008892 cur_arg = 1;
8893
8894 if (!*args[cur_arg] ||
8895 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8896 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8897 file, linenum, args[0]);
8898 goto out_err;
8899 }
8900
8901 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8902 if (err && *err != '\0') {
8903 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8904 file, linenum, err, args[0]);
8905 goto out_err;
8906 }
8907 cur_arg++;
8908 global.last_checks |= LSTCHK_NETADM;
8909#else
8910 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8911 goto out_err;
8912#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008913 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008914 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008915 cur_arg = 1;
8916
8917 if (!*args[cur_arg] ||
8918 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8919 bad_log_level:
8920 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8921 file, linenum, args[0]);
8922 goto out_err;
8923 }
8924 if (strcmp(args[cur_arg], "silent") == 0)
8925 rule->arg.loglevel = -1;
8926 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8927 goto bad_log_level;
8928 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008929 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008930 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008931 cur_arg = 1;
8932
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008933 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8934 (*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 +01008935 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8936 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008937 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008938 }
8939
8940 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8941 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8942 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008943
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008944 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008945 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008946 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8947 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008948 free(proxy->conf.lfs_file);
8949 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8950 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008951 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02008952 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008953 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008954 cur_arg = 1;
8955
8956 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02008957 (*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 -06008958 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
8959 file, linenum, args[0]);
8960 goto out_err;
8961 }
8962
8963 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8964 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8965 LIST_INIT(&rule->arg.hdr_add.fmt);
8966
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008967 error = NULL;
8968 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
8969 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8970 args[cur_arg + 1], error);
8971 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008972 goto out_err;
8973 }
8974
8975 proxy->conf.args.ctx = ARGC_HRQ;
8976 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
8977 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8978 file, linenum);
8979
8980 free(proxy->conf.lfs_file);
8981 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8982 proxy->conf.lfs_line = proxy->conf.args.line;
8983 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008984 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008985 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008986 cur_arg = 1;
8987
8988 if (!*args[cur_arg] ||
8989 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8990 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8991 file, linenum, args[0]);
8992 goto out_err;
8993 }
8994
8995 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8996 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8997
8998 proxy->conf.args.ctx = ARGC_HRQ;
8999 free(proxy->conf.lfs_file);
9000 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9001 proxy->conf.lfs_line = proxy->conf.args.line;
9002 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009003 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9004 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009005 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009006 struct sample_expr *expr;
9007 unsigned int where;
9008 char *err = NULL;
9009
9010 cur_arg = 1;
9011 proxy->conf.args.ctx = ARGC_TRK;
9012
9013 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9014 if (!expr) {
9015 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9016 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9017 free(err);
9018 goto out_err;
9019 }
9020
9021 where = 0;
9022 if (proxy->cap & PR_CAP_FE)
9023 where |= SMP_VAL_FE_HRQ_HDR;
9024 if (proxy->cap & PR_CAP_BE)
9025 where |= SMP_VAL_BE_HRQ_HDR;
9026
9027 if (!(expr->fetch->val & where)) {
9028 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9029 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9030 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9031 args[cur_arg-1], sample_src_names(expr->fetch->use));
9032 free(expr);
9033 goto out_err;
9034 }
9035
9036 if (strcmp(args[cur_arg], "table") == 0) {
9037 cur_arg++;
9038 if (!args[cur_arg]) {
9039 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9040 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9041 free(expr);
9042 goto out_err;
9043 }
9044 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009045 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02009046 cur_arg++;
9047 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009048 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009049 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009050 } else if (strcmp(args[0], "redirect") == 0) {
9051 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009052 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009053
Willy Tarreaube4653b2015-05-28 15:26:58 +02009054 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009055 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9056 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9057 goto out_err;
9058 }
9059
9060 /* this redirect rule might already contain a parsed condition which
9061 * we'll pass to the http-request rule.
9062 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009063 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009064 rule->arg.redir = redir;
9065 rule->cond = redir->cond;
9066 redir->cond = NULL;
9067 cur_arg = 2;
9068 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009069 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9070 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009071 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009072 /*
9073 * '+ 8' for 'add-acl('
9074 * '- 9' for 'add-acl(' + trailing ')'
9075 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009076 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009077
9078 cur_arg = 1;
9079
9080 if (!*args[cur_arg] ||
9081 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9082 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9083 file, linenum, args[0]);
9084 goto out_err;
9085 }
9086
9087 LIST_INIT(&rule->arg.map.key);
9088 proxy->conf.args.ctx = ARGC_HRQ;
9089 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9090 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9091 file, linenum);
9092 free(proxy->conf.lfs_file);
9093 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9094 proxy->conf.lfs_line = proxy->conf.args.line;
9095 cur_arg += 1;
9096 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9097 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009098 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009099 /*
9100 * '+ 8' for 'del-acl('
9101 * '- 9' for 'del-acl(' + trailing ')'
9102 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009103 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009104
9105 cur_arg = 1;
9106
9107 if (!*args[cur_arg] ||
9108 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9109 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9110 file, linenum, args[0]);
9111 goto out_err;
9112 }
9113
9114 LIST_INIT(&rule->arg.map.key);
9115 proxy->conf.args.ctx = ARGC_HRQ;
9116 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9117 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9118 file, linenum);
9119 free(proxy->conf.lfs_file);
9120 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9121 proxy->conf.lfs_line = proxy->conf.args.line;
9122 cur_arg += 1;
9123 } else if (strncmp(args[0], "del-map", 7) == 0) {
9124 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009125 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009126 /*
9127 * '+ 8' for 'del-map('
9128 * '- 9' for 'del-map(' + trailing ')'
9129 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009130 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009131
9132 cur_arg = 1;
9133
9134 if (!*args[cur_arg] ||
9135 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9136 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9137 file, linenum, args[0]);
9138 goto out_err;
9139 }
9140
9141 LIST_INIT(&rule->arg.map.key);
9142 proxy->conf.args.ctx = ARGC_HRQ;
9143 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9144 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9145 file, linenum);
9146 free(proxy->conf.lfs_file);
9147 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9148 proxy->conf.lfs_line = proxy->conf.args.line;
9149 cur_arg += 1;
9150 } else if (strncmp(args[0], "set-map", 7) == 0) {
9151 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009152 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009153 /*
9154 * '+ 8' for 'set-map('
9155 * '- 9' for 'set-map(' + trailing ')'
9156 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009157 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009158
9159 cur_arg = 1;
9160
9161 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9162 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9163 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9164 file, linenum, args[0]);
9165 goto out_err;
9166 }
9167
9168 LIST_INIT(&rule->arg.map.key);
9169 LIST_INIT(&rule->arg.map.value);
9170 proxy->conf.args.ctx = ARGC_HRQ;
9171
9172 /* key pattern */
9173 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9174 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9175 file, linenum);
9176
9177 /* value pattern */
9178 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9179 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9180 file, linenum);
9181 free(proxy->conf.lfs_file);
9182 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9183 proxy->conf.lfs_line = proxy->conf.args.line;
9184
9185 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009186 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9187 char *errmsg = NULL;
9188 cur_arg = 1;
9189 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009190 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009191 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009192 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009193 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9194 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9195 free(errmsg);
9196 goto out_err;
9197 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009198 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009199 action_build_list(&http_req_keywords.list, &trash);
9200 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
9201 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
William Lallemand2e785f22016-05-25 01:48:42 +02009202 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map'"
9203 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009204 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009205 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009206 }
9207
9208 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9209 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009210 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009211
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009212 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9213 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9214 file, linenum, args[0], errmsg);
9215 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009216 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009217 }
9218 rule->cond = cond;
9219 }
9220 else if (*args[cur_arg]) {
9221 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9222 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9223 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009224 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009225 }
9226
9227 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009228 out_err:
9229 free(rule);
9230 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009231}
9232
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009233/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009234struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009235{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009236 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009237 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009238 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009239 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009240
9241 rule = calloc(1, sizeof(*rule));
9242 if (!rule) {
9243 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9244 goto out_err;
9245 }
9246
9247 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009248 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009249 cur_arg = 1;
9250 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009251 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009252 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009253 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009254 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009255 cur_arg = 1;
9256
9257 if (!*args[cur_arg] ||
9258 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9259 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9260 file, linenum, args[0]);
9261 goto out_err;
9262 }
9263 rule->arg.nice = atoi(args[cur_arg]);
9264 if (rule->arg.nice < -1024)
9265 rule->arg.nice = -1024;
9266 else if (rule->arg.nice > 1024)
9267 rule->arg.nice = 1024;
9268 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009269 } else if (!strcmp(args[0], "set-tos")) {
9270#ifdef IP_TOS
9271 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009272 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009273 cur_arg = 1;
9274
9275 if (!*args[cur_arg] ||
9276 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9277 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9278 file, linenum, args[0]);
9279 goto out_err;
9280 }
9281
9282 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9283 if (err && *err != '\0') {
9284 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9285 file, linenum, err, args[0]);
9286 goto out_err;
9287 }
9288 cur_arg++;
9289#else
9290 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9291 goto out_err;
9292#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009293 } else if (!strcmp(args[0], "set-mark")) {
9294#ifdef SO_MARK
9295 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009296 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009297 cur_arg = 1;
9298
9299 if (!*args[cur_arg] ||
9300 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9301 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9302 file, linenum, args[0]);
9303 goto out_err;
9304 }
9305
9306 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9307 if (err && *err != '\0') {
9308 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9309 file, linenum, err, args[0]);
9310 goto out_err;
9311 }
9312 cur_arg++;
9313 global.last_checks |= LSTCHK_NETADM;
9314#else
9315 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9316 goto out_err;
9317#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009318 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009319 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009320 cur_arg = 1;
9321
9322 if (!*args[cur_arg] ||
9323 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9324 bad_log_level:
9325 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9326 file, linenum, args[0]);
9327 goto out_err;
9328 }
9329 if (strcmp(args[cur_arg], "silent") == 0)
9330 rule->arg.loglevel = -1;
9331 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9332 goto bad_log_level;
9333 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009334 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009335 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009336 cur_arg = 1;
9337
9338 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9339 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9340 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9341 file, linenum, args[0]);
9342 goto out_err;
9343 }
9344
9345 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9346 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9347 LIST_INIT(&rule->arg.hdr_add.fmt);
9348
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009349 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009350 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009351 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9352 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009353 free(proxy->conf.lfs_file);
9354 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9355 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009356 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009357 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009358 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009359 cur_arg = 1;
9360
9361 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009362 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9363 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009364 file, linenum, args[0]);
9365 goto out_err;
9366 }
9367
9368 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9369 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9370 LIST_INIT(&rule->arg.hdr_add.fmt);
9371
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009372 error = NULL;
9373 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9374 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9375 args[cur_arg + 1], error);
9376 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009377 goto out_err;
9378 }
9379
9380 proxy->conf.args.ctx = ARGC_HRQ;
9381 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9382 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9383 file, linenum);
9384
9385 free(proxy->conf.lfs_file);
9386 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9387 proxy->conf.lfs_line = proxy->conf.args.line;
9388 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009389 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009390 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009391 cur_arg = 1;
9392
9393 if (!*args[cur_arg] ||
9394 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9395 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9396 file, linenum, args[0]);
9397 goto out_err;
9398 }
9399
9400 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9401 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9402
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009403 proxy->conf.args.ctx = ARGC_HRS;
9404 free(proxy->conf.lfs_file);
9405 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9406 proxy->conf.lfs_line = proxy->conf.args.line;
9407 cur_arg += 1;
9408 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9409 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009410 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009411 /*
9412 * '+ 8' for 'add-acl('
9413 * '- 9' for 'add-acl(' + trailing ')'
9414 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009415 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009416
9417 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 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009427 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009428 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9429 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9430 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009431 free(proxy->conf.lfs_file);
9432 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9433 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009434
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009435 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009436 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9437 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009438 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009439 /*
9440 * '+ 8' for 'del-acl('
9441 * '- 9' for 'del-acl(' + trailing ')'
9442 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009443 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009444
9445 cur_arg = 1;
9446
9447 if (!*args[cur_arg] ||
9448 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9449 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9450 file, linenum, args[0]);
9451 goto out_err;
9452 }
9453
9454 LIST_INIT(&rule->arg.map.key);
9455 proxy->conf.args.ctx = ARGC_HRS;
9456 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9457 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9458 file, linenum);
9459 free(proxy->conf.lfs_file);
9460 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9461 proxy->conf.lfs_line = proxy->conf.args.line;
9462 cur_arg += 1;
9463 } else if (strncmp(args[0], "del-map", 7) == 0) {
9464 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009465 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009466 /*
9467 * '+ 8' for 'del-map('
9468 * '- 9' for 'del-map(' + trailing ')'
9469 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009470 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009471
9472 cur_arg = 1;
9473
9474 if (!*args[cur_arg] ||
9475 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9476 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9477 file, linenum, args[0]);
9478 goto out_err;
9479 }
9480
9481 LIST_INIT(&rule->arg.map.key);
9482 proxy->conf.args.ctx = ARGC_HRS;
9483 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9484 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9485 file, linenum);
9486 free(proxy->conf.lfs_file);
9487 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9488 proxy->conf.lfs_line = proxy->conf.args.line;
9489 cur_arg += 1;
9490 } else if (strncmp(args[0], "set-map", 7) == 0) {
9491 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009492 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009493 /*
9494 * '+ 8' for 'set-map('
9495 * '- 9' for 'set-map(' + trailing ')'
9496 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009497 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009498
9499 cur_arg = 1;
9500
9501 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9502 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9503 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9504 file, linenum, args[0]);
9505 goto out_err;
9506 }
9507
9508 LIST_INIT(&rule->arg.map.key);
9509 LIST_INIT(&rule->arg.map.value);
9510
9511 proxy->conf.args.ctx = ARGC_HRS;
9512
9513 /* key pattern */
9514 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9515 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9516 file, linenum);
9517
9518 /* value pattern */
9519 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9520 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9521 file, linenum);
9522
9523 free(proxy->conf.lfs_file);
9524 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9525 proxy->conf.lfs_line = proxy->conf.args.line;
9526
9527 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009528 } else if (strcmp(args[0], "redirect") == 0) {
9529 struct redirect_rule *redir;
9530 char *errmsg = NULL;
9531
9532 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9533 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9534 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9535 goto out_err;
9536 }
9537
9538 /* this redirect rule might already contain a parsed condition which
9539 * we'll pass to the http-request rule.
9540 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009541 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009542 rule->arg.redir = redir;
9543 rule->cond = redir->cond;
9544 redir->cond = NULL;
9545 cur_arg = 2;
9546 return rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009547 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9548 char *errmsg = NULL;
9549 cur_arg = 1;
9550 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009551 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009552 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009553 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009554 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9555 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9556 free(errmsg);
9557 goto out_err;
9558 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009559 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009560 action_build_list(&http_res_keywords.list, &trash);
9561 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9562 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
William Lallemand2e785f22016-05-25 01:48:42 +02009563 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map'"
9564 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009565 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009566 goto out_err;
9567 }
9568
9569 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9570 struct acl_cond *cond;
9571 char *errmsg = NULL;
9572
9573 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9574 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9575 file, linenum, args[0], errmsg);
9576 free(errmsg);
9577 goto out_err;
9578 }
9579 rule->cond = cond;
9580 }
9581 else if (*args[cur_arg]) {
9582 Alert("parsing [%s:%d]: 'http-response %s' expects"
9583 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9584 file, linenum, args[0], args[cur_arg]);
9585 goto out_err;
9586 }
9587
9588 return rule;
9589 out_err:
9590 free(rule);
9591 return NULL;
9592}
9593
Willy Tarreau4baae242012-12-27 12:00:31 +01009594/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009595 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009596 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9597 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009598 */
9599struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009600 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009601{
9602 struct redirect_rule *rule;
9603 int cur_arg;
9604 int type = REDIRECT_TYPE_NONE;
9605 int code = 302;
9606 const char *destination = NULL;
9607 const char *cookie = NULL;
9608 int cookie_set = 0;
9609 unsigned int flags = REDIRECT_FLAG_NONE;
9610 struct acl_cond *cond = NULL;
9611
9612 cur_arg = 0;
9613 while (*(args[cur_arg])) {
9614 if (strcmp(args[cur_arg], "location") == 0) {
9615 if (!*args[cur_arg + 1])
9616 goto missing_arg;
9617
9618 type = REDIRECT_TYPE_LOCATION;
9619 cur_arg++;
9620 destination = args[cur_arg];
9621 }
9622 else if (strcmp(args[cur_arg], "prefix") == 0) {
9623 if (!*args[cur_arg + 1])
9624 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009625 type = REDIRECT_TYPE_PREFIX;
9626 cur_arg++;
9627 destination = args[cur_arg];
9628 }
9629 else if (strcmp(args[cur_arg], "scheme") == 0) {
9630 if (!*args[cur_arg + 1])
9631 goto missing_arg;
9632
9633 type = REDIRECT_TYPE_SCHEME;
9634 cur_arg++;
9635 destination = args[cur_arg];
9636 }
9637 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9638 if (!*args[cur_arg + 1])
9639 goto missing_arg;
9640
9641 cur_arg++;
9642 cookie = args[cur_arg];
9643 cookie_set = 1;
9644 }
9645 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9646 if (!*args[cur_arg + 1])
9647 goto missing_arg;
9648
9649 cur_arg++;
9650 cookie = args[cur_arg];
9651 cookie_set = 0;
9652 }
9653 else if (strcmp(args[cur_arg], "code") == 0) {
9654 if (!*args[cur_arg + 1])
9655 goto missing_arg;
9656
9657 cur_arg++;
9658 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009659 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009660 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009661 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009662 args[cur_arg - 1], args[cur_arg]);
9663 return NULL;
9664 }
9665 }
9666 else if (!strcmp(args[cur_arg],"drop-query")) {
9667 flags |= REDIRECT_FLAG_DROP_QS;
9668 }
9669 else if (!strcmp(args[cur_arg],"append-slash")) {
9670 flags |= REDIRECT_FLAG_APPEND_SLASH;
9671 }
9672 else if (strcmp(args[cur_arg], "if") == 0 ||
9673 strcmp(args[cur_arg], "unless") == 0) {
9674 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9675 if (!cond) {
9676 memprintf(errmsg, "error in condition: %s", *errmsg);
9677 return NULL;
9678 }
9679 break;
9680 }
9681 else {
9682 memprintf(errmsg,
9683 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9684 args[cur_arg]);
9685 return NULL;
9686 }
9687 cur_arg++;
9688 }
9689
9690 if (type == REDIRECT_TYPE_NONE) {
9691 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9692 return NULL;
9693 }
9694
Willy Tarreaube4653b2015-05-28 15:26:58 +02009695 if (dir && type != REDIRECT_TYPE_LOCATION) {
9696 memprintf(errmsg, "response only supports redirect type 'location'");
9697 return NULL;
9698 }
9699
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02009700 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +01009701 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009702 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009703
9704 if (!use_fmt) {
9705 /* old-style static redirect rule */
9706 rule->rdr_str = strdup(destination);
9707 rule->rdr_len = strlen(destination);
9708 }
9709 else {
9710 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009711
9712 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9713 * if prefix == "/", we don't want to add anything, otherwise it
9714 * makes it hard for the user to configure a self-redirection.
9715 */
Godbachd9722032014-12-18 15:44:58 +08009716 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009717 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009718 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009719 dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR
9720 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009721 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009722 free(curproxy->conf.lfs_file);
9723 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9724 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009725 }
9726 }
9727
Willy Tarreau4baae242012-12-27 12:00:31 +01009728 if (cookie) {
9729 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9730 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9731 */
9732 rule->cookie_len = strlen(cookie);
9733 if (cookie_set) {
9734 rule->cookie_str = malloc(rule->cookie_len + 10);
9735 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9736 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9737 rule->cookie_len += 9;
9738 } else {
9739 rule->cookie_str = malloc(rule->cookie_len + 21);
9740 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9741 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9742 rule->cookie_len += 20;
9743 }
9744 }
9745 rule->type = type;
9746 rule->code = code;
9747 rule->flags = flags;
9748 LIST_INIT(&rule->list);
9749 return rule;
9750
9751 missing_arg:
9752 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9753 return NULL;
9754}
9755
Willy Tarreau8797c062007-05-07 00:55:35 +02009756/************************************************************************/
9757/* The code below is dedicated to ACL parsing and matching */
9758/************************************************************************/
9759
9760
Willy Tarreau14174bc2012-04-16 14:34:04 +02009761/* This function ensures that the prerequisites for an L7 fetch are ready,
9762 * which means that a request or response is ready. If some data is missing,
9763 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009764 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9765 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009766 *
9767 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009768 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9769 * decide whether or not an HTTP message is present ;
9770 * 0 if the requested data cannot be fetched or if it is certain that
9771 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009772 * 1 if an HTTP message is ready
9773 */
James Rosewell91a41cb2015-09-18 17:11:16 +01009774int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009775 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009776{
Willy Tarreau192252e2015-04-04 01:47:55 +02009777 struct http_txn *txn;
9778 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009779
Willy Tarreaube508f12016-03-10 11:47:01 +01009780 /* Note: it is possible that <s> is NULL when called before stream
9781 * initialization (eg: tcp-request connection), so this function is the
9782 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009783 */
Willy Tarreau192252e2015-04-04 01:47:55 +02009784 if (!s)
9785 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01009786
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +02009787 if (!s->txn) {
9788 if (unlikely(!http_alloc_txn(s)))
9789 return 0; /* not enough memory */
9790 http_init_txn(s);
9791 }
Willy Tarreau192252e2015-04-04 01:47:55 +02009792 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +02009793 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009794
9795 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009796 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009797
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009798 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009799 /* If the buffer does not leave enough free space at the end,
9800 * we must first realign it.
9801 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009802 if (s->req.buf->p > s->req.buf->data &&
9803 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
9804 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009805
Willy Tarreau14174bc2012-04-16 14:34:04 +02009806 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009807 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009808 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009809
9810 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009811 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009812 http_msg_analyzer(msg, &txn->hdr_idx);
9813
9814 /* Still no valid request ? */
9815 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009816 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009817 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009818 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009819 }
9820 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009821 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009822 return 0;
9823 }
9824
9825 /* OK we just got a valid HTTP request. We have some minor
9826 * preparation to perform so that further checks can rely
9827 * on HTTP tests.
9828 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009829
9830 /* If the request was parsed but was too large, we must absolutely
9831 * return an error so that it is not processed. At the moment this
9832 * cannot happen, but if the parsers are to change in the future,
9833 * we want this check to be maintained.
9834 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009835 if (unlikely(s->req.buf->i + s->req.buf->p >
9836 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009837 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009838 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009839 return 1;
9840 }
9841
Willy Tarreau9b28e032012-10-12 23:49:43 +02009842 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009843 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02009844 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009845
Willy Tarreau506d0502013-07-06 13:29:24 +02009846 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9847 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009848 }
9849
Willy Tarreau506d0502013-07-06 13:29:24 +02009850 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009851 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009852 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009853
9854 /* otherwise everything's ready for the request */
9855 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009856 else {
9857 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009858 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9859 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009860 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009861 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009862 }
9863
9864 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009865 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009866 return 1;
9867}
Willy Tarreau8797c062007-05-07 00:55:35 +02009868
Willy Tarreau8797c062007-05-07 00:55:35 +02009869/* 1. Check on METHOD
9870 * We use the pre-parsed method if it is known, and store its number as an
9871 * integer. If it is unknown, we use the pointer and the length.
9872 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009873static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009874{
9875 int len, meth;
9876
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009877 len = strlen(text);
9878 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009879
9880 pattern->val.i = meth;
9881 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +02009882 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009883 pattern->len = len;
9884 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009885 else {
9886 pattern->ptr.str = NULL;
9887 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009888 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009889 return 1;
9890}
9891
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009892/* This function fetches the method of current HTTP request and stores
9893 * it in the global pattern struct as a chunk. There are two possibilities :
9894 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9895 * in <len> and <ptr> is NULL ;
9896 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9897 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009898 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009899 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009900static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009901smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009902{
9903 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +01009904 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009905
Willy Tarreau24e32d82012-04-23 23:55:44 +02009906 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009907
Willy Tarreaube508f12016-03-10 11:47:01 +01009908 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009909 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009910 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009911 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009912 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009913 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9914 /* ensure the indexes are not affected */
9915 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009916 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009917 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
9918 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009919 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009920 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009921 return 1;
9922}
9923
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009924/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009925static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009926{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009927 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009928 struct pattern_list *lst;
9929 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009930
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009931 list_for_each_entry(lst, &expr->patterns, list) {
9932 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009933
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009934 /* well-known method */
9935 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009936 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009937 return pattern;
9938 else
9939 continue;
9940 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009941
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009942 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009943 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009944 continue;
9945
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009946 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009947 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
9948 (!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 +01009949 return pattern;
9950 }
9951 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009952}
9953
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009954static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009955smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009956{
Willy Tarreaube508f12016-03-10 11:47:01 +01009957 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009958 char *ptr;
9959 int len;
9960
Willy Tarreauc0239e02012-04-16 14:42:55 +02009961 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009962
Willy Tarreaube508f12016-03-10 11:47:01 +01009963 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009964 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009965 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009966
9967 while ((len-- > 0) && (*ptr++ != '/'));
9968 if (len <= 0)
9969 return 0;
9970
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009971 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009972 smp->data.u.str.str = ptr;
9973 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009974
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009975 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009976 return 1;
9977}
9978
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009979static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009980smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009981{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009982 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009983 char *ptr;
9984 int len;
9985
Willy Tarreauc0239e02012-04-16 14:42:55 +02009986 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009987
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009988 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009989 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9990 return 0;
9991
Willy Tarreau8797c062007-05-07 00:55:35 +02009992 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009993 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009994
9995 while ((len-- > 0) && (*ptr++ != '/'));
9996 if (len <= 0)
9997 return 0;
9998
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009999 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010000 smp->data.u.str.str = ptr;
10001 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010002
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010003 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010004 return 1;
10005}
10006
10007/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010008static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010009smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010010{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010011 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010012 char *ptr;
10013 int len;
10014
Willy Tarreauc0239e02012-04-16 14:42:55 +020010015 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010016
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010017 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010018 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10019 return 0;
10020
Willy Tarreau8797c062007-05-07 00:55:35 +020010021 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010022 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010023
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010024 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010025 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010026 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010027 return 1;
10028}
10029
Thierry Fournierf4011dd2016-03-29 17:23:51 +020010030static int
10031smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
10032{
10033 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
10034 return 0;
10035
10036 if (!smp->strm->unique_id) {
10037 if ((smp->strm->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
10038 return 0;
10039 smp->strm->unique_id[0] = '\0';
10040 }
10041 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
10042 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
10043
10044 smp->data.type = SMP_T_STR;
10045 smp->data.u.str.str = smp->strm->unique_id;
10046 smp->flags = SMP_F_CONST;
10047 return 1;
10048}
10049
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010050/* returns the longest available part of the body. This requires that the body
10051 * has been waited for using http-buffer-request.
10052 */
10053static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010054smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010055{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010056 struct http_msg *msg;
10057 unsigned long len;
10058 unsigned long block1;
10059 char *body;
10060 struct chunk *temp;
10061
10062 CHECK_HTTP_MESSAGE_FIRST();
10063
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010064 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010065 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010066 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010067 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010068
10069 len = http_body_bytes(msg);
10070 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10071
10072 block1 = len;
10073 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10074 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10075
10076 if (block1 == len) {
10077 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010078 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010079 smp->data.u.str.str = body;
10080 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010081 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10082 }
10083 else {
10084 /* buffer is wrapped, we need to defragment it */
10085 temp = get_trash_chunk();
10086 memcpy(temp->str, body, block1);
10087 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010088 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010089 smp->data.u.str.str = temp->str;
10090 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010091 smp->flags = SMP_F_VOL_TEST;
10092 }
10093 return 1;
10094}
10095
10096
10097/* returns the available length of the body. This requires that the body
10098 * has been waited for using http-buffer-request.
10099 */
10100static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010101smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010102{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010103 struct http_msg *msg;
10104
10105 CHECK_HTTP_MESSAGE_FIRST();
10106
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010107 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010108 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010109 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010110 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010111
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010112 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010113 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010114
10115 smp->flags = SMP_F_VOL_TEST;
10116 return 1;
10117}
10118
10119
10120/* returns the advertised length of the body, or the advertised size of the
10121 * chunks available in the buffer. This requires that the body has been waited
10122 * for using http-buffer-request.
10123 */
10124static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010125smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010126{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010127 struct http_msg *msg;
10128
10129 CHECK_HTTP_MESSAGE_FIRST();
10130
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010131 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010132 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010133 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010134 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010135
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010136 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010137 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010138
10139 smp->flags = SMP_F_VOL_TEST;
10140 return 1;
10141}
10142
10143
Willy Tarreau8797c062007-05-07 00:55:35 +020010144/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010145static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010146smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010147{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010148 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010149
Willy Tarreauc0239e02012-04-16 14:42:55 +020010150 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010151
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010152 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010153 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010154 smp->data.u.str.len = txn->req.sl.rq.u_l;
10155 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010156 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010157 return 1;
10158}
10159
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010160static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010161smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010162{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010163 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010164 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010165
Willy Tarreauc0239e02012-04-16 14:42:55 +020010166 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010167
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010168 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010169 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 +020010170 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010171 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010172
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010173 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010174 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010175 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010176 return 1;
10177}
10178
10179static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010180smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010181{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010182 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010183 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010184
Willy Tarreauc0239e02012-04-16 14:42:55 +020010185 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010186
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010187 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010188 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 +020010189 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10190 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010191
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010192 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010193 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010194 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010195 return 1;
10196}
10197
Willy Tarreau185b5c42012-04-26 15:11:51 +020010198/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10199 * Accepts an optional argument of type string containing the header field name,
10200 * and an optional argument of type signed or unsigned integer to request an
10201 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010202 * headers are considered from the first one. It does not stop on commas and
10203 * returns full lines instead (useful for User-Agent or Date for example).
10204 */
10205static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010206smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010207{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010208 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010209 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010210 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010211 int occ = 0;
10212 const char *name_str = NULL;
10213 int name_len = 0;
10214
10215 if (!ctx) {
10216 /* first call */
10217 ctx = &static_hdr_ctx;
10218 ctx->idx = 0;
10219 smp->ctx.a[0] = ctx;
10220 }
10221
10222 if (args) {
10223 if (args[0].type != ARGT_STR)
10224 return 0;
10225 name_str = args[0].data.str.str;
10226 name_len = args[0].data.str.len;
10227
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010228 if (args[1].type == ARGT_SINT)
10229 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010230 }
10231
10232 CHECK_HTTP_MESSAGE_FIRST();
10233
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010234 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010235 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 +020010236
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010237 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10238 /* search for header from the beginning */
10239 ctx->idx = 0;
10240
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010241 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010242 /* no explicit occurrence and single fetch => last header by default */
10243 occ = -1;
10244
10245 if (!occ)
10246 /* prepare to report multiple occurrences for ACL fetches */
10247 smp->flags |= SMP_F_NOT_LAST;
10248
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010249 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010250 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010251 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 +020010252 return 1;
10253
10254 smp->flags &= ~SMP_F_NOT_LAST;
10255 return 0;
10256}
10257
10258/* 6. Check on HTTP header count. The number of occurrences is returned.
10259 * Accepts exactly 1 argument of type string. It does not stop on commas and
10260 * returns full lines instead (useful for User-Agent or Date for example).
10261 */
10262static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010263smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010264{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010265 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010266 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010267 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010268 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010269 const char *name = NULL;
10270 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010271
Willy Tarreau601a4d12015-04-01 19:16:09 +020010272 if (args && args->type == ARGT_STR) {
10273 name = args->data.str.str;
10274 len = args->data.str.len;
10275 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010276
10277 CHECK_HTTP_MESSAGE_FIRST();
10278
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010279 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010280 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 +020010281
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010282 ctx.idx = 0;
10283 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010284 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010285 cnt++;
10286
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010287 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010288 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010289 smp->flags = SMP_F_VOL_HDR;
10290 return 1;
10291}
10292
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010293static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010294smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010295{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010296 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010297 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010298 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010299 struct chunk *temp;
10300 char del = ',';
10301
10302 if (args && args->type == ARGT_STR)
10303 del = *args[0].data.str.str;
10304
10305 CHECK_HTTP_MESSAGE_FIRST();
10306
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010307 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010308 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 +020010309
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010310 temp = get_trash_chunk();
10311
10312 ctx.idx = 0;
10313 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10314 if (temp->len)
10315 temp->str[temp->len++] = del;
10316 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10317 temp->len += ctx.del;
10318 }
10319
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010320 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010321 smp->data.u.str.str = temp->str;
10322 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010323 smp->flags = SMP_F_VOL_HDR;
10324 return 1;
10325}
10326
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010327/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10328 * Accepts an optional argument of type string containing the header field name,
10329 * and an optional argument of type signed or unsigned integer to request an
10330 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010331 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010332 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010333static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010334smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010335{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010336 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010337 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010338 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010339 int occ = 0;
10340 const char *name_str = NULL;
10341 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010342
Willy Tarreaua890d072013-04-02 12:01:06 +020010343 if (!ctx) {
10344 /* first call */
10345 ctx = &static_hdr_ctx;
10346 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010347 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010348 }
10349
Willy Tarreau185b5c42012-04-26 15:11:51 +020010350 if (args) {
10351 if (args[0].type != ARGT_STR)
10352 return 0;
10353 name_str = args[0].data.str.str;
10354 name_len = args[0].data.str.len;
10355
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010356 if (args[1].type == ARGT_SINT)
10357 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010358 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010359
Willy Tarreaue333ec92012-04-16 16:26:40 +020010360 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010361
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010362 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010363 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 +020010364
Willy Tarreau185b5c42012-04-26 15:11:51 +020010365 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010366 /* search for header from the beginning */
10367 ctx->idx = 0;
10368
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010369 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010370 /* no explicit occurrence and single fetch => last header by default */
10371 occ = -1;
10372
10373 if (!occ)
10374 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010375 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010376
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010377 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010378 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010379 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 +020010380 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010381
Willy Tarreau37406352012-04-23 16:16:37 +020010382 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010383 return 0;
10384}
10385
Willy Tarreauc11416f2007-06-17 16:58:38 +020010386/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010387 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010388 */
10389static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010390smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010391{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010392 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010393 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010394 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010395 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010396 const char *name = NULL;
10397 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010398
Willy Tarreau601a4d12015-04-01 19:16:09 +020010399 if (args && args->type == ARGT_STR) {
10400 name = args->data.str.str;
10401 len = args->data.str.len;
10402 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010403
Willy Tarreaue333ec92012-04-16 16:26:40 +020010404 CHECK_HTTP_MESSAGE_FIRST();
10405
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010406 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010407 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 +020010408
Willy Tarreau33a7e692007-06-10 19:45:56 +020010409 ctx.idx = 0;
10410 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010411 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010412 cnt++;
10413
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010414 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010415 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010416 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010417 return 1;
10418}
10419
Willy Tarreau185b5c42012-04-26 15:11:51 +020010420/* Fetch an HTTP header's integer value. The integer value is returned. It
10421 * takes a mandatory argument of type string and an optional one of type int
10422 * to designate a specific occurrence. It returns an unsigned integer, which
10423 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010424 */
10425static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010426smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010427{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010428 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010429
Willy Tarreauf853c462012-04-23 18:53:56 +020010430 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010431 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010432 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010433 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010434
Willy Tarreaud53e2422012-04-16 17:21:11 +020010435 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010436}
10437
Cyril Bonté69fa9922012-10-25 00:01:06 +020010438/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10439 * and an optional one of type int to designate a specific occurrence.
10440 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010441 */
10442static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010443smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010444{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010445 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010446
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010447 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010448 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010449 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010450 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010451 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010452 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010453 if (smp->data.u.str.len < temp->size - 1) {
10454 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10455 temp->str[smp->data.u.str.len] = '\0';
10456 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010457 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010458 break;
10459 }
10460 }
10461 }
10462
Willy Tarreaud53e2422012-04-16 17:21:11 +020010463 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010464 if (!(smp->flags & SMP_F_NOT_LAST))
10465 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010466 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010467 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010468}
10469
Willy Tarreau737b0c12007-06-10 21:28:46 +020010470/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10471 * the first '/' after the possible hostname, and ends before the possible '?'.
10472 */
10473static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010474smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010475{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010476 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010477 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010478
Willy Tarreauc0239e02012-04-16 14:42:55 +020010479 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010480
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010481 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010482 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010483 ptr = http_get_path(txn);
10484 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010485 return 0;
10486
10487 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010488 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010489 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010490
10491 while (ptr < end && *ptr != '?')
10492 ptr++;
10493
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010494 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010495 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010496 return 1;
10497}
10498
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010499/* This produces a concatenation of the first occurrence of the Host header
10500 * followed by the path component if it begins with a slash ('/'). This means
10501 * that '*' will not be added, resulting in exactly the first Host entry.
10502 * If no Host header is found, then the path is returned as-is. The returned
10503 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010504 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010505 */
10506static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010507smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010508{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010509 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010510 char *ptr, *end, *beg;
10511 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010512 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010513
10514 CHECK_HTTP_MESSAGE_FIRST();
10515
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010516 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010517 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010518 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010519 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010520
10521 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010522 temp = get_trash_chunk();
10523 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010524 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010525 smp->data.u.str.str = temp->str;
10526 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010527
10528 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010529 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010530 beg = http_get_path(txn);
10531 if (!beg)
10532 beg = end;
10533
10534 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10535
10536 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010537 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10538 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010539 }
10540
10541 smp->flags = SMP_F_VOL_1ST;
10542 return 1;
10543}
10544
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010545/* This produces a 32-bit hash of the concatenation of the first occurrence of
10546 * the Host header followed by the path component if it begins with a slash ('/').
10547 * This means that '*' will not be added, resulting in exactly the first Host
10548 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010549 * is hashed using the path hash followed by a full avalanche hash and provides a
10550 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010551 * high-traffic sites without having to store whole paths.
10552 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010553int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010554smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010555{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010556 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010557 struct hdr_ctx ctx;
10558 unsigned int hash = 0;
10559 char *ptr, *beg, *end;
10560 int len;
10561
10562 CHECK_HTTP_MESSAGE_FIRST();
10563
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010564 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010565 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010566 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010567 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10568 ptr = ctx.line + ctx.val;
10569 len = ctx.vlen;
10570 while (len--)
10571 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10572 }
10573
10574 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010575 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010576 beg = http_get_path(txn);
10577 if (!beg)
10578 beg = end;
10579
10580 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10581
10582 if (beg < ptr && *beg == '/') {
10583 while (beg < ptr)
10584 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10585 }
10586 hash = full_hash(hash);
10587
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010588 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010589 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010590 smp->flags = SMP_F_VOL_1ST;
10591 return 1;
10592}
10593
Willy Tarreau4a550602012-12-09 14:53:32 +010010594/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010595 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10596 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10597 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010598 * that in environments where IPv6 is insignificant, truncating the output to
10599 * 8 bytes would still work.
10600 */
10601static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010602smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010603{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010604 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010605 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010606
10607 if (!cli_conn)
10608 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010609
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010610 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010611 return 0;
10612
Willy Tarreau47ca5452012-12-23 20:22:19 +010010613 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010614 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010615 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010616
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010617 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010618 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010619 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010620 temp->len += 4;
10621 break;
10622 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010623 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010624 temp->len += 16;
10625 break;
10626 default:
10627 return 0;
10628 }
10629
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010630 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010631 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010632 return 1;
10633}
10634
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010635/* Extracts the query string, which comes after the question mark '?'. If no
10636 * question mark is found, nothing is returned. Otherwise it returns a sample
10637 * of type string carrying the whole query string.
10638 */
10639static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010640smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010641{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010642 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010643 char *ptr, *end;
10644
10645 CHECK_HTTP_MESSAGE_FIRST();
10646
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010647 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010648 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10649 end = ptr + txn->req.sl.rq.u_l;
10650
10651 /* look up the '?' */
10652 do {
10653 if (ptr == end)
10654 return 0;
10655 } while (*ptr++ != '?');
10656
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010657 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010658 smp->data.u.str.str = ptr;
10659 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010660 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10661 return 1;
10662}
10663
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010664static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010665smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010666{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010667 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10668 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10669 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010670
Willy Tarreau24e32d82012-04-23 23:55:44 +020010671 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010672
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010673 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010674 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010675 return 1;
10676}
10677
Willy Tarreau7f18e522010-10-22 20:04:13 +020010678/* return a valid test if the current request is the first one on the connection */
10679static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010680smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010681{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010682 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010683 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010684 return 1;
10685}
10686
Willy Tarreau34db1082012-04-19 17:16:54 +020010687/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010688static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010689smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010690{
10691
Willy Tarreau24e32d82012-04-23 23:55:44 +020010692 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010693 return 0;
10694
Willy Tarreauc0239e02012-04-16 14:42:55 +020010695 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010696
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010697 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010698 return 0;
10699
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010700 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010701 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010702 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010703 return 1;
10704}
Willy Tarreau8797c062007-05-07 00:55:35 +020010705
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010706/* Accepts exactly 1 argument of type userlist */
10707static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010708smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010709{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010710 if (!args || args->type != ARGT_USR)
10711 return 0;
10712
10713 CHECK_HTTP_MESSAGE_FIRST();
10714
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010715 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010716 return 0;
10717
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010718 /* if the user does not belong to the userlist or has a wrong password,
10719 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010720 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010721 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010722 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10723 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010724 return 0;
10725
10726 /* pat_match_auth() will need the user list */
10727 smp->ctx.a[0] = args->data.usr;
10728
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010729 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010730 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010731 smp->data.u.str.str = smp->strm->txn->auth.user;
10732 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010733
10734 return 1;
10735}
10736
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010737/* Try to find the next occurrence of a cookie name in a cookie header value.
10738 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10739 * the cookie value is returned into *value and *value_l, and the function
10740 * returns a pointer to the next pointer to search from if the value was found.
10741 * Otherwise if the cookie was not found, NULL is returned and neither value
10742 * nor value_l are touched. The input <hdr> string should first point to the
10743 * header's value, and the <hdr_end> pointer must point to the first character
10744 * not part of the value. <list> must be non-zero if value may represent a list
10745 * of values (cookie headers). This makes it faster to abort parsing when no
10746 * list is expected.
10747 */
David Carlier4686f792015-09-25 14:10:50 +010010748char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010749extract_cookie_value(char *hdr, const char *hdr_end,
10750 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010751 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010752{
10753 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10754 char *next;
10755
10756 /* we search at least a cookie name followed by an equal, and more
10757 * generally something like this :
10758 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10759 */
10760 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10761 /* Iterate through all cookies on this line */
10762
10763 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10764 att_beg++;
10765
10766 /* find att_end : this is the first character after the last non
10767 * space before the equal. It may be equal to hdr_end.
10768 */
10769 equal = att_end = att_beg;
10770
10771 while (equal < hdr_end) {
10772 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10773 break;
10774 if (http_is_spht[(unsigned char)*equal++])
10775 continue;
10776 att_end = equal;
10777 }
10778
10779 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10780 * is between <att_beg> and <equal>, both may be identical.
10781 */
10782
10783 /* look for end of cookie if there is an equal sign */
10784 if (equal < hdr_end && *equal == '=') {
10785 /* look for the beginning of the value */
10786 val_beg = equal + 1;
10787 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10788 val_beg++;
10789
10790 /* find the end of the value, respecting quotes */
10791 next = find_cookie_value_end(val_beg, hdr_end);
10792
10793 /* make val_end point to the first white space or delimitor after the value */
10794 val_end = next;
10795 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10796 val_end--;
10797 } else {
10798 val_beg = val_end = next = equal;
10799 }
10800
10801 /* We have nothing to do with attributes beginning with '$'. However,
10802 * they will automatically be removed if a header before them is removed,
10803 * since they're supposed to be linked together.
10804 */
10805 if (*att_beg == '$')
10806 continue;
10807
10808 /* Ignore cookies with no equal sign */
10809 if (equal == next)
10810 continue;
10811
10812 /* Now we have the cookie name between att_beg and att_end, and
10813 * its value between val_beg and val_end.
10814 */
10815
10816 if (att_end - att_beg == cookie_name_l &&
10817 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10818 /* let's return this value and indicate where to go on from */
10819 *value = val_beg;
10820 *value_l = val_end - val_beg;
10821 return next + 1;
10822 }
10823
10824 /* Set-Cookie headers only have the name in the first attr=value part */
10825 if (!list)
10826 break;
10827 }
10828
10829 return NULL;
10830}
10831
William Lallemanda43ba4e2014-01-28 18:14:25 +010010832/* Fetch a captured HTTP request header. The index is the position of
10833 * the "capture" option in the configuration file
10834 */
10835static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010836smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010837{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010838 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010839 int idx;
10840
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010841 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010842 return 0;
10843
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010844 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010845
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010846 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 +010010847 return 0;
10848
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010849 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010850 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010851 smp->data.u.str.str = smp->strm->req_cap[idx];
10852 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010853
10854 return 1;
10855}
10856
10857/* Fetch a captured HTTP response header. The index is the position of
10858 * the "capture" option in the configuration file
10859 */
10860static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010861smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010862{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010863 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010864 int idx;
10865
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010866 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010867 return 0;
10868
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010869 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010870
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010871 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 +010010872 return 0;
10873
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010874 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010875 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010876 smp->data.u.str.str = smp->strm->res_cap[idx];
10877 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010878
10879 return 1;
10880}
10881
William Lallemand65ad6e12014-01-31 15:08:02 +010010882/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10883static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010884smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010885{
10886 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010887 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010010888 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010889
Willy Tarreau15e91e12015-04-04 00:52:09 +020010890 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010891 return 0;
10892
William Lallemand96a77852014-02-05 00:30:02 +010010893 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010894
William Lallemand96a77852014-02-05 00:30:02 +010010895 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10896 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010897
William Lallemand96a77852014-02-05 00:30:02 +010010898 temp = get_trash_chunk();
10899 temp->str = txn->uri;
10900 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010901 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010902 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010903 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010904
10905 return 1;
10906
10907}
10908
10909/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10910static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010911smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010912{
10913 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010914 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010010915 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010916
Willy Tarreau15e91e12015-04-04 00:52:09 +020010917 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010918 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010919
William Lallemand65ad6e12014-01-31 15:08:02 +010010920 ptr = txn->uri;
10921
10922 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10923 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010924
William Lallemand65ad6e12014-01-31 15:08:02 +010010925 if (!*ptr)
10926 return 0;
10927
10928 ptr++; /* skip the space */
10929
10930 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010931 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010932 if (!ptr)
10933 return 0;
10934 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10935 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010936
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010937 smp->data.u.str = *temp;
10938 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010939 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010940 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010941
10942 return 1;
10943}
10944
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010945/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10946 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10947 */
10948static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010949smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010950{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010951 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010952
Willy Tarreau15e91e12015-04-04 00:52:09 +020010953 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010954 return 0;
10955
10956 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010957 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010958 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010959 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010960
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010961 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010962 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010963 smp->flags = SMP_F_CONST;
10964 return 1;
10965
10966}
10967
10968/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10969 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10970 */
10971static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010972smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010973{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010974 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010975
Willy Tarreau15e91e12015-04-04 00:52:09 +020010976 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010977 return 0;
10978
10979 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010980 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010981 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010982 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010983
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010984 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010985 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010986 smp->flags = SMP_F_CONST;
10987 return 1;
10988
10989}
10990
William Lallemand65ad6e12014-01-31 15:08:02 +010010991
Willy Tarreaue333ec92012-04-16 16:26:40 +020010992/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010993 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010994 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010995 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010996 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010997 * Accepts exactly 1 argument of type string. If the input options indicate
10998 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010999 * The returned sample is of type CSTR. Can be used to parse cookies in other
11000 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011001 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011002int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011003{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011004 struct http_txn *txn;
11005 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011006 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011007 const struct http_msg *msg;
11008 const char *hdr_name;
11009 int hdr_name_len;
11010 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011011 int occ = 0;
11012 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011013
Willy Tarreau24e32d82012-04-23 23:55:44 +020011014 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011015 return 0;
11016
Willy Tarreaua890d072013-04-02 12:01:06 +020011017 if (!ctx) {
11018 /* first call */
11019 ctx = &static_hdr_ctx;
11020 ctx->idx = 0;
11021 smp->ctx.a[2] = ctx;
11022 }
11023
Willy Tarreaue333ec92012-04-16 16:26:40 +020011024 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011025
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011026 txn = smp->strm->txn;
11027 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011028
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011029 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011030 msg = &txn->req;
11031 hdr_name = "Cookie";
11032 hdr_name_len = 6;
11033 } else {
11034 msg = &txn->rsp;
11035 hdr_name = "Set-Cookie";
11036 hdr_name_len = 10;
11037 }
11038
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011039 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011040 /* no explicit occurrence and single fetch => last cookie by default */
11041 occ = -1;
11042
11043 /* OK so basically here, either we want only one value and it's the
11044 * last one, or we want to iterate over all of them and we fetch the
11045 * next one.
11046 */
11047
Willy Tarreau9b28e032012-10-12 23:49:43 +020011048 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011049 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011050 /* search for the header from the beginning, we must first initialize
11051 * the search parameters.
11052 */
Willy Tarreau37406352012-04-23 16:16:37 +020011053 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011054 ctx->idx = 0;
11055 }
11056
Willy Tarreau28376d62012-04-26 21:26:10 +020011057 smp->flags |= SMP_F_VOL_HDR;
11058
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011059 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011060 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11061 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011062 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11063 goto out;
11064
Willy Tarreau24e32d82012-04-23 23:55:44 +020011065 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011066 continue;
11067
Willy Tarreau37406352012-04-23 16:16:37 +020011068 smp->ctx.a[0] = ctx->line + ctx->val;
11069 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011070 }
11071
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011072 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011073 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011074 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011075 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011076 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011077 &smp->data.u.str.str,
11078 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011079 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011080 found = 1;
11081 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011082 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011083 smp->flags |= SMP_F_NOT_LAST;
11084 return 1;
11085 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011086 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011087 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011088 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011089 /* all cookie headers and values were scanned. If we're looking for the
11090 * last occurrence, we may return it now.
11091 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011092 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011093 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011094 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011095}
11096
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011097/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011098 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011099 * multiple cookies may be parsed on the same line. The returned sample is of
11100 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011101 */
11102static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011103smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011104{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011105 struct http_txn *txn;
11106 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011107 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011108 const struct http_msg *msg;
11109 const char *hdr_name;
11110 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011111 int cnt;
11112 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011113 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011114
Willy Tarreau24e32d82012-04-23 23:55:44 +020011115 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011116 return 0;
11117
Willy Tarreaue333ec92012-04-16 16:26:40 +020011118 CHECK_HTTP_MESSAGE_FIRST();
11119
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011120 txn = smp->strm->txn;
11121 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011122
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011123 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011124 msg = &txn->req;
11125 hdr_name = "Cookie";
11126 hdr_name_len = 6;
11127 } else {
11128 msg = &txn->rsp;
11129 hdr_name = "Set-Cookie";
11130 hdr_name_len = 10;
11131 }
11132
Willy Tarreau9b28e032012-10-12 23:49:43 +020011133 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011134 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011135 ctx.idx = 0;
11136 cnt = 0;
11137
11138 while (1) {
11139 /* Note: val_beg == NULL every time we need to fetch a new header */
11140 if (!val_beg) {
11141 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11142 break;
11143
Willy Tarreau24e32d82012-04-23 23:55:44 +020011144 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011145 continue;
11146
11147 val_beg = ctx.line + ctx.val;
11148 val_end = val_beg + ctx.vlen;
11149 }
11150
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011151 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011152 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011153 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011154 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011155 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011156 &smp->data.u.str.str,
11157 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011158 cnt++;
11159 }
11160 }
11161
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011162 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011163 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011164 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011165 return 1;
11166}
11167
Willy Tarreau51539362012-05-08 12:46:28 +020011168/* Fetch an cookie's integer value. The integer value is returned. It
11169 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11170 */
11171static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011172smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011173{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011174 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011175
11176 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011177 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011178 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011179 }
11180
11181 return ret;
11182}
11183
Willy Tarreau8797c062007-05-07 00:55:35 +020011184/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011185/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011186/************************************************************************/
11187
David Cournapeau16023ee2010-12-23 20:55:41 +090011188/*
11189 * Given a path string and its length, find the position of beginning of the
11190 * query string. Returns NULL if no query string is found in the path.
11191 *
11192 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11193 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011194 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011195 */
bedis4c75cca2012-10-05 08:38:24 +020011196static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011197{
11198 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011199
bedis4c75cca2012-10-05 08:38:24 +020011200 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011201 return p ? p + 1 : NULL;
11202}
11203
bedis4c75cca2012-10-05 08:38:24 +020011204static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011205{
bedis4c75cca2012-10-05 08:38:24 +020011206 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011207}
11208
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011209/* after increasing a pointer value, it can exceed the first buffer
11210 * size. This function transform the value of <ptr> according with
11211 * the expected position. <chunks> is an array of the one or two
11212 * avalaible chunks. The first value is the start of the first chunk,
11213 * the second value if the end+1 of the first chunks. The third value
11214 * is NULL or the start of the second chunk and the fourth value is
11215 * the end+1 of the second chunk. The function returns 1 if does a
11216 * wrap, else returns 0.
11217 */
11218static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11219{
11220 if (*ptr < chunks[1])
11221 return 0;
11222 if (!chunks[2])
11223 return 0;
11224 *ptr = chunks[2] + ( *ptr - chunks[1] );
11225 return 1;
11226}
11227
David Cournapeau16023ee2010-12-23 20:55:41 +090011228/*
11229 * Given a url parameter, find the starting position of the first occurence,
11230 * or NULL if the parameter is not found.
11231 *
11232 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11233 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011234 *
Willy Tarreauf6625822015-12-27 14:51:01 +010011235 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011236 * or the second chunk. The caller must be check the position before using the
11237 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011238 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011239static const char *
11240find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011241 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011242 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011243{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011244 const char *pos, *last, *equal;
11245 const char **bufs = chunks;
11246 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011247
David Cournapeau16023ee2010-12-23 20:55:41 +090011248
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011249 pos = bufs[0];
11250 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010011251 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011252 /* Check the equal. */
11253 equal = pos + url_param_name_l;
11254 if (fix_pointer_if_wrap(chunks, &equal)) {
11255 if (equal >= chunks[3])
11256 return NULL;
11257 } else {
11258 if (equal >= chunks[1])
11259 return NULL;
11260 }
11261 if (*equal == '=') {
11262 if (pos + url_param_name_l > last) {
11263 /* process wrap case, we detect a wrap. In this case, the
11264 * comparison is performed in two parts.
11265 */
11266
11267 /* This is the end, we dont have any other chunk. */
11268 if (bufs != chunks || !bufs[2])
11269 return NULL;
11270
11271 /* Compute the length of each part of the comparison. */
11272 l1 = last - pos;
11273 l2 = url_param_name_l - l1;
11274
11275 /* The second buffer is too short to contain the compared string. */
11276 if (bufs[2] + l2 > bufs[3])
11277 return NULL;
11278
11279 if (memcmp(pos, url_param_name, l1) == 0 &&
11280 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11281 return pos;
11282
11283 /* Perform wrapping and jump the string who fail the comparison. */
11284 bufs += 2;
11285 pos = bufs[0] + l2;
11286 last = bufs[1];
11287
11288 } else {
11289 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011290 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11291 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011292 pos += url_param_name_l + 1;
11293 if (fix_pointer_if_wrap(chunks, &pos))
11294 last = bufs[2];
11295 }
11296 }
11297
11298 while (1) {
11299 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011300 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011301 pos++;
11302 if (pos < last)
11303 break;
11304 /* process buffer wrapping. */
11305 if (bufs != chunks || !bufs[2])
11306 return NULL;
11307 bufs += 2;
11308 pos = bufs[0];
11309 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011310 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011311 pos++;
11312 }
11313 return NULL;
11314}
11315
11316/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011317 * Given a url parameter name and a query string, find the next value.
11318 * An empty url_param_name matches the first available parameter.
11319 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11320 * respectively provide a pointer to the value and its end.
11321 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011322 */
11323static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011324find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011325 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011326 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011327{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011328 const char *arg_start, *qs_end;
11329 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011330
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011331 arg_start = chunks[0];
11332 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011333 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011334 /* Looks for an argument name. */
11335 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011336 url_param_name, url_param_name_l,
11337 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011338 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011339 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011340 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011341 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011342 if (!arg_start)
11343 return 0;
11344
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011345 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011346 while (1) {
11347 /* looks for the first argument. */
11348 value_start = memchr(arg_start, '=', qs_end - arg_start);
11349 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011350 /* Check for wrapping. */
11351 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011352 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011353 chunks[2]) {
11354 arg_start = chunks[2];
11355 qs_end = chunks[3];
11356 continue;
11357 }
11358 return 0;
11359 }
11360 break;
11361 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011362 value_start++;
11363 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011364 else {
11365 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011366 value_start = arg_start + url_param_name_l + 1;
11367
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011368 /* Check for pointer wrapping. */
11369 if (fix_pointer_if_wrap(chunks, &value_start)) {
11370 /* Update the end pointer. */
11371 qs_end = chunks[3];
11372
11373 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011374 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011375 return 0;
11376 }
11377 }
11378
David Cournapeau16023ee2010-12-23 20:55:41 +090011379 value_end = value_start;
11380
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011381 while (1) {
11382 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11383 value_end++;
11384 if (value_end < qs_end)
11385 break;
11386 /* process buffer wrapping. */
11387 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011388 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011389 chunks[2]) {
11390 value_end = chunks[2];
11391 qs_end = chunks[3];
11392 continue;
11393 }
11394 break;
11395 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011396
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011397 *vstart = value_start;
11398 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011399 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011400}
11401
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011402/* This scans a URL-encoded query string. It takes an optionally wrapping
11403 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11404 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11405 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011406 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011407static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011408smp_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 +090011409{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011410 const char *vstart, *vend;
11411 struct chunk *temp;
11412 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011413
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011414 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011415 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011416 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011417 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011418 return 0;
11419
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011420 /* Create sample. If the value is contiguous, return the pointer as CONST,
11421 * if the value is wrapped, copy-it in a buffer.
11422 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011423 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011424 if (chunks[2] &&
11425 vstart >= chunks[0] && vstart <= chunks[1] &&
11426 vend >= chunks[2] && vend <= chunks[3]) {
11427 /* Wrapped case. */
11428 temp = get_trash_chunk();
11429 memcpy(temp->str, vstart, chunks[1] - vstart);
11430 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011431 smp->data.u.str.str = temp->str;
11432 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011433 } else {
11434 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011435 smp->data.u.str.str = (char *)vstart;
11436 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011437 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11438 }
11439
11440 /* Update context, check wrapping. */
11441 chunks[0] = vend;
11442 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11443 chunks[1] = chunks[3];
11444 chunks[2] = NULL;
11445 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011446
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011447 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011448 smp->flags |= SMP_F_NOT_LAST;
11449
David Cournapeau16023ee2010-12-23 20:55:41 +090011450 return 1;
11451}
11452
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011453/* This function iterates over each parameter of the query string. It uses
11454 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011455 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11456 * An optional parameter name is passed in args[0], otherwise any parameter is
11457 * considered. It supports an optional delimiter argument for the beginning of
11458 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011459 */
11460static int
11461smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11462{
11463 struct http_msg *msg;
11464 char delim = '?';
11465 const char *name;
11466 int name_len;
11467
Dragan Dosen26f77e52015-05-25 10:02:11 +020011468 if (!args ||
11469 (args[0].type && args[0].type != ARGT_STR) ||
11470 (args[1].type && args[1].type != ARGT_STR))
11471 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011472
Dragan Dosen26f77e52015-05-25 10:02:11 +020011473 name = "";
11474 name_len = 0;
11475 if (args->type == ARGT_STR) {
11476 name = args->data.str.str;
11477 name_len = args->data.str.len;
11478 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011479
Dragan Dosen26f77e52015-05-25 10:02:11 +020011480 if (args[1].type)
11481 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011482
Dragan Dosen26f77e52015-05-25 10:02:11 +020011483 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011484 CHECK_HTTP_MESSAGE_FIRST();
11485
11486 msg = &smp->strm->txn->req;
11487
11488 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11489 msg->sl.rq.u_l, delim);
11490 if (!smp->ctx.a[0])
11491 return 0;
11492
11493 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011494
11495 /* Assume that the context is filled with NULL pointer
11496 * before the first call.
11497 * smp->ctx.a[2] = NULL;
11498 * smp->ctx.a[3] = NULL;
11499 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011500 }
11501
11502 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11503}
11504
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011505/* This function iterates over each parameter of the body. This requires
11506 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011507 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11508 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11509 * optional second part if the body wraps at the end of the buffer. An optional
11510 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011511 */
11512static int
11513smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11514{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011515 struct http_msg *msg;
11516 unsigned long len;
11517 unsigned long block1;
11518 char *body;
11519 const char *name;
11520 int name_len;
11521
11522 if (!args || (args[0].type && args[0].type != ARGT_STR))
11523 return 0;
11524
11525 name = "";
11526 name_len = 0;
11527 if (args[0].type == ARGT_STR) {
11528 name = args[0].data.str.str;
11529 name_len = args[0].data.str.len;
11530 }
11531
11532 if (!smp->ctx.a[0]) { // first call, find the query string
11533 CHECK_HTTP_MESSAGE_FIRST();
11534
11535 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011536 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011537 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011538 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011539
11540 len = http_body_bytes(msg);
11541 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11542
11543 block1 = len;
11544 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11545 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11546
11547 if (block1 == len) {
11548 /* buffer is not wrapped (or empty) */
11549 smp->ctx.a[0] = body;
11550 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011551
11552 /* Assume that the context is filled with NULL pointer
11553 * before the first call.
11554 * smp->ctx.a[2] = NULL;
11555 * smp->ctx.a[3] = NULL;
11556 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011557 }
11558 else {
11559 /* buffer is wrapped, we need to defragment it */
11560 smp->ctx.a[0] = body;
11561 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011562 smp->ctx.a[2] = msg->chn->buf->data;
11563 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011564 }
11565 }
11566 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11567}
11568
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011569/* Return the signed integer value for the specified url parameter (see url_param
11570 * above).
11571 */
11572static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011573smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011574{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011575 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011576
11577 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011578 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011579 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011580 }
11581
11582 return ret;
11583}
11584
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011585/* This produces a 32-bit hash of the concatenation of the first occurrence of
11586 * the Host header followed by the path component if it begins with a slash ('/').
11587 * This means that '*' will not be added, resulting in exactly the first Host
11588 * entry. If no Host header is found, then the path is used. The resulting value
11589 * is hashed using the url hash followed by a full avalanche hash and provides a
11590 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11591 * high-traffic sites without having to store whole paths.
11592 * this differs from the base32 functions in that it includes the url parameters
11593 * as well as the path
11594 */
11595static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011596smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011597{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011598 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011599 struct hdr_ctx ctx;
11600 unsigned int hash = 0;
11601 char *ptr, *beg, *end;
11602 int len;
11603
11604 CHECK_HTTP_MESSAGE_FIRST();
11605
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011606 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011607 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011608 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011609 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11610 ptr = ctx.line + ctx.val;
11611 len = ctx.vlen;
11612 while (len--)
11613 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11614 }
11615
11616 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011617 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 +000011618 beg = http_get_path(txn);
11619 if (!beg)
11620 beg = end;
11621
11622 for (ptr = beg; ptr < end ; ptr++);
11623
11624 if (beg < ptr && *beg == '/') {
11625 while (beg < ptr)
11626 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11627 }
11628 hash = full_hash(hash);
11629
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011630 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011631 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011632 smp->flags = SMP_F_VOL_1ST;
11633 return 1;
11634}
11635
11636/* This concatenates the source address with the 32-bit hash of the Host and
11637 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11638 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11639 * on the source address length. The URL hash is stored before the address so
11640 * that in environments where IPv6 is insignificant, truncating the output to
11641 * 8 bytes would still work.
11642 */
11643static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011644smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011645{
11646 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011647 struct connection *cli_conn = objt_conn(smp->sess->origin);
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011648 unsigned int hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011649
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011650 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011651 return 0;
11652
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011653 /* The returned hash is a 32 bytes integer. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011654 hash = smp->data.u.sint;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011655
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011656 temp = get_trash_chunk();
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011657 memcpy(temp->str + temp->len, &hash, sizeof(hash));
11658 temp->len += sizeof(hash);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011659
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011660 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011661 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011662 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011663 temp->len += 4;
11664 break;
11665 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011666 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011667 temp->len += 16;
11668 break;
11669 default:
11670 return 0;
11671 }
11672
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011673 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011674 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011675 return 1;
11676}
11677
Willy Tarreau185b5c42012-04-26 15:11:51 +020011678/* This function is used to validate the arguments passed to any "hdr" fetch
11679 * keyword. These keywords support an optional positive or negative occurrence
11680 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11681 * is assumed that the types are already the correct ones. Returns 0 on error,
11682 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11683 * error message in case of error, that the caller is responsible for freeing.
11684 * The initial location must either be freeable or NULL.
11685 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011686int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011687{
11688 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011689 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011690 return 0;
11691 }
11692 return 1;
11693}
11694
Willy Tarreau276fae92013-07-25 14:36:01 +020011695/* takes an UINT value on input supposed to represent the time since EPOCH,
11696 * adds an optional offset found in args[0] and emits a string representing
11697 * the date in RFC-1123/5322 format.
11698 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011699static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011700{
Cyril Bontéf78d8962016-01-22 19:40:28 +010011701 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020011702 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11703 struct chunk *temp;
11704 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011705 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011706 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011707
11708 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011709 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011710 curr_date += args[0].data.sint;
11711
11712 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011713 if (!tm)
11714 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011715
11716 temp = get_trash_chunk();
11717 temp->len = snprintf(temp->str, temp->size - temp->len,
11718 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11719 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11720 tm->tm_hour, tm->tm_min, tm->tm_sec);
11721
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011722 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011723 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011724 return 1;
11725}
11726
Thierry FOURNIERad903512014-04-11 17:51:01 +020011727/* Match language range with language tag. RFC2616 14.4:
11728 *
11729 * A language-range matches a language-tag if it exactly equals
11730 * the tag, or if it exactly equals a prefix of the tag such
11731 * that the first tag character following the prefix is "-".
11732 *
11733 * Return 1 if the strings match, else return 0.
11734 */
11735static inline int language_range_match(const char *range, int range_len,
11736 const char *tag, int tag_len)
11737{
11738 const char *end = range + range_len;
11739 const char *tend = tag + tag_len;
11740 while (range < end) {
11741 if (*range == '-' && tag == tend)
11742 return 1;
11743 if (*range != *tag || tag == tend)
11744 return 0;
11745 range++;
11746 tag++;
11747 }
11748 /* Return true only if the last char of the tag is matched. */
11749 return tag == tend;
11750}
11751
11752/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011753static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011754{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011755 const char *al = smp->data.u.str.str;
11756 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011757 const char *token;
11758 int toklen;
11759 int qvalue;
11760 const char *str;
11761 const char *w;
11762 int best_q = 0;
11763
11764 /* Set the constant to the sample, because the output of the
11765 * function will be peek in the constant configuration string.
11766 */
11767 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011768 smp->data.u.str.size = 0;
11769 smp->data.u.str.str = "";
11770 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011771
11772 /* Parse the accept language */
11773 while (1) {
11774
11775 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011776 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011777 al++;
11778 if (al >= end)
11779 break;
11780
11781 /* Start of the fisrt word. */
11782 token = al;
11783
11784 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011785 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011786 al++;
11787 if (al == token)
11788 goto expect_comma;
11789
11790 /* Length of the token. */
11791 toklen = al - token;
11792 qvalue = 1000;
11793
11794 /* Check if the token exists in the list. If the token not exists,
11795 * jump to the next token.
11796 */
11797 str = args[0].data.str.str;
11798 w = str;
11799 while (1) {
11800 if (*str == ';' || *str == '\0') {
11801 if (language_range_match(token, toklen, w, str-w))
11802 goto look_for_q;
11803 if (*str == '\0')
11804 goto expect_comma;
11805 w = str + 1;
11806 }
11807 str++;
11808 }
11809 goto expect_comma;
11810
11811look_for_q:
11812
11813 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011814 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011815 al++;
11816 if (al >= end)
11817 goto process_value;
11818
11819 /* If ',' is found, process the result */
11820 if (*al == ',')
11821 goto process_value;
11822
11823 /* If the character is different from ';', look
11824 * for the end of the header part in best effort.
11825 */
11826 if (*al != ';')
11827 goto expect_comma;
11828
11829 /* Assumes that the char is ';', now expect "q=". */
11830 al++;
11831
11832 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011833 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011834 al++;
11835 if (al >= end)
11836 goto process_value;
11837
11838 /* Expect 'q'. If no 'q', continue in best effort */
11839 if (*al != 'q')
11840 goto process_value;
11841 al++;
11842
11843 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011844 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011845 al++;
11846 if (al >= end)
11847 goto process_value;
11848
11849 /* Expect '='. If no '=', continue in best effort */
11850 if (*al != '=')
11851 goto process_value;
11852 al++;
11853
11854 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011855 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011856 al++;
11857 if (al >= end)
11858 goto process_value;
11859
11860 /* Parse the q value. */
11861 qvalue = parse_qvalue(al, &al);
11862
11863process_value:
11864
11865 /* If the new q value is the best q value, then store the associated
11866 * language in the response. If qvalue is the biggest value (1000),
11867 * break the process.
11868 */
11869 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011870 smp->data.u.str.str = (char *)w;
11871 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011872 if (qvalue >= 1000)
11873 break;
11874 best_q = qvalue;
11875 }
11876
11877expect_comma:
11878
11879 /* Expect comma or end. If the end is detected, quit the loop. */
11880 while (al < end && *al != ',')
11881 al++;
11882 if (al >= end)
11883 break;
11884
11885 /* Comma is found, jump it and restart the analyzer. */
11886 al++;
11887 }
11888
11889 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011890 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
11891 smp->data.u.str.str = args[1].data.str.str;
11892 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011893 }
11894
11895 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011896 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011897}
11898
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011899/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011900static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011901{
11902 /* If the constant flag is set or if not size is avalaible at
11903 * the end of the buffer, copy the string in other buffer
11904 * before decoding.
11905 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011906 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011907 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011908 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
11909 smp->data.u.str.str = str->str;
11910 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011911 smp->flags &= ~SMP_F_CONST;
11912 }
11913
11914 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011915 smp->data.u.str.str[smp->data.u.str.len] = '\0';
11916 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011917 return 1;
11918}
11919
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011920static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
11921{
11922 struct proxy *fe = strm_fe(smp->strm);
11923 int idx, i;
11924 struct cap_hdr *hdr;
11925 int len;
11926
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011927 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011928 return 0;
11929
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011930 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011931
11932 /* Check the availibity of the capture id. */
11933 if (idx > fe->nb_req_cap - 1)
11934 return 0;
11935
11936 /* Look for the original configuration. */
11937 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
11938 hdr != NULL && i != idx ;
11939 i--, hdr = hdr->next);
11940 if (!hdr)
11941 return 0;
11942
11943 /* check for the memory allocation */
11944 if (smp->strm->req_cap[hdr->index] == NULL)
11945 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
11946 if (smp->strm->req_cap[hdr->index] == NULL)
11947 return 0;
11948
11949 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011950 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011951 if (len > hdr->len)
11952 len = hdr->len;
11953
11954 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011955 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011956 smp->strm->req_cap[idx][len] = '\0';
11957
11958 return 1;
11959}
11960
11961static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
11962{
11963 struct proxy *fe = strm_fe(smp->strm);
11964 int idx, i;
11965 struct cap_hdr *hdr;
11966 int len;
11967
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011968 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011969 return 0;
11970
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011971 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011972
11973 /* Check the availibity of the capture id. */
11974 if (idx > fe->nb_rsp_cap - 1)
11975 return 0;
11976
11977 /* Look for the original configuration. */
11978 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
11979 hdr != NULL && i != idx ;
11980 i--, hdr = hdr->next);
11981 if (!hdr)
11982 return 0;
11983
11984 /* check for the memory allocation */
11985 if (smp->strm->res_cap[hdr->index] == NULL)
11986 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
11987 if (smp->strm->res_cap[hdr->index] == NULL)
11988 return 0;
11989
11990 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011991 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011992 if (len > hdr->len)
11993 len = hdr->len;
11994
11995 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011996 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011997 smp->strm->res_cap[idx][len] = '\0';
11998
11999 return 1;
12000}
12001
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012002/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012003 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012004 * the relevant part of the request line accordingly. Then it updates various
12005 * pointers to the next elements which were moved, and the total buffer length.
12006 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012007 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12008 * error, though this can be revisited when this code is finally exploited.
12009 *
12010 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12011 * query string and 3 to replace uri.
12012 *
12013 * In query string case, the mark question '?' must be set at the start of the
12014 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012015 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012016int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012017 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012018{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012019 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012020 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012021 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012022 int delta;
12023
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012024 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012025 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012026 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012027 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12028
12029 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012030 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012031 txn->req.sl.rq.m_l += delta;
12032 txn->req.sl.rq.u += delta;
12033 txn->req.sl.rq.v += delta;
12034 break;
12035
12036 case 1: // path
12037 cur_ptr = http_get_path(txn);
12038 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012039 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012040
12041 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012042 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 +010012043 cur_end++;
12044
12045 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012046 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012047 txn->req.sl.rq.u_l += delta;
12048 txn->req.sl.rq.v += delta;
12049 break;
12050
12051 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012052 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012053 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012054 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12055 while (cur_ptr < cur_end && *cur_ptr != '?')
12056 cur_ptr++;
12057
12058 /* skip the question mark or indicate that we must insert it
12059 * (but only if the format string is not empty then).
12060 */
12061 if (cur_ptr < cur_end)
12062 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012063 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012064 offset = 0;
12065
12066 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012067 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012068 txn->req.sl.rq.u_l += delta;
12069 txn->req.sl.rq.v += delta;
12070 break;
12071
12072 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012073 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012074 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12075
12076 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012077 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012078 txn->req.sl.rq.u_l += delta;
12079 txn->req.sl.rq.v += delta;
12080 break;
12081
12082 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012083 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012084 }
12085
12086 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012087 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012088 txn->req.sl.rq.l += delta;
12089 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012090 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012091 return 0;
12092}
12093
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012094/* This function replace the HTTP status code and the associated message. The
12095 * variable <status> contains the new status code. This function never fails.
12096 */
12097void http_set_status(unsigned int status, struct stream *s)
12098{
12099 struct http_txn *txn = s->txn;
12100 char *cur_ptr, *cur_end;
12101 int delta;
12102 char *res;
12103 int c_l;
12104 const char *msg;
12105 int msg_len;
12106
12107 chunk_reset(&trash);
12108
12109 res = ultoa_o(status, trash.str, trash.size);
12110 c_l = res - trash.str;
12111
12112 trash.str[c_l] = ' ';
12113 trash.len = c_l + 1;
12114
12115 msg = get_reason(status);
12116 msg_len = strlen(msg);
12117
12118 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
12119 trash.len += msg_len;
12120
12121 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
12122 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
12123
12124 /* commit changes and adjust message */
12125 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
12126
12127 /* adjust res line offsets and lengths */
12128 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
12129 txn->rsp.sl.st.c_l = c_l;
12130 txn->rsp.sl.st.r_l = msg_len;
12131
12132 delta = trash.len - (cur_end - cur_ptr);
12133 txn->rsp.sl.st.l += delta;
12134 txn->hdr_idx.v[0].len += delta;
12135 http_msg_move_end(&txn->rsp, delta);
12136}
12137
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012138/* This function executes one of the set-{method,path,query,uri} actions. It
12139 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012140 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012141 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012142 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12143 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012144 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012145enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012146 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012147{
12148 chunk_reset(&trash);
12149
12150 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012151 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012152 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012153 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012154
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012155 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012156 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012157}
12158
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012159/* This function is just a compliant action wrapper for "set-status". */
12160enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012161 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012162{
12163 http_set_status(rule->arg.status.code, s);
12164 return ACT_RET_CONT;
12165}
12166
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012167/* parse an http-request action among :
12168 * set-method
12169 * set-path
12170 * set-query
12171 * set-uri
12172 *
12173 * All of them accept a single argument of type string representing a log-format.
12174 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12175 * 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 +020012176 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012177 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012178enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12179 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012180{
12181 int cur_arg = *orig_arg;
12182
Thierry FOURNIER42148732015-09-02 17:17:33 +020012183 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012184
12185 switch (args[0][4]) {
12186 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012187 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012188 rule->action_ptr = http_action_set_req_line;
12189 break;
12190 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012191 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012192 rule->action_ptr = http_action_set_req_line;
12193 break;
12194 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012195 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012196 rule->action_ptr = http_action_set_req_line;
12197 break;
12198 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012199 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012200 rule->action_ptr = http_action_set_req_line;
12201 break;
12202 default:
12203 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012204 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012205 }
12206
12207 if (!*args[cur_arg] ||
12208 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12209 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012210 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012211 }
12212
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012213 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012214 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012215 parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012216 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
12217 proxy->conf.args.file, proxy->conf.args.line);
12218
12219 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012220 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012221}
12222
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012223/* parse set-status action:
12224 * This action accepts a single argument of type int representing
12225 * an http status code. It returns ACT_RET_PRS_OK on success,
12226 * ACT_RET_PRS_ERR on error.
12227 */
12228enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12229 struct act_rule *rule, char **err)
12230{
12231 char *error;
12232
Thierry FOURNIER42148732015-09-02 17:17:33 +020012233 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012234 rule->action_ptr = action_http_set_status;
12235
12236 /* Check if an argument is available */
12237 if (!*args[*orig_arg]) {
12238 memprintf(err, "expects exactly 1 argument <status>");
12239 return ACT_RET_PRS_ERR;
12240 }
12241
12242 /* convert status code as integer */
12243 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12244 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12245 memprintf(err, "expects an integer status code between 100 and 999");
12246 return ACT_RET_PRS_ERR;
12247 }
12248
12249 (*orig_arg)++;
12250 return ACT_RET_PRS_OK;
12251}
12252
Willy Tarreaua9083d02015-05-08 15:27:59 +020012253/* This function executes the "capture" action. It executes a fetch expression,
12254 * turns the result into a string and puts it in a capture slot. It always
12255 * returns 1. If an error occurs the action is cancelled, but the rule
12256 * processing continues.
12257 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012258enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012259 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012260{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012261 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012262 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012263 char **cap = s->req_cap;
12264 int len;
12265
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012266 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 +020012267 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012268 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012269
12270 if (cap[h->index] == NULL)
12271 cap[h->index] = pool_alloc2(h->pool);
12272
12273 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012274 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012275
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012276 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012277 if (len > h->len)
12278 len = h->len;
12279
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012280 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012281 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012282 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012283}
12284
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012285/* This function executes the "capture" action and store the result in a
12286 * capture slot if exists. It executes a fetch expression, turns the result
12287 * into a string and puts it in a capture slot. It always returns 1. If an
12288 * error occurs the action is cancelled, but the rule processing continues.
12289 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012290enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012291 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012292{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012293 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012294 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012295 char **cap = s->req_cap;
12296 struct proxy *fe = strm_fe(s);
12297 int len;
12298 int i;
12299
12300 /* Look for the original configuration. */
12301 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012302 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012303 i--, h = h->next);
12304 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012305 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012306
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012307 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 +020012308 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012309 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012310
12311 if (cap[h->index] == NULL)
12312 cap[h->index] = pool_alloc2(h->pool);
12313
12314 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012315 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012316
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012317 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012318 if (len > h->len)
12319 len = h->len;
12320
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012321 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012322 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012323 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012324}
12325
Willy Tarreaua9083d02015-05-08 15:27:59 +020012326/* parse an "http-request capture" action. It takes a single argument which is
12327 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012328 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012329 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012330 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012331enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12332 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012333{
12334 struct sample_expr *expr;
12335 struct cap_hdr *hdr;
12336 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012337 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012338
12339 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12340 if (strcmp(args[cur_arg], "if") == 0 ||
12341 strcmp(args[cur_arg], "unless") == 0)
12342 break;
12343
12344 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012345 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012346 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012347 }
12348
Willy Tarreaua9083d02015-05-08 15:27:59 +020012349 cur_arg = *orig_arg;
12350 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12351 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012352 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012353
12354 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12355 memprintf(err,
12356 "fetch method '%s' extracts information from '%s', none of which is available here",
12357 args[cur_arg-1], sample_src_names(expr->fetch->use));
12358 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012359 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012360 }
12361
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012362 if (!args[cur_arg] || !*args[cur_arg]) {
12363 memprintf(err, "expects 'len or 'id'");
12364 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012365 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012366 }
12367
Willy Tarreaua9083d02015-05-08 15:27:59 +020012368 if (strcmp(args[cur_arg], "len") == 0) {
12369 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012370
12371 if (!(px->cap & PR_CAP_FE)) {
12372 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012373 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012374 }
12375
12376 proxy->conf.args.ctx = ARGC_CAP;
12377
Willy Tarreaua9083d02015-05-08 15:27:59 +020012378 if (!args[cur_arg]) {
12379 memprintf(err, "missing length value");
12380 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012381 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012382 }
12383 /* we copy the table name for now, it will be resolved later */
12384 len = atoi(args[cur_arg]);
12385 if (len <= 0) {
12386 memprintf(err, "length must be > 0");
12387 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012388 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012389 }
12390 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012391
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012392 if (!len) {
12393 memprintf(err, "a positive 'len' argument is mandatory");
12394 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012395 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012396 }
12397
Vincent Bernat02779b62016-04-03 13:48:43 +020012398 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012399 hdr->next = px->req_cap;
12400 hdr->name = NULL; /* not a header capture */
12401 hdr->namelen = 0;
12402 hdr->len = len;
12403 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12404 hdr->index = px->nb_req_cap++;
12405
12406 px->req_cap = hdr;
12407 px->to_log |= LW_REQHDR;
12408
Thierry FOURNIER42148732015-09-02 17:17:33 +020012409 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012410 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012411 rule->arg.cap.expr = expr;
12412 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012413 }
12414
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012415 else if (strcmp(args[cur_arg], "id") == 0) {
12416 int id;
12417 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012418
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012419 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012420
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012421 if (!args[cur_arg]) {
12422 memprintf(err, "missing id value");
12423 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012424 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012425 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012426
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012427 id = strtol(args[cur_arg], &error, 10);
12428 if (*error != '\0') {
12429 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12430 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012431 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012432 }
12433 cur_arg++;
12434
12435 proxy->conf.args.ctx = ARGC_CAP;
12436
Thierry FOURNIER42148732015-09-02 17:17:33 +020012437 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012438 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012439 rule->arg.capid.expr = expr;
12440 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012441 }
12442
12443 else {
12444 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12445 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012446 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012447 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012448
12449 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012450 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012451}
12452
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012453/* This function executes the "capture" action and store the result in a
12454 * capture slot if exists. It executes a fetch expression, turns the result
12455 * into a string and puts it in a capture slot. It always returns 1. If an
12456 * error occurs the action is cancelled, but the rule processing continues.
12457 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012458enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012459 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012460{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012461 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012462 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012463 char **cap = s->res_cap;
12464 struct proxy *fe = strm_fe(s);
12465 int len;
12466 int i;
12467
12468 /* Look for the original configuration. */
12469 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012470 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012471 i--, h = h->next);
12472 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012473 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012474
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012475 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 +020012476 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012477 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012478
12479 if (cap[h->index] == NULL)
12480 cap[h->index] = pool_alloc2(h->pool);
12481
12482 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012483 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012484
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012485 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012486 if (len > h->len)
12487 len = h->len;
12488
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012489 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012490 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012491 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012492}
12493
12494/* parse an "http-response capture" action. It takes a single argument which is
12495 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12496 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012497 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012498 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012499enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12500 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012501{
12502 struct sample_expr *expr;
12503 int cur_arg;
12504 int id;
12505 char *error;
12506
12507 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12508 if (strcmp(args[cur_arg], "if") == 0 ||
12509 strcmp(args[cur_arg], "unless") == 0)
12510 break;
12511
12512 if (cur_arg < *orig_arg + 3) {
12513 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012514 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012515 }
12516
12517 cur_arg = *orig_arg;
12518 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12519 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012520 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012521
12522 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12523 memprintf(err,
12524 "fetch method '%s' extracts information from '%s', none of which is available here",
12525 args[cur_arg-1], sample_src_names(expr->fetch->use));
12526 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012527 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012528 }
12529
12530 if (!args[cur_arg] || !*args[cur_arg]) {
12531 memprintf(err, "expects 'len or 'id'");
12532 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012533 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012534 }
12535
12536 if (strcmp(args[cur_arg], "id") != 0) {
12537 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12538 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012539 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012540 }
12541
12542 cur_arg++;
12543
12544 if (!args[cur_arg]) {
12545 memprintf(err, "missing id value");
12546 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012547 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012548 }
12549
12550 id = strtol(args[cur_arg], &error, 10);
12551 if (*error != '\0') {
12552 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12553 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012554 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012555 }
12556 cur_arg++;
12557
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012558 proxy->conf.args.ctx = ARGC_CAP;
12559
Thierry FOURNIER42148732015-09-02 17:17:33 +020012560 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012561 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012562 rule->arg.capid.expr = expr;
12563 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012564
12565 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012566 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012567}
12568
William Lallemand73025dd2014-04-24 14:38:37 +020012569/*
12570 * Return the struct http_req_action_kw associated to a keyword.
12571 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012572struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012573{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012574 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012575}
12576
12577/*
12578 * Return the struct http_res_action_kw associated to a keyword.
12579 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012580struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012581{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012582 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012583}
12584
Willy Tarreau4a568972010-05-12 08:08:50 +020012585/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012586/* All supported ACL keywords must be declared here. */
12587/************************************************************************/
12588
12589/* Note: must not be declared <const> as its list will be overwritten.
12590 * Please take care of keeping this list alphabetically sorted.
12591 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012592static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012593 { "base", "base", PAT_MATCH_STR },
12594 { "base_beg", "base", PAT_MATCH_BEG },
12595 { "base_dir", "base", PAT_MATCH_DIR },
12596 { "base_dom", "base", PAT_MATCH_DOM },
12597 { "base_end", "base", PAT_MATCH_END },
12598 { "base_len", "base", PAT_MATCH_LEN },
12599 { "base_reg", "base", PAT_MATCH_REG },
12600 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012601
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012602 { "cook", "req.cook", PAT_MATCH_STR },
12603 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12604 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12605 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12606 { "cook_end", "req.cook", PAT_MATCH_END },
12607 { "cook_len", "req.cook", PAT_MATCH_LEN },
12608 { "cook_reg", "req.cook", PAT_MATCH_REG },
12609 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012610
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012611 { "hdr", "req.hdr", PAT_MATCH_STR },
12612 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12613 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12614 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12615 { "hdr_end", "req.hdr", PAT_MATCH_END },
12616 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12617 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12618 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012619
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012620 /* these two declarations uses strings with list storage (in place
12621 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12622 * and delete functions are relative to the list management. The parse
12623 * and match method are related to the corresponding fetch methods. This
12624 * is very particular ACL declaration mode.
12625 */
12626 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12627 { "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 +020012628
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012629 { "path", "path", PAT_MATCH_STR },
12630 { "path_beg", "path", PAT_MATCH_BEG },
12631 { "path_dir", "path", PAT_MATCH_DIR },
12632 { "path_dom", "path", PAT_MATCH_DOM },
12633 { "path_end", "path", PAT_MATCH_END },
12634 { "path_len", "path", PAT_MATCH_LEN },
12635 { "path_reg", "path", PAT_MATCH_REG },
12636 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012637
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012638 { "req_ver", "req.ver", PAT_MATCH_STR },
12639 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012640
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012641 { "scook", "res.cook", PAT_MATCH_STR },
12642 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12643 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12644 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12645 { "scook_end", "res.cook", PAT_MATCH_END },
12646 { "scook_len", "res.cook", PAT_MATCH_LEN },
12647 { "scook_reg", "res.cook", PAT_MATCH_REG },
12648 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012649
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012650 { "shdr", "res.hdr", PAT_MATCH_STR },
12651 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12652 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12653 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12654 { "shdr_end", "res.hdr", PAT_MATCH_END },
12655 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12656 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12657 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012658
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012659 { "url", "url", PAT_MATCH_STR },
12660 { "url_beg", "url", PAT_MATCH_BEG },
12661 { "url_dir", "url", PAT_MATCH_DIR },
12662 { "url_dom", "url", PAT_MATCH_DOM },
12663 { "url_end", "url", PAT_MATCH_END },
12664 { "url_len", "url", PAT_MATCH_LEN },
12665 { "url_reg", "url", PAT_MATCH_REG },
12666 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012667
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012668 { "urlp", "urlp", PAT_MATCH_STR },
12669 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12670 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12671 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12672 { "urlp_end", "urlp", PAT_MATCH_END },
12673 { "urlp_len", "urlp", PAT_MATCH_LEN },
12674 { "urlp_reg", "urlp", PAT_MATCH_REG },
12675 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012676
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012677 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012678}};
12679
12680/************************************************************************/
12681/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012682/************************************************************************/
12683/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012684static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012685 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012686 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012687 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12688
Willy Tarreau87b09662015-04-03 00:22:06 +020012689 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012690 { "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 +020012691
12692 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012693 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12694 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12695 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012696
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012697 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12698 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012699
Willy Tarreau409bcde2013-01-08 00:31:00 +010012700 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12701 * are only here to match the ACL's name, are request-only and are used
12702 * for ACL compatibility only.
12703 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012704 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12705 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012706 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12707 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012708
12709 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12710 * only here to match the ACL's name, are request-only and are used for
12711 * ACL compatibility only.
12712 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012713 { "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 +020012714 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012715 { "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 +020012716 { "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 +010012717
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012718 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012719 { "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 +010012720 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012721 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012722 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012723 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012724
12725 /* HTTP protocol on the request path */
12726 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012727 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012728
12729 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012730 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12731 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012732
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012733 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012734 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12735 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012736 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012737
Willy Tarreau18ed2562013-01-14 15:56:36 +010012738 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012739 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12740 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012741
Willy Tarreau18ed2562013-01-14 15:56:36 +010012742 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012743 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012744 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12745 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012746
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012747 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012748 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012749 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012750 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012751 { "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 +010012752 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012753 { "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 +010012754
12755 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012756 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012757 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12758 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012759
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012760 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012761 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012762 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012763 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012764 { "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 +010012765 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012766 { "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 +010012767
Willy Tarreau409bcde2013-01-08 00:31:00 +010012768 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012769 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012770 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12771 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012772 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012773
12774 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012775 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012776 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012777 { "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 +020012778 { "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 +010012779
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012780 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020012781 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012782 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012783 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012784 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012785 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012786 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020012787 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12788 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012789 { "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 +010012790 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012791}};
12792
Willy Tarreau8797c062007-05-07 00:55:35 +020012793
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012794/************************************************************************/
12795/* All supported converter keywords must be declared here. */
12796/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012797/* Note: must not be declared <const> as its list will be overwritten */
12798static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012799 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020012800 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012801 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
12802 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012803 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012804 { NULL, NULL, 0, 0, 0 },
12805}};
12806
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012807
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012808/************************************************************************/
12809/* All supported http-request action keywords must be declared here. */
12810/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012811struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012812 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020012813 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012814 { "set-method", parse_set_req_line },
12815 { "set-path", parse_set_req_line },
12816 { "set-query", parse_set_req_line },
12817 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012818 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012819 }
12820};
12821
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012822struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012823 .kw = {
12824 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012825 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012826 { NULL, NULL }
12827 }
12828};
12829
Willy Tarreau8797c062007-05-07 00:55:35 +020012830__attribute__((constructor))
12831static void __http_protocol_init(void)
12832{
12833 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012834 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012835 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012836 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012837 http_res_keywords_register(&http_res_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020012838}
12839
12840
Willy Tarreau58f10d72006-12-04 02:26:12 +010012841/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012842 * Local variables:
12843 * c-indent-level: 8
12844 * c-basic-offset: 8
12845 * End:
12846 */