blob: 3e18a6c84f1956b1646369ed843737ee049661c2 [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;
Thierry Fournier4b788f72016-06-01 13:35:36 +02003293 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003294
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003295 /* If "the current_rule_list" match the executed rule list, we are in
3296 * resume condition. If a resume is needed it is always in the action
3297 * and never in the ACL or converters. In this case, we initialise the
3298 * current rule, and go to the action execution point.
3299 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003300 if (s->current_rule) {
3301 rule = s->current_rule;
3302 s->current_rule = NULL;
3303 if (s->current_rule_list == rules)
3304 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003305 }
3306 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003307
Willy Tarreauff011f22011-01-06 17:51:27 +01003308 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003309
Willy Tarreau96257ec2012-12-27 10:46:37 +01003310 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003311 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003312 int ret;
3313
Willy Tarreau192252e2015-04-04 01:47:55 +02003314 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003315 ret = acl_pass(ret);
3316
Willy Tarreauff011f22011-01-06 17:51:27 +01003317 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003318 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003319
3320 if (!ret) /* condition not matched */
3321 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003322 }
3323
Willy Tarreauacc98002015-09-27 23:34:39 +02003324 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003325resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003326 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003327 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003328 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003329
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003330 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02003331 if (deny_status)
3332 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003333 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003334
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003335 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01003336 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003337 if (deny_status)
3338 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003339 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003340
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003341 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003342 /* Auth might be performed on regular http-req rules as well as on stats */
3343 auth_realm = rule->arg.auth.realm;
3344 if (!auth_realm) {
3345 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3346 auth_realm = STATS_DEFAULT_REALM;
3347 else
3348 auth_realm = px->id;
3349 }
3350 /* send 401/407 depending on whether we use a proxy or not. We still
3351 * count one error, because normal browsing won't significantly
3352 * increase the counter but brute force attempts will.
3353 */
3354 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3355 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003356 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003357 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003358 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003359
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003360 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003361 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3362 return HTTP_RULE_RES_BADREQ;
3363 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003364
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003365 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003366 s->task->nice = rule->arg.nice;
3367 break;
3368
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003369 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003370 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Vincent Bernat6e615892016-05-18 16:17:44 +02003371 inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003372 break;
3373
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003374 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003375#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003376 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003377 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003378#endif
3379 break;
3380
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003381 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003382 s->logs.level = rule->arg.loglevel;
3383 break;
3384
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003385 case ACT_HTTP_REPLACE_HDR:
3386 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003387 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3388 rule->arg.hdr_add.name_len,
3389 &rule->arg.hdr_add.fmt,
3390 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003391 return HTTP_RULE_RES_BADREQ;
3392 break;
3393
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003394 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003395 ctx.idx = 0;
3396 /* remove all occurrences of the header */
3397 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3398 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3399 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003400 }
Willy Tarreau85603282015-01-21 20:39:27 +01003401 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003402
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003403 case ACT_HTTP_SET_HDR:
3404 case ACT_HTTP_ADD_HDR:
Thierry Fournier4b788f72016-06-01 13:35:36 +02003405 /* The scope of the trash buffer must be limited to this function. The
3406 * build_logline() function can execute a lot of other function which
3407 * can use the trash buffer. So for limiting the scope of this global
3408 * buffer, we build first the header value using build_logline, and
3409 * after we store the header name.
3410 */
3411 len = rule->arg.hdr_add.name_len + 2,
3412 len += build_logline(s, trash.str + len, trash.size - len, &rule->arg.hdr_add.fmt);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003413 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
Thierry Fournier4b788f72016-06-01 13:35:36 +02003414 trash.str[rule->arg.hdr_add.name_len] = ':';
3415 trash.str[rule->arg.hdr_add.name_len + 1] = ' ';
3416 trash.len = len;
Willy Tarreau85603282015-01-21 20:39:27 +01003417
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003418 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003419 /* remove all occurrences of the header */
3420 ctx.idx = 0;
3421 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3422 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3423 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3424 }
3425 }
3426
Willy Tarreau96257ec2012-12-27 10:46:37 +01003427 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3428 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003429
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003430 case ACT_HTTP_DEL_ACL:
3431 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003432 struct pat_ref *ref;
3433 char *key;
3434 int len;
3435
3436 /* collect reference */
3437 ref = pat_ref_lookup(rule->arg.map.ref);
3438 if (!ref)
3439 continue;
3440
3441 /* collect key */
3442 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3443 key = trash.str;
3444 key[len] = '\0';
3445
3446 /* perform update */
3447 /* returned code: 1=ok, 0=ko */
3448 pat_ref_delete(ref, key);
3449
3450 break;
3451 }
3452
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003453 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003454 struct pat_ref *ref;
3455 char *key;
3456 struct chunk *trash_key;
3457 int len;
3458
3459 trash_key = get_trash_chunk();
3460
3461 /* collect reference */
3462 ref = pat_ref_lookup(rule->arg.map.ref);
3463 if (!ref)
3464 continue;
3465
3466 /* collect key */
3467 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3468 key = trash_key->str;
3469 key[len] = '\0';
3470
3471 /* perform update */
3472 /* add entry only if it does not already exist */
3473 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003474 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003475
3476 break;
3477 }
3478
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003479 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003480 struct pat_ref *ref;
3481 char *key, *value;
3482 struct chunk *trash_key, *trash_value;
3483 int len;
3484
3485 trash_key = get_trash_chunk();
3486 trash_value = get_trash_chunk();
3487
3488 /* collect reference */
3489 ref = pat_ref_lookup(rule->arg.map.ref);
3490 if (!ref)
3491 continue;
3492
3493 /* collect key */
3494 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3495 key = trash_key->str;
3496 key[len] = '\0';
3497
3498 /* collect value */
3499 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3500 value = trash_value->str;
3501 value[len] = '\0';
3502
3503 /* perform update */
3504 if (pat_ref_find_elt(ref, key) != NULL)
3505 /* update entry if it exists */
3506 pat_ref_set(ref, key, value, NULL);
3507 else
3508 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003509 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003510
3511 break;
3512 }
William Lallemand73025dd2014-04-24 14:38:37 +02003513
Thierry FOURNIER42148732015-09-02 17:17:33 +02003514 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003515 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3516 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003517
Willy Tarreauacc98002015-09-27 23:34:39 +02003518 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003519 case ACT_RET_ERR:
3520 case ACT_RET_CONT:
3521 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003522 case ACT_RET_STOP:
3523 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003524 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003525 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003526 return HTTP_RULE_RES_YIELD;
3527 }
William Lallemand73025dd2014-04-24 14:38:37 +02003528 break;
3529
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003530 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02003531 /* Note: only the first valid tracking parameter of each
3532 * applies.
3533 */
3534
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003535 if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02003536 struct stktable *t;
3537 struct stksess *ts;
3538 struct stktable_key *key;
3539 void *ptr;
3540
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02003541 t = rule->arg.trk_ctr.table.t;
3542 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 +02003543
3544 if (key && (ts = stktable_get_entry(t, key))) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003545 stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003546
3547 /* let's count a new HTTP request as it's the first time we do it */
3548 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3549 if (ptr)
3550 stktable_data_cast(ptr, http_req_cnt)++;
3551
3552 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3553 if (ptr)
3554 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3555 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3556
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003557 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003558 if (sess->fe != s->be)
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003559 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02003560 }
3561 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003562 break;
3563
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003564 /* other flags exists, but normaly, they never be matched. */
3565 default:
3566 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003567 }
3568 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003569
3570 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003571 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003572}
3573
Willy Tarreau71241ab2012-12-27 11:30:54 +01003574
Willy Tarreau51d861a2015-05-22 17:30:48 +02003575/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3576 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3577 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3578 * is returned, the process can continue the evaluation of next rule list. If
3579 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3580 * is returned, it means the operation could not be processed and a server error
3581 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3582 * deny rule. If *YIELD is returned, the caller must call again the function
3583 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003584 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003585static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003586http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003587{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003588 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003589 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003590 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003591 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003592 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02003593 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003594
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003595 /* If "the current_rule_list" match the executed rule list, we are in
3596 * resume condition. If a resume is needed it is always in the action
3597 * and never in the ACL or converters. In this case, we initialise the
3598 * current rule, and go to the action execution point.
3599 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003600 if (s->current_rule) {
3601 rule = s->current_rule;
3602 s->current_rule = NULL;
3603 if (s->current_rule_list == rules)
3604 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003605 }
3606 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003607
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003608 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003609
3610 /* check optional condition */
3611 if (rule->cond) {
3612 int ret;
3613
Willy Tarreau192252e2015-04-04 01:47:55 +02003614 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003615 ret = acl_pass(ret);
3616
3617 if (rule->cond->pol == ACL_COND_UNLESS)
3618 ret = !ret;
3619
3620 if (!ret) /* condition not matched */
3621 continue;
3622 }
3623
Willy Tarreauacc98002015-09-27 23:34:39 +02003624 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003625resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003626 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003627 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003628 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003629
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003630 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003631 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003632 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003633
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003634 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003635 s->task->nice = rule->arg.nice;
3636 break;
3637
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003638 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003639 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Vincent Bernat6e615892016-05-18 16:17:44 +02003640 inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003641 break;
3642
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003643 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003644#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003645 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003646 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003647#endif
3648 break;
3649
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003650 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003651 s->logs.level = rule->arg.loglevel;
3652 break;
3653
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003654 case ACT_HTTP_REPLACE_HDR:
3655 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003656 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3657 rule->arg.hdr_add.name_len,
3658 &rule->arg.hdr_add.fmt,
3659 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003660 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003661 break;
3662
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003663 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003664 ctx.idx = 0;
3665 /* remove all occurrences of the header */
3666 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3667 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3668 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3669 }
Willy Tarreau85603282015-01-21 20:39:27 +01003670 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003671
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003672 case ACT_HTTP_SET_HDR:
3673 case ACT_HTTP_ADD_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003674 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3675 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3676 trash.len = rule->arg.hdr_add.name_len;
3677 trash.str[trash.len++] = ':';
3678 trash.str[trash.len++] = ' ';
3679 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 +01003680
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003681 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003682 /* remove all occurrences of the header */
3683 ctx.idx = 0;
3684 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3685 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3686 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3687 }
3688 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003689 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3690 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003691
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003692 case ACT_HTTP_DEL_ACL:
3693 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003694 struct pat_ref *ref;
3695 char *key;
3696 int len;
3697
3698 /* collect reference */
3699 ref = pat_ref_lookup(rule->arg.map.ref);
3700 if (!ref)
3701 continue;
3702
3703 /* collect key */
3704 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3705 key = trash.str;
3706 key[len] = '\0';
3707
3708 /* perform update */
3709 /* returned code: 1=ok, 0=ko */
3710 pat_ref_delete(ref, key);
3711
3712 break;
3713 }
3714
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003715 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003716 struct pat_ref *ref;
3717 char *key;
3718 struct chunk *trash_key;
3719 int len;
3720
3721 trash_key = get_trash_chunk();
3722
3723 /* collect reference */
3724 ref = pat_ref_lookup(rule->arg.map.ref);
3725 if (!ref)
3726 continue;
3727
3728 /* collect key */
3729 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3730 key = trash_key->str;
3731 key[len] = '\0';
3732
3733 /* perform update */
3734 /* check if the entry already exists */
3735 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003736 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003737
3738 break;
3739 }
3740
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003741 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003742 struct pat_ref *ref;
3743 char *key, *value;
3744 struct chunk *trash_key, *trash_value;
3745 int len;
3746
3747 trash_key = get_trash_chunk();
3748 trash_value = get_trash_chunk();
3749
3750 /* collect reference */
3751 ref = pat_ref_lookup(rule->arg.map.ref);
3752 if (!ref)
3753 continue;
3754
3755 /* collect key */
3756 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3757 key = trash_key->str;
3758 key[len] = '\0';
3759
3760 /* collect value */
3761 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3762 value = trash_value->str;
3763 value[len] = '\0';
3764
3765 /* perform update */
3766 if (pat_ref_find_elt(ref, key) != NULL)
3767 /* update entry if it exists */
3768 pat_ref_set(ref, key, value, NULL);
3769 else
3770 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003771 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003772
3773 break;
3774 }
William Lallemand73025dd2014-04-24 14:38:37 +02003775
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003776 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003777 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3778 return HTTP_RULE_RES_BADREQ;
3779 return HTTP_RULE_RES_DONE;
3780
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003781 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3782 /* Note: only the first valid tracking parameter of each
3783 * applies.
3784 */
3785
3786 if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) {
3787 struct stktable *t;
3788 struct stksess *ts;
3789 struct stktable_key *key;
3790 void *ptr;
3791
3792 t = rule->arg.trk_ctr.table.t;
3793 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
3794
3795 if (key && (ts = stktable_get_entry(t, key))) {
3796 stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts);
3797
3798 /* let's count a new HTTP request as it's the first time we do it */
3799 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3800 if (ptr)
3801 stktable_data_cast(ptr, http_req_cnt)++;
3802
3803 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3804 if (ptr)
3805 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3806 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3807
3808 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3809 if (sess->fe != s->be)
3810 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3811
3812 /* When the client triggers a 4xx from the server, it's most often due
3813 * to a missing object or permission. These events should be tracked
3814 * because if they happen often, it may indicate a brute force or a
3815 * vulnerability scan. Normally this is done when receiving the response
3816 * but here we're tracking after this ought to have been done so we have
3817 * to do it on purpose.
3818 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02003819 if ((unsigned)(txn->status - 400) < 100) {
3820 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3821 if (ptr)
3822 stktable_data_cast(ptr, http_err_cnt)++;
3823
3824 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3825 if (ptr)
3826 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3827 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3828 }
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003829 }
3830 }
3831 break;
3832
Thierry FOURNIER42148732015-09-02 17:17:33 +02003833 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003834 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3835 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003836
Willy Tarreauacc98002015-09-27 23:34:39 +02003837 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003838 case ACT_RET_ERR:
3839 case ACT_RET_CONT:
3840 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003841 case ACT_RET_STOP:
3842 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003843 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003844 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003845 return HTTP_RULE_RES_YIELD;
3846 }
William Lallemand73025dd2014-04-24 14:38:37 +02003847 break;
3848
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003849 /* other flags exists, but normaly, they never be matched. */
3850 default:
3851 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003852 }
3853 }
3854
3855 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003856 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003857}
3858
3859
Willy Tarreau71241ab2012-12-27 11:30:54 +01003860/* Perform an HTTP redirect based on the information in <rule>. The function
3861 * returns non-zero on success, or zero in case of a, irrecoverable error such
3862 * as too large a request to build a valid response.
3863 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003864static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003865{
Willy Tarreaub329a312015-05-22 16:27:37 +02003866 struct http_msg *req = &txn->req;
3867 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003868 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003869 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003870
3871 /* build redirect message */
3872 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003873 case 308:
3874 msg_fmt = HTTP_308;
3875 break;
3876 case 307:
3877 msg_fmt = HTTP_307;
3878 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003879 case 303:
3880 msg_fmt = HTTP_303;
3881 break;
3882 case 301:
3883 msg_fmt = HTTP_301;
3884 break;
3885 case 302:
3886 default:
3887 msg_fmt = HTTP_302;
3888 break;
3889 }
3890
3891 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3892 return 0;
3893
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003894 location = trash.str + trash.len;
3895
Willy Tarreau71241ab2012-12-27 11:30:54 +01003896 switch(rule->type) {
3897 case REDIRECT_TYPE_SCHEME: {
3898 const char *path;
3899 const char *host;
3900 struct hdr_ctx ctx;
3901 int pathlen;
3902 int hostlen;
3903
3904 host = "";
3905 hostlen = 0;
3906 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02003907 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003908 host = ctx.line + ctx.val;
3909 hostlen = ctx.vlen;
3910 }
3911
3912 path = http_get_path(txn);
3913 /* build message using path */
3914 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003915 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003916 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3917 int qs = 0;
3918 while (qs < pathlen) {
3919 if (path[qs] == '?') {
3920 pathlen = qs;
3921 break;
3922 }
3923 qs++;
3924 }
3925 }
3926 } else {
3927 path = "/";
3928 pathlen = 1;
3929 }
3930
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003931 if (rule->rdr_str) { /* this is an old "redirect" rule */
3932 /* check if we can add scheme + "://" + host + path */
3933 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3934 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003935
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003936 /* add scheme */
3937 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3938 trash.len += rule->rdr_len;
3939 }
3940 else {
3941 /* add scheme with executing log format */
3942 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003943
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003944 /* check if we can add scheme + "://" + host + path */
3945 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3946 return 0;
3947 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003948 /* add "://" */
3949 memcpy(trash.str + trash.len, "://", 3);
3950 trash.len += 3;
3951
3952 /* add host */
3953 memcpy(trash.str + trash.len, host, hostlen);
3954 trash.len += hostlen;
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_PREFIX: {
3972 const char *path;
3973 int pathlen;
3974
3975 path = http_get_path(txn);
3976 /* build message using path */
3977 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003978 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003979 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3980 int qs = 0;
3981 while (qs < pathlen) {
3982 if (path[qs] == '?') {
3983 pathlen = qs;
3984 break;
3985 }
3986 qs++;
3987 }
3988 }
3989 } else {
3990 path = "/";
3991 pathlen = 1;
3992 }
3993
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003994 if (rule->rdr_str) { /* this is an old "redirect" rule */
3995 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3996 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003997
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003998 /* add prefix. Note that if prefix == "/", we don't want to
3999 * add anything, otherwise it makes it hard for the user to
4000 * configure a self-redirection.
4001 */
4002 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
4003 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4004 trash.len += rule->rdr_len;
4005 }
4006 }
4007 else {
4008 /* add prefix with executing log format */
4009 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
4010
4011 /* Check length */
4012 if (trash.len + pathlen > trash.size - 4)
4013 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004014 }
4015
4016 /* add path */
4017 memcpy(trash.str + trash.len, path, pathlen);
4018 trash.len += pathlen;
4019
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004020 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004021 if (trash.len && trash.str[trash.len - 1] != '/' &&
4022 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4023 if (trash.len > trash.size - 5)
4024 return 0;
4025 trash.str[trash.len] = '/';
4026 trash.len++;
4027 }
4028
4029 break;
4030 }
4031 case REDIRECT_TYPE_LOCATION:
4032 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004033 if (rule->rdr_str) { /* this is an old "redirect" rule */
4034 if (trash.len + rule->rdr_len > trash.size - 4)
4035 return 0;
4036
4037 /* add location */
4038 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4039 trash.len += rule->rdr_len;
4040 }
4041 else {
4042 /* add location with executing log format */
4043 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004044
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004045 /* Check left length */
4046 if (trash.len > trash.size - 4)
4047 return 0;
4048 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004049 break;
4050 }
4051
4052 if (rule->cookie_len) {
4053 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
4054 trash.len += 14;
4055 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4056 trash.len += rule->cookie_len;
4057 memcpy(trash.str + trash.len, "\r\n", 2);
4058 trash.len += 2;
4059 }
4060
4061 /* add end of headers and the keep-alive/close status.
4062 * We may choose to set keep-alive if the Location begins
4063 * with a slash, because the client will come back to the
4064 * same server.
4065 */
4066 txn->status = rule->code;
4067 /* let's log the request time */
4068 s->logs.tv_request = now;
4069
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004070 if (*location == '/' &&
Willy Tarreaub329a312015-05-22 16:27:37 +02004071 (req->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau2de8a502015-05-28 17:23:54 +02004072 ((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004073 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4074 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4075 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02004076 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004077 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4078 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4079 trash.len += 30;
4080 } else {
4081 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4082 trash.len += 24;
4083 }
4084 }
4085 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4086 trash.len += 4;
Christopher Faulet3e344292015-11-24 16:24:13 +01004087 FLT_STRM_CB(s, flt_http_reply(s, txn->status, &trash));
Willy Tarreaub329a312015-05-22 16:27:37 +02004088 bo_inject(res->chn, trash.str, trash.len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004089 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02004090 bi_fast_delete(req->chn->buf, req->sov);
4091 req->next -= req->sov;
4092 req->sov = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004093 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_FLT_END);
4094 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->req.analysers & AN_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02004095 req->msg_state = HTTP_MSG_CLOSED;
4096 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02004097 /* Trim any possible response */
4098 res->chn->buf->i = 0;
4099 res->next = res->sov = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004100 } else {
4101 /* keep-alive not possible */
4102 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4103 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4104 trash.len += 29;
4105 } else {
4106 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4107 trash.len += 23;
4108 }
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004109 http_reply_and_close(s, txn->status, &trash);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004110 req->chn->analysers &= AN_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004111 }
4112
Willy Tarreaue7dff022015-04-03 01:14:29 +02004113 if (!(s->flags & SF_ERR_MASK))
4114 s->flags |= SF_ERR_LOCAL;
4115 if (!(s->flags & SF_FINST_MASK))
4116 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004117
4118 return 1;
4119}
4120
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004121/* This stream analyser runs all HTTP request processing which is common to
4122 * frontends and backends, which means blocking ACLs, filters, connection-close,
4123 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004124 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004125 * either needs more data or wants to immediately abort the request (eg: deny,
4126 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004127 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004128int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004129{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004130 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004131 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004132 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004133 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004134 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004135 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004136 int deny_status = HTTP_ERR_403;
Willy Tarreaud787e662009-07-07 10:14:51 +02004137
Willy Tarreau655dce92009-11-08 13:10:58 +01004138 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004139 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004140 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004141 }
4142
Willy Tarreau87b09662015-04-03 00:22:06 +02004143 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 +02004144 now_ms, __FUNCTION__,
4145 s,
4146 req,
4147 req->rex, req->wex,
4148 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004149 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004150 req->analysers);
4151
Willy Tarreau65410832014-04-28 21:25:43 +02004152 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004153 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004154
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004155 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004156 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02004157 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01004158
Willy Tarreau0b748332014-04-29 00:13:29 +02004159 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004160 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4161 goto return_prx_yield;
4162
Willy Tarreau0b748332014-04-29 00:13:29 +02004163 case HTTP_RULE_RES_CONT:
4164 case HTTP_RULE_RES_STOP: /* nothing to do */
4165 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004166
Willy Tarreau0b748332014-04-29 00:13:29 +02004167 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4168 if (txn->flags & TX_CLTARPIT)
4169 goto tarpit;
4170 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004171
Willy Tarreau0b748332014-04-29 00:13:29 +02004172 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4173 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004174
Willy Tarreau0b748332014-04-29 00:13:29 +02004175 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004176 goto done;
4177
Willy Tarreau0b748332014-04-29 00:13:29 +02004178 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4179 goto return_bad_req;
4180 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004181 }
4182
Willy Tarreau52542592014-04-28 18:33:26 +02004183 /* OK at this stage, we know that the request was accepted according to
4184 * the http-request rules, we can check for the stats. Note that the
4185 * URI is detected *before* the req* rules in order not to be affected
4186 * by a possible reqrep, while they are processed *after* so that a
4187 * reqdeny can still block them. This clearly needs to change in 1.6!
4188 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004189 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004190 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004191 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004192 txn->status = 500;
4193 s->logs.tv_request = now;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004194 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004195
Willy Tarreaue7dff022015-04-03 01:14:29 +02004196 if (!(s->flags & SF_ERR_MASK))
4197 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004198 goto return_prx_cond;
4199 }
4200
4201 /* parse the whole stats request and extract the relevant information */
4202 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02004203 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02004204 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004205
Willy Tarreau0b748332014-04-29 00:13:29 +02004206 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4207 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004208
Willy Tarreau0b748332014-04-29 00:13:29 +02004209 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4210 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004211 }
4212
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004213 /* evaluate the req* rules except reqadd */
4214 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004215 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004216 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004217
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004218 if (txn->flags & TX_CLDENY)
4219 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004220
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004221 if (txn->flags & TX_CLTARPIT)
4222 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004223 }
Willy Tarreau06619262006-12-17 08:37:22 +01004224
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004225 /* add request headers from the rule sets in the same order */
4226 list_for_each_entry(wl, &px->req_add, list) {
4227 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004228 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004229 ret = acl_pass(ret);
4230 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4231 ret = !ret;
4232 if (!ret)
4233 continue;
4234 }
4235
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004236 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004237 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004238 }
4239
Willy Tarreau52542592014-04-28 18:33:26 +02004240
4241 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004242 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004243 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004244 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4245 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004246
Willy Tarreaue7dff022015-04-03 01:14:29 +02004247 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4248 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4249 if (!(s->flags & SF_FINST_MASK))
4250 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004251
Willy Tarreau70730dd2014-04-24 18:06:27 +02004252 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet309c6412015-12-02 09:57:32 +01004253 req->analysers &= (AN_REQ_HTTP_BODY | AN_FLT_HTTP_HDRS | AN_FLT_END);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004254 req->analysers &= ~AN_FLT_XFER_DATA;
4255 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004256 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004257 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004258
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004259 /* check whether we have some ACLs set to redirect this request */
4260 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004261 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004262 int ret;
4263
Willy Tarreau192252e2015-04-04 01:47:55 +02004264 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004265 ret = acl_pass(ret);
4266 if (rule->cond->pol == ACL_COND_UNLESS)
4267 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004268 if (!ret)
4269 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004270 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004271 if (!http_apply_redirect_rule(rule, s, txn))
4272 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004273 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004274 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004275
Willy Tarreau2be39392010-01-03 17:24:51 +01004276 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4277 * If this happens, then the data will not come immediately, so we must
4278 * send all what we have without waiting. Note that due to the small gain
4279 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004280 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004281 * itself once used.
4282 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004283 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004284
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004285 done: /* done with this analyser, continue with next ones that the calling
4286 * points will have set, if any.
4287 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004288 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004289 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4290 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004291 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004292
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004293 tarpit:
4294 /* When a connection is tarpitted, we use the tarpit timeout,
4295 * which may be the same as the connect timeout if unspecified.
4296 * If unset, then set it to zero because we really want it to
4297 * eventually expire. We build the tarpit as an analyser.
4298 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004299 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004300
4301 /* wipe the request out so that we can drop the connection early
4302 * if the client closes first.
4303 */
4304 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004305
4306 /* Allow cookie logging
4307 */
4308 if (s->be->cookie_name || sess->fe->capture_name)
4309 manage_client_side_cookies(s, req);
4310
Christopher Fauletd7c91962015-04-30 11:48:27 +02004311 req->analysers &= AN_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004312 req->analysers |= AN_REQ_HTTP_TARPIT;
4313 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4314 if (!req->analyse_exp)
4315 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004316 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004317 sess->fe->fe_counters.denied_req++;
4318 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004319 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004320 if (sess->listener->counters)
4321 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004322 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004323
4324 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004325
4326 /* Allow cookie logging
4327 */
4328 if (s->be->cookie_name || sess->fe->capture_name)
4329 manage_client_side_cookies(s, req);
4330
Willy Tarreau0b748332014-04-29 00:13:29 +02004331 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004332 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004333 s->logs.tv_request = now;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004334 http_reply_and_close(s, txn->status, http_error_message(s, deny_status));
Willy Tarreau87b09662015-04-03 00:22:06 +02004335 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004336 sess->fe->fe_counters.denied_req++;
4337 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004338 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004339 if (sess->listener->counters)
4340 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004341 goto return_prx_cond;
4342
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004343 return_bad_req:
4344 /* We centralize bad requests processing here */
4345 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4346 /* we detected a parsing error. We want to archive this request
4347 * in the dedicated proxy area for later troubleshooting.
4348 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004349 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004350 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004351
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004352 txn->req.msg_state = HTTP_MSG_ERROR;
4353 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004354 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004355
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004356 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004357 if (sess->listener->counters)
4358 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004359
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004360 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004361 if (!(s->flags & SF_ERR_MASK))
4362 s->flags |= SF_ERR_PRXCOND;
4363 if (!(s->flags & SF_FINST_MASK))
4364 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004365
Christopher Fauletd7c91962015-04-30 11:48:27 +02004366 req->analysers &= AN_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004367 req->analyse_exp = TICK_ETERNITY;
4368 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004369
4370 return_prx_yield:
4371 channel_dont_connect(req);
4372 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004373}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004374
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004375/* This function performs all the processing enabled for the current request.
4376 * It returns 1 if the processing can continue on next analysers, or zero if it
4377 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004378 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004379 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004380int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004381{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004382 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004383 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004384 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004385 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004386
Willy Tarreau655dce92009-11-08 13:10:58 +01004387 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004388 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004389 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004390 return 0;
4391 }
4392
Willy Tarreau87b09662015-04-03 00:22:06 +02004393 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 +02004394 now_ms, __FUNCTION__,
4395 s,
4396 req,
4397 req->rex, req->wex,
4398 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004399 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004400 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004401
Willy Tarreau59234e92008-11-30 23:51:27 +01004402 /*
4403 * Right now, we know that we have processed the entire headers
4404 * and that unwanted requests have been filtered out. We can do
4405 * whatever we want with the remaining request. Also, now we
4406 * may have separate values for ->fe, ->be.
4407 */
Willy Tarreau06619262006-12-17 08:37:22 +01004408
Willy Tarreau59234e92008-11-30 23:51:27 +01004409 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004410 * If HTTP PROXY is set we simply get remote server address parsing
4411 * incoming request. Note that this requires that a connection is
4412 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004413 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004414 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004415 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004416 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004417
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004418 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau973a5422015-08-05 21:47:23 +02004419 if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004420 txn->req.msg_state = HTTP_MSG_ERROR;
4421 txn->status = 500;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004422 req->analysers &= AN_FLT_END;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004423 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004424
Willy Tarreaue7dff022015-04-03 01:14:29 +02004425 if (!(s->flags & SF_ERR_MASK))
4426 s->flags |= SF_ERR_RESOURCE;
4427 if (!(s->flags & SF_FINST_MASK))
4428 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004429
4430 return 0;
4431 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004432
4433 path = http_get_path(txn);
4434 url2sa(req->buf->p + msg->sl.rq.u,
4435 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004436 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004437 /* if the path was found, we have to remove everything between
4438 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4439 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4440 * u_l characters by a single "/".
4441 */
4442 if (path) {
4443 char *cur_ptr = req->buf->p;
4444 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4445 int delta;
4446
4447 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4448 http_msg_move_end(&txn->req, delta);
4449 cur_end += delta;
4450 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4451 goto return_bad_req;
4452 }
4453 else {
4454 char *cur_ptr = req->buf->p;
4455 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4456 int delta;
4457
4458 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4459 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4460 http_msg_move_end(&txn->req, delta);
4461 cur_end += delta;
4462 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4463 goto return_bad_req;
4464 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004465 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004466
Willy Tarreau59234e92008-11-30 23:51:27 +01004467 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004468 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004469 * Note that doing so might move headers in the request, but
4470 * the fields will stay coherent and the URI will not move.
4471 * This should only be performed in the backend.
4472 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004473 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004474 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004475
William Lallemanda73203e2012-03-12 12:48:57 +01004476 /* add unique-id if "header-unique-id" is specified */
4477
Thierry Fournierf4011dd2016-03-29 17:23:51 +02004478 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004479 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4480 goto return_bad_req;
4481 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004482 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004483 }
William Lallemanda73203e2012-03-12 12:48:57 +01004484
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004485 if (sess->fe->header_unique_id && s->unique_id) {
4486 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004487 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004488 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004489 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004490 goto return_bad_req;
4491 }
4492
Cyril Bontéb21570a2009-11-29 20:04:48 +01004493 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004494 * 9: add X-Forwarded-For if either the frontend or the backend
4495 * asks for it.
4496 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004497 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004498 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004499 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4500 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4501 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004502 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004503 /* The header is set to be added only if none is present
4504 * and we found it, so don't do anything.
4505 */
4506 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004507 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004508 /* Add an X-Forwarded-For header unless the source IP is
4509 * in the 'except' network range.
4510 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004511 if ((!sess->fe->except_mask.s_addr ||
4512 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4513 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004514 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004515 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004516 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004517 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004518 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004519 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004520
4521 /* Note: we rely on the backend to get the header name to be used for
4522 * x-forwarded-for, because the header is really meant for the backends.
4523 * However, if the backend did not specify any option, we have to rely
4524 * on the frontend's header name.
4525 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004526 if (s->be->fwdfor_hdr_len) {
4527 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004528 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004529 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004530 len = sess->fe->fwdfor_hdr_len;
4531 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004532 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004533 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 +01004534
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004535 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004536 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004537 }
4538 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004539 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004540 /* FIXME: for the sake of completeness, we should also support
4541 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004542 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004543 int len;
4544 char pn[INET6_ADDRSTRLEN];
4545 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004546 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004547 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004548
Willy Tarreau59234e92008-11-30 23:51:27 +01004549 /* Note: we rely on the backend to get the header name to be used for
4550 * x-forwarded-for, because the header is really meant for the backends.
4551 * However, if the backend did not specify any option, we have to rely
4552 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004553 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004554 if (s->be->fwdfor_hdr_len) {
4555 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004556 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004557 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004558 len = sess->fe->fwdfor_hdr_len;
4559 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004560 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004561 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004562
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004563 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004564 goto return_bad_req;
4565 }
4566 }
4567
4568 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004569 * 10: add X-Original-To if either the frontend or the backend
4570 * asks for it.
4571 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004572 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004573
4574 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004575 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004576 /* Add an X-Original-To header unless the destination IP is
4577 * in the 'except' network range.
4578 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004579 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004580
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004581 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004582 ((!sess->fe->except_mask_to.s_addr ||
4583 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4584 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004585 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004586 (((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 +02004587 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004588 int len;
4589 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004590 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004591
4592 /* Note: we rely on the backend to get the header name to be used for
4593 * x-original-to, because the header is really meant for the backends.
4594 * However, if the backend did not specify any option, we have to rely
4595 * on the frontend's header name.
4596 */
4597 if (s->be->orgto_hdr_len) {
4598 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004599 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004600 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004601 len = sess->fe->orgto_hdr_len;
4602 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004603 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004604 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 +02004605
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004606 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004607 goto return_bad_req;
4608 }
4609 }
4610 }
4611
Willy Tarreau50fc7772012-11-11 22:19:57 +01004612 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4613 * If an "Upgrade" token is found, the header is left untouched in order not to have
4614 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4615 * "Upgrade" is present in the Connection header.
4616 */
4617 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4618 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004619 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004620 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004621 unsigned int want_flags = 0;
4622
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004623 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004624 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004625 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004626 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004627 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004628 want_flags |= TX_CON_CLO_SET;
4629 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004630 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004631 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004632 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004633 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004634 want_flags |= TX_CON_KAL_SET;
4635 }
4636
4637 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004638 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004639 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004640
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004641
Willy Tarreau522d6c02009-12-06 18:49:18 +01004642 /* If we have no server assigned yet and we're balancing on url_param
4643 * with a POST request, we may be interested in checking the body for
4644 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004645 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004646 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004647 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004648 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004649 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004650 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004651 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004652
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004653 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02004654 req->analysers &= ~AN_FLT_XFER_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004655 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004656#ifdef TCP_QUICKACK
4657 /* We expect some data from the client. Unless we know for sure
4658 * we already have a full request, we have to re-enable quick-ack
4659 * in case we previously disabled it, otherwise we might cause
4660 * the client to delay further data.
4661 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004662 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004663 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004664 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004665 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004666 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004667#endif
4668 }
Willy Tarreau03945942009-12-22 16:50:27 +01004669
Willy Tarreau59234e92008-11-30 23:51:27 +01004670 /*************************************************************
4671 * OK, that's finished for the headers. We have done what we *
4672 * could. Let's switch to the DATA state. *
4673 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004674 req->analyse_exp = TICK_ETERNITY;
4675 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004676
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004677 /* if the server closes the connection, we want to immediately react
4678 * and close the socket to save packets and syscalls.
4679 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004680 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004681 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004682
Willy Tarreau59234e92008-11-30 23:51:27 +01004683 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004684 /* OK let's go on with the BODY now */
4685 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004686
Willy Tarreau59234e92008-11-30 23:51:27 +01004687 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004688 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004689 /* we detected a parsing error. We want to archive this request
4690 * in the dedicated proxy area for later troubleshooting.
4691 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004692 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004693 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004694
Willy Tarreau59234e92008-11-30 23:51:27 +01004695 txn->req.msg_state = HTTP_MSG_ERROR;
4696 txn->status = 400;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004697 req->analysers &= AN_FLT_END;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004698 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004699
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004700 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004701 if (sess->listener->counters)
4702 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004703
Willy Tarreaue7dff022015-04-03 01:14:29 +02004704 if (!(s->flags & SF_ERR_MASK))
4705 s->flags |= SF_ERR_PRXCOND;
4706 if (!(s->flags & SF_FINST_MASK))
4707 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004708 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004709}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004710
Willy Tarreau60b85b02008-11-30 23:28:40 +01004711/* This function is an analyser which processes the HTTP tarpit. It always
4712 * returns zero, at the beginning because it prevents any other processing
4713 * from occurring, and at the end because it terminates the request.
4714 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004715int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004716{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004717 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004718
4719 /* This connection is being tarpitted. The CLIENT side has
4720 * already set the connect expiration date to the right
4721 * timeout. We just have to check that the client is still
4722 * there and that the timeout has not expired.
4723 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004724 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004725 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004726 !tick_is_expired(req->analyse_exp, now_ms))
4727 return 0;
4728
4729 /* We will set the queue timer to the time spent, just for
4730 * logging purposes. We fake a 500 server error, so that the
4731 * attacker will not suspect his connection has been tarpitted.
4732 * It will not cause trouble to the logs because we can exclude
4733 * the tarpitted connections by filtering on the 'PT' status flags.
4734 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004735 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4736
4737 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004738 if (!(req->flags & CF_READ_ERROR))
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004739 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004740
Christopher Fauletd7c91962015-04-30 11:48:27 +02004741 req->analysers &= AN_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004742 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004743
Willy Tarreaue7dff022015-04-03 01:14:29 +02004744 if (!(s->flags & SF_ERR_MASK))
4745 s->flags |= SF_ERR_PRXCOND;
4746 if (!(s->flags & SF_FINST_MASK))
4747 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004748 return 0;
4749}
4750
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004751/* This function is an analyser which waits for the HTTP request body. It waits
4752 * for either the buffer to be full, or the full advertised contents to have
4753 * reached the buffer. It must only be called after the standard HTTP request
4754 * processing has occurred, because it expects the request to be parsed and will
4755 * look for the Expect header. It may send a 100-Continue interim response. It
4756 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4757 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4758 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004759 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004760int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004761{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004762 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004763 struct http_txn *txn = s->txn;
4764 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004765
4766 /* We have to parse the HTTP request body to find any required data.
4767 * "balance url_param check_post" should have been the only way to get
4768 * into this. We were brought here after HTTP header analysis, so all
4769 * related structures are ready.
4770 */
4771
Willy Tarreau890988f2014-04-10 11:59:33 +02004772 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4773 /* This is the first call */
4774 if (msg->msg_state < HTTP_MSG_BODY)
4775 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004776
Willy Tarreau890988f2014-04-10 11:59:33 +02004777 if (msg->msg_state < HTTP_MSG_100_SENT) {
4778 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4779 * send an HTTP/1.1 100 Continue intermediate response.
4780 */
4781 if (msg->flags & HTTP_MSGF_VER_11) {
4782 struct hdr_ctx ctx;
4783 ctx.idx = 0;
4784 /* Expect is allowed in 1.1, look for it */
4785 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4786 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004787 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004788 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004789 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004790 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004791 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004792
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004793 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004794 * req->buf->p still points to the beginning of the message. We
4795 * must save the body in msg->next because it survives buffer
4796 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004797 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004798 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004799
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004800 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004801 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4802 else
4803 msg->msg_state = HTTP_MSG_DATA;
4804 }
4805
Willy Tarreau890988f2014-04-10 11:59:33 +02004806 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4807 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004808 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004809 goto missing_data;
4810
4811 /* OK we have everything we need now */
4812 goto http_end;
4813 }
4814
4815 /* OK here we're parsing a chunked-encoded message */
4816
Willy Tarreau522d6c02009-12-06 18:49:18 +01004817 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004818 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004819 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004820 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004821 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004822 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004823
Willy Tarreau115acb92009-12-26 13:56:06 +01004824 if (!ret)
4825 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004826 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004827 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004828 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004829 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01004830 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004831 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01004832 }
4833
Willy Tarreaud98cf932009-12-27 22:54:55 +01004834 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004835 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4836 * for at least a whole chunk or the whole content length bytes after
4837 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004838 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004839 if (msg->msg_state == HTTP_MSG_TRAILERS)
4840 goto http_end;
4841
Willy Tarreaue115b492015-05-01 23:05:14 +02004842 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004843 goto http_end;
4844
4845 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004846 /* we get here if we need to wait for more data. If the buffer is full,
4847 * we have the maximum we can expect.
4848 */
4849 if (buffer_full(req->buf, global.tune.maxrewrite))
4850 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004851
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004852 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004853 txn->status = 408;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004854 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004855
Willy Tarreaue7dff022015-04-03 01:14:29 +02004856 if (!(s->flags & SF_ERR_MASK))
4857 s->flags |= SF_ERR_CLITO;
4858 if (!(s->flags & SF_FINST_MASK))
4859 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004860 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004861 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004862
4863 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004864 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004865 /* Not enough data. We'll re-use the http-request
4866 * timeout here. Ideally, we should set the timeout
4867 * relative to the accept() date. We just set the
4868 * request timeout once at the beginning of the
4869 * request.
4870 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004871 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004872 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004873 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004874 return 0;
4875 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004876
4877 http_end:
4878 /* The situation will not evolve, so let's give up on the analysis. */
4879 s->logs.tv_request = now; /* update the request timer to reflect full request */
4880 req->analysers &= ~an_bit;
4881 req->analyse_exp = TICK_ETERNITY;
4882 return 1;
4883
4884 return_bad_req: /* let's centralize all bad requests */
4885 txn->req.msg_state = HTTP_MSG_ERROR;
4886 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004887 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004888
Willy Tarreaue7dff022015-04-03 01:14:29 +02004889 if (!(s->flags & SF_ERR_MASK))
4890 s->flags |= SF_ERR_PRXCOND;
4891 if (!(s->flags & SF_FINST_MASK))
4892 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004893
Willy Tarreau522d6c02009-12-06 18:49:18 +01004894 return_err_msg:
Christopher Fauletd7c91962015-04-30 11:48:27 +02004895 req->analysers &= AN_FLT_END;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004896 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004897 if (sess->listener->counters)
4898 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004899 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004900}
4901
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004902/* send a server's name with an outgoing request over an established connection.
4903 * Note: this function is designed to be called once the request has been scheduled
4904 * for being forwarded. This is the reason why it rewinds the buffer before
4905 * proceeding.
4906 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004907int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004908
4909 struct hdr_ctx ctx;
4910
Mark Lamourinec2247f02012-01-04 13:02:01 -05004911 char *hdr_name = be->server_id_hdr_name;
4912 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004913 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004914 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004915 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004916
William Lallemandd9e90662012-01-30 17:27:17 +01004917 ctx.idx = 0;
4918
Willy Tarreau211cdec2014-04-17 20:18:08 +02004919 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004920 if (old_o) {
4921 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004922 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004923 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004924 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004925 }
4926
Willy Tarreau9b28e032012-10-12 23:49:43 +02004927 old_i = chn->buf->i;
4928 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 -05004929 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004930 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004931 }
4932
4933 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004934 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004935 memcpy(hdr_val, hdr_name, hdr_name_len);
4936 hdr_val += hdr_name_len;
4937 *hdr_val++ = ':';
4938 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004939 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4940 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004941
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004942 if (old_o) {
4943 /* If this was a forwarded request, we must readjust the amount of
4944 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004945 * variations. Note that the current state is >= HTTP_MSG_BODY,
4946 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004947 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004948 old_o += chn->buf->i - old_i;
4949 b_adv(chn->buf, old_o);
4950 txn->req.next -= old_o;
4951 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004952 }
4953
Mark Lamourinec2247f02012-01-04 13:02:01 -05004954 return 0;
4955}
4956
Willy Tarreau610ecce2010-01-04 21:15:02 +01004957/* Terminate current transaction and prepare a new one. This is very tricky
4958 * right now but it works.
4959 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004960void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004961{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004962 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02004963 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01004964 struct proxy *be = s->be;
Willy Tarreau323a2d92015-08-04 19:00:17 +02004965 struct connection *srv_conn;
4966 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02004967 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01004968
Willy Tarreau610ecce2010-01-04 21:15:02 +01004969 /* FIXME: We need a more portable way of releasing a backend's and a
4970 * server's connections. We need a safer way to reinitialize buffer
4971 * flags. We also need a more accurate method for computing per-request
4972 * data.
4973 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02004974 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004975
Willy Tarreau4213a112013-12-15 10:25:42 +01004976 /* unless we're doing keep-alive, we want to quickly close the connection
4977 * to the server.
4978 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004979 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004980 !si_conn_ready(&s->si[1])) {
4981 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4982 si_shutr(&s->si[1]);
4983 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004984 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004985
Willy Tarreaue7dff022015-04-03 01:14:29 +02004986 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau858b1032015-12-07 17:04:59 +01004987 be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004988 if (unlikely(s->srv_conn))
4989 sess_change_server(s, NULL);
4990 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004991
4992 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02004993 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004994
Willy Tarreaueee5b512015-04-03 23:46:31 +02004995 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004996 int n;
4997
Willy Tarreaueee5b512015-04-03 23:46:31 +02004998 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004999 if (n < 1 || n > 5)
5000 n = 0;
5001
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005002 if (fe->mode == PR_MODE_HTTP) {
5003 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005004 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005005 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01005006 (be->mode == PR_MODE_HTTP)) {
5007 be->be_counters.p.http.rsp[n]++;
5008 be->be_counters.p.http.cum_req++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005009 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005010 }
5011
5012 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005013 s->logs.bytes_in -= s->req.buf->i;
5014 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005015
5016 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005017 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005018 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005019 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005020 s->do_log(s);
5021 }
5022
Willy Tarreaud713bcc2014-06-25 15:36:04 +02005023 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02005024 stream_stop_content_counters(s);
5025 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02005026
Willy Tarreau610ecce2010-01-04 21:15:02 +01005027 s->logs.accept_date = date; /* user-visible date for logging */
5028 s->logs.tv_accept = now; /* corrected date for internal use */
5029 tv_zero(&s->logs.tv_request);
5030 s->logs.t_queue = -1;
5031 s->logs.t_connect = -1;
5032 s->logs.t_data = -1;
5033 s->logs.t_close = 0;
5034 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5035 s->logs.srv_queue_size = 0; /* we will get this number soon */
5036
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005037 s->logs.bytes_in = s->req.total = s->req.buf->i;
5038 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005039
5040 if (s->pend_pos)
5041 pendconn_free(s->pend_pos);
5042
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005043 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005044 if (s->flags & SF_CURR_SESS) {
5045 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005046 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005047 }
Willy Tarreau858b1032015-12-07 17:04:59 +01005048 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005049 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005050 }
5051
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005052 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005053
Willy Tarreau4213a112013-12-15 10:25:42 +01005054 /* only release our endpoint if we don't intend to reuse the
5055 * connection.
5056 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005057 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005058 !si_conn_ready(&s->si[1])) {
5059 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005060 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005061 }
5062
Willy Tarreau350f4872014-11-28 14:42:25 +01005063 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5064 s->si[1].err_type = SI_ET_NONE;
5065 s->si[1].conn_retries = 0; /* used for logging too */
5066 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005067 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 +01005068 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);
5069 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 +02005070 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5071 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5072 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005073
Willy Tarreaueee5b512015-04-03 23:46:31 +02005074 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005075 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005076 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005077
5078 if (prev_status == 401 || prev_status == 407) {
5079 /* In HTTP keep-alive mode, if we receive a 401, we still have
5080 * a chance of being able to send the visitor again to the same
5081 * server over the same connection. This is required by some
5082 * broken protocols such as NTLM, and anyway whenever there is
5083 * an opportunity for sending the challenge to the proper place,
5084 * it's better to do it (at least it helps with debugging).
5085 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005086 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02005087 if (srv_conn)
5088 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01005089 }
5090
Willy Tarreau53f96852016-02-02 18:50:47 +01005091 /* Never ever allow to reuse a connection from a non-reuse backend */
5092 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
5093 srv_conn->flags |= CO_FL_PRIVATE;
5094
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005095 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005096 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005097
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005098 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005099 s->req.flags |= CF_NEVER_WAIT;
5100 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005101 }
5102
Willy Tarreau610ecce2010-01-04 21:15:02 +01005103 /* if the request buffer is not empty, it means we're
5104 * about to process another request, so send pending
5105 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005106 * Just don't do this if the buffer is close to be full,
5107 * because the request will wait for it to flush a little
5108 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005109 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005110 if (s->req.buf->i) {
5111 if (s->res.buf->o &&
5112 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5113 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5114 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005115 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005116
Willy Tarreau714ea782015-11-25 20:11:11 +01005117 /* we're removing the analysers, we MUST re-enable events detection.
5118 * We don't enable close on the response channel since it's either
5119 * already closed, or in keep-alive with an idle connection handler.
5120 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005121 channel_auto_read(&s->req);
5122 channel_auto_close(&s->req);
5123 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005124
Willy Tarreau1c59bd52015-11-02 20:20:11 +01005125 /* we're in keep-alive with an idle connection, monitor it if not already done */
5126 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02005127 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005128 if (!srv)
5129 si_idle_conn(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01005130 else if (srv_conn->flags & CO_FL_PRIVATE)
Willy Tarreau8dff9982015-08-04 20:45:52 +02005131 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02005132 else if (prev_flags & TX_NOT_FIRST)
5133 /* note: we check the request, not the connection, but
5134 * this is valid for strategies SAFE and AGGR, and in
5135 * case of ALWS, we don't care anyway.
5136 */
5137 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005138 else
5139 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005140 }
Willy Tarreau27375622013-12-17 00:00:28 +01005141
Christopher Faulet3e344292015-11-24 16:24:13 +01005142 if (HAS_FILTERS(s)) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02005143 s->req.analysers &= AN_FLT_END;
5144 s->res.analysers &= AN_FLT_END;
5145 }
Christopher Faulet3e344292015-11-24 16:24:13 +01005146 else {
5147 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
5148 s->res.analysers = 0;
5149 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005150}
5151
5152
5153/* This function updates the request state machine according to the response
5154 * state machine and buffer flags. It returns 1 if it changes anything (flag
5155 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5156 * it is only used to find when a request/response couple is complete. Both
5157 * this function and its equivalent should loop until both return zero. It
5158 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5159 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005160int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005161{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005162 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005163 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005164 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005165 unsigned int old_state = txn->req.msg_state;
5166
Willy Tarreau610ecce2010-01-04 21:15:02 +01005167 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5168 return 0;
5169
5170 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005171 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005172 * We can shut the read side unless we want to abort_on_close,
5173 * or we have a POST request. The issue with POST requests is
5174 * that some browsers still send a CRLF after the request, and
5175 * this CRLF must be read so that it does not remain in the kernel
5176 * buffers, otherwise a close could cause an RST on some systems
5177 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005178 * Note that if we're using keep-alive on the client side, we'd
5179 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005180 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005181 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005182 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005183 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5184 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5185 !(s->be->options & PR_O_ABRT_CLOSE) &&
5186 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005187 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005188
Willy Tarreau40f151a2012-12-20 12:10:09 +01005189 /* if the server closes the connection, we want to immediately react
5190 * and close the socket to save packets and syscalls.
5191 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005192 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005193
Willy Tarreau7f876a12015-11-18 11:59:55 +01005194 /* In any case we've finished parsing the request so we must
5195 * disable Nagle when sending data because 1) we're not going
5196 * to shut this side, and 2) the server is waiting for us to
5197 * send pending data.
5198 */
5199 chn->flags |= CF_NEVER_WAIT;
5200
Willy Tarreau610ecce2010-01-04 21:15:02 +01005201 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5202 goto wait_other_side;
5203
5204 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5205 /* The server has not finished to respond, so we
5206 * don't want to move in order not to upset it.
5207 */
5208 goto wait_other_side;
5209 }
5210
5211 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5212 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005213 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005214 txn->req.msg_state = HTTP_MSG_TUNNEL;
5215 goto wait_other_side;
5216 }
5217
5218 /* When we get here, it means that both the request and the
5219 * response have finished receiving. Depending on the connection
5220 * mode, we'll have to wait for the last bytes to leave in either
5221 * direction, and sometimes for a close to be effective.
5222 */
5223
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005224 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5225 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005226 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5227 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005228 }
5229 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5230 /* Option forceclose is set, or either side wants to close,
5231 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005232 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005233 * once both states are CLOSED.
5234 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005235 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5236 channel_shutr_now(chn);
5237 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005238 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005239 }
5240 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005241 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5242 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005243 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005244 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5245 channel_auto_read(chn);
5246 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005247 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005248 }
5249
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005250 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005251 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005252 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005253
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005254 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005255 txn->req.msg_state = HTTP_MSG_CLOSING;
5256 goto http_msg_closing;
5257 }
5258 else {
5259 txn->req.msg_state = HTTP_MSG_CLOSED;
5260 goto http_msg_closed;
5261 }
5262 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005263 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005264 }
5265
5266 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5267 http_msg_closing:
5268 /* nothing else to forward, just waiting for the output buffer
5269 * to be empty and for the shutw_now to take effect.
5270 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005271 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005272 txn->req.msg_state = HTTP_MSG_CLOSED;
5273 goto http_msg_closed;
5274 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005275 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005276 txn->req.msg_state = HTTP_MSG_ERROR;
5277 goto wait_other_side;
5278 }
5279 }
5280
5281 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5282 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005283 /* see above in MSG_DONE why we only do this in these states */
5284 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5285 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5286 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005287 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005288 goto wait_other_side;
5289 }
5290
5291 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005292 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005293}
5294
5295
5296/* This function updates the response state machine according to the request
5297 * state machine and buffer flags. It returns 1 if it changes anything (flag
5298 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5299 * it is only used to find when a request/response couple is complete. Both
5300 * this function and its equivalent should loop until both return zero. It
5301 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5302 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005303int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005304{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005305 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005306 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005307 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005308 unsigned int old_state = txn->rsp.msg_state;
5309
Willy Tarreau610ecce2010-01-04 21:15:02 +01005310 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5311 return 0;
5312
5313 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5314 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005315 * still monitor the server connection for a possible close
5316 * while the request is being uploaded, so we don't disable
5317 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005318 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005319 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005320
5321 if (txn->req.msg_state == HTTP_MSG_ERROR)
5322 goto wait_other_side;
5323
5324 if (txn->req.msg_state < HTTP_MSG_DONE) {
5325 /* The client seems to still be sending data, probably
5326 * because we got an error response during an upload.
5327 * We have the choice of either breaking the connection
5328 * or letting it pass through. Let's do the later.
5329 */
5330 goto wait_other_side;
5331 }
5332
5333 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5334 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005335 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005336 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005337 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005338 goto wait_other_side;
5339 }
5340
5341 /* When we get here, it means that both the request and the
5342 * response have finished receiving. Depending on the connection
5343 * mode, we'll have to wait for the last bytes to leave in either
5344 * direction, and sometimes for a close to be effective.
5345 */
5346
5347 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5348 /* Server-close mode : shut read and wait for the request
5349 * side to close its output buffer. The caller will detect
5350 * when we're in DONE and the other is in CLOSED and will
5351 * catch that for the final cleanup.
5352 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005353 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5354 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005355 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005356 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5357 /* Option forceclose is set, or either side wants to close,
5358 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005359 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005360 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005361 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005362 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5363 channel_shutr_now(chn);
5364 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005365 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005366 }
5367 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005368 /* The last possible modes are keep-alive and tunnel. Tunnel will
5369 * need to forward remaining data. Keep-alive will need to monitor
5370 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005371 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005372 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005373 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005374 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5375 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005376 }
5377
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005378 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005379 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005380 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005381 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5382 goto http_msg_closing;
5383 }
5384 else {
5385 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5386 goto http_msg_closed;
5387 }
5388 }
5389 goto wait_other_side;
5390 }
5391
5392 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5393 http_msg_closing:
5394 /* nothing else to forward, just waiting for the output buffer
5395 * to be empty and for the shutw_now to take effect.
5396 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005397 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005398 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5399 goto http_msg_closed;
5400 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005401 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005402 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005403 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005404 if (objt_server(s->target))
5405 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005406 goto wait_other_side;
5407 }
5408 }
5409
5410 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5411 http_msg_closed:
5412 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005413 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005414 channel_auto_close(chn);
5415 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005416 goto wait_other_side;
5417 }
5418
5419 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005420 /* We force the response to leave immediately if we're waiting for the
5421 * other side, since there is no pending shutdown to push it out.
5422 */
5423 if (!channel_is_empty(chn))
5424 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005425 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005426}
5427
5428
5429/* Resync the request and response state machines. Return 1 if either state
5430 * changes.
5431 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005432int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005433{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005434 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005435 int old_req_state = txn->req.msg_state;
5436 int old_res_state = txn->rsp.msg_state;
5437
Willy Tarreau610ecce2010-01-04 21:15:02 +01005438 http_sync_req_state(s);
5439 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005440 if (!http_sync_res_state(s))
5441 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005442 if (!http_sync_req_state(s))
5443 break;
5444 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005445
Willy Tarreau610ecce2010-01-04 21:15:02 +01005446 /* OK, both state machines agree on a compatible state.
5447 * There are a few cases we're interested in :
5448 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5449 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5450 * directions, so let's simply disable both analysers.
5451 * - HTTP_MSG_CLOSED on the response only means we must abort the
5452 * request.
5453 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5454 * with server-close mode means we've completed one request and we
5455 * must re-initialize the server connection.
5456 */
5457
5458 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5459 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5460 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5461 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02005462 s->req.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005463 channel_auto_close(&s->req);
5464 channel_auto_read(&s->req);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005465 s->res.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005466 channel_auto_close(&s->res);
5467 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005468 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005469 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005470 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005471 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005472 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02005473 s->res.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005474 channel_auto_close(&s->res);
5475 channel_auto_read(&s->res);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005476 s->req.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005477 channel_abort(&s->req);
5478 channel_auto_close(&s->req);
5479 channel_auto_read(&s->req);
5480 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005481 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005482 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5483 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005484 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005485 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5486 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5487 /* server-close/keep-alive: terminate this transaction,
5488 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005489 * a fresh-new transaction, but only once we're sure there's
5490 * enough room in the request and response buffer to process
5491 * another request. The request buffer must not hold any
5492 * pending output data and the request buffer must not have
5493 * output data occupying the reserve.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005494 */
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005495 if (s->req.buf->o)
5496 s->req.flags |= CF_WAKE_WRITE;
5497 else if (channel_congested(&s->res))
5498 s->res.flags |= CF_WAKE_WRITE;
5499 else
5500 http_end_txn_clean_session(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005501 }
5502
Willy Tarreau610ecce2010-01-04 21:15:02 +01005503 return txn->req.msg_state != old_req_state ||
5504 txn->rsp.msg_state != old_res_state;
5505}
5506
Willy Tarreaud98cf932009-12-27 22:54:55 +01005507/* This function is an analyser which forwards request body (including chunk
5508 * sizes if any). It is called as soon as we must forward, even if we forward
5509 * zero byte. The only situation where it must not be called is when we're in
5510 * tunnel mode and we want to forward till the close. It's used both to forward
5511 * remaining data and to resync after end of body. It expects the msg_state to
5512 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005513 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005514 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005515 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005516 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005517int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005518{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005519 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005520 struct http_txn *txn = s->txn;
5521 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01005522 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005523
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005524 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5525 return 0;
5526
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005527 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005528 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005529 /* Output closed while we were sending data. We must abort and
5530 * wake the other side up.
5531 */
5532 msg->msg_state = HTTP_MSG_ERROR;
5533 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005534 return 1;
5535 }
5536
Willy Tarreaud98cf932009-12-27 22:54:55 +01005537 /* Note that we don't have to send 100-continue back because we don't
5538 * need the data to complete our job, and it's up to the server to
5539 * decide whether to return 100, 417 or anything else in return of
5540 * an "Expect: 100-continue" header.
5541 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02005542 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005543 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5544 ? HTTP_MSG_CHUNK_SIZE
5545 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005546
5547 /* TODO/filters: when http-buffer-request option is set or if a
5548 * rule on url_param exists, the first chunk size could be
5549 * already parsed. In that case, msg->next is after the chunk
5550 * size (including the CRLF after the size). So this case should
5551 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005552 }
5553
Willy Tarreau7ba23542014-04-17 21:50:00 +02005554 /* Some post-connect processing might want us to refrain from starting to
5555 * forward data. Currently, the only reason for this is "balance url_param"
5556 * whichs need to parse/process the request after we've enabled forwarding.
5557 */
5558 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005559 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005560 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005561 req->flags |= CF_WAKE_CONNECT;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005562 goto missing_data_or_waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005563 }
5564 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5565 }
5566
Willy Tarreau80a92c02014-03-12 10:41:13 +01005567 /* in most states, we should abort in case of early close */
5568 channel_auto_close(req);
5569
Willy Tarreauefdf0942014-04-24 20:08:57 +02005570 if (req->to_forward) {
5571 /* We can't process the buffer's contents yet */
5572 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005573 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005574 }
5575
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005576 if (msg->msg_state < HTTP_MSG_DONE) {
5577 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5578 ? http_msg_forward_chunked_body(s, msg)
5579 : http_msg_forward_body(s, msg));
5580 if (!ret)
5581 goto missing_data_or_waiting;
5582 if (ret < 0)
5583 goto return_bad_req;
5584 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005585
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005586 /* other states, DONE...TUNNEL */
5587 /* we don't want to forward closes on DONE except in tunnel mode. */
5588 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5589 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005590
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005591 ret = msg->msg_state;
5592 if (http_resync_states(s)) {
5593 /* some state changes occurred, maybe the analyser
5594 * was disabled too. */
5595 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5596 if (req->flags & CF_SHUTW) {
5597 /* request errors are most likely due to the
5598 * server aborting the transfer. */
5599 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005600 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005601 if (msg->err_pos >= 0)
5602 http_capture_bad_message(&sess->fe->invalid_req, s, msg, ret, s->be);
5603 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005604 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005605 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005606 }
5607
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005608 /* If "option abortonclose" is set on the backend, we want to monitor
5609 * the client's connection and forward any shutdown notification to the
5610 * server, which will decide whether to close or to go on processing the
5611 * request. We only do that in tunnel mode, and not in other modes since
5612 * it can be abused to exhaust source ports. */
5613 if (s->be->options & PR_O_ABRT_CLOSE) {
5614 channel_auto_read(req);
5615 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5616 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5617 s->si[1].flags |= SI_FL_NOLINGER;
5618 channel_auto_close(req);
5619 }
5620 else if (s->txn->meth == HTTP_METH_POST) {
5621 /* POST requests may require to read extra CRLF sent by broken
5622 * browsers and which could cause an RST to be sent upon close
5623 * on some systems (eg: Linux). */
5624 channel_auto_read(req);
5625 }
5626 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005627
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005628 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005629 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005630 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005631 if (!(s->flags & SF_ERR_MASK))
5632 s->flags |= SF_ERR_CLICL;
5633 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005634 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005635 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005636 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005637 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005638 }
5639
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005640 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005641 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005642 if (objt_server(s->target))
5643 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005644
5645 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005646 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005647
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005648 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005649 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005650 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005651
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005652 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005653 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005654 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005655 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005656 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005657
Willy Tarreau5c620922011-05-11 19:56:11 +02005658 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005659 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005660 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005661 * modes are already handled by the stream sock layer. We must not do
5662 * this in content-length mode because it could present the MSG_MORE
5663 * flag with the last block of forwarded data, which would cause an
5664 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005665 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005666 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005667 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005668
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005669 return 0;
5670
Willy Tarreaud98cf932009-12-27 22:54:55 +01005671 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005672 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005673 if (sess->listener->counters)
5674 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005675
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005676 return_bad_req_stats_ok:
5677 txn->req.msg_state = HTTP_MSG_ERROR;
5678 if (txn->status) {
5679 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005680 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005681 } else {
5682 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005683 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005684 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005685 req->analysers &= AN_FLT_END;
5686 s->res.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005687
Willy Tarreaue7dff022015-04-03 01:14:29 +02005688 if (!(s->flags & SF_ERR_MASK))
5689 s->flags |= SF_ERR_PRXCOND;
5690 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005691 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005692 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005693 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005694 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005695 }
5696 return 0;
5697
5698 aborted_xfer:
5699 txn->req.msg_state = HTTP_MSG_ERROR;
5700 if (txn->status) {
5701 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005702 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005703 } else {
5704 txn->status = 502;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005705 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005706 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005707 req->analysers &= AN_FLT_END;
5708 s->res.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005709
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005710 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005711 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005712 if (objt_server(s->target))
5713 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005714
Willy Tarreaue7dff022015-04-03 01:14:29 +02005715 if (!(s->flags & SF_ERR_MASK))
5716 s->flags |= SF_ERR_SRVCL;
5717 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005718 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005719 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005720 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005721 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005722 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005723 return 0;
5724}
5725
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005726/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5727 * processing can continue on next analysers, or zero if it either needs more
5728 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005729 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005730 * when it has nothing left to do, and may remove any analyser when it wants to
5731 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005732 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005733int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005734{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005735 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005736 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005737 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005738 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005739 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005740 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005741 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005742
Willy Tarreau87b09662015-04-03 00:22:06 +02005743 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 +02005744 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005745 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005746 rep,
5747 rep->rex, rep->wex,
5748 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005749 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005750 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005751
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005752 /*
5753 * Now parse the partial (or complete) lines.
5754 * We will check the response syntax, and also join multi-line
5755 * headers. An index of all the lines will be elaborated while
5756 * parsing.
5757 *
5758 * For the parsing, we use a 28 states FSM.
5759 *
5760 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005761 * rep->buf->p = beginning of response
5762 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5763 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005764 * msg->eol = end of current header or line (LF or CRLF)
5765 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005766 */
5767
Willy Tarreau628c40c2014-04-24 19:11:26 +02005768 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005769 /* There's a protected area at the end of the buffer for rewriting
5770 * purposes. We don't want to start to parse the request if the
5771 * protected area is affected, because we may have to move processed
5772 * data later, which is much more complicated.
5773 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005774 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005775 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005776 /* some data has still not left the buffer, wake us once that's done */
5777 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5778 goto abort_response;
5779 channel_dont_close(rep);
5780 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005781 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005782 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005783 }
5784
Willy Tarreau379357a2013-06-08 12:55:46 +02005785 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5786 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5787 buffer_slow_realign(rep->buf);
5788
Willy Tarreau9b28e032012-10-12 23:49:43 +02005789 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005790 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005791 }
5792
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005793 /* 1: we might have to print this header in debug mode */
5794 if (unlikely((global.mode & MODE_DEBUG) &&
5795 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005796 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005797 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005798
Willy Tarreau9b28e032012-10-12 23:49:43 +02005799 sol = rep->buf->p;
5800 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005801 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005802
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005803 sol += hdr_idx_first_pos(&txn->hdr_idx);
5804 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005805
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005806 while (cur_idx) {
5807 eol = sol + txn->hdr_idx.v[cur_idx].len;
5808 debug_hdr("srvhdr", s, sol, eol);
5809 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5810 cur_idx = txn->hdr_idx.v[cur_idx].next;
5811 }
5812 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005813
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005814 /*
5815 * Now we quickly check if we have found a full valid response.
5816 * If not so, we check the FD and buffer states before leaving.
5817 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005818 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005819 * responses are checked first.
5820 *
5821 * Depending on whether the client is still there or not, we
5822 * may send an error response back or not. Note that normally
5823 * we should only check for HTTP status there, and check I/O
5824 * errors somewhere else.
5825 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005826
Willy Tarreau655dce92009-11-08 13:10:58 +01005827 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005828 /* Invalid response */
5829 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5830 /* we detected a parsing error. We want to archive this response
5831 * in the dedicated proxy area for later troubleshooting.
5832 */
5833 hdr_response_bad:
5834 if (msg->msg_state == HTTP_MSG_ERROR || 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 Tarreaub37c27e2009-10-18 22:53:08 +02005836
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_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005841 }
Willy Tarreau64648412010-03-05 10:41:54 +01005842 abort_response:
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 = 502;
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_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005849
Willy Tarreaue7dff022015-04-03 01:14:29 +02005850 if (!(s->flags & SF_ERR_MASK))
5851 s->flags |= SF_ERR_PRXCOND;
5852 if (!(s->flags & SF_FINST_MASK))
5853 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005854
5855 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005856 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005857
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005858 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005859 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005860 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005861 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005862 goto hdr_response_bad;
5863 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005864
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005865 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005866 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005867 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005868 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005869 else if (txn->flags & TX_NOT_FIRST)
5870 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005871
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005872 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005873 if (objt_server(s->target)) {
5874 objt_server(s->target)->counters.failed_resp++;
5875 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005876 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005877
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005878 channel_auto_close(rep);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005879 rep->analysers &= AN_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005880 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005881 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005882 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005883 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005884
Willy Tarreaue7dff022015-04-03 01:14:29 +02005885 if (!(s->flags & SF_ERR_MASK))
5886 s->flags |= SF_ERR_SRVCL;
5887 if (!(s->flags & SF_FINST_MASK))
5888 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005889 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005890 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005891
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005892 /* read timeout : return a 504 to the client. */
5893 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005894 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005895 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005896
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005897 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005898 if (objt_server(s->target)) {
5899 objt_server(s->target)->counters.failed_resp++;
5900 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005901 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005902
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005903 channel_auto_close(rep);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005904 rep->analysers &= AN_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005905 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005906 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005907 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005908 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005909
Willy Tarreaue7dff022015-04-03 01:14:29 +02005910 if (!(s->flags & SF_ERR_MASK))
5911 s->flags |= SF_ERR_SRVTO;
5912 if (!(s->flags & SF_FINST_MASK))
5913 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005914 return 0;
5915 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005916
Willy Tarreauf003d372012-11-26 13:35:37 +01005917 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005918 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005919 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01005920 s->be->be_counters.cli_aborts++;
5921 if (objt_server(s->target))
5922 objt_server(s->target)->counters.cli_aborts++;
5923
Christopher Fauletd7c91962015-04-30 11:48:27 +02005924 rep->analysers &= AN_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01005925 channel_auto_close(rep);
5926
5927 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005928 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005929 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01005930
Willy Tarreaue7dff022015-04-03 01:14:29 +02005931 if (!(s->flags & SF_ERR_MASK))
5932 s->flags |= SF_ERR_CLICL;
5933 if (!(s->flags & SF_FINST_MASK))
5934 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01005935
Willy Tarreau87b09662015-04-03 00:22:06 +02005936 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01005937 return 0;
5938 }
5939
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005940 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005941 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005942 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005943 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005944 else if (txn->flags & TX_NOT_FIRST)
5945 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005946
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005947 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005948 if (objt_server(s->target)) {
5949 objt_server(s->target)->counters.failed_resp++;
5950 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005951 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005952
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005953 channel_auto_close(rep);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005954 rep->analysers &= AN_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005955 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005956 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005957 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005958 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005959
Willy Tarreaue7dff022015-04-03 01:14:29 +02005960 if (!(s->flags & SF_ERR_MASK))
5961 s->flags |= SF_ERR_SRVCL;
5962 if (!(s->flags & SF_FINST_MASK))
5963 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005964 return 0;
5965 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005966
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005967 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005968 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005969 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005970 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005971 else if (txn->flags & TX_NOT_FIRST)
5972 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005973
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005974 s->be->be_counters.failed_resp++;
Christopher Fauletd7c91962015-04-30 11:48:27 +02005975 rep->analysers &= AN_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005976 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005977
Willy Tarreaue7dff022015-04-03 01:14:29 +02005978 if (!(s->flags & SF_ERR_MASK))
5979 s->flags |= SF_ERR_CLICL;
5980 if (!(s->flags & SF_FINST_MASK))
5981 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005982
Willy Tarreau87b09662015-04-03 00:22:06 +02005983 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005984 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005985 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005986
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005987 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005988 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005989 return 0;
5990 }
5991
5992 /* More interesting part now : we know that we have a complete
5993 * response which at least looks like HTTP. We have an indicator
5994 * of each header's length, so we can parse them quickly.
5995 */
5996
5997 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005998 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005999
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006000 /*
6001 * 1: get the status code
6002 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006003 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006004 if (n < 1 || n > 5)
6005 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006006 /* when the client triggers a 4xx from the server, it's most often due
6007 * to a missing object or permission. These events should be tracked
6008 * because if they happen often, it may indicate a brute force or a
6009 * vulnerability scan.
6010 */
6011 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006012 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006013
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006014 if (objt_server(s->target))
6015 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006016
Willy Tarreau91852eb2015-05-01 13:26:00 +02006017 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
6018 * exactly one digit "." one digit. This check may be disabled using
6019 * option accept-invalid-http-response.
6020 */
6021 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
6022 if (msg->sl.st.v_l != 8) {
6023 msg->err_pos = 0;
6024 goto hdr_response_bad;
6025 }
6026
6027 if (rep->buf->p[4] != '/' ||
6028 !isdigit((unsigned char)rep->buf->p[5]) ||
6029 rep->buf->p[6] != '.' ||
6030 !isdigit((unsigned char)rep->buf->p[7])) {
6031 msg->err_pos = 4;
6032 goto hdr_response_bad;
6033 }
6034 }
6035
Willy Tarreau5b154472009-12-21 20:11:07 +01006036 /* check if the response is HTTP/1.1 or above */
6037 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006038 ((rep->buf->p[5] > '1') ||
6039 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006040 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006041
6042 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006043 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 +01006044
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006045 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006046 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006047
Willy Tarreau9b28e032012-10-12 23:49:43 +02006048 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006049
Willy Tarreau39650402010-03-15 19:44:39 +01006050 /* Adjust server's health based on status code. Note: status codes 501
6051 * and 505 are triggered on demand by client request, so we must not
6052 * count them as server failures.
6053 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006054 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006055 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006056 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006057 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006058 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006059 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006060
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006061 /*
6062 * 2: check for cacheability.
6063 */
6064
6065 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006066 case 100:
6067 /*
6068 * We may be facing a 100-continue response, in which case this
6069 * is not the right response, and we're waiting for the next one.
6070 * Let's allow this response to go to the client and wait for the
6071 * next one.
6072 */
6073 hdr_idx_init(&txn->hdr_idx);
6074 msg->next -= channel_forward(rep, msg->next);
6075 msg->msg_state = HTTP_MSG_RPBEFORE;
6076 txn->status = 0;
6077 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01006078 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02006079 goto next_one;
6080
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006081 case 200:
6082 case 203:
6083 case 206:
6084 case 300:
6085 case 301:
6086 case 410:
6087 /* RFC2616 @13.4:
6088 * "A response received with a status code of
6089 * 200, 203, 206, 300, 301 or 410 MAY be stored
6090 * by a cache (...) unless a cache-control
6091 * directive prohibits caching."
6092 *
6093 * RFC2616 @9.5: POST method :
6094 * "Responses to this method are not cacheable,
6095 * unless the response includes appropriate
6096 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006097 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006098 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006099 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006100 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6101 break;
6102 default:
6103 break;
6104 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006105
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006106 /*
6107 * 3: we may need to capture headers
6108 */
6109 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006110 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006111 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006112 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006113
Willy Tarreau557f1992015-05-01 10:05:17 +02006114 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6115 * by RFC7230#3.3.3 :
6116 *
6117 * The length of a message body is determined by one of the following
6118 * (in order of precedence):
6119 *
6120 * 1. Any response to a HEAD request and any response with a 1xx
6121 * (Informational), 204 (No Content), or 304 (Not Modified) status
6122 * code is always terminated by the first empty line after the
6123 * header fields, regardless of the header fields present in the
6124 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006125 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006126 * 2. Any 2xx (Successful) response to a CONNECT request implies that
6127 * the connection will become a tunnel immediately after the empty
6128 * line that concludes the header fields. A client MUST ignore any
6129 * Content-Length or Transfer-Encoding header fields received in
6130 * such a message.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006131 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006132 * 3. If a Transfer-Encoding header field is present and the chunked
6133 * transfer coding (Section 4.1) is the final encoding, the message
6134 * body length is determined by reading and decoding the chunked
6135 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006136 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006137 * If a Transfer-Encoding header field is present in a response and
6138 * the chunked transfer coding is not the final encoding, the
6139 * message body length is determined by reading the connection until
6140 * it is closed by the server. If a Transfer-Encoding header field
6141 * is present in a request and the chunked transfer coding is not
6142 * the final encoding, the message body length cannot be determined
6143 * reliably; the server MUST respond with the 400 (Bad Request)
6144 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006145 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006146 * If a message is received with both a Transfer-Encoding and a
6147 * Content-Length header field, the Transfer-Encoding overrides the
6148 * Content-Length. Such a message might indicate an attempt to
6149 * perform request smuggling (Section 9.5) or response splitting
6150 * (Section 9.4) and ought to be handled as an error. A sender MUST
6151 * remove the received Content-Length field prior to forwarding such
6152 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006153 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006154 * 4. If a message is received without Transfer-Encoding and with
6155 * either multiple Content-Length header fields having differing
6156 * field-values or a single Content-Length header field having an
6157 * invalid value, then the message framing is invalid and the
6158 * recipient MUST treat it as an unrecoverable error. If this is a
6159 * request message, the server MUST respond with a 400 (Bad Request)
6160 * status code and then close the connection. If this is a response
6161 * message received by a proxy, the proxy MUST close the connection
6162 * to the server, discard the received response, and send a 502 (Bad
6163 * Gateway) response to the client. If this is a response message
6164 * received by a user agent, the user agent MUST close the
6165 * connection to the server and discard the received response.
6166 *
6167 * 5. If a valid Content-Length header field is present without
6168 * Transfer-Encoding, its decimal value defines the expected message
6169 * body length in octets. If the sender closes the connection or
6170 * the recipient times out before the indicated number of octets are
6171 * received, the recipient MUST consider the message to be
6172 * incomplete and close the connection.
6173 *
6174 * 6. If this is a request message and none of the above are true, then
6175 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006176 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006177 * 7. Otherwise, this is a response message without a declared message
6178 * body length, so the message body length is determined by the
6179 * number of octets received prior to the server closing the
6180 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006181 */
6182
6183 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006184 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006185 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006186 * FIXME: should we parse anyway and return an error on chunked encoding ?
6187 */
6188 if (txn->meth == HTTP_METH_HEAD ||
6189 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006190 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006191 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006192 goto skip_content_length;
6193 }
6194
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006195 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006196 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006197 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006198 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006199 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6200 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006201 /* bad transfer-encoding (chunked followed by something else) */
6202 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006203 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006204 break;
6205 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006206 }
6207
Willy Tarreau1c913912015-04-30 10:57:51 +02006208 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006209 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006210 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006211 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6212 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6213 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006214 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006215 signed long long cl;
6216
Willy Tarreauad14f752011-09-02 20:33:27 +02006217 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006218 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006219 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006220 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006221
Willy Tarreauad14f752011-09-02 20:33:27 +02006222 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006223 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006224 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006225 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006226
Willy Tarreauad14f752011-09-02 20:33:27 +02006227 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006228 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006229 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006230 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006231
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006232 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006233 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006234 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006235 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006236
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006237 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006238 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006239 }
6240
6241skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006242 /* Now we have to check if we need to modify the Connection header.
6243 * This is more difficult on the response than it is on the request,
6244 * because we can have two different HTTP versions and we don't know
6245 * how the client will interprete a response. For instance, let's say
6246 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6247 * HTTP/1.1 response without any header. Maybe it will bound itself to
6248 * HTTP/1.0 because it only knows about it, and will consider the lack
6249 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6250 * the lack of header as a keep-alive. Thus we will use two flags
6251 * indicating how a request MAY be understood by the client. In case
6252 * of multiple possibilities, we'll fix the header to be explicit. If
6253 * ambiguous cases such as both close and keepalive are seen, then we
6254 * will fall back to explicit close. Note that we won't take risks with
6255 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006256 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006257 */
6258
Willy Tarreaudc008c52010-02-01 16:20:08 +01006259 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6260 txn->status == 101)) {
6261 /* Either we've established an explicit tunnel, or we're
6262 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006263 * to understand the next protocols. We have to switch to tunnel
6264 * mode, so that we transfer the request and responses then let
6265 * this protocol pass unmodified. When we later implement specific
6266 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006267 * header which contains information about that protocol for
6268 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006269 */
6270 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6271 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006272 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6273 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006274 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006275 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006276 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006277
Willy Tarreau70dffda2014-01-30 03:07:23 +01006278 /* this situation happens when combining pretend-keepalive with httpclose. */
6279 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006280 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006281 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006282 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6283
Willy Tarreau60466522010-01-18 19:08:45 +01006284 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006285 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006286 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6287 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006288
Willy Tarreau60466522010-01-18 19:08:45 +01006289 /* now adjust header transformations depending on current state */
6290 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6291 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6292 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006293 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006294 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006295 }
Willy Tarreau60466522010-01-18 19:08:45 +01006296 else { /* SCL / KAL */
6297 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006298 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006299 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006300 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006301
Willy Tarreau60466522010-01-18 19:08:45 +01006302 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006303 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006304
Willy Tarreau60466522010-01-18 19:08:45 +01006305 /* Some keep-alive responses are converted to Server-close if
6306 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006307 */
Willy Tarreau60466522010-01-18 19:08:45 +01006308 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6309 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006310 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006311 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006312 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006313 }
6314
Willy Tarreau7959a552013-09-23 16:44:27 +02006315 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006316 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006317
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006318 /* end of job, return OK */
6319 rep->analysers &= ~an_bit;
6320 rep->analyse_exp = TICK_ETERNITY;
6321 channel_auto_close(rep);
6322 return 1;
6323
6324 abort_keep_alive:
6325 /* A keep-alive request to the server failed on a network error.
6326 * The client is required to retry. We need to close without returning
6327 * any other information so that the client retries.
6328 */
6329 txn->status = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +02006330 rep->analysers &= AN_FLT_END;
6331 s->req.analysers &= AN_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006332 channel_auto_close(rep);
6333 s->logs.logwait = 0;
6334 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006335 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006336 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006337 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006338 return 0;
6339}
6340
6341/* This function performs all the processing enabled for the current response.
6342 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006343 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006344 * other functions. It works like process_request (see indications above).
6345 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006346int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006347{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006348 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006349 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006350 struct http_msg *msg = &txn->rsp;
6351 struct proxy *cur_proxy;
6352 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006353 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006354
Willy Tarreau87b09662015-04-03 00:22:06 +02006355 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 +02006356 now_ms, __FUNCTION__,
6357 s,
6358 rep,
6359 rep->rex, rep->wex,
6360 rep->flags,
6361 rep->buf->i,
6362 rep->analysers);
6363
6364 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6365 return 0;
6366
Willy Tarreau70730dd2014-04-24 18:06:27 +02006367 /* The stats applet needs to adjust the Connection header but we don't
6368 * apply any filter there.
6369 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006370 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6371 rep->analysers &= ~an_bit;
6372 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006373 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006374 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006375
Willy Tarreau58975672014-04-24 21:13:57 +02006376 /*
6377 * We will have to evaluate the filters.
6378 * As opposed to version 1.2, now they will be evaluated in the
6379 * filters order and not in the header order. This means that
6380 * each filter has to be validated among all headers.
6381 *
6382 * Filters are tried with ->be first, then with ->fe if it is
6383 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006384 *
6385 * Maybe we are in resume condiion. In this case I choose the
6386 * "struct proxy" which contains the rule list matching the resume
6387 * pointer. If none of theses "struct proxy" match, I initialise
6388 * the process with the first one.
6389 *
6390 * In fact, I check only correspondance betwwen the current list
6391 * pointer and the ->fe rule list. If it doesn't match, I initialize
6392 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006393 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006394 if (s->current_rule_list == &sess->fe->http_res_rules)
6395 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006396 else
6397 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006398 while (1) {
6399 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006400
Willy Tarreau58975672014-04-24 21:13:57 +02006401 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02006402 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006403 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006404
Willy Tarreau51d861a2015-05-22 17:30:48 +02006405 if (ret == HTTP_RULE_RES_BADREQ)
6406 goto return_srv_prx_502;
6407
6408 if (ret == HTTP_RULE_RES_DONE) {
6409 rep->analysers &= ~an_bit;
6410 rep->analyse_exp = TICK_ETERNITY;
6411 return 1;
6412 }
6413 }
6414
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006415 /* we need to be called again. */
6416 if (ret == HTTP_RULE_RES_YIELD) {
6417 channel_dont_close(rep);
6418 return 0;
6419 }
6420
Willy Tarreau58975672014-04-24 21:13:57 +02006421 /* try headers filters */
6422 if (rule_set->rsp_exp != NULL) {
6423 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6424 return_bad_resp:
6425 if (objt_server(s->target)) {
6426 objt_server(s->target)->counters.failed_resp++;
6427 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006428 }
Willy Tarreau58975672014-04-24 21:13:57 +02006429 s->be->be_counters.failed_resp++;
6430 return_srv_prx_502:
Christopher Fauletd7c91962015-04-30 11:48:27 +02006431 rep->analysers &= AN_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02006432 txn->status = 502;
6433 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006434 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006435 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006436 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006437 if (!(s->flags & SF_ERR_MASK))
6438 s->flags |= SF_ERR_PRXCOND;
6439 if (!(s->flags & SF_FINST_MASK))
6440 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006441 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006442 }
Willy Tarreau58975672014-04-24 21:13:57 +02006443 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006444
Willy Tarreau58975672014-04-24 21:13:57 +02006445 /* has the response been denied ? */
6446 if (txn->flags & TX_SVDENY) {
6447 if (objt_server(s->target))
6448 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006449
Willy Tarreau58975672014-04-24 21:13:57 +02006450 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006451 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006452 if (sess->listener->counters)
6453 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006454
Willy Tarreau58975672014-04-24 21:13:57 +02006455 goto return_srv_prx_502;
6456 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006457
Willy Tarreau58975672014-04-24 21:13:57 +02006458 /* add response headers from the rule sets in the same order */
6459 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006460 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006461 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006462 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006463 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006464 ret = acl_pass(ret);
6465 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6466 ret = !ret;
6467 if (!ret)
6468 continue;
6469 }
6470 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6471 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006472 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006473
Willy Tarreau58975672014-04-24 21:13:57 +02006474 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006475 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006476 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006477 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006478 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006479
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006480 /* After this point, this anayzer can't return yield, so we can
6481 * remove the bit corresponding to this analyzer from the list.
6482 *
6483 * Note that the intermediate returns and goto found previously
6484 * reset the analyzers.
6485 */
6486 rep->analysers &= ~an_bit;
6487 rep->analyse_exp = TICK_ETERNITY;
6488
Willy Tarreau58975672014-04-24 21:13:57 +02006489 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006490 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006491 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006492
Willy Tarreau58975672014-04-24 21:13:57 +02006493 /*
6494 * Now check for a server cookie.
6495 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006496 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02006497 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006498
Willy Tarreau58975672014-04-24 21:13:57 +02006499 /*
6500 * Check for cache-control or pragma headers if required.
6501 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006502 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 +02006503 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006504
Willy Tarreau58975672014-04-24 21:13:57 +02006505 /*
6506 * Add server cookie in the response if needed
6507 */
6508 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6509 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006510 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006511 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6512 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6513 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6514 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6515 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006516 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006517 /* the server is known, it's not the one the client requested, or the
6518 * cookie's last seen date needs to be refreshed. We have to
6519 * insert a set-cookie here, except if we want to insert only on POST
6520 * requests and this one isn't. Note that servers which don't have cookies
6521 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006522 */
Willy Tarreau58975672014-04-24 21:13:57 +02006523 if (!objt_server(s->target)->cookie) {
6524 chunk_printf(&trash,
6525 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6526 s->be->cookie_name);
6527 }
6528 else {
6529 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006530
Willy Tarreau58975672014-04-24 21:13:57 +02006531 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6532 /* emit last_date, which is mandatory */
6533 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6534 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6535 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006536
Willy Tarreau58975672014-04-24 21:13:57 +02006537 if (s->be->cookie_maxlife) {
6538 /* emit first_date, which is either the original one or
6539 * the current date.
6540 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006541 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006542 s30tob64(txn->cookie_first_date ?
6543 txn->cookie_first_date >> 2 :
6544 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006545 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006546 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006547 }
Willy Tarreau58975672014-04-24 21:13:57 +02006548 chunk_appendf(&trash, "; path=/");
6549 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006550
Willy Tarreau58975672014-04-24 21:13:57 +02006551 if (s->be->cookie_domain)
6552 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006553
Willy Tarreau58975672014-04-24 21:13:57 +02006554 if (s->be->ck_opts & PR_CK_HTTPONLY)
6555 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006556
Willy Tarreau58975672014-04-24 21:13:57 +02006557 if (s->be->ck_opts & PR_CK_SECURE)
6558 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006559
Willy Tarreau58975672014-04-24 21:13:57 +02006560 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6561 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006562
Willy Tarreau58975672014-04-24 21:13:57 +02006563 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006564 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006565 /* the server did not change, only the date was updated */
6566 txn->flags |= TX_SCK_UPDATED;
6567 else
6568 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006569
Willy Tarreau58975672014-04-24 21:13:57 +02006570 /* Here, we will tell an eventual cache on the client side that we don't
6571 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6572 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6573 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006574 */
Willy Tarreau58975672014-04-24 21:13:57 +02006575 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006576
Willy Tarreau58975672014-04-24 21:13:57 +02006577 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006578
Willy Tarreau58975672014-04-24 21:13:57 +02006579 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6580 "Cache-control: private", 22) < 0))
6581 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006582 }
Willy Tarreau58975672014-04-24 21:13:57 +02006583 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006584
Willy Tarreau58975672014-04-24 21:13:57 +02006585 /*
6586 * Check if result will be cacheable with a cookie.
6587 * We'll block the response if security checks have caught
6588 * nasty things such as a cacheable cookie.
6589 */
6590 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6591 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6592 (s->be->options & PR_O_CHK_CACHE)) {
6593 /* we're in presence of a cacheable response containing
6594 * a set-cookie header. We'll block it as requested by
6595 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006596 */
Willy Tarreau58975672014-04-24 21:13:57 +02006597 if (objt_server(s->target))
6598 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006599
Willy Tarreau58975672014-04-24 21:13:57 +02006600 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006601 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006602 if (sess->listener->counters)
6603 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006604
Willy Tarreau58975672014-04-24 21:13:57 +02006605 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6606 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6607 send_log(s->be, LOG_ALERT,
6608 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6609 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6610 goto return_srv_prx_502;
6611 }
Willy Tarreau03945942009-12-22 16:50:27 +01006612
Willy Tarreau70730dd2014-04-24 18:06:27 +02006613 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006614 /*
6615 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6616 * If an "Upgrade" token is found, the header is left untouched in order
6617 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006618 * if anything but "Upgrade" is present in the Connection header. We don't
6619 * want to touch any 101 response either since it's switching to another
6620 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006621 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006622 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006623 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006624 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006625 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6626 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006627
Willy Tarreau58975672014-04-24 21:13:57 +02006628 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6629 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6630 /* we want a keep-alive response here. Keep-alive header
6631 * required if either side is not 1.1.
6632 */
6633 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6634 want_flags |= TX_CON_KAL_SET;
6635 }
6636 else {
6637 /* we want a close response here. Close header required if
6638 * the server is 1.1, regardless of the client.
6639 */
6640 if (msg->flags & HTTP_MSGF_VER_11)
6641 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006642 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006643
Willy Tarreau58975672014-04-24 21:13:57 +02006644 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6645 http_change_connection_header(txn, msg, want_flags);
6646 }
6647
6648 skip_header_mangling:
Christopher Faulet3e344292015-11-24 16:24:13 +01006649 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_FILTERS(s) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006650 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
6651 rep->analysers &= ~AN_FLT_XFER_DATA;
Willy Tarreau58975672014-04-24 21:13:57 +02006652 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Christopher Fauletd7c91962015-04-30 11:48:27 +02006653 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006654
Willy Tarreau58975672014-04-24 21:13:57 +02006655 /* if the user wants to log as soon as possible, without counting
6656 * bytes from the server, then this is the right moment. We have
6657 * to temporarily assign bytes_out to log what we currently have.
6658 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006659 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006660 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6661 s->logs.bytes_out = txn->rsp.eoh;
6662 s->do_log(s);
6663 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006664 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006665 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006666}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006667
Willy Tarreaud98cf932009-12-27 22:54:55 +01006668/* This function is an analyser which forwards response body (including chunk
6669 * sizes if any). It is called as soon as we must forward, even if we forward
6670 * zero byte. The only situation where it must not be called is when we're in
6671 * tunnel mode and we want to forward till the close. It's used both to forward
6672 * remaining data and to resync after end of body. It expects the msg_state to
6673 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006674 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006675 *
6676 * It is capable of compressing response data both in content-length mode and
6677 * in chunked mode. The state machines follows different flows depending on
6678 * whether content-length and chunked modes are used, since there are no
6679 * trailers in content-length :
6680 *
6681 * chk-mode cl-mode
6682 * ,----- BODY -----.
6683 * / \
6684 * V size > 0 V chk-mode
6685 * .--> SIZE -------------> DATA -------------> CRLF
6686 * | | size == 0 | last byte |
6687 * | v final crlf v inspected |
6688 * | TRAILERS -----------> DONE |
6689 * | |
6690 * `----------------------------------------------'
6691 *
6692 * Compression only happens in the DATA state, and must be flushed in final
6693 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6694 * is performed at once on final states for all bytes parsed, or when leaving
6695 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006696 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006697int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006698{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006699 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006700 struct http_txn *txn = s->txn;
6701 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01006702 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006703
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006704 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6705 return 0;
6706
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006707 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006708 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006709 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006710 /* Output closed while we were sending data. We must abort and
6711 * wake the other side up.
6712 */
6713 msg->msg_state = HTTP_MSG_ERROR;
6714 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006715 return 1;
6716 }
6717
Willy Tarreau4fe41902010-06-07 22:27:41 +02006718 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006719 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006720
Christopher Fauletd7c91962015-04-30 11:48:27 +02006721 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006722 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
6723 ? HTTP_MSG_CHUNK_SIZE
6724 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006725 }
6726
Willy Tarreauefdf0942014-04-24 20:08:57 +02006727 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006728 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02006729 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006730 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006731 }
6732
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006733 if (msg->msg_state < HTTP_MSG_DONE) {
6734 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6735 ? http_msg_forward_chunked_body(s, msg)
6736 : http_msg_forward_body(s, msg));
6737 if (!ret)
6738 goto missing_data_or_waiting;
6739 if (ret < 0)
6740 goto return_bad_res;
6741 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006742
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006743 /* other states, DONE...TUNNEL */
6744 /* for keep-alive we don't want to forward closes on DONE */
6745 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6746 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6747 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006748
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006749 ret = msg->msg_state;
6750 if (http_resync_states(s)) {
6751 /* some state changes occurred, maybe the analyser was disabled
6752 * too. */
6753 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6754 if (res->flags & CF_SHUTW) {
6755 /* response errors are most likely due to the
6756 * client aborting the transfer. */
6757 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006758 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006759 if (msg->err_pos >= 0)
6760 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, strm_fe(s));
6761 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006762 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006763 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006764 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006765 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006766
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006767 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006768 if (res->flags & CF_SHUTW)
6769 goto aborted_xfer;
6770
6771 /* stop waiting for data if the input is closed before the end. If the
6772 * client side was already closed, it means that the client has aborted,
6773 * so we don't want to count this as a server abort. Otherwise it's a
6774 * server abort.
6775 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006776 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006777 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006778 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006779 /* If we have some pending data, we continue the processing */
6780 if (!buffer_pending(res->buf)) {
6781 if (!(s->flags & SF_ERR_MASK))
6782 s->flags |= SF_ERR_SRVCL;
6783 s->be->be_counters.srv_aborts++;
6784 if (objt_server(s->target))
6785 objt_server(s->target)->counters.srv_aborts++;
6786 goto return_bad_res_stats_ok;
6787 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006788 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006789
Willy Tarreau40dba092010-03-04 18:14:51 +01006790 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006791 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006792 goto return_bad_res;
6793
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006794 /* When TE: chunked is used, we need to get there again to parse
6795 * remaining chunks even if the server has closed, so we don't want to
6796 * set CF_DONTCLOSE. Similarly, if the body length is undefined, if
6797 * keep-alive is set on the client side or if there are filters
6798 * registered on the stream, we don't want to forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006799 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006800 if ((msg->flags & HTTP_MSGF_TE_CHNK) || !msg->body_len ||
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006801 HAS_FILTERS(s) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006802 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6803 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006804 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006805
Willy Tarreau5c620922011-05-11 19:56:11 +02006806 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006807 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006808 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006809 * modes are already handled by the stream sock layer. We must not do
6810 * this in content-length mode because it could present the MSG_MORE
6811 * flag with the last block of forwarded data, which would cause an
6812 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006813 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006814 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006815 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006816
Willy Tarreau87b09662015-04-03 00:22:06 +02006817 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006818 return 0;
6819
Willy Tarreau40dba092010-03-04 18:14:51 +01006820 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006821 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006822 if (objt_server(s->target))
6823 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006824
6825 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006826 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006827 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006828 http_reply_and_close(s, txn->status, NULL);
Christopher Fauletd7c91962015-04-30 11:48:27 +02006829 res->analysers &= AN_FLT_END;
6830 s->req.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006831 if (objt_server(s->target))
6832 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006833
Willy Tarreaue7dff022015-04-03 01:14:29 +02006834 if (!(s->flags & SF_ERR_MASK))
6835 s->flags |= SF_ERR_PRXCOND;
6836 if (!(s->flags & SF_FINST_MASK))
6837 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006838 return 0;
6839
6840 aborted_xfer:
6841 txn->rsp.msg_state = HTTP_MSG_ERROR;
6842 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006843 http_reply_and_close(s, txn->status, NULL);
Christopher Fauletd7c91962015-04-30 11:48:27 +02006844 res->analysers &= AN_FLT_END;
6845 s->req.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006846
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006847 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006848 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006849 if (objt_server(s->target))
6850 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006851
Willy Tarreaue7dff022015-04-03 01:14:29 +02006852 if (!(s->flags & SF_ERR_MASK))
6853 s->flags |= SF_ERR_CLICL;
6854 if (!(s->flags & SF_FINST_MASK))
6855 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006856 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006857}
6858
6859
6860static inline int
6861http_msg_forward_body(struct stream *s, struct http_msg *msg)
6862{
6863 struct channel *chn = msg->chn;
6864 int ret;
6865
6866 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
6867
6868 if (msg->msg_state == HTTP_MSG_ENDING)
6869 goto ending;
6870
6871 /* Neither content-length, nor transfer-encoding was found, so we must
6872 * read the body until the server connection is closed. In that case, we
6873 * eat data as they come. Of course, this happens for response only. */
6874 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6875 unsigned long long len = (chn->buf->i - msg->next);
6876 msg->chunk_len += len;
6877 msg->body_len += len;
6878 }
Christopher Fauletda02e172015-12-04 09:25:05 +01006879 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6880 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6881 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006882 msg->next += ret;
6883 msg->chunk_len -= ret;
6884 if (msg->chunk_len) {
6885 /* input empty or output full */
6886 if (chn->buf->i > msg->next)
6887 chn->flags |= CF_WAKE_WRITE;
6888 goto missing_data_or_waiting;
6889 }
6890
6891 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && !(chn->flags & CF_SHUTR)) {
6892 /* The server still sending data */
6893 goto missing_data_or_waiting;
6894 }
6895 msg->msg_state = HTTP_MSG_ENDING;
6896
6897 ending:
6898 /* we may have some pending data starting at res->buf->p such as a last
6899 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006900 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6901 /* default_ret */ msg->next,
6902 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006903 b_adv(chn->buf, ret);
6904 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006905 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6906 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006907 if (msg->next)
6908 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006909
Christopher Fauletda02e172015-12-04 09:25:05 +01006910 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
6911 /* default_ret */ 1,
6912 /* on_error */ goto error,
6913 /* on_wait */ goto waiting);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006914 msg->msg_state = HTTP_MSG_DONE;
6915 return 1;
6916
6917 missing_data_or_waiting:
6918 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006919 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6920 /* default_ret */ msg->next,
6921 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006922 b_adv(chn->buf, ret);
6923 msg->next -= ret;
6924 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6925 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006926 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006927 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006928 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006929 return 0;
6930 error:
6931 return -1;
6932}
6933
6934static inline int
6935http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
6936{
6937 struct channel *chn = msg->chn;
6938 int ret;
6939
6940 /* Here we have the guarantee to be in one of the following state:
6941 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
6942 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
6943
6944 switch_states:
6945 switch (msg->msg_state) {
6946 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01006947 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6948 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6949 /* on_error */ goto error);
6950 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006951 msg->chunk_len -= ret;
6952 if (msg->chunk_len) {
6953 /* input empty or output full */
6954 if (chn->buf->i > msg->next)
6955 chn->flags |= CF_WAKE_WRITE;
6956 goto missing_data_or_waiting;
6957 }
6958
6959 /* nothing left to forward for this chunk*/
6960 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
6961 /* fall through for HTTP_MSG_CHUNK_CRLF */
6962
6963 case HTTP_MSG_CHUNK_CRLF:
6964 /* we want the CRLF after the data */
6965 ret = http_skip_chunk_crlf(msg);
6966 if (ret == 0)
6967 goto missing_data_or_waiting;
6968 if (ret < 0)
6969 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01006970 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006971 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6972 /* fall through for HTTP_MSG_CHUNK_SIZE */
6973
6974 case HTTP_MSG_CHUNK_SIZE:
6975 /* read the chunk size and assign it to ->chunk_len,
6976 * then set ->next to point to the body and switch to
6977 * DATA or TRAILERS state.
6978 */
6979 ret = http_parse_chunk_size(msg);
6980 if (ret == 0)
6981 goto missing_data_or_waiting;
6982 if (ret < 0)
6983 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01006984 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006985 if (msg->chunk_len) {
6986 msg->msg_state = HTTP_MSG_DATA;
6987 goto switch_states;
6988 }
6989 msg->msg_state = HTTP_MSG_TRAILERS;
6990 /* fall through for HTTP_MSG_TRAILERS */
6991
6992 case HTTP_MSG_TRAILERS:
6993 ret = http_forward_trailers(msg);
6994 if (ret < 0)
6995 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01006996 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
6997 /* default_ret */ 1,
6998 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006999 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007000 if (!ret)
7001 goto missing_data_or_waiting;
7002 break;
7003
7004 case HTTP_MSG_ENDING:
7005 goto ending;
7006
7007 default:
7008 /* This should no happen in this function */
7009 goto error;
7010 }
7011
7012 msg->msg_state = HTTP_MSG_ENDING;
7013 ending:
7014 /* we may have some pending data starting at res->buf->p such as a last
7015 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01007016 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007017 /* default_ret */ msg->next,
7018 /* on_error */ goto error);
7019 b_adv(chn->buf, ret);
7020 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007021 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
7022 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02007023 if (msg->next)
7024 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007025
Christopher Fauletda02e172015-12-04 09:25:05 +01007026 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007027 /* default_ret */ 1,
7028 /* on_error */ goto error,
7029 /* on_wait */ goto waiting);
7030 msg->msg_state = HTTP_MSG_DONE;
7031 return 1;
7032
7033 missing_data_or_waiting:
7034 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01007035 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007036 /* default_ret */ msg->next,
7037 /* on_error */ goto error);
7038 b_adv(chn->buf, ret);
7039 msg->next -= ret;
7040 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
7041 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01007042 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007043 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02007044 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007045 return 0;
7046
7047 chunk_parsing_error:
7048 if (msg->err_pos >= 0) {
7049 if (chn->flags & CF_ISRESP)
7050 http_capture_bad_message(&s->be->invalid_rep, s, msg,
7051 msg->msg_state, strm_fe(s));
7052 else
7053 http_capture_bad_message(&strm_fe(s)->invalid_req, s,
7054 msg, msg->msg_state, s->be);
7055 }
7056 error:
7057 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007058}
7059
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007060
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007061/* Iterate the same filter through all request headers.
7062 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007063 * Since it can manage the switch to another backend, it updates the per-proxy
7064 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007065 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007066int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007067{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007068 char *cur_ptr, *cur_end, *cur_next;
7069 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007070 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007071 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007072 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007073
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007074 last_hdr = 0;
7075
Willy Tarreau9b28e032012-10-12 23:49:43 +02007076 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007077 old_idx = 0;
7078
7079 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007080 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007081 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007082 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007083 (exp->action == ACT_ALLOW ||
7084 exp->action == ACT_DENY ||
7085 exp->action == ACT_TARPIT))
7086 return 0;
7087
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007088 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007089 if (!cur_idx)
7090 break;
7091
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007092 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007093 cur_ptr = cur_next;
7094 cur_end = cur_ptr + cur_hdr->len;
7095 cur_next = cur_end + cur_hdr->cr + 1;
7096
7097 /* Now we have one header between cur_ptr and cur_end,
7098 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007099 */
7100
Willy Tarreau15a53a42015-01-21 13:39:42 +01007101 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007102 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007103 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007104 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007105 last_hdr = 1;
7106 break;
7107
7108 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007109 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007110 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007111 break;
7112
7113 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007114 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007115 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007116 break;
7117
7118 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007119 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7120 if (trash.len < 0)
7121 return -1;
7122
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007123 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007124 /* FIXME: if the user adds a newline in the replacement, the
7125 * index will not be recalculated for now, and the new line
7126 * will not be counted as a new header.
7127 */
7128
7129 cur_end += delta;
7130 cur_next += delta;
7131 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007132 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007133 break;
7134
7135 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007136 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007137 cur_next += delta;
7138
Willy Tarreaufa355d42009-11-29 18:12:29 +01007139 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007140 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7141 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007142 cur_hdr->len = 0;
7143 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007144 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007145 break;
7146
7147 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007148 }
7149
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007150 /* keep the link from this header to next one in case of later
7151 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007152 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007153 old_idx = cur_idx;
7154 }
7155 return 0;
7156}
7157
7158
7159/* Apply the filter to the request line.
7160 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7161 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007162 * Since it can manage the switch to another backend, it updates the per-proxy
7163 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007164 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007165int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007166{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007167 char *cur_ptr, *cur_end;
7168 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007169 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007170 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007171
Willy Tarreau3d300592007-03-18 18:34:41 +01007172 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007173 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007174 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007175 (exp->action == ACT_ALLOW ||
7176 exp->action == ACT_DENY ||
7177 exp->action == ACT_TARPIT))
7178 return 0;
7179 else if (exp->action == ACT_REMOVE)
7180 return 0;
7181
7182 done = 0;
7183
Willy Tarreau9b28e032012-10-12 23:49:43 +02007184 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007185 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007186
7187 /* Now we have the request line between cur_ptr and cur_end */
7188
Willy Tarreau15a53a42015-01-21 13:39:42 +01007189 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007190 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007191 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007192 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007193 done = 1;
7194 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007195
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007196 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007197 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007198 done = 1;
7199 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007200
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007201 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007202 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007203 done = 1;
7204 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007205
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007206 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007207 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7208 if (trash.len < 0)
7209 return -1;
7210
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007211 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007212 /* FIXME: if the user adds a newline in the replacement, the
7213 * index will not be recalculated for now, and the new line
7214 * will not be counted as a new header.
7215 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007216
Willy Tarreaufa355d42009-11-29 18:12:29 +01007217 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007218 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007219 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007220 HTTP_MSG_RQMETH,
7221 cur_ptr, cur_end + 1,
7222 NULL, NULL);
7223 if (unlikely(!cur_end))
7224 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007225
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007226 /* we have a full request and we know that we have either a CR
7227 * or an LF at <ptr>.
7228 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007229 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7230 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007231 /* there is no point trying this regex on headers */
7232 return 1;
7233 }
7234 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007235 return done;
7236}
Willy Tarreau97de6242006-12-27 17:18:38 +01007237
Willy Tarreau58f10d72006-12-04 02:26:12 +01007238
Willy Tarreau58f10d72006-12-04 02:26:12 +01007239
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007240/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007241 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007242 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007243 * unparsable request. Since it can manage the switch to another backend, it
7244 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007245 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007246int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007247{
Willy Tarreau192252e2015-04-04 01:47:55 +02007248 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007249 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007250 struct hdr_exp *exp;
7251
7252 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007253 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007254
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007255 /*
7256 * The interleaving of transformations and verdicts
7257 * makes it difficult to decide to continue or stop
7258 * the evaluation.
7259 */
7260
Willy Tarreau6c123b12010-01-28 20:22:06 +01007261 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7262 break;
7263
Willy Tarreau3d300592007-03-18 18:34:41 +01007264 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007265 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007266 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007267 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007268
7269 /* if this filter had a condition, evaluate it now and skip to
7270 * next filter if the condition does not match.
7271 */
7272 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007273 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007274 ret = acl_pass(ret);
7275 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7276 ret = !ret;
7277
7278 if (!ret)
7279 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007280 }
7281
7282 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007283 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007284 if (unlikely(ret < 0))
7285 return -1;
7286
7287 if (likely(ret == 0)) {
7288 /* The filter did not match the request, it can be
7289 * iterated through all headers.
7290 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007291 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7292 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007293 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007294 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007295 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007296}
7297
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007298
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007299/* Find the end of a cookie value contained between <s> and <e>. It works the
7300 * same way as with headers above except that the semi-colon also ends a token.
7301 * See RFC2965 for more information. Note that it requires a valid header to
7302 * return a valid result.
7303 */
7304char *find_cookie_value_end(char *s, const char *e)
7305{
7306 int quoted, qdpair;
7307
7308 quoted = qdpair = 0;
7309 for (; s < e; s++) {
7310 if (qdpair) qdpair = 0;
7311 else if (quoted) {
7312 if (*s == '\\') qdpair = 1;
7313 else if (*s == '"') quoted = 0;
7314 }
7315 else if (*s == '"') quoted = 1;
7316 else if (*s == ',' || *s == ';') return s;
7317 }
7318 return s;
7319}
7320
7321/* Delete a value in a header between delimiters <from> and <next> in buffer
7322 * <buf>. The number of characters displaced is returned, and the pointer to
7323 * the first delimiter is updated if required. The function tries as much as
7324 * possible to respect the following principles :
7325 * - replace <from> delimiter by the <next> one unless <from> points to a
7326 * colon, in which case <next> is simply removed
7327 * - set exactly one space character after the new first delimiter, unless
7328 * there are not enough characters in the block being moved to do so.
7329 * - remove unneeded spaces before the previous delimiter and after the new
7330 * one.
7331 *
7332 * It is the caller's responsibility to ensure that :
7333 * - <from> points to a valid delimiter or the colon ;
7334 * - <next> points to a valid delimiter or the final CR/LF ;
7335 * - there are non-space chars before <from> ;
7336 * - there is a CR/LF at or after <next>.
7337 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007338int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007339{
7340 char *prev = *from;
7341
7342 if (*prev == ':') {
7343 /* We're removing the first value, preserve the colon and add a
7344 * space if possible.
7345 */
7346 if (!http_is_crlf[(unsigned char)*next])
7347 next++;
7348 prev++;
7349 if (prev < next)
7350 *prev++ = ' ';
7351
7352 while (http_is_spht[(unsigned char)*next])
7353 next++;
7354 } else {
7355 /* Remove useless spaces before the old delimiter. */
7356 while (http_is_spht[(unsigned char)*(prev-1)])
7357 prev--;
7358 *from = prev;
7359
7360 /* copy the delimiter and if possible a space if we're
7361 * not at the end of the line.
7362 */
7363 if (!http_is_crlf[(unsigned char)*next]) {
7364 *prev++ = *next++;
7365 if (prev + 1 < next)
7366 *prev++ = ' ';
7367 while (http_is_spht[(unsigned char)*next])
7368 next++;
7369 }
7370 }
7371 return buffer_replace2(buf, prev, next, NULL, 0);
7372}
7373
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007374/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007375 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007376 * desirable to call it only when needed. This code is quite complex because
7377 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7378 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007379 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007380void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007381{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007382 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007383 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007384 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007385 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007386 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7387 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007388
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007389 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007390 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007391 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007392
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007393 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007394 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007395 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007396
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007397 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007398 hdr_beg = hdr_next;
7399 hdr_end = hdr_beg + cur_hdr->len;
7400 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007401
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007402 /* We have one full header between hdr_beg and hdr_end, and the
7403 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007404 * "Cookie:" headers.
7405 */
7406
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007407 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007408 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007409 old_idx = cur_idx;
7410 continue;
7411 }
7412
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007413 del_from = NULL; /* nothing to be deleted */
7414 preserve_hdr = 0; /* assume we may kill the whole header */
7415
Willy Tarreau58f10d72006-12-04 02:26:12 +01007416 /* Now look for cookies. Conforming to RFC2109, we have to support
7417 * attributes whose name begin with a '$', and associate them with
7418 * the right cookie, if we want to delete this cookie.
7419 * So there are 3 cases for each cookie read :
7420 * 1) it's a special attribute, beginning with a '$' : ignore it.
7421 * 2) it's a server id cookie that we *MAY* want to delete : save
7422 * some pointers on it (last semi-colon, beginning of cookie...)
7423 * 3) it's an application cookie : we *MAY* have to delete a previous
7424 * "special" cookie.
7425 * At the end of loop, if a "special" cookie remains, we may have to
7426 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007427 * *MUST* delete it.
7428 *
7429 * Note: RFC2965 is unclear about the processing of spaces around
7430 * the equal sign in the ATTR=VALUE form. A careful inspection of
7431 * the RFC explicitly allows spaces before it, and not within the
7432 * tokens (attrs or values). An inspection of RFC2109 allows that
7433 * too but section 10.1.3 lets one think that spaces may be allowed
7434 * after the equal sign too, resulting in some (rare) buggy
7435 * implementations trying to do that. So let's do what servers do.
7436 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7437 * allowed quoted strings in values, with any possible character
7438 * after a backslash, including control chars and delimitors, which
7439 * causes parsing to become ambiguous. Browsers also allow spaces
7440 * within values even without quotes.
7441 *
7442 * We have to keep multiple pointers in order to support cookie
7443 * removal at the beginning, middle or end of header without
7444 * corrupting the header. All of these headers are valid :
7445 *
7446 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7447 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7448 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7449 * | | | | | | | | |
7450 * | | | | | | | | hdr_end <--+
7451 * | | | | | | | +--> next
7452 * | | | | | | +----> val_end
7453 * | | | | | +-----------> val_beg
7454 * | | | | +--------------> equal
7455 * | | | +----------------> att_end
7456 * | | +---------------------> att_beg
7457 * | +--------------------------> prev
7458 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007459 */
7460
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007461 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7462 /* Iterate through all cookies on this line */
7463
7464 /* find att_beg */
7465 att_beg = prev + 1;
7466 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7467 att_beg++;
7468
7469 /* find att_end : this is the first character after the last non
7470 * space before the equal. It may be equal to hdr_end.
7471 */
7472 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007473
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007474 while (equal < hdr_end) {
7475 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007476 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007477 if (http_is_spht[(unsigned char)*equal++])
7478 continue;
7479 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007480 }
7481
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007482 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7483 * is between <att_beg> and <equal>, both may be identical.
7484 */
7485
7486 /* look for end of cookie if there is an equal sign */
7487 if (equal < hdr_end && *equal == '=') {
7488 /* look for the beginning of the value */
7489 val_beg = equal + 1;
7490 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7491 val_beg++;
7492
7493 /* find the end of the value, respecting quotes */
7494 next = find_cookie_value_end(val_beg, hdr_end);
7495
7496 /* make val_end point to the first white space or delimitor after the value */
7497 val_end = next;
7498 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7499 val_end--;
7500 } else {
7501 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007502 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007503
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007504 /* We have nothing to do with attributes beginning with '$'. However,
7505 * they will automatically be removed if a header before them is removed,
7506 * since they're supposed to be linked together.
7507 */
7508 if (*att_beg == '$')
7509 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007510
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007511 /* Ignore cookies with no equal sign */
7512 if (equal == next) {
7513 /* This is not our cookie, so we must preserve it. But if we already
7514 * scheduled another cookie for removal, we cannot remove the
7515 * complete header, but we can remove the previous block itself.
7516 */
7517 preserve_hdr = 1;
7518 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007519 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007520 val_end += delta;
7521 next += delta;
7522 hdr_end += delta;
7523 hdr_next += delta;
7524 cur_hdr->len += delta;
7525 http_msg_move_end(&txn->req, delta);
7526 prev = del_from;
7527 del_from = NULL;
7528 }
7529 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007530 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007531
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007532 /* if there are spaces around the equal sign, we need to
7533 * strip them otherwise we'll get trouble for cookie captures,
7534 * or even for rewrites. Since this happens extremely rarely,
7535 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007536 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007537 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7538 int stripped_before = 0;
7539 int stripped_after = 0;
7540
7541 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007542 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007543 equal += stripped_before;
7544 val_beg += stripped_before;
7545 }
7546
7547 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007548 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007549 val_beg += stripped_after;
7550 stripped_before += stripped_after;
7551 }
7552
7553 val_end += stripped_before;
7554 next += stripped_before;
7555 hdr_end += stripped_before;
7556 hdr_next += stripped_before;
7557 cur_hdr->len += stripped_before;
7558 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007559 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007560 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007561
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007562 /* First, let's see if we want to capture this cookie. We check
7563 * that we don't already have a client side cookie, because we
7564 * can only capture one. Also as an optimisation, we ignore
7565 * cookies shorter than the declared name.
7566 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007567 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7568 (val_end - att_beg >= sess->fe->capture_namelen) &&
7569 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007570 int log_len = val_end - att_beg;
7571
7572 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7573 Alert("HTTP logging : out of memory.\n");
7574 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007575 if (log_len > sess->fe->capture_len)
7576 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007577 memcpy(txn->cli_cookie, att_beg, log_len);
7578 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007579 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007580 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007581
Willy Tarreaubca99692010-10-06 19:25:55 +02007582 /* Persistence cookies in passive, rewrite or insert mode have the
7583 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007584 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007585 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007586 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007587 * For cookies in prefix mode, the form is :
7588 *
7589 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007590 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007591 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7592 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7593 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007594 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007595
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007596 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7597 * have the server ID between val_beg and delim, and the original cookie between
7598 * delim+1 and val_end. Otherwise, delim==val_end :
7599 *
7600 * Cookie: NAME=SRV; # in all but prefix modes
7601 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7602 * | || || | |+-> next
7603 * | || || | +--> val_end
7604 * | || || +---------> delim
7605 * | || |+------------> val_beg
7606 * | || +-------------> att_end = equal
7607 * | |+-----------------> att_beg
7608 * | +------------------> prev
7609 * +-------------------------> hdr_beg
7610 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007611
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007612 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007613 for (delim = val_beg; delim < val_end; delim++)
7614 if (*delim == COOKIE_DELIM)
7615 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007616 } else {
7617 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007618 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007619 /* Now check if the cookie contains a date field, which would
7620 * appear after a vertical bar ('|') just after the server name
7621 * and before the delimiter.
7622 */
7623 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7624 if (vbar1) {
7625 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007626 * right is the last seen date. It is a base64 encoded
7627 * 30-bit value representing the UNIX date since the
7628 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007629 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007630 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007631 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007632 if (val_end - vbar1 >= 5) {
7633 val = b64tos30(vbar1);
7634 if (val > 0)
7635 txn->cookie_last_date = val << 2;
7636 }
7637 /* look for a second vertical bar */
7638 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7639 if (vbar1 && (val_end - vbar1 > 5)) {
7640 val = b64tos30(vbar1 + 1);
7641 if (val > 0)
7642 txn->cookie_first_date = val << 2;
7643 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007644 }
7645 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007646
Willy Tarreauf64d1412010-10-07 20:06:11 +02007647 /* if the cookie has an expiration date and the proxy wants to check
7648 * it, then we do that now. We first check if the cookie is too old,
7649 * then only if it has expired. We detect strict overflow because the
7650 * time resolution here is not great (4 seconds). Cookies with dates
7651 * in the future are ignored if their offset is beyond one day. This
7652 * allows an admin to fix timezone issues without expiring everyone
7653 * and at the same time avoids keeping unwanted side effects for too
7654 * long.
7655 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007656 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7657 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007658 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007659 txn->flags &= ~TX_CK_MASK;
7660 txn->flags |= TX_CK_OLD;
7661 delim = val_beg; // let's pretend we have not found the cookie
7662 txn->cookie_first_date = 0;
7663 txn->cookie_last_date = 0;
7664 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007665 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7666 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007667 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007668 txn->flags &= ~TX_CK_MASK;
7669 txn->flags |= TX_CK_EXPIRED;
7670 delim = val_beg; // let's pretend we have not found the cookie
7671 txn->cookie_first_date = 0;
7672 txn->cookie_last_date = 0;
7673 }
7674
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007675 /* Here, we'll look for the first running server which supports the cookie.
7676 * This allows to share a same cookie between several servers, for example
7677 * to dedicate backup servers to specific servers only.
7678 * However, to prevent clients from sticking to cookie-less backup server
7679 * when they have incidentely learned an empty cookie, we simply ignore
7680 * empty cookies and mark them as invalid.
7681 * The same behaviour is applied when persistence must be ignored.
7682 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007683 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007684 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007685
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007686 while (srv) {
7687 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7688 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007689 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007690 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007691 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007692 /* we found the server and we can use it */
7693 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007694 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007695 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007696 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007697 break;
7698 } else {
7699 /* we found a server, but it's down,
7700 * mark it as such and go on in case
7701 * another one is available.
7702 */
7703 txn->flags &= ~TX_CK_MASK;
7704 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007705 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007706 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007707 srv = srv->next;
7708 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007709
Willy Tarreauf64d1412010-10-07 20:06:11 +02007710 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007711 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007712 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007713 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007714 txn->flags |= TX_CK_UNUSED;
7715 else
7716 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007717 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007718
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007719 /* depending on the cookie mode, we may have to either :
7720 * - delete the complete cookie if we're in insert+indirect mode, so that
7721 * the server never sees it ;
7722 * - remove the server id from the cookie value, and tag the cookie as an
7723 * application cookie so that it does not get accidentely removed later,
7724 * if we're in cookie prefix mode
7725 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007726 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007727 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007728
Willy Tarreau9b28e032012-10-12 23:49:43 +02007729 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007730 val_end += delta;
7731 next += delta;
7732 hdr_end += delta;
7733 hdr_next += delta;
7734 cur_hdr->len += delta;
7735 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007736
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007737 del_from = NULL;
7738 preserve_hdr = 1; /* we want to keep this cookie */
7739 }
7740 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007741 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007742 del_from = prev;
7743 }
7744 } else {
7745 /* This is not our cookie, so we must preserve it. But if we already
7746 * scheduled another cookie for removal, we cannot remove the
7747 * complete header, but we can remove the previous block itself.
7748 */
7749 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007750
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007751 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007752 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007753 if (att_beg >= del_from)
7754 att_beg += delta;
7755 if (att_end >= del_from)
7756 att_end += delta;
7757 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007758 val_end += delta;
7759 next += delta;
7760 hdr_end += delta;
7761 hdr_next += delta;
7762 cur_hdr->len += delta;
7763 http_msg_move_end(&txn->req, delta);
7764 prev = del_from;
7765 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007766 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007767 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007768
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007769 /* continue with next cookie on this header line */
7770 att_beg = next;
7771 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007772
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007773 /* There are no more cookies on this line.
7774 * We may still have one (or several) marked for deletion at the
7775 * end of the line. We must do this now in two ways :
7776 * - if some cookies must be preserved, we only delete from the
7777 * mark to the end of line ;
7778 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007779 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007780 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007781 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007782 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007783 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007784 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007785 cur_hdr->len += delta;
7786 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007787 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007788
7789 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007790 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7791 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007792 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007793 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007794 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007795 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007796 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007797 }
7798
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007799 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007800 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007801 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007802}
7803
7804
Willy Tarreaua15645d2007-03-18 16:22:39 +01007805/* Iterate the same filter through all response headers contained in <rtr>.
7806 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7807 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007808int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007809{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007810 char *cur_ptr, *cur_end, *cur_next;
7811 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007812 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007813 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007814 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007815
7816 last_hdr = 0;
7817
Willy Tarreau9b28e032012-10-12 23:49:43 +02007818 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007819 old_idx = 0;
7820
7821 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007822 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007823 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007824 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007825 (exp->action == ACT_ALLOW ||
7826 exp->action == ACT_DENY))
7827 return 0;
7828
7829 cur_idx = txn->hdr_idx.v[old_idx].next;
7830 if (!cur_idx)
7831 break;
7832
7833 cur_hdr = &txn->hdr_idx.v[cur_idx];
7834 cur_ptr = cur_next;
7835 cur_end = cur_ptr + cur_hdr->len;
7836 cur_next = cur_end + cur_hdr->cr + 1;
7837
7838 /* Now we have one header between cur_ptr and cur_end,
7839 * and the next header starts at cur_next.
7840 */
7841
Willy Tarreau15a53a42015-01-21 13:39:42 +01007842 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007843 switch (exp->action) {
7844 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007845 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007846 last_hdr = 1;
7847 break;
7848
7849 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007850 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007851 last_hdr = 1;
7852 break;
7853
7854 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007855 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7856 if (trash.len < 0)
7857 return -1;
7858
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007859 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007860 /* FIXME: if the user adds a newline in the replacement, the
7861 * index will not be recalculated for now, and the new line
7862 * will not be counted as a new header.
7863 */
7864
7865 cur_end += delta;
7866 cur_next += delta;
7867 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007868 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007869 break;
7870
7871 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007872 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007873 cur_next += delta;
7874
Willy Tarreaufa355d42009-11-29 18:12:29 +01007875 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007876 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7877 txn->hdr_idx.used--;
7878 cur_hdr->len = 0;
7879 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007880 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007881 break;
7882
7883 }
7884 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007885
7886 /* keep the link from this header to next one in case of later
7887 * removal of next header.
7888 */
7889 old_idx = cur_idx;
7890 }
7891 return 0;
7892}
7893
7894
7895/* Apply the filter to the status line in the response buffer <rtr>.
7896 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7897 * or -1 if a replacement resulted in an invalid status line.
7898 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007899int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007900{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007901 char *cur_ptr, *cur_end;
7902 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007903 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007904 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007905
7906
Willy Tarreau3d300592007-03-18 18:34:41 +01007907 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007908 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007909 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007910 (exp->action == ACT_ALLOW ||
7911 exp->action == ACT_DENY))
7912 return 0;
7913 else if (exp->action == ACT_REMOVE)
7914 return 0;
7915
7916 done = 0;
7917
Willy Tarreau9b28e032012-10-12 23:49:43 +02007918 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007919 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007920
7921 /* Now we have the status line between cur_ptr and cur_end */
7922
Willy Tarreau15a53a42015-01-21 13:39:42 +01007923 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007924 switch (exp->action) {
7925 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007926 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007927 done = 1;
7928 break;
7929
7930 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007931 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007932 done = 1;
7933 break;
7934
7935 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007936 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7937 if (trash.len < 0)
7938 return -1;
7939
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007940 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007941 /* FIXME: if the user adds a newline in the replacement, the
7942 * index will not be recalculated for now, and the new line
7943 * will not be counted as a new header.
7944 */
7945
Willy Tarreaufa355d42009-11-29 18:12:29 +01007946 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007947 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007948 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007949 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007950 cur_ptr, cur_end + 1,
7951 NULL, NULL);
7952 if (unlikely(!cur_end))
7953 return -1;
7954
7955 /* we have a full respnse and we know that we have either a CR
7956 * or an LF at <ptr>.
7957 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007958 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007959 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007960 /* there is no point trying this regex on headers */
7961 return 1;
7962 }
7963 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007964 return done;
7965}
7966
7967
7968
7969/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007970 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007971 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7972 * unparsable response.
7973 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007974int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007975{
Willy Tarreau192252e2015-04-04 01:47:55 +02007976 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007977 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007978 struct hdr_exp *exp;
7979
7980 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007981 int ret;
7982
7983 /*
7984 * The interleaving of transformations and verdicts
7985 * makes it difficult to decide to continue or stop
7986 * the evaluation.
7987 */
7988
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007989 if (txn->flags & TX_SVDENY)
7990 break;
7991
Willy Tarreau3d300592007-03-18 18:34:41 +01007992 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007993 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7994 exp->action == ACT_PASS)) {
7995 exp = exp->next;
7996 continue;
7997 }
7998
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007999 /* if this filter had a condition, evaluate it now and skip to
8000 * next filter if the condition does not match.
8001 */
8002 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008003 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008004 ret = acl_pass(ret);
8005 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8006 ret = !ret;
8007 if (!ret)
8008 continue;
8009 }
8010
Willy Tarreaua15645d2007-03-18 16:22:39 +01008011 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008012 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008013 if (unlikely(ret < 0))
8014 return -1;
8015
8016 if (likely(ret == 0)) {
8017 /* The filter did not match the response, it can be
8018 * iterated through all headers.
8019 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008020 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8021 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008022 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008023 }
8024 return 0;
8025}
8026
8027
Willy Tarreaua15645d2007-03-18 16:22:39 +01008028/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008029 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008030 * desirable to call it only when needed. This function is also used when we
8031 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008032 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008033void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008034{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008035 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008036 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008037 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008038 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008039 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008040 char *hdr_beg, *hdr_end, *hdr_next;
8041 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008042
Willy Tarreaua15645d2007-03-18 16:22:39 +01008043 /* Iterate through the headers.
8044 * we start with the start line.
8045 */
8046 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008047 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008048
8049 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8050 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008051 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008052
8053 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008054 hdr_beg = hdr_next;
8055 hdr_end = hdr_beg + cur_hdr->len;
8056 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008057
Willy Tarreau24581ba2010-08-31 22:39:35 +02008058 /* We have one full header between hdr_beg and hdr_end, and the
8059 * next header starts at hdr_next. We're only interested in
8060 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008061 */
8062
Willy Tarreau24581ba2010-08-31 22:39:35 +02008063 is_cookie2 = 0;
8064 prev = hdr_beg + 10;
8065 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008066 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008067 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8068 if (!val) {
8069 old_idx = cur_idx;
8070 continue;
8071 }
8072 is_cookie2 = 1;
8073 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008074 }
8075
Willy Tarreau24581ba2010-08-31 22:39:35 +02008076 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8077 * <prev> points to the colon.
8078 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008079 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008080
Willy Tarreau24581ba2010-08-31 22:39:35 +02008081 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8082 * check-cache is enabled) and we are not interested in checking
8083 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008084 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02008085 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008086 return;
8087
Willy Tarreau24581ba2010-08-31 22:39:35 +02008088 /* OK so now we know we have to process this response cookie.
8089 * The format of the Set-Cookie header is slightly different
8090 * from the format of the Cookie header in that it does not
8091 * support the comma as a cookie delimiter (thus the header
8092 * cannot be folded) because the Expires attribute described in
8093 * the original Netscape's spec may contain an unquoted date
8094 * with a comma inside. We have to live with this because
8095 * many browsers don't support Max-Age and some browsers don't
8096 * support quoted strings. However the Set-Cookie2 header is
8097 * clean.
8098 *
8099 * We have to keep multiple pointers in order to support cookie
8100 * removal at the beginning, middle or end of header without
8101 * corrupting the header (in case of set-cookie2). A special
8102 * pointer, <scav> points to the beginning of the set-cookie-av
8103 * fields after the first semi-colon. The <next> pointer points
8104 * either to the end of line (set-cookie) or next unquoted comma
8105 * (set-cookie2). All of these headers are valid :
8106 *
8107 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8108 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8109 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8110 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8111 * | | | | | | | | | |
8112 * | | | | | | | | +-> next hdr_end <--+
8113 * | | | | | | | +------------> scav
8114 * | | | | | | +--------------> val_end
8115 * | | | | | +--------------------> val_beg
8116 * | | | | +----------------------> equal
8117 * | | | +------------------------> att_end
8118 * | | +----------------------------> att_beg
8119 * | +------------------------------> prev
8120 * +-----------------------------------------> hdr_beg
8121 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008122
Willy Tarreau24581ba2010-08-31 22:39:35 +02008123 for (; prev < hdr_end; prev = next) {
8124 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008125
Willy Tarreau24581ba2010-08-31 22:39:35 +02008126 /* find att_beg */
8127 att_beg = prev + 1;
8128 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8129 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008130
Willy Tarreau24581ba2010-08-31 22:39:35 +02008131 /* find att_end : this is the first character after the last non
8132 * space before the equal. It may be equal to hdr_end.
8133 */
8134 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008135
Willy Tarreau24581ba2010-08-31 22:39:35 +02008136 while (equal < hdr_end) {
8137 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8138 break;
8139 if (http_is_spht[(unsigned char)*equal++])
8140 continue;
8141 att_end = equal;
8142 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008143
Willy Tarreau24581ba2010-08-31 22:39:35 +02008144 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8145 * is between <att_beg> and <equal>, both may be identical.
8146 */
8147
8148 /* look for end of cookie if there is an equal sign */
8149 if (equal < hdr_end && *equal == '=') {
8150 /* look for the beginning of the value */
8151 val_beg = equal + 1;
8152 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8153 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008154
Willy Tarreau24581ba2010-08-31 22:39:35 +02008155 /* find the end of the value, respecting quotes */
8156 next = find_cookie_value_end(val_beg, hdr_end);
8157
8158 /* make val_end point to the first white space or delimitor after the value */
8159 val_end = next;
8160 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8161 val_end--;
8162 } else {
8163 /* <equal> points to next comma, semi-colon or EOL */
8164 val_beg = val_end = next = equal;
8165 }
8166
8167 if (next < hdr_end) {
8168 /* Set-Cookie2 supports multiple cookies, and <next> points to
8169 * a colon or semi-colon before the end. So skip all attr-value
8170 * pairs and look for the next comma. For Set-Cookie, since
8171 * commas are permitted in values, skip to the end.
8172 */
8173 if (is_cookie2)
8174 next = find_hdr_value_end(next, hdr_end);
8175 else
8176 next = hdr_end;
8177 }
8178
8179 /* Now everything is as on the diagram above */
8180
8181 /* Ignore cookies with no equal sign */
8182 if (equal == val_end)
8183 continue;
8184
8185 /* If there are spaces around the equal sign, we need to
8186 * strip them otherwise we'll get trouble for cookie captures,
8187 * or even for rewrites. Since this happens extremely rarely,
8188 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008189 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008190 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8191 int stripped_before = 0;
8192 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008193
Willy Tarreau24581ba2010-08-31 22:39:35 +02008194 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008195 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008196 equal += stripped_before;
8197 val_beg += stripped_before;
8198 }
8199
8200 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008201 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008202 val_beg += stripped_after;
8203 stripped_before += stripped_after;
8204 }
8205
8206 val_end += stripped_before;
8207 next += stripped_before;
8208 hdr_end += stripped_before;
8209 hdr_next += stripped_before;
8210 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008211 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008212 }
8213
8214 /* First, let's see if we want to capture this cookie. We check
8215 * that we don't already have a server side cookie, because we
8216 * can only capture one. Also as an optimisation, we ignore
8217 * cookies shorter than the declared name.
8218 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008219 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008220 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008221 (val_end - att_beg >= sess->fe->capture_namelen) &&
8222 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008223 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008224 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008225 Alert("HTTP logging : out of memory.\n");
8226 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008227 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008228 if (log_len > sess->fe->capture_len)
8229 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008230 memcpy(txn->srv_cookie, att_beg, log_len);
8231 txn->srv_cookie[log_len] = 0;
8232 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008233 }
8234
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008235 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008236 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008237 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008238 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8239 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008240 /* assume passive cookie by default */
8241 txn->flags &= ~TX_SCK_MASK;
8242 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008243
8244 /* If the cookie is in insert mode on a known server, we'll delete
8245 * this occurrence because we'll insert another one later.
8246 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008247 * a direct access.
8248 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008249 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008250 /* The "preserve" flag was set, we don't want to touch the
8251 * server's cookie.
8252 */
8253 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008254 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008255 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008256 /* this cookie must be deleted */
8257 if (*prev == ':' && next == hdr_end) {
8258 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008259 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008260 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8261 txn->hdr_idx.used--;
8262 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008263 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008264 hdr_next += delta;
8265 http_msg_move_end(&txn->rsp, delta);
8266 /* note: while both invalid now, <next> and <hdr_end>
8267 * are still equal, so the for() will stop as expected.
8268 */
8269 } else {
8270 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008271 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008272 next = prev;
8273 hdr_end += delta;
8274 hdr_next += delta;
8275 cur_hdr->len += delta;
8276 http_msg_move_end(&txn->rsp, delta);
8277 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008278 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008279 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008280 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008281 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008282 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008283 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008284 * with this server since we know it.
8285 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008286 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008287 next += delta;
8288 hdr_end += delta;
8289 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008290 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008291 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008292
Willy Tarreauf1348312010-10-07 15:54:11 +02008293 txn->flags &= ~TX_SCK_MASK;
8294 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008295 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008296 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008297 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008298 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008299 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008300 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008301 next += delta;
8302 hdr_end += delta;
8303 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008304 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008305 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008306
Willy Tarreau827aee92011-03-10 16:55:02 +01008307 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008308 txn->flags &= ~TX_SCK_MASK;
8309 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008310 }
8311 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008312 /* that's done for this cookie, check the next one on the same
8313 * line when next != hdr_end (only if is_cookie2).
8314 */
8315 }
8316 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008317 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008318 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008319}
8320
8321
Willy Tarreaua15645d2007-03-18 16:22:39 +01008322/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008323 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008324 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008325void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008326{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008327 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008328 char *p1, *p2;
8329
8330 char *cur_ptr, *cur_end, *cur_next;
8331 int cur_idx;
8332
Willy Tarreau5df51872007-11-25 16:20:08 +01008333 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008334 return;
8335
8336 /* Iterate through the headers.
8337 * we start with the start line.
8338 */
8339 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008340 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008341
8342 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8343 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008344 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008345
8346 cur_hdr = &txn->hdr_idx.v[cur_idx];
8347 cur_ptr = cur_next;
8348 cur_end = cur_ptr + cur_hdr->len;
8349 cur_next = cur_end + cur_hdr->cr + 1;
8350
8351 /* We have one full header between cur_ptr and cur_end, and the
8352 * next header starts at cur_next. We're only interested in
8353 * "Cookie:" headers.
8354 */
8355
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008356 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8357 if (val) {
8358 if ((cur_end - (cur_ptr + val) >= 8) &&
8359 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8360 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8361 return;
8362 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008363 }
8364
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008365 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8366 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008367 continue;
8368
8369 /* OK, right now we know we have a cache-control header at cur_ptr */
8370
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008371 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008372
8373 if (p1 >= cur_end) /* no more info */
8374 continue;
8375
8376 /* p1 is at the beginning of the value */
8377 p2 = p1;
8378
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008379 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008380 p2++;
8381
8382 /* we have a complete value between p1 and p2 */
8383 if (p2 < cur_end && *p2 == '=') {
8384 /* we have something of the form no-cache="set-cookie" */
8385 if ((cur_end - p1 >= 21) &&
8386 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8387 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008388 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008389 continue;
8390 }
8391
8392 /* OK, so we know that either p2 points to the end of string or to a comma */
8393 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008394 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008395 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8396 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8397 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008398 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008399 return;
8400 }
8401
8402 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008403 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008404 continue;
8405 }
8406 }
8407}
8408
Willy Tarreau58f10d72006-12-04 02:26:12 +01008409
Willy Tarreaub2513902006-12-17 14:52:38 +01008410/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008411 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008412 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008413 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008414 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008415 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008416 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008417 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008418 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008419int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008420{
8421 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008422 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008423 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008424
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008425 if (!uri_auth)
8426 return 0;
8427
Cyril Bonté70be45d2010-10-12 00:14:35 +02008428 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008429 return 0;
8430
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008431 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008432 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008433 return 0;
8434
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008435 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008436 return 0;
8437
Willy Tarreaub2513902006-12-17 14:52:38 +01008438 return 1;
8439}
8440
Willy Tarreau4076a152009-04-02 15:18:36 +02008441/*
8442 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008443 * By default it tries to report the error position as msg->err_pos. However if
8444 * this one is not set, it will then report msg->next, which is the last known
8445 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008446 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008447 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008448void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008449 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008450 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008451{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008452 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008453 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008454 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008455
Willy Tarreauf3764b72016-03-31 13:45:10 +02008456 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02008457 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008458 len1 = MIN(len1, es->len);
8459 len2 = es->len - len1; /* remaining data if buffer wraps */
8460
Willy Tarreauf3764b72016-03-31 13:45:10 +02008461 if (!es->buf)
8462 es->buf = malloc(global.tune.bufsize);
8463
8464 if (es->buf) {
8465 memcpy(es->buf, chn->buf->p, len1);
8466 if (len2)
8467 memcpy(es->buf + len1, chn->buf->data, len2);
8468 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008469
Willy Tarreau4076a152009-04-02 15:18:36 +02008470 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008471 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008472 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008473 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008474
Willy Tarreau4076a152009-04-02 15:18:36 +02008475 es->when = date; // user-visible date
8476 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008477 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008478 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008479 if (objt_conn(sess->origin))
8480 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008481 else
8482 memset(&es->src, 0, sizeof(es->src));
8483
Willy Tarreau078272e2010-12-12 12:46:33 +01008484 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008485 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008486 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008487 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008488 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008489 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008490 es->b_out = chn->buf->o;
8491 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008492 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008493 es->m_clen = msg->chunk_len;
8494 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008495}
Willy Tarreaub2513902006-12-17 14:52:38 +01008496
Willy Tarreau294c4732011-12-16 21:35:50 +01008497/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8498 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8499 * performed over the whole headers. Otherwise it must contain a valid header
8500 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8501 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8502 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8503 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008504 * -1. The value fetch stops at commas, so this function is suited for use with
8505 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008506 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008507 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008508unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008509 struct hdr_idx *idx, int occ,
8510 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008511{
Willy Tarreau294c4732011-12-16 21:35:50 +01008512 struct hdr_ctx local_ctx;
8513 char *ptr_hist[MAX_HDR_HISTORY];
8514 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008515 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008516 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008517
Willy Tarreau294c4732011-12-16 21:35:50 +01008518 if (!ctx) {
8519 local_ctx.idx = 0;
8520 ctx = &local_ctx;
8521 }
8522
Willy Tarreaubce70882009-09-07 11:51:47 +02008523 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008524 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008525 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008526 occ--;
8527 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008528 *vptr = ctx->line + ctx->val;
8529 *vlen = ctx->vlen;
8530 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008531 }
8532 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008533 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008534 }
8535
8536 /* negative occurrence, we scan all the list then walk back */
8537 if (-occ > MAX_HDR_HISTORY)
8538 return 0;
8539
Willy Tarreau294c4732011-12-16 21:35:50 +01008540 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008541 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008542 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8543 len_hist[hist_ptr] = ctx->vlen;
8544 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008545 hist_ptr = 0;
8546 found++;
8547 }
8548 if (-occ > found)
8549 return 0;
8550 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008551 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8552 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8553 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008554 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008555 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008556 if (hist_ptr >= MAX_HDR_HISTORY)
8557 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008558 *vptr = ptr_hist[hist_ptr];
8559 *vlen = len_hist[hist_ptr];
8560 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008561}
8562
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008563/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8564 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8565 * performed over the whole headers. Otherwise it must contain a valid header
8566 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8567 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8568 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8569 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8570 * -1. This function differs from http_get_hdr() in that it only returns full
8571 * line header values and does not stop at commas.
8572 * The return value is 0 if nothing was found, or non-zero otherwise.
8573 */
8574unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8575 struct hdr_idx *idx, int occ,
8576 struct hdr_ctx *ctx, char **vptr, int *vlen)
8577{
8578 struct hdr_ctx local_ctx;
8579 char *ptr_hist[MAX_HDR_HISTORY];
8580 int len_hist[MAX_HDR_HISTORY];
8581 unsigned int hist_ptr;
8582 int found;
8583
8584 if (!ctx) {
8585 local_ctx.idx = 0;
8586 ctx = &local_ctx;
8587 }
8588
8589 if (occ >= 0) {
8590 /* search from the beginning */
8591 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8592 occ--;
8593 if (occ <= 0) {
8594 *vptr = ctx->line + ctx->val;
8595 *vlen = ctx->vlen;
8596 return 1;
8597 }
8598 }
8599 return 0;
8600 }
8601
8602 /* negative occurrence, we scan all the list then walk back */
8603 if (-occ > MAX_HDR_HISTORY)
8604 return 0;
8605
8606 found = hist_ptr = 0;
8607 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8608 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8609 len_hist[hist_ptr] = ctx->vlen;
8610 if (++hist_ptr >= MAX_HDR_HISTORY)
8611 hist_ptr = 0;
8612 found++;
8613 }
8614 if (-occ > found)
8615 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008616
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008617 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008618 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8619 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8620 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008621 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008622 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008623 if (hist_ptr >= MAX_HDR_HISTORY)
8624 hist_ptr -= MAX_HDR_HISTORY;
8625 *vptr = ptr_hist[hist_ptr];
8626 *vlen = len_hist[hist_ptr];
8627 return 1;
8628}
8629
Willy Tarreaubaaee002006-06-26 02:48:02 +02008630/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008631 * Print a debug line with a header. Always stop at the first CR or LF char,
8632 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8633 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008634 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008635void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008636{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008637 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008638 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008639
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008640 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008641 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008642 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008643 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 +02008644
8645 for (max = 0; start + max < end; max++)
8646 if (start[max] == '\r' || start[max] == '\n')
8647 break;
8648
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008649 UBOUND(max, trash.size - trash.len - 3);
8650 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8651 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008652 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008653}
8654
Willy Tarreaueee5b512015-04-03 23:46:31 +02008655
8656/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8657 * The hdr_idx is allocated as well. In case of allocation failure, everything
8658 * allocated is freed and NULL is returned. Otherwise the new transaction is
8659 * assigned to the stream and returned.
8660 */
8661struct http_txn *http_alloc_txn(struct stream *s)
8662{
8663 struct http_txn *txn = s->txn;
8664
8665 if (txn)
8666 return txn;
8667
8668 txn = pool_alloc2(pool2_http_txn);
8669 if (!txn)
8670 return txn;
8671
8672 txn->hdr_idx.size = global.tune.max_http_hdr;
8673 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8674 if (!txn->hdr_idx.v) {
8675 pool_free2(pool2_http_txn, txn);
8676 return NULL;
8677 }
8678
8679 s->txn = txn;
8680 return txn;
8681}
8682
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008683void http_txn_reset_req(struct http_txn *txn)
8684{
8685 txn->req.flags = 0;
8686 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8687 txn->req.next = 0;
8688 txn->req.chunk_len = 0LL;
8689 txn->req.body_len = 0LL;
8690 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8691}
8692
8693void http_txn_reset_res(struct http_txn *txn)
8694{
8695 txn->rsp.flags = 0;
8696 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8697 txn->rsp.next = 0;
8698 txn->rsp.chunk_len = 0LL;
8699 txn->rsp.body_len = 0LL;
8700 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8701}
8702
Willy Tarreau0937bc42009-12-22 15:03:09 +01008703/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008704 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008705 * the required fields are properly allocated and that we only need to (re)init
8706 * them. This should be used before processing any new request.
8707 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008708void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008709{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008710 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008711 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008712
8713 txn->flags = 0;
8714 txn->status = -1;
8715
Willy Tarreauf64d1412010-10-07 20:06:11 +02008716 txn->cookie_first_date = 0;
8717 txn->cookie_last_date = 0;
8718
Willy Tarreaueee5b512015-04-03 23:46:31 +02008719 txn->srv_cookie = NULL;
8720 txn->cli_cookie = NULL;
8721 txn->uri = NULL;
8722
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008723 http_txn_reset_req(txn);
8724 http_txn_reset_res(txn);
8725
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008726 txn->req.chn = &s->req;
8727 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008728
8729 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008730
8731 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8732 if (fe->options2 & PR_O2_REQBUG_OK)
8733 txn->req.err_pos = -1; /* let buggy requests pass */
8734
Willy Tarreau0937bc42009-12-22 15:03:09 +01008735 if (txn->hdr_idx.v)
8736 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008737
8738 vars_init(&s->vars_txn, SCOPE_TXN);
8739 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008740}
8741
8742/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008743void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008744{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008745 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008746 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008747
8748 /* these ones will have been dynamically allocated */
8749 pool_free2(pool2_requri, txn->uri);
8750 pool_free2(pool2_capture, txn->cli_cookie);
8751 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008752 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008753
William Lallemanda73203e2012-03-12 12:48:57 +01008754 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008755 txn->uri = NULL;
8756 txn->srv_cookie = NULL;
8757 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008758
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008759 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008760 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008761 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008762 pool_free2(h->pool, s->req_cap[h->index]);
8763 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008764 }
8765
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008766 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008767 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008768 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008769 pool_free2(h->pool, s->res_cap[h->index]);
8770 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008771 }
8772
Willy Tarreau6204cd92016-03-10 16:33:04 +01008773 vars_prune(&s->vars_txn, s->sess, s);
8774 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008775}
8776
8777/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008778void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008779{
8780 http_end_txn(s);
8781 http_init_txn(s);
8782
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008783 /* reinitialise the current rule list pointer to NULL. We are sure that
8784 * any rulelist match the NULL pointer.
8785 */
8786 s->current_rule_list = NULL;
8787
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008788 s->be = strm_fe(s);
8789 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008790 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008791 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008792 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008793 /* re-init store persistence */
8794 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008795 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008796
Willy Tarreau0937bc42009-12-22 15:03:09 +01008797 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008798
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008799 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008800
Willy Tarreau739cfba2010-01-25 23:11:14 +01008801 /* We must trim any excess data from the response buffer, because we
8802 * may have blocked an invalid response from a server that we don't
8803 * want to accidentely forward once we disable the analysers, nor do
8804 * we want those data to come along with next response. A typical
8805 * example of such data would be from a buggy server responding to
8806 * a HEAD with some data, or sending more than the advertised
8807 * content-length.
8808 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008809 if (unlikely(s->res.buf->i))
8810 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008811
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008812 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008813 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008814
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008815 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008816 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008817
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008818 s->req.rex = TICK_ETERNITY;
8819 s->req.wex = TICK_ETERNITY;
8820 s->req.analyse_exp = TICK_ETERNITY;
8821 s->res.rex = TICK_ETERNITY;
8822 s->res.wex = TICK_ETERNITY;
8823 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008824}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008825
Sasha Pachev218f0642014-06-16 12:05:59 -06008826void free_http_res_rules(struct list *r)
8827{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008828 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008829
8830 list_for_each_entry_safe(pr, tr, r, list) {
8831 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008832 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008833 free(pr);
8834 }
8835}
8836
8837void free_http_req_rules(struct list *r)
8838{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008839 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01008840
8841 list_for_each_entry_safe(pr, tr, r, list) {
8842 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008843 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008844 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008845
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008846 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008847 free(pr);
8848 }
8849}
8850
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008851/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008852struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01008853{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008854 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008855 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008856 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008857 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008858
Vincent Bernat02779b62016-04-03 13:48:43 +02008859 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01008860 if (!rule) {
8861 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008862 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008863 }
8864
CJ Ess108b1dd2015-04-07 12:03:37 -04008865 rule->deny_status = HTTP_ERR_403;
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008866 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008867 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01008868 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02008869 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04008870 int code;
8871 int hc;
8872
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008873 rule->action = ACT_ACTION_DENY;
Willy Tarreauff011f22011-01-06 17:51:27 +01008874 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04008875 if (strcmp(args[cur_arg], "deny_status") == 0) {
8876 cur_arg++;
8877 if (!args[cur_arg]) {
8878 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
8879 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
8880 goto out_err;
8881 }
8882
8883 code = atol(args[cur_arg]);
8884 cur_arg++;
8885 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
8886 if (http_err_codes[hc] == code) {
8887 rule->deny_status = hc;
8888 break;
8889 }
8890 }
8891
8892 if (hc >= HTTP_ERR_SIZE) {
8893 Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n",
8894 file, linenum, code);
8895 }
8896 }
Willy Tarreauccbcc372012-12-27 12:37:57 +01008897 } else if (!strcmp(args[0], "tarpit")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008898 rule->action = ACT_HTTP_REQ_TARPIT;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008899 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008900 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008901 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008902 cur_arg = 1;
8903
8904 while(*args[cur_arg]) {
8905 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008906 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008907 cur_arg+=2;
8908 continue;
8909 } else
8910 break;
8911 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008912 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008913 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008914 cur_arg = 1;
8915
8916 if (!*args[cur_arg] ||
8917 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8918 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8919 file, linenum, args[0]);
8920 goto out_err;
8921 }
8922 rule->arg.nice = atoi(args[cur_arg]);
8923 if (rule->arg.nice < -1024)
8924 rule->arg.nice = -1024;
8925 else if (rule->arg.nice > 1024)
8926 rule->arg.nice = 1024;
8927 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008928 } else if (!strcmp(args[0], "set-tos")) {
8929#ifdef IP_TOS
8930 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008931 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008932 cur_arg = 1;
8933
8934 if (!*args[cur_arg] ||
8935 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8936 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8937 file, linenum, args[0]);
8938 goto out_err;
8939 }
8940
8941 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8942 if (err && *err != '\0') {
8943 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8944 file, linenum, err, args[0]);
8945 goto out_err;
8946 }
8947 cur_arg++;
8948#else
8949 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8950 goto out_err;
8951#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008952 } else if (!strcmp(args[0], "set-mark")) {
8953#ifdef SO_MARK
8954 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008955 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008956 cur_arg = 1;
8957
8958 if (!*args[cur_arg] ||
8959 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8960 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8961 file, linenum, args[0]);
8962 goto out_err;
8963 }
8964
8965 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8966 if (err && *err != '\0') {
8967 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8968 file, linenum, err, args[0]);
8969 goto out_err;
8970 }
8971 cur_arg++;
8972 global.last_checks |= LSTCHK_NETADM;
8973#else
8974 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8975 goto out_err;
8976#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008977 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008978 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008979 cur_arg = 1;
8980
8981 if (!*args[cur_arg] ||
8982 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8983 bad_log_level:
8984 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8985 file, linenum, args[0]);
8986 goto out_err;
8987 }
8988 if (strcmp(args[cur_arg], "silent") == 0)
8989 rule->arg.loglevel = -1;
8990 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8991 goto bad_log_level;
8992 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008993 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008994 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008995 cur_arg = 1;
8996
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008997 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8998 (*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 +01008999 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9000 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009001 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009002 }
9003
9004 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9005 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9006 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009007
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009008 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009009 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009010 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9011 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009012 free(proxy->conf.lfs_file);
9013 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9014 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009015 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009016 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009017 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009018 cur_arg = 1;
9019
9020 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009021 (*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 -06009022 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9023 file, linenum, args[0]);
9024 goto out_err;
9025 }
9026
9027 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9028 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9029 LIST_INIT(&rule->arg.hdr_add.fmt);
9030
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009031 error = NULL;
9032 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9033 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9034 args[cur_arg + 1], error);
9035 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009036 goto out_err;
9037 }
9038
9039 proxy->conf.args.ctx = ARGC_HRQ;
9040 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9041 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9042 file, linenum);
9043
9044 free(proxy->conf.lfs_file);
9045 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9046 proxy->conf.lfs_line = proxy->conf.args.line;
9047 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009048 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009049 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009050 cur_arg = 1;
9051
9052 if (!*args[cur_arg] ||
9053 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9054 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9055 file, linenum, args[0]);
9056 goto out_err;
9057 }
9058
9059 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9060 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9061
9062 proxy->conf.args.ctx = ARGC_HRQ;
9063 free(proxy->conf.lfs_file);
9064 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9065 proxy->conf.lfs_line = proxy->conf.args.line;
9066 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009067 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9068 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009069 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009070 struct sample_expr *expr;
9071 unsigned int where;
9072 char *err = NULL;
9073
9074 cur_arg = 1;
9075 proxy->conf.args.ctx = ARGC_TRK;
9076
9077 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9078 if (!expr) {
9079 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9080 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9081 free(err);
9082 goto out_err;
9083 }
9084
9085 where = 0;
9086 if (proxy->cap & PR_CAP_FE)
9087 where |= SMP_VAL_FE_HRQ_HDR;
9088 if (proxy->cap & PR_CAP_BE)
9089 where |= SMP_VAL_BE_HRQ_HDR;
9090
9091 if (!(expr->fetch->val & where)) {
9092 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9093 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9094 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9095 args[cur_arg-1], sample_src_names(expr->fetch->use));
9096 free(expr);
9097 goto out_err;
9098 }
9099
9100 if (strcmp(args[cur_arg], "table") == 0) {
9101 cur_arg++;
9102 if (!args[cur_arg]) {
9103 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9104 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9105 free(expr);
9106 goto out_err;
9107 }
9108 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009109 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02009110 cur_arg++;
9111 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009112 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009113 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009114 } else if (strcmp(args[0], "redirect") == 0) {
9115 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009116 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009117
Willy Tarreaube4653b2015-05-28 15:26:58 +02009118 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009119 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9120 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9121 goto out_err;
9122 }
9123
9124 /* this redirect rule might already contain a parsed condition which
9125 * we'll pass to the http-request rule.
9126 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009127 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009128 rule->arg.redir = redir;
9129 rule->cond = redir->cond;
9130 redir->cond = NULL;
9131 cur_arg = 2;
9132 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009133 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9134 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009135 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009136 /*
9137 * '+ 8' for 'add-acl('
9138 * '- 9' for 'add-acl(' + trailing ')'
9139 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009140 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009141
9142 cur_arg = 1;
9143
9144 if (!*args[cur_arg] ||
9145 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9146 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9147 file, linenum, args[0]);
9148 goto out_err;
9149 }
9150
9151 LIST_INIT(&rule->arg.map.key);
9152 proxy->conf.args.ctx = ARGC_HRQ;
9153 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9154 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9155 file, linenum);
9156 free(proxy->conf.lfs_file);
9157 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9158 proxy->conf.lfs_line = proxy->conf.args.line;
9159 cur_arg += 1;
9160 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9161 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009162 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009163 /*
9164 * '+ 8' for 'del-acl('
9165 * '- 9' for 'del-acl(' + trailing ')'
9166 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009167 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009168
9169 cur_arg = 1;
9170
9171 if (!*args[cur_arg] ||
9172 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9173 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9174 file, linenum, args[0]);
9175 goto out_err;
9176 }
9177
9178 LIST_INIT(&rule->arg.map.key);
9179 proxy->conf.args.ctx = ARGC_HRQ;
9180 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9181 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9182 file, linenum);
9183 free(proxy->conf.lfs_file);
9184 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9185 proxy->conf.lfs_line = proxy->conf.args.line;
9186 cur_arg += 1;
9187 } else if (strncmp(args[0], "del-map", 7) == 0) {
9188 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009189 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009190 /*
9191 * '+ 8' for 'del-map('
9192 * '- 9' for 'del-map(' + trailing ')'
9193 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009194 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009195
9196 cur_arg = 1;
9197
9198 if (!*args[cur_arg] ||
9199 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9200 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9201 file, linenum, args[0]);
9202 goto out_err;
9203 }
9204
9205 LIST_INIT(&rule->arg.map.key);
9206 proxy->conf.args.ctx = ARGC_HRQ;
9207 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9208 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9209 file, linenum);
9210 free(proxy->conf.lfs_file);
9211 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9212 proxy->conf.lfs_line = proxy->conf.args.line;
9213 cur_arg += 1;
9214 } else if (strncmp(args[0], "set-map", 7) == 0) {
9215 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009216 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009217 /*
9218 * '+ 8' for 'set-map('
9219 * '- 9' for 'set-map(' + trailing ')'
9220 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009221 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009222
9223 cur_arg = 1;
9224
9225 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9226 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9227 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9228 file, linenum, args[0]);
9229 goto out_err;
9230 }
9231
9232 LIST_INIT(&rule->arg.map.key);
9233 LIST_INIT(&rule->arg.map.value);
9234 proxy->conf.args.ctx = ARGC_HRQ;
9235
9236 /* key pattern */
9237 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9238 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9239 file, linenum);
9240
9241 /* value pattern */
9242 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9243 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9244 file, linenum);
9245 free(proxy->conf.lfs_file);
9246 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9247 proxy->conf.lfs_line = proxy->conf.args.line;
9248
9249 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009250 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9251 char *errmsg = NULL;
9252 cur_arg = 1;
9253 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009254 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009255 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009256 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009257 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9258 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9259 free(errmsg);
9260 goto out_err;
9261 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009262 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009263 action_build_list(&http_req_keywords.list, &trash);
9264 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
9265 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009266 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009267 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009268 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009269 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009270 }
9271
9272 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9273 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009274 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009275
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009276 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9277 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9278 file, linenum, args[0], errmsg);
9279 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009280 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009281 }
9282 rule->cond = cond;
9283 }
9284 else if (*args[cur_arg]) {
9285 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9286 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9287 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009288 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009289 }
9290
9291 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009292 out_err:
9293 free(rule);
9294 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009295}
9296
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009297/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009298struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009299{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009300 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009301 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009302 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009303 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009304
9305 rule = calloc(1, sizeof(*rule));
9306 if (!rule) {
9307 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9308 goto out_err;
9309 }
9310
9311 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009312 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009313 cur_arg = 1;
9314 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009315 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009316 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009317 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009318 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009319 cur_arg = 1;
9320
9321 if (!*args[cur_arg] ||
9322 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9323 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9324 file, linenum, args[0]);
9325 goto out_err;
9326 }
9327 rule->arg.nice = atoi(args[cur_arg]);
9328 if (rule->arg.nice < -1024)
9329 rule->arg.nice = -1024;
9330 else if (rule->arg.nice > 1024)
9331 rule->arg.nice = 1024;
9332 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009333 } else if (!strcmp(args[0], "set-tos")) {
9334#ifdef IP_TOS
9335 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009336 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009337 cur_arg = 1;
9338
9339 if (!*args[cur_arg] ||
9340 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9341 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9342 file, linenum, args[0]);
9343 goto out_err;
9344 }
9345
9346 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9347 if (err && *err != '\0') {
9348 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9349 file, linenum, err, args[0]);
9350 goto out_err;
9351 }
9352 cur_arg++;
9353#else
9354 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9355 goto out_err;
9356#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009357 } else if (!strcmp(args[0], "set-mark")) {
9358#ifdef SO_MARK
9359 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009360 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009361 cur_arg = 1;
9362
9363 if (!*args[cur_arg] ||
9364 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9365 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9366 file, linenum, args[0]);
9367 goto out_err;
9368 }
9369
9370 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9371 if (err && *err != '\0') {
9372 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9373 file, linenum, err, args[0]);
9374 goto out_err;
9375 }
9376 cur_arg++;
9377 global.last_checks |= LSTCHK_NETADM;
9378#else
9379 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9380 goto out_err;
9381#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009382 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009383 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009384 cur_arg = 1;
9385
9386 if (!*args[cur_arg] ||
9387 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9388 bad_log_level:
9389 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9390 file, linenum, args[0]);
9391 goto out_err;
9392 }
9393 if (strcmp(args[cur_arg], "silent") == 0)
9394 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08009395 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009396 goto bad_log_level;
9397 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009398 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009399 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009400 cur_arg = 1;
9401
9402 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9403 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9404 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9405 file, linenum, args[0]);
9406 goto out_err;
9407 }
9408
9409 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9410 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9411 LIST_INIT(&rule->arg.hdr_add.fmt);
9412
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009413 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009414 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009415 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9416 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009417 free(proxy->conf.lfs_file);
9418 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9419 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009420 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009421 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009422 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009423 cur_arg = 1;
9424
9425 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009426 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9427 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009428 file, linenum, args[0]);
9429 goto out_err;
9430 }
9431
9432 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9433 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9434 LIST_INIT(&rule->arg.hdr_add.fmt);
9435
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009436 error = NULL;
9437 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9438 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9439 args[cur_arg + 1], error);
9440 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009441 goto out_err;
9442 }
9443
9444 proxy->conf.args.ctx = ARGC_HRQ;
9445 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9446 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9447 file, linenum);
9448
9449 free(proxy->conf.lfs_file);
9450 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9451 proxy->conf.lfs_line = proxy->conf.args.line;
9452 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009453 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009454 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009455 cur_arg = 1;
9456
9457 if (!*args[cur_arg] ||
9458 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9459 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9460 file, linenum, args[0]);
9461 goto out_err;
9462 }
9463
9464 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9465 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9466
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009467 proxy->conf.args.ctx = ARGC_HRS;
9468 free(proxy->conf.lfs_file);
9469 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9470 proxy->conf.lfs_line = proxy->conf.args.line;
9471 cur_arg += 1;
9472 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9473 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009474 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009475 /*
9476 * '+ 8' for 'add-acl('
9477 * '- 9' for 'add-acl(' + trailing ')'
9478 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009479 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009480
9481 cur_arg = 1;
9482
9483 if (!*args[cur_arg] ||
9484 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9485 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9486 file, linenum, args[0]);
9487 goto out_err;
9488 }
9489
9490 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009491 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009492 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9493 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9494 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009495 free(proxy->conf.lfs_file);
9496 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9497 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009498
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009499 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009500 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9501 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009502 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009503 /*
9504 * '+ 8' for 'del-acl('
9505 * '- 9' for 'del-acl(' + trailing ')'
9506 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009507 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009508
9509 cur_arg = 1;
9510
9511 if (!*args[cur_arg] ||
9512 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9513 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9514 file, linenum, args[0]);
9515 goto out_err;
9516 }
9517
9518 LIST_INIT(&rule->arg.map.key);
9519 proxy->conf.args.ctx = ARGC_HRS;
9520 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9521 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9522 file, linenum);
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 cur_arg += 1;
9527 } else if (strncmp(args[0], "del-map", 7) == 0) {
9528 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009529 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009530 /*
9531 * '+ 8' for 'del-map('
9532 * '- 9' for 'del-map(' + trailing ')'
9533 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009534 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009535
9536 cur_arg = 1;
9537
9538 if (!*args[cur_arg] ||
9539 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9540 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9541 file, linenum, args[0]);
9542 goto out_err;
9543 }
9544
9545 LIST_INIT(&rule->arg.map.key);
9546 proxy->conf.args.ctx = ARGC_HRS;
9547 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9548 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9549 file, linenum);
9550 free(proxy->conf.lfs_file);
9551 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9552 proxy->conf.lfs_line = proxy->conf.args.line;
9553 cur_arg += 1;
9554 } else if (strncmp(args[0], "set-map", 7) == 0) {
9555 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009556 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009557 /*
9558 * '+ 8' for 'set-map('
9559 * '- 9' for 'set-map(' + trailing ')'
9560 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009561 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009562
9563 cur_arg = 1;
9564
9565 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9566 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9567 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9568 file, linenum, args[0]);
9569 goto out_err;
9570 }
9571
9572 LIST_INIT(&rule->arg.map.key);
9573 LIST_INIT(&rule->arg.map.value);
9574
9575 proxy->conf.args.ctx = ARGC_HRS;
9576
9577 /* key pattern */
9578 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9579 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9580 file, linenum);
9581
9582 /* value pattern */
9583 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9584 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9585 file, linenum);
9586
9587 free(proxy->conf.lfs_file);
9588 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9589 proxy->conf.lfs_line = proxy->conf.args.line;
9590
9591 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009592 } else if (strcmp(args[0], "redirect") == 0) {
9593 struct redirect_rule *redir;
9594 char *errmsg = NULL;
9595
9596 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9597 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9598 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9599 goto out_err;
9600 }
9601
9602 /* this redirect rule might already contain a parsed condition which
9603 * we'll pass to the http-request rule.
9604 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009605 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009606 rule->arg.redir = redir;
9607 rule->cond = redir->cond;
9608 redir->cond = NULL;
9609 cur_arg = 2;
9610 return rule;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009611 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9612 args[0][9] == '\0' && args[0][8] >= '0' &&
9613 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
9614 struct sample_expr *expr;
9615 unsigned int where;
9616 char *err = NULL;
9617
9618 cur_arg = 1;
9619 proxy->conf.args.ctx = ARGC_TRK;
9620
9621 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9622 if (!expr) {
9623 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9624 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9625 free(err);
9626 goto out_err;
9627 }
9628
9629 where = 0;
9630 if (proxy->cap & PR_CAP_FE)
9631 where |= SMP_VAL_FE_HRS_HDR;
9632 if (proxy->cap & PR_CAP_BE)
9633 where |= SMP_VAL_BE_HRS_HDR;
9634
9635 if (!(expr->fetch->val & where)) {
9636 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
9637 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9638 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9639 args[cur_arg-1], sample_src_names(expr->fetch->use));
9640 free(expr);
9641 goto out_err;
9642 }
9643
9644 if (strcmp(args[cur_arg], "table") == 0) {
9645 cur_arg++;
9646 if (!args[cur_arg]) {
9647 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9648 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9649 free(expr);
9650 goto out_err;
9651 }
9652 /* we copy the table name for now, it will be resolved later */
9653 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9654 cur_arg++;
9655 }
9656 rule->arg.trk_ctr.expr = expr;
9657 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
William Lallemand73025dd2014-04-24 14:38:37 +02009658 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9659 char *errmsg = NULL;
9660 cur_arg = 1;
9661 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009662 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009663 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009664 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009665 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9666 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9667 free(errmsg);
9668 goto out_err;
9669 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009670 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009671 action_build_list(&http_res_keywords.list, &trash);
9672 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9673 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009674 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009675 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009676 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009677 goto out_err;
9678 }
9679
9680 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9681 struct acl_cond *cond;
9682 char *errmsg = NULL;
9683
9684 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9685 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9686 file, linenum, args[0], errmsg);
9687 free(errmsg);
9688 goto out_err;
9689 }
9690 rule->cond = cond;
9691 }
9692 else if (*args[cur_arg]) {
9693 Alert("parsing [%s:%d]: 'http-response %s' expects"
9694 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9695 file, linenum, args[0], args[cur_arg]);
9696 goto out_err;
9697 }
9698
9699 return rule;
9700 out_err:
9701 free(rule);
9702 return NULL;
9703}
9704
Willy Tarreau4baae242012-12-27 12:00:31 +01009705/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009706 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009707 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9708 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009709 */
9710struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009711 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009712{
9713 struct redirect_rule *rule;
9714 int cur_arg;
9715 int type = REDIRECT_TYPE_NONE;
9716 int code = 302;
9717 const char *destination = NULL;
9718 const char *cookie = NULL;
9719 int cookie_set = 0;
9720 unsigned int flags = REDIRECT_FLAG_NONE;
9721 struct acl_cond *cond = NULL;
9722
9723 cur_arg = 0;
9724 while (*(args[cur_arg])) {
9725 if (strcmp(args[cur_arg], "location") == 0) {
9726 if (!*args[cur_arg + 1])
9727 goto missing_arg;
9728
9729 type = REDIRECT_TYPE_LOCATION;
9730 cur_arg++;
9731 destination = args[cur_arg];
9732 }
9733 else if (strcmp(args[cur_arg], "prefix") == 0) {
9734 if (!*args[cur_arg + 1])
9735 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009736 type = REDIRECT_TYPE_PREFIX;
9737 cur_arg++;
9738 destination = args[cur_arg];
9739 }
9740 else if (strcmp(args[cur_arg], "scheme") == 0) {
9741 if (!*args[cur_arg + 1])
9742 goto missing_arg;
9743
9744 type = REDIRECT_TYPE_SCHEME;
9745 cur_arg++;
9746 destination = args[cur_arg];
9747 }
9748 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9749 if (!*args[cur_arg + 1])
9750 goto missing_arg;
9751
9752 cur_arg++;
9753 cookie = args[cur_arg];
9754 cookie_set = 1;
9755 }
9756 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9757 if (!*args[cur_arg + 1])
9758 goto missing_arg;
9759
9760 cur_arg++;
9761 cookie = args[cur_arg];
9762 cookie_set = 0;
9763 }
9764 else if (strcmp(args[cur_arg], "code") == 0) {
9765 if (!*args[cur_arg + 1])
9766 goto missing_arg;
9767
9768 cur_arg++;
9769 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009770 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009771 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009772 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009773 args[cur_arg - 1], args[cur_arg]);
9774 return NULL;
9775 }
9776 }
9777 else if (!strcmp(args[cur_arg],"drop-query")) {
9778 flags |= REDIRECT_FLAG_DROP_QS;
9779 }
9780 else if (!strcmp(args[cur_arg],"append-slash")) {
9781 flags |= REDIRECT_FLAG_APPEND_SLASH;
9782 }
9783 else if (strcmp(args[cur_arg], "if") == 0 ||
9784 strcmp(args[cur_arg], "unless") == 0) {
9785 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9786 if (!cond) {
9787 memprintf(errmsg, "error in condition: %s", *errmsg);
9788 return NULL;
9789 }
9790 break;
9791 }
9792 else {
9793 memprintf(errmsg,
9794 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9795 args[cur_arg]);
9796 return NULL;
9797 }
9798 cur_arg++;
9799 }
9800
9801 if (type == REDIRECT_TYPE_NONE) {
9802 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9803 return NULL;
9804 }
9805
Willy Tarreaube4653b2015-05-28 15:26:58 +02009806 if (dir && type != REDIRECT_TYPE_LOCATION) {
9807 memprintf(errmsg, "response only supports redirect type 'location'");
9808 return NULL;
9809 }
9810
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02009811 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +01009812 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009813 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009814
9815 if (!use_fmt) {
9816 /* old-style static redirect rule */
9817 rule->rdr_str = strdup(destination);
9818 rule->rdr_len = strlen(destination);
9819 }
9820 else {
9821 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009822
9823 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9824 * if prefix == "/", we don't want to add anything, otherwise it
9825 * makes it hard for the user to configure a self-redirection.
9826 */
Godbachd9722032014-12-18 15:44:58 +08009827 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009828 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009829 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009830 dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR
9831 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009832 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009833 free(curproxy->conf.lfs_file);
9834 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9835 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009836 }
9837 }
9838
Willy Tarreau4baae242012-12-27 12:00:31 +01009839 if (cookie) {
9840 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9841 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9842 */
9843 rule->cookie_len = strlen(cookie);
9844 if (cookie_set) {
9845 rule->cookie_str = malloc(rule->cookie_len + 10);
9846 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9847 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9848 rule->cookie_len += 9;
9849 } else {
9850 rule->cookie_str = malloc(rule->cookie_len + 21);
9851 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9852 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9853 rule->cookie_len += 20;
9854 }
9855 }
9856 rule->type = type;
9857 rule->code = code;
9858 rule->flags = flags;
9859 LIST_INIT(&rule->list);
9860 return rule;
9861
9862 missing_arg:
9863 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9864 return NULL;
9865}
9866
Willy Tarreau8797c062007-05-07 00:55:35 +02009867/************************************************************************/
9868/* The code below is dedicated to ACL parsing and matching */
9869/************************************************************************/
9870
9871
Willy Tarreau14174bc2012-04-16 14:34:04 +02009872/* This function ensures that the prerequisites for an L7 fetch are ready,
9873 * which means that a request or response is ready. If some data is missing,
9874 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009875 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9876 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009877 *
9878 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009879 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9880 * decide whether or not an HTTP message is present ;
9881 * 0 if the requested data cannot be fetched or if it is certain that
9882 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009883 * 1 if an HTTP message is ready
9884 */
James Rosewell91a41cb2015-09-18 17:11:16 +01009885int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009886 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009887{
Willy Tarreau192252e2015-04-04 01:47:55 +02009888 struct http_txn *txn;
9889 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009890
Willy Tarreaube508f12016-03-10 11:47:01 +01009891 /* Note: it is possible that <s> is NULL when called before stream
9892 * initialization (eg: tcp-request connection), so this function is the
9893 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009894 */
Willy Tarreau192252e2015-04-04 01:47:55 +02009895 if (!s)
9896 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01009897
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +02009898 if (!s->txn) {
9899 if (unlikely(!http_alloc_txn(s)))
9900 return 0; /* not enough memory */
9901 http_init_txn(s);
9902 }
Willy Tarreau192252e2015-04-04 01:47:55 +02009903 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +02009904 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009905
9906 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009907 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009908
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009909 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009910 /* If the buffer does not leave enough free space at the end,
9911 * we must first realign it.
9912 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009913 if (s->req.buf->p > s->req.buf->data &&
9914 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
9915 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009916
Willy Tarreau14174bc2012-04-16 14:34:04 +02009917 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009918 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009919 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009920
9921 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009922 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009923 http_msg_analyzer(msg, &txn->hdr_idx);
9924
9925 /* Still no valid request ? */
9926 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009927 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009928 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009929 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009930 }
9931 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009932 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009933 return 0;
9934 }
9935
9936 /* OK we just got a valid HTTP request. We have some minor
9937 * preparation to perform so that further checks can rely
9938 * on HTTP tests.
9939 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009940
9941 /* If the request was parsed but was too large, we must absolutely
9942 * return an error so that it is not processed. At the moment this
9943 * cannot happen, but if the parsers are to change in the future,
9944 * we want this check to be maintained.
9945 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009946 if (unlikely(s->req.buf->i + s->req.buf->p >
9947 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009948 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009949 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009950 return 1;
9951 }
9952
Willy Tarreau9b28e032012-10-12 23:49:43 +02009953 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009954 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02009955 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009956
Willy Tarreau506d0502013-07-06 13:29:24 +02009957 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9958 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009959 }
9960
Willy Tarreau506d0502013-07-06 13:29:24 +02009961 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009962 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009963 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009964
9965 /* otherwise everything's ready for the request */
9966 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009967 else {
9968 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009969 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9970 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009971 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009972 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009973 }
9974
9975 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009976 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009977 return 1;
9978}
Willy Tarreau8797c062007-05-07 00:55:35 +02009979
Willy Tarreau8797c062007-05-07 00:55:35 +02009980/* 1. Check on METHOD
9981 * We use the pre-parsed method if it is known, and store its number as an
9982 * integer. If it is unknown, we use the pointer and the length.
9983 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009984static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009985{
9986 int len, meth;
9987
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009988 len = strlen(text);
9989 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009990
9991 pattern->val.i = meth;
9992 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +02009993 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009994 pattern->len = len;
9995 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009996 else {
9997 pattern->ptr.str = NULL;
9998 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009999 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010000 return 1;
10001}
10002
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010003/* This function fetches the method of current HTTP request and stores
10004 * it in the global pattern struct as a chunk. There are two possibilities :
10005 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10006 * in <len> and <ptr> is NULL ;
10007 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10008 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010009 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010010 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010011static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010012smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010013{
10014 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +010010015 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010016
Willy Tarreau24e32d82012-04-23 23:55:44 +020010017 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010018
Willy Tarreaube508f12016-03-10 11:47:01 +010010019 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010020 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010021 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010022 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010023 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010024 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10025 /* ensure the indexes are not affected */
10026 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010027 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010028 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
10029 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010030 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010031 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010032 return 1;
10033}
10034
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010035/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010036static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010037{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010038 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010039 struct pattern_list *lst;
10040 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010041
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010042 list_for_each_entry(lst, &expr->patterns, list) {
10043 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010044
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010045 /* well-known method */
10046 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010047 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010048 return pattern;
10049 else
10050 continue;
10051 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010052
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010053 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010054 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010055 continue;
10056
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010057 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010058 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
10059 (!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 +010010060 return pattern;
10061 }
10062 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010063}
10064
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010065static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010066smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010067{
Willy Tarreaube508f12016-03-10 11:47:01 +010010068 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010069 char *ptr;
10070 int len;
10071
Willy Tarreauc0239e02012-04-16 14:42:55 +020010072 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010073
Willy Tarreaube508f12016-03-10 11:47:01 +010010074 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010075 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010076 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010077
10078 while ((len-- > 0) && (*ptr++ != '/'));
10079 if (len <= 0)
10080 return 0;
10081
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010082 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010083 smp->data.u.str.str = ptr;
10084 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010085
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010086 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010087 return 1;
10088}
10089
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010090static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010091smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010092{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010093 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010094 char *ptr;
10095 int len;
10096
Willy Tarreauc0239e02012-04-16 14:42:55 +020010097 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010098
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010099 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010100 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10101 return 0;
10102
Willy Tarreau8797c062007-05-07 00:55:35 +020010103 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010104 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010105
10106 while ((len-- > 0) && (*ptr++ != '/'));
10107 if (len <= 0)
10108 return 0;
10109
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010110 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010111 smp->data.u.str.str = ptr;
10112 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010113
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010114 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010115 return 1;
10116}
10117
10118/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010119static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010120smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010121{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010122 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010123 char *ptr;
10124 int len;
10125
Willy Tarreauc0239e02012-04-16 14:42:55 +020010126 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010127
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010128 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010129 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10130 return 0;
10131
Willy Tarreau8797c062007-05-07 00:55:35 +020010132 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010133 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010134
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010135 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010136 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010137 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010138 return 1;
10139}
10140
Thierry Fournierf4011dd2016-03-29 17:23:51 +020010141static int
10142smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
10143{
10144 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
10145 return 0;
10146
10147 if (!smp->strm->unique_id) {
10148 if ((smp->strm->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
10149 return 0;
10150 smp->strm->unique_id[0] = '\0';
10151 }
10152 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
10153 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
10154
10155 smp->data.type = SMP_T_STR;
10156 smp->data.u.str.str = smp->strm->unique_id;
10157 smp->flags = SMP_F_CONST;
10158 return 1;
10159}
10160
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010161/* returns the longest available part of the body. This requires that the body
10162 * has been waited for using http-buffer-request.
10163 */
10164static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010165smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010166{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010167 struct http_msg *msg;
10168 unsigned long len;
10169 unsigned long block1;
10170 char *body;
10171 struct chunk *temp;
10172
10173 CHECK_HTTP_MESSAGE_FIRST();
10174
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010175 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010176 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010177 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010178 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010179
10180 len = http_body_bytes(msg);
10181 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10182
10183 block1 = len;
10184 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10185 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10186
10187 if (block1 == len) {
10188 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010189 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010190 smp->data.u.str.str = body;
10191 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010192 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10193 }
10194 else {
10195 /* buffer is wrapped, we need to defragment it */
10196 temp = get_trash_chunk();
10197 memcpy(temp->str, body, block1);
10198 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010199 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010200 smp->data.u.str.str = temp->str;
10201 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010202 smp->flags = SMP_F_VOL_TEST;
10203 }
10204 return 1;
10205}
10206
10207
10208/* returns the available length of the body. This requires that the body
10209 * has been waited for using http-buffer-request.
10210 */
10211static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010212smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010213{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010214 struct http_msg *msg;
10215
10216 CHECK_HTTP_MESSAGE_FIRST();
10217
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010218 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010219 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010220 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010221 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010222
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010223 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010224 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010225
10226 smp->flags = SMP_F_VOL_TEST;
10227 return 1;
10228}
10229
10230
10231/* returns the advertised length of the body, or the advertised size of the
10232 * chunks available in the buffer. This requires that the body has been waited
10233 * for using http-buffer-request.
10234 */
10235static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010236smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010237{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010238 struct http_msg *msg;
10239
10240 CHECK_HTTP_MESSAGE_FIRST();
10241
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010242 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010243 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010244 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010245 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010246
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010247 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010248 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010249
10250 smp->flags = SMP_F_VOL_TEST;
10251 return 1;
10252}
10253
10254
Willy Tarreau8797c062007-05-07 00:55:35 +020010255/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010256static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010257smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010258{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010259 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010260
Willy Tarreauc0239e02012-04-16 14:42:55 +020010261 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010262
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010263 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010264 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010265 smp->data.u.str.len = txn->req.sl.rq.u_l;
10266 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010267 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010268 return 1;
10269}
10270
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010271static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010272smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010273{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010274 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010275 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010276
Willy Tarreauc0239e02012-04-16 14:42:55 +020010277 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010278
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010279 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010280 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 +020010281 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010282 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010283
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010284 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010285 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010286 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010287 return 1;
10288}
10289
10290static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010291smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010292{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010293 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010294 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010295
Willy Tarreauc0239e02012-04-16 14:42:55 +020010296 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010297
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010298 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010299 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 +020010300 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10301 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010302
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010303 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010304 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010305 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010306 return 1;
10307}
10308
Willy Tarreau185b5c42012-04-26 15:11:51 +020010309/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10310 * Accepts an optional argument of type string containing the header field name,
10311 * and an optional argument of type signed or unsigned integer to request an
10312 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010313 * headers are considered from the first one. It does not stop on commas and
10314 * returns full lines instead (useful for User-Agent or Date for example).
10315 */
10316static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010317smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010318{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010319 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010320 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010321 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010322 int occ = 0;
10323 const char *name_str = NULL;
10324 int name_len = 0;
10325
10326 if (!ctx) {
10327 /* first call */
10328 ctx = &static_hdr_ctx;
10329 ctx->idx = 0;
10330 smp->ctx.a[0] = ctx;
10331 }
10332
10333 if (args) {
10334 if (args[0].type != ARGT_STR)
10335 return 0;
10336 name_str = args[0].data.str.str;
10337 name_len = args[0].data.str.len;
10338
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010339 if (args[1].type == ARGT_SINT)
10340 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010341 }
10342
10343 CHECK_HTTP_MESSAGE_FIRST();
10344
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010345 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010346 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 +020010347
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010348 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10349 /* search for header from the beginning */
10350 ctx->idx = 0;
10351
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010352 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010353 /* no explicit occurrence and single fetch => last header by default */
10354 occ = -1;
10355
10356 if (!occ)
10357 /* prepare to report multiple occurrences for ACL fetches */
10358 smp->flags |= SMP_F_NOT_LAST;
10359
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010360 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010361 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010362 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 +020010363 return 1;
10364
10365 smp->flags &= ~SMP_F_NOT_LAST;
10366 return 0;
10367}
10368
10369/* 6. Check on HTTP header count. The number of occurrences is returned.
10370 * Accepts exactly 1 argument of type string. It does not stop on commas and
10371 * returns full lines instead (useful for User-Agent or Date for example).
10372 */
10373static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010374smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010375{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010376 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010377 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010378 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010379 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010380 const char *name = NULL;
10381 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010382
Willy Tarreau601a4d12015-04-01 19:16:09 +020010383 if (args && args->type == ARGT_STR) {
10384 name = args->data.str.str;
10385 len = args->data.str.len;
10386 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010387
10388 CHECK_HTTP_MESSAGE_FIRST();
10389
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010390 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010391 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 +020010392
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010393 ctx.idx = 0;
10394 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010395 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010396 cnt++;
10397
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010398 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010399 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010400 smp->flags = SMP_F_VOL_HDR;
10401 return 1;
10402}
10403
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010404static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010405smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010406{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010407 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010408 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010409 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010410 struct chunk *temp;
10411 char del = ',';
10412
10413 if (args && args->type == ARGT_STR)
10414 del = *args[0].data.str.str;
10415
10416 CHECK_HTTP_MESSAGE_FIRST();
10417
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010418 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010419 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 +020010420
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010421 temp = get_trash_chunk();
10422
10423 ctx.idx = 0;
10424 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10425 if (temp->len)
10426 temp->str[temp->len++] = del;
10427 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10428 temp->len += ctx.del;
10429 }
10430
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010431 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010432 smp->data.u.str.str = temp->str;
10433 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010434 smp->flags = SMP_F_VOL_HDR;
10435 return 1;
10436}
10437
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010438/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10439 * Accepts an optional argument of type string containing the header field name,
10440 * and an optional argument of type signed or unsigned integer to request an
10441 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010442 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010443 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010444static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010445smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010446{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010447 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010448 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010449 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010450 int occ = 0;
10451 const char *name_str = NULL;
10452 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010453
Willy Tarreaua890d072013-04-02 12:01:06 +020010454 if (!ctx) {
10455 /* first call */
10456 ctx = &static_hdr_ctx;
10457 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010458 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010459 }
10460
Willy Tarreau185b5c42012-04-26 15:11:51 +020010461 if (args) {
10462 if (args[0].type != ARGT_STR)
10463 return 0;
10464 name_str = args[0].data.str.str;
10465 name_len = args[0].data.str.len;
10466
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010467 if (args[1].type == ARGT_SINT)
10468 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010469 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010470
Willy Tarreaue333ec92012-04-16 16:26:40 +020010471 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010472
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010473 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010474 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 +020010475
Willy Tarreau185b5c42012-04-26 15:11:51 +020010476 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010477 /* search for header from the beginning */
10478 ctx->idx = 0;
10479
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010480 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010481 /* no explicit occurrence and single fetch => last header by default */
10482 occ = -1;
10483
10484 if (!occ)
10485 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010486 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010487
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010488 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010489 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010490 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 +020010491 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010492
Willy Tarreau37406352012-04-23 16:16:37 +020010493 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010494 return 0;
10495}
10496
Willy Tarreauc11416f2007-06-17 16:58:38 +020010497/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010498 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010499 */
10500static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010501smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010502{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010503 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010504 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010505 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010506 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010507 const char *name = NULL;
10508 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010509
Willy Tarreau601a4d12015-04-01 19:16:09 +020010510 if (args && args->type == ARGT_STR) {
10511 name = args->data.str.str;
10512 len = args->data.str.len;
10513 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010514
Willy Tarreaue333ec92012-04-16 16:26:40 +020010515 CHECK_HTTP_MESSAGE_FIRST();
10516
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010517 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010518 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 +020010519
Willy Tarreau33a7e692007-06-10 19:45:56 +020010520 ctx.idx = 0;
10521 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010522 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010523 cnt++;
10524
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010525 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010526 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010527 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010528 return 1;
10529}
10530
Willy Tarreau185b5c42012-04-26 15:11:51 +020010531/* Fetch an HTTP header's integer value. The integer value is returned. It
10532 * takes a mandatory argument of type string and an optional one of type int
10533 * to designate a specific occurrence. It returns an unsigned integer, which
10534 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010535 */
10536static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010537smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010538{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010539 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010540
Willy Tarreauf853c462012-04-23 18:53:56 +020010541 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010542 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010543 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010544 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010545
Willy Tarreaud53e2422012-04-16 17:21:11 +020010546 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010547}
10548
Cyril Bonté69fa9922012-10-25 00:01:06 +020010549/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10550 * and an optional one of type int to designate a specific occurrence.
10551 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010552 */
10553static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010554smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010555{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010556 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010557
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010558 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010559 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010560 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010561 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010562 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010563 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010564 if (smp->data.u.str.len < temp->size - 1) {
10565 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10566 temp->str[smp->data.u.str.len] = '\0';
10567 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010568 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010569 break;
10570 }
10571 }
10572 }
10573
Willy Tarreaud53e2422012-04-16 17:21:11 +020010574 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010575 if (!(smp->flags & SMP_F_NOT_LAST))
10576 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010577 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010578 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010579}
10580
Willy Tarreau737b0c12007-06-10 21:28:46 +020010581/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10582 * the first '/' after the possible hostname, and ends before the possible '?'.
10583 */
10584static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010585smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010586{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010587 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010588 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010589
Willy Tarreauc0239e02012-04-16 14:42:55 +020010590 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010591
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010592 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010593 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010594 ptr = http_get_path(txn);
10595 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010596 return 0;
10597
10598 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010599 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010600 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010601
10602 while (ptr < end && *ptr != '?')
10603 ptr++;
10604
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010605 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010606 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010607 return 1;
10608}
10609
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010610/* This produces a concatenation of the first occurrence of the Host header
10611 * followed by the path component if it begins with a slash ('/'). This means
10612 * that '*' will not be added, resulting in exactly the first Host entry.
10613 * If no Host header is found, then the path is returned as-is. The returned
10614 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010615 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010616 */
10617static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010618smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010619{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010620 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010621 char *ptr, *end, *beg;
10622 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010623 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010624
10625 CHECK_HTTP_MESSAGE_FIRST();
10626
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010627 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010628 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010629 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010630 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010631
10632 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010633 temp = get_trash_chunk();
10634 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010635 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010636 smp->data.u.str.str = temp->str;
10637 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010638
10639 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010640 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010641 beg = http_get_path(txn);
10642 if (!beg)
10643 beg = end;
10644
10645 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10646
10647 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010648 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10649 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010650 }
10651
10652 smp->flags = SMP_F_VOL_1ST;
10653 return 1;
10654}
10655
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010656/* This produces a 32-bit hash of the concatenation of the first occurrence of
10657 * the Host header followed by the path component if it begins with a slash ('/').
10658 * This means that '*' will not be added, resulting in exactly the first Host
10659 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010660 * is hashed using the path hash followed by a full avalanche hash and provides a
10661 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010662 * high-traffic sites without having to store whole paths.
10663 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010664int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010665smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010666{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010667 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010668 struct hdr_ctx ctx;
10669 unsigned int hash = 0;
10670 char *ptr, *beg, *end;
10671 int len;
10672
10673 CHECK_HTTP_MESSAGE_FIRST();
10674
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010675 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010676 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010677 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010678 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10679 ptr = ctx.line + ctx.val;
10680 len = ctx.vlen;
10681 while (len--)
10682 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10683 }
10684
10685 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010686 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010687 beg = http_get_path(txn);
10688 if (!beg)
10689 beg = end;
10690
10691 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10692
10693 if (beg < ptr && *beg == '/') {
10694 while (beg < ptr)
10695 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10696 }
10697 hash = full_hash(hash);
10698
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010699 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010700 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010701 smp->flags = SMP_F_VOL_1ST;
10702 return 1;
10703}
10704
Willy Tarreau4a550602012-12-09 14:53:32 +010010705/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010706 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10707 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10708 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010709 * that in environments where IPv6 is insignificant, truncating the output to
10710 * 8 bytes would still work.
10711 */
10712static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010713smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010714{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010715 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010716 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010717
10718 if (!cli_conn)
10719 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010720
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010721 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010722 return 0;
10723
Willy Tarreau47ca5452012-12-23 20:22:19 +010010724 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010725 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010726 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010727
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010728 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010729 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010730 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010731 temp->len += 4;
10732 break;
10733 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010734 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010735 temp->len += 16;
10736 break;
10737 default:
10738 return 0;
10739 }
10740
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010741 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010742 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010743 return 1;
10744}
10745
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010746/* Extracts the query string, which comes after the question mark '?'. If no
10747 * question mark is found, nothing is returned. Otherwise it returns a sample
10748 * of type string carrying the whole query string.
10749 */
10750static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010751smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010752{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010753 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010754 char *ptr, *end;
10755
10756 CHECK_HTTP_MESSAGE_FIRST();
10757
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010758 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010759 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10760 end = ptr + txn->req.sl.rq.u_l;
10761
10762 /* look up the '?' */
10763 do {
10764 if (ptr == end)
10765 return 0;
10766 } while (*ptr++ != '?');
10767
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010768 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010769 smp->data.u.str.str = ptr;
10770 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010771 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10772 return 1;
10773}
10774
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010775static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010776smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010777{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010778 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10779 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10780 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010781
Willy Tarreau24e32d82012-04-23 23:55:44 +020010782 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010783
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010784 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010785 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010786 return 1;
10787}
10788
Willy Tarreau7f18e522010-10-22 20:04:13 +020010789/* return a valid test if the current request is the first one on the connection */
10790static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010791smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010792{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010793 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010794 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010795 return 1;
10796}
10797
Willy Tarreau34db1082012-04-19 17:16:54 +020010798/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010799static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010800smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010801{
10802
Willy Tarreau24e32d82012-04-23 23:55:44 +020010803 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010804 return 0;
10805
Willy Tarreauc0239e02012-04-16 14:42:55 +020010806 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010807
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010808 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010809 return 0;
10810
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010811 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010812 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010813 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010814 return 1;
10815}
Willy Tarreau8797c062007-05-07 00:55:35 +020010816
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010817/* Accepts exactly 1 argument of type userlist */
10818static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010819smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010820{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010821 if (!args || args->type != ARGT_USR)
10822 return 0;
10823
10824 CHECK_HTTP_MESSAGE_FIRST();
10825
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010826 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010827 return 0;
10828
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010829 /* if the user does not belong to the userlist or has a wrong password,
10830 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010831 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010832 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010833 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10834 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010835 return 0;
10836
10837 /* pat_match_auth() will need the user list */
10838 smp->ctx.a[0] = args->data.usr;
10839
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010840 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010841 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010842 smp->data.u.str.str = smp->strm->txn->auth.user;
10843 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010844
10845 return 1;
10846}
10847
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010848/* Try to find the next occurrence of a cookie name in a cookie header value.
10849 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10850 * the cookie value is returned into *value and *value_l, and the function
10851 * returns a pointer to the next pointer to search from if the value was found.
10852 * Otherwise if the cookie was not found, NULL is returned and neither value
10853 * nor value_l are touched. The input <hdr> string should first point to the
10854 * header's value, and the <hdr_end> pointer must point to the first character
10855 * not part of the value. <list> must be non-zero if value may represent a list
10856 * of values (cookie headers). This makes it faster to abort parsing when no
10857 * list is expected.
10858 */
David Carlier4686f792015-09-25 14:10:50 +010010859char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010860extract_cookie_value(char *hdr, const char *hdr_end,
10861 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010862 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010863{
10864 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10865 char *next;
10866
10867 /* we search at least a cookie name followed by an equal, and more
10868 * generally something like this :
10869 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10870 */
10871 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10872 /* Iterate through all cookies on this line */
10873
10874 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10875 att_beg++;
10876
10877 /* find att_end : this is the first character after the last non
10878 * space before the equal. It may be equal to hdr_end.
10879 */
10880 equal = att_end = att_beg;
10881
10882 while (equal < hdr_end) {
10883 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10884 break;
10885 if (http_is_spht[(unsigned char)*equal++])
10886 continue;
10887 att_end = equal;
10888 }
10889
10890 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10891 * is between <att_beg> and <equal>, both may be identical.
10892 */
10893
10894 /* look for end of cookie if there is an equal sign */
10895 if (equal < hdr_end && *equal == '=') {
10896 /* look for the beginning of the value */
10897 val_beg = equal + 1;
10898 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10899 val_beg++;
10900
10901 /* find the end of the value, respecting quotes */
10902 next = find_cookie_value_end(val_beg, hdr_end);
10903
10904 /* make val_end point to the first white space or delimitor after the value */
10905 val_end = next;
10906 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10907 val_end--;
10908 } else {
10909 val_beg = val_end = next = equal;
10910 }
10911
10912 /* We have nothing to do with attributes beginning with '$'. However,
10913 * they will automatically be removed if a header before them is removed,
10914 * since they're supposed to be linked together.
10915 */
10916 if (*att_beg == '$')
10917 continue;
10918
10919 /* Ignore cookies with no equal sign */
10920 if (equal == next)
10921 continue;
10922
10923 /* Now we have the cookie name between att_beg and att_end, and
10924 * its value between val_beg and val_end.
10925 */
10926
10927 if (att_end - att_beg == cookie_name_l &&
10928 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10929 /* let's return this value and indicate where to go on from */
10930 *value = val_beg;
10931 *value_l = val_end - val_beg;
10932 return next + 1;
10933 }
10934
10935 /* Set-Cookie headers only have the name in the first attr=value part */
10936 if (!list)
10937 break;
10938 }
10939
10940 return NULL;
10941}
10942
William Lallemanda43ba4e2014-01-28 18:14:25 +010010943/* Fetch a captured HTTP request header. The index is the position of
10944 * the "capture" option in the configuration file
10945 */
10946static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010947smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010948{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010949 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010950 int idx;
10951
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010952 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010953 return 0;
10954
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010955 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010956
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010957 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 +010010958 return 0;
10959
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010960 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010961 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010962 smp->data.u.str.str = smp->strm->req_cap[idx];
10963 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010964
10965 return 1;
10966}
10967
10968/* Fetch a captured HTTP response header. The index is the position of
10969 * the "capture" option in the configuration file
10970 */
10971static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010972smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010973{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010974 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010975 int idx;
10976
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010977 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010978 return 0;
10979
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010980 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010981
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010982 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 +010010983 return 0;
10984
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010985 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010986 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010987 smp->data.u.str.str = smp->strm->res_cap[idx];
10988 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010989
10990 return 1;
10991}
10992
William Lallemand65ad6e12014-01-31 15:08:02 +010010993/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10994static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010995smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010996{
10997 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010998 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010010999 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011000
Willy Tarreau15e91e12015-04-04 00:52:09 +020011001 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011002 return 0;
11003
William Lallemand96a77852014-02-05 00:30:02 +010011004 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011005
William Lallemand96a77852014-02-05 00:30:02 +010011006 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11007 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011008
William Lallemand96a77852014-02-05 00:30:02 +010011009 temp = get_trash_chunk();
11010 temp->str = txn->uri;
11011 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011012 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011013 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011014 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011015
11016 return 1;
11017
11018}
11019
11020/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11021static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011022smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011023{
11024 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011025 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011026 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011027
Willy Tarreau15e91e12015-04-04 00:52:09 +020011028 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011029 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011030
William Lallemand65ad6e12014-01-31 15:08:02 +010011031 ptr = txn->uri;
11032
11033 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11034 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011035
William Lallemand65ad6e12014-01-31 15:08:02 +010011036 if (!*ptr)
11037 return 0;
11038
11039 ptr++; /* skip the space */
11040
11041 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011042 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011043 if (!ptr)
11044 return 0;
11045 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11046 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011047
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011048 smp->data.u.str = *temp;
11049 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011050 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011051 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011052
11053 return 1;
11054}
11055
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011056/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11057 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11058 */
11059static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011060smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011061{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011062 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011063
Willy Tarreau15e91e12015-04-04 00:52:09 +020011064 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011065 return 0;
11066
11067 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011068 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011069 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011070 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011071
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011072 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011073 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011074 smp->flags = SMP_F_CONST;
11075 return 1;
11076
11077}
11078
11079/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11080 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11081 */
11082static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011083smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011084{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011085 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011086
Willy Tarreau15e91e12015-04-04 00:52:09 +020011087 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011088 return 0;
11089
11090 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011091 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011092 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011093 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011094
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011095 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011096 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011097 smp->flags = SMP_F_CONST;
11098 return 1;
11099
11100}
11101
William Lallemand65ad6e12014-01-31 15:08:02 +010011102
Willy Tarreaue333ec92012-04-16 16:26:40 +020011103/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011104 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011105 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011106 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011107 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011108 * Accepts exactly 1 argument of type string. If the input options indicate
11109 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011110 * The returned sample is of type CSTR. Can be used to parse cookies in other
11111 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011112 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011113int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011114{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011115 struct http_txn *txn;
11116 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011117 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011118 const struct http_msg *msg;
11119 const char *hdr_name;
11120 int hdr_name_len;
11121 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011122 int occ = 0;
11123 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011124
Willy Tarreau24e32d82012-04-23 23:55:44 +020011125 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011126 return 0;
11127
Willy Tarreaua890d072013-04-02 12:01:06 +020011128 if (!ctx) {
11129 /* first call */
11130 ctx = &static_hdr_ctx;
11131 ctx->idx = 0;
11132 smp->ctx.a[2] = ctx;
11133 }
11134
Willy Tarreaue333ec92012-04-16 16:26:40 +020011135 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011136
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011137 txn = smp->strm->txn;
11138 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011139
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011140 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011141 msg = &txn->req;
11142 hdr_name = "Cookie";
11143 hdr_name_len = 6;
11144 } else {
11145 msg = &txn->rsp;
11146 hdr_name = "Set-Cookie";
11147 hdr_name_len = 10;
11148 }
11149
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011150 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011151 /* no explicit occurrence and single fetch => last cookie by default */
11152 occ = -1;
11153
11154 /* OK so basically here, either we want only one value and it's the
11155 * last one, or we want to iterate over all of them and we fetch the
11156 * next one.
11157 */
11158
Willy Tarreau9b28e032012-10-12 23:49:43 +020011159 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011160 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011161 /* search for the header from the beginning, we must first initialize
11162 * the search parameters.
11163 */
Willy Tarreau37406352012-04-23 16:16:37 +020011164 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011165 ctx->idx = 0;
11166 }
11167
Willy Tarreau28376d62012-04-26 21:26:10 +020011168 smp->flags |= SMP_F_VOL_HDR;
11169
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011170 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011171 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11172 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011173 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11174 goto out;
11175
Willy Tarreau24e32d82012-04-23 23:55:44 +020011176 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011177 continue;
11178
Willy Tarreau37406352012-04-23 16:16:37 +020011179 smp->ctx.a[0] = ctx->line + ctx->val;
11180 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011181 }
11182
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011183 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011184 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011185 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011186 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011187 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011188 &smp->data.u.str.str,
11189 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011190 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011191 found = 1;
11192 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011193 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011194 smp->flags |= SMP_F_NOT_LAST;
11195 return 1;
11196 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011197 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011198 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011199 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011200 /* all cookie headers and values were scanned. If we're looking for the
11201 * last occurrence, we may return it now.
11202 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011203 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011204 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011205 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011206}
11207
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011208/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011209 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011210 * multiple cookies may be parsed on the same line. The returned sample is of
11211 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011212 */
11213static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011214smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011215{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011216 struct http_txn *txn;
11217 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011218 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011219 const struct http_msg *msg;
11220 const char *hdr_name;
11221 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011222 int cnt;
11223 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011224 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011225
Willy Tarreau24e32d82012-04-23 23:55:44 +020011226 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011227 return 0;
11228
Willy Tarreaue333ec92012-04-16 16:26:40 +020011229 CHECK_HTTP_MESSAGE_FIRST();
11230
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011231 txn = smp->strm->txn;
11232 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011233
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011234 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011235 msg = &txn->req;
11236 hdr_name = "Cookie";
11237 hdr_name_len = 6;
11238 } else {
11239 msg = &txn->rsp;
11240 hdr_name = "Set-Cookie";
11241 hdr_name_len = 10;
11242 }
11243
Willy Tarreau9b28e032012-10-12 23:49:43 +020011244 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011245 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011246 ctx.idx = 0;
11247 cnt = 0;
11248
11249 while (1) {
11250 /* Note: val_beg == NULL every time we need to fetch a new header */
11251 if (!val_beg) {
11252 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11253 break;
11254
Willy Tarreau24e32d82012-04-23 23:55:44 +020011255 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011256 continue;
11257
11258 val_beg = ctx.line + ctx.val;
11259 val_end = val_beg + ctx.vlen;
11260 }
11261
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011262 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011263 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011264 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011265 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011266 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011267 &smp->data.u.str.str,
11268 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011269 cnt++;
11270 }
11271 }
11272
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011273 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011274 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011275 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011276 return 1;
11277}
11278
Willy Tarreau51539362012-05-08 12:46:28 +020011279/* Fetch an cookie's integer value. The integer value is returned. It
11280 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11281 */
11282static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011283smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011284{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011285 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011286
11287 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011288 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011289 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011290 }
11291
11292 return ret;
11293}
11294
Willy Tarreau8797c062007-05-07 00:55:35 +020011295/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011296/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011297/************************************************************************/
11298
David Cournapeau16023ee2010-12-23 20:55:41 +090011299/*
11300 * Given a path string and its length, find the position of beginning of the
11301 * query string. Returns NULL if no query string is found in the path.
11302 *
11303 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11304 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011305 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011306 */
bedis4c75cca2012-10-05 08:38:24 +020011307static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011308{
11309 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011310
bedis4c75cca2012-10-05 08:38:24 +020011311 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011312 return p ? p + 1 : NULL;
11313}
11314
bedis4c75cca2012-10-05 08:38:24 +020011315static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011316{
bedis4c75cca2012-10-05 08:38:24 +020011317 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011318}
11319
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011320/* after increasing a pointer value, it can exceed the first buffer
11321 * size. This function transform the value of <ptr> according with
11322 * the expected position. <chunks> is an array of the one or two
11323 * avalaible chunks. The first value is the start of the first chunk,
11324 * the second value if the end+1 of the first chunks. The third value
11325 * is NULL or the start of the second chunk and the fourth value is
11326 * the end+1 of the second chunk. The function returns 1 if does a
11327 * wrap, else returns 0.
11328 */
11329static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11330{
11331 if (*ptr < chunks[1])
11332 return 0;
11333 if (!chunks[2])
11334 return 0;
11335 *ptr = chunks[2] + ( *ptr - chunks[1] );
11336 return 1;
11337}
11338
David Cournapeau16023ee2010-12-23 20:55:41 +090011339/*
11340 * Given a url parameter, find the starting position of the first occurence,
11341 * or NULL if the parameter is not found.
11342 *
11343 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11344 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011345 *
Willy Tarreauf6625822015-12-27 14:51:01 +010011346 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011347 * or the second chunk. The caller must be check the position before using the
11348 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011349 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011350static const char *
11351find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011352 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011353 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011354{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011355 const char *pos, *last, *equal;
11356 const char **bufs = chunks;
11357 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011358
David Cournapeau16023ee2010-12-23 20:55:41 +090011359
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011360 pos = bufs[0];
11361 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010011362 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011363 /* Check the equal. */
11364 equal = pos + url_param_name_l;
11365 if (fix_pointer_if_wrap(chunks, &equal)) {
11366 if (equal >= chunks[3])
11367 return NULL;
11368 } else {
11369 if (equal >= chunks[1])
11370 return NULL;
11371 }
11372 if (*equal == '=') {
11373 if (pos + url_param_name_l > last) {
11374 /* process wrap case, we detect a wrap. In this case, the
11375 * comparison is performed in two parts.
11376 */
11377
11378 /* This is the end, we dont have any other chunk. */
11379 if (bufs != chunks || !bufs[2])
11380 return NULL;
11381
11382 /* Compute the length of each part of the comparison. */
11383 l1 = last - pos;
11384 l2 = url_param_name_l - l1;
11385
11386 /* The second buffer is too short to contain the compared string. */
11387 if (bufs[2] + l2 > bufs[3])
11388 return NULL;
11389
11390 if (memcmp(pos, url_param_name, l1) == 0 &&
11391 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11392 return pos;
11393
11394 /* Perform wrapping and jump the string who fail the comparison. */
11395 bufs += 2;
11396 pos = bufs[0] + l2;
11397 last = bufs[1];
11398
11399 } else {
11400 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011401 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11402 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011403 pos += url_param_name_l + 1;
11404 if (fix_pointer_if_wrap(chunks, &pos))
11405 last = bufs[2];
11406 }
11407 }
11408
11409 while (1) {
11410 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011411 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011412 pos++;
11413 if (pos < last)
11414 break;
11415 /* process buffer wrapping. */
11416 if (bufs != chunks || !bufs[2])
11417 return NULL;
11418 bufs += 2;
11419 pos = bufs[0];
11420 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011421 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011422 pos++;
11423 }
11424 return NULL;
11425}
11426
11427/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011428 * Given a url parameter name and a query string, find the next value.
11429 * An empty url_param_name matches the first available parameter.
11430 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11431 * respectively provide a pointer to the value and its end.
11432 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011433 */
11434static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011435find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011436 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011437 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011438{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011439 const char *arg_start, *qs_end;
11440 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011441
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011442 arg_start = chunks[0];
11443 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011444 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011445 /* Looks for an argument name. */
11446 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011447 url_param_name, url_param_name_l,
11448 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011449 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011450 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011451 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011452 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011453 if (!arg_start)
11454 return 0;
11455
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011456 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011457 while (1) {
11458 /* looks for the first argument. */
11459 value_start = memchr(arg_start, '=', qs_end - arg_start);
11460 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011461 /* Check for wrapping. */
11462 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011463 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011464 chunks[2]) {
11465 arg_start = chunks[2];
11466 qs_end = chunks[3];
11467 continue;
11468 }
11469 return 0;
11470 }
11471 break;
11472 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011473 value_start++;
11474 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011475 else {
11476 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011477 value_start = arg_start + url_param_name_l + 1;
11478
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011479 /* Check for pointer wrapping. */
11480 if (fix_pointer_if_wrap(chunks, &value_start)) {
11481 /* Update the end pointer. */
11482 qs_end = chunks[3];
11483
11484 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011485 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011486 return 0;
11487 }
11488 }
11489
David Cournapeau16023ee2010-12-23 20:55:41 +090011490 value_end = value_start;
11491
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011492 while (1) {
11493 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11494 value_end++;
11495 if (value_end < qs_end)
11496 break;
11497 /* process buffer wrapping. */
11498 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011499 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011500 chunks[2]) {
11501 value_end = chunks[2];
11502 qs_end = chunks[3];
11503 continue;
11504 }
11505 break;
11506 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011507
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011508 *vstart = value_start;
11509 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011510 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011511}
11512
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011513/* This scans a URL-encoded query string. It takes an optionally wrapping
11514 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11515 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11516 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011517 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011518static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011519smp_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 +090011520{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011521 const char *vstart, *vend;
11522 struct chunk *temp;
11523 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011524
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011525 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011526 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011527 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011528 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011529 return 0;
11530
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011531 /* Create sample. If the value is contiguous, return the pointer as CONST,
11532 * if the value is wrapped, copy-it in a buffer.
11533 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011534 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011535 if (chunks[2] &&
11536 vstart >= chunks[0] && vstart <= chunks[1] &&
11537 vend >= chunks[2] && vend <= chunks[3]) {
11538 /* Wrapped case. */
11539 temp = get_trash_chunk();
11540 memcpy(temp->str, vstart, chunks[1] - vstart);
11541 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011542 smp->data.u.str.str = temp->str;
11543 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011544 } else {
11545 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011546 smp->data.u.str.str = (char *)vstart;
11547 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011548 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11549 }
11550
11551 /* Update context, check wrapping. */
11552 chunks[0] = vend;
11553 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11554 chunks[1] = chunks[3];
11555 chunks[2] = NULL;
11556 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011557
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011558 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011559 smp->flags |= SMP_F_NOT_LAST;
11560
David Cournapeau16023ee2010-12-23 20:55:41 +090011561 return 1;
11562}
11563
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011564/* This function iterates over each parameter of the query string. It uses
11565 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011566 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11567 * An optional parameter name is passed in args[0], otherwise any parameter is
11568 * considered. It supports an optional delimiter argument for the beginning of
11569 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011570 */
11571static int
11572smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11573{
11574 struct http_msg *msg;
11575 char delim = '?';
11576 const char *name;
11577 int name_len;
11578
Dragan Dosen26f77e52015-05-25 10:02:11 +020011579 if (!args ||
11580 (args[0].type && args[0].type != ARGT_STR) ||
11581 (args[1].type && args[1].type != ARGT_STR))
11582 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011583
Dragan Dosen26f77e52015-05-25 10:02:11 +020011584 name = "";
11585 name_len = 0;
11586 if (args->type == ARGT_STR) {
11587 name = args->data.str.str;
11588 name_len = args->data.str.len;
11589 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011590
Dragan Dosen26f77e52015-05-25 10:02:11 +020011591 if (args[1].type)
11592 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011593
Dragan Dosen26f77e52015-05-25 10:02:11 +020011594 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011595 CHECK_HTTP_MESSAGE_FIRST();
11596
11597 msg = &smp->strm->txn->req;
11598
11599 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11600 msg->sl.rq.u_l, delim);
11601 if (!smp->ctx.a[0])
11602 return 0;
11603
11604 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011605
11606 /* Assume that the context is filled with NULL pointer
11607 * before the first call.
11608 * smp->ctx.a[2] = NULL;
11609 * smp->ctx.a[3] = NULL;
11610 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011611 }
11612
11613 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11614}
11615
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011616/* This function iterates over each parameter of the body. This requires
11617 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011618 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11619 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11620 * optional second part if the body wraps at the end of the buffer. An optional
11621 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011622 */
11623static int
11624smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11625{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011626 struct http_msg *msg;
11627 unsigned long len;
11628 unsigned long block1;
11629 char *body;
11630 const char *name;
11631 int name_len;
11632
11633 if (!args || (args[0].type && args[0].type != ARGT_STR))
11634 return 0;
11635
11636 name = "";
11637 name_len = 0;
11638 if (args[0].type == ARGT_STR) {
11639 name = args[0].data.str.str;
11640 name_len = args[0].data.str.len;
11641 }
11642
11643 if (!smp->ctx.a[0]) { // first call, find the query string
11644 CHECK_HTTP_MESSAGE_FIRST();
11645
11646 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011647 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011648 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011649 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011650
11651 len = http_body_bytes(msg);
11652 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11653
11654 block1 = len;
11655 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11656 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11657
11658 if (block1 == len) {
11659 /* buffer is not wrapped (or empty) */
11660 smp->ctx.a[0] = body;
11661 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011662
11663 /* Assume that the context is filled with NULL pointer
11664 * before the first call.
11665 * smp->ctx.a[2] = NULL;
11666 * smp->ctx.a[3] = NULL;
11667 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011668 }
11669 else {
11670 /* buffer is wrapped, we need to defragment it */
11671 smp->ctx.a[0] = body;
11672 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011673 smp->ctx.a[2] = msg->chn->buf->data;
11674 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011675 }
11676 }
11677 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11678}
11679
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011680/* Return the signed integer value for the specified url parameter (see url_param
11681 * above).
11682 */
11683static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011684smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011685{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011686 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011687
11688 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011689 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011690 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011691 }
11692
11693 return ret;
11694}
11695
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011696/* This produces a 32-bit hash of the concatenation of the first occurrence of
11697 * the Host header followed by the path component if it begins with a slash ('/').
11698 * This means that '*' will not be added, resulting in exactly the first Host
11699 * entry. If no Host header is found, then the path is used. The resulting value
11700 * is hashed using the url hash followed by a full avalanche hash and provides a
11701 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11702 * high-traffic sites without having to store whole paths.
11703 * this differs from the base32 functions in that it includes the url parameters
11704 * as well as the path
11705 */
11706static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011707smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011708{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011709 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011710 struct hdr_ctx ctx;
11711 unsigned int hash = 0;
11712 char *ptr, *beg, *end;
11713 int len;
11714
11715 CHECK_HTTP_MESSAGE_FIRST();
11716
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011717 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011718 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011719 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011720 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11721 ptr = ctx.line + ctx.val;
11722 len = ctx.vlen;
11723 while (len--)
11724 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11725 }
11726
11727 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011728 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 +000011729 beg = http_get_path(txn);
11730 if (!beg)
11731 beg = end;
11732
11733 for (ptr = beg; ptr < end ; ptr++);
11734
11735 if (beg < ptr && *beg == '/') {
11736 while (beg < ptr)
11737 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11738 }
11739 hash = full_hash(hash);
11740
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011741 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011742 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011743 smp->flags = SMP_F_VOL_1ST;
11744 return 1;
11745}
11746
11747/* This concatenates the source address with the 32-bit hash of the Host and
11748 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11749 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11750 * on the source address length. The URL hash is stored before the address so
11751 * that in environments where IPv6 is insignificant, truncating the output to
11752 * 8 bytes would still work.
11753 */
11754static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011755smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011756{
11757 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011758 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011759
Dragan Dosendb5af612016-06-16 11:23:01 +020011760 if (!cli_conn)
11761 return 0;
11762
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011763 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011764 return 0;
11765
11766 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020011767 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
11768 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011769
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011770 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011771 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011772 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011773 temp->len += 4;
11774 break;
11775 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011776 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011777 temp->len += 16;
11778 break;
11779 default:
11780 return 0;
11781 }
11782
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011783 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011784 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011785 return 1;
11786}
11787
Willy Tarreau185b5c42012-04-26 15:11:51 +020011788/* This function is used to validate the arguments passed to any "hdr" fetch
11789 * keyword. These keywords support an optional positive or negative occurrence
11790 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11791 * is assumed that the types are already the correct ones. Returns 0 on error,
11792 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11793 * error message in case of error, that the caller is responsible for freeing.
11794 * The initial location must either be freeable or NULL.
11795 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011796int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011797{
11798 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011799 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011800 return 0;
11801 }
11802 return 1;
11803}
11804
Willy Tarreau276fae92013-07-25 14:36:01 +020011805/* takes an UINT value on input supposed to represent the time since EPOCH,
11806 * adds an optional offset found in args[0] and emits a string representing
11807 * the date in RFC-1123/5322 format.
11808 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011809static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011810{
Cyril Bontéf78d8962016-01-22 19:40:28 +010011811 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020011812 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11813 struct chunk *temp;
11814 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011815 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011816 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011817
11818 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011819 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011820 curr_date += args[0].data.sint;
11821
11822 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011823 if (!tm)
11824 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011825
11826 temp = get_trash_chunk();
11827 temp->len = snprintf(temp->str, temp->size - temp->len,
11828 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11829 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11830 tm->tm_hour, tm->tm_min, tm->tm_sec);
11831
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011832 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011833 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011834 return 1;
11835}
11836
Thierry FOURNIERad903512014-04-11 17:51:01 +020011837/* Match language range with language tag. RFC2616 14.4:
11838 *
11839 * A language-range matches a language-tag if it exactly equals
11840 * the tag, or if it exactly equals a prefix of the tag such
11841 * that the first tag character following the prefix is "-".
11842 *
11843 * Return 1 if the strings match, else return 0.
11844 */
11845static inline int language_range_match(const char *range, int range_len,
11846 const char *tag, int tag_len)
11847{
11848 const char *end = range + range_len;
11849 const char *tend = tag + tag_len;
11850 while (range < end) {
11851 if (*range == '-' && tag == tend)
11852 return 1;
11853 if (*range != *tag || tag == tend)
11854 return 0;
11855 range++;
11856 tag++;
11857 }
11858 /* Return true only if the last char of the tag is matched. */
11859 return tag == tend;
11860}
11861
11862/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011863static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011864{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011865 const char *al = smp->data.u.str.str;
11866 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011867 const char *token;
11868 int toklen;
11869 int qvalue;
11870 const char *str;
11871 const char *w;
11872 int best_q = 0;
11873
11874 /* Set the constant to the sample, because the output of the
11875 * function will be peek in the constant configuration string.
11876 */
11877 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011878 smp->data.u.str.size = 0;
11879 smp->data.u.str.str = "";
11880 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011881
11882 /* Parse the accept language */
11883 while (1) {
11884
11885 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011886 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011887 al++;
11888 if (al >= end)
11889 break;
11890
11891 /* Start of the fisrt word. */
11892 token = al;
11893
11894 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011895 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011896 al++;
11897 if (al == token)
11898 goto expect_comma;
11899
11900 /* Length of the token. */
11901 toklen = al - token;
11902 qvalue = 1000;
11903
11904 /* Check if the token exists in the list. If the token not exists,
11905 * jump to the next token.
11906 */
11907 str = args[0].data.str.str;
11908 w = str;
11909 while (1) {
11910 if (*str == ';' || *str == '\0') {
11911 if (language_range_match(token, toklen, w, str-w))
11912 goto look_for_q;
11913 if (*str == '\0')
11914 goto expect_comma;
11915 w = str + 1;
11916 }
11917 str++;
11918 }
11919 goto expect_comma;
11920
11921look_for_q:
11922
11923 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011924 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011925 al++;
11926 if (al >= end)
11927 goto process_value;
11928
11929 /* If ',' is found, process the result */
11930 if (*al == ',')
11931 goto process_value;
11932
11933 /* If the character is different from ';', look
11934 * for the end of the header part in best effort.
11935 */
11936 if (*al != ';')
11937 goto expect_comma;
11938
11939 /* Assumes that the char is ';', now expect "q=". */
11940 al++;
11941
11942 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011943 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011944 al++;
11945 if (al >= end)
11946 goto process_value;
11947
11948 /* Expect 'q'. If no 'q', continue in best effort */
11949 if (*al != 'q')
11950 goto process_value;
11951 al++;
11952
11953 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011954 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011955 al++;
11956 if (al >= end)
11957 goto process_value;
11958
11959 /* Expect '='. If no '=', continue in best effort */
11960 if (*al != '=')
11961 goto process_value;
11962 al++;
11963
11964 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011965 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011966 al++;
11967 if (al >= end)
11968 goto process_value;
11969
11970 /* Parse the q value. */
11971 qvalue = parse_qvalue(al, &al);
11972
11973process_value:
11974
11975 /* If the new q value is the best q value, then store the associated
11976 * language in the response. If qvalue is the biggest value (1000),
11977 * break the process.
11978 */
11979 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011980 smp->data.u.str.str = (char *)w;
11981 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011982 if (qvalue >= 1000)
11983 break;
11984 best_q = qvalue;
11985 }
11986
11987expect_comma:
11988
11989 /* Expect comma or end. If the end is detected, quit the loop. */
11990 while (al < end && *al != ',')
11991 al++;
11992 if (al >= end)
11993 break;
11994
11995 /* Comma is found, jump it and restart the analyzer. */
11996 al++;
11997 }
11998
11999 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012000 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
12001 smp->data.u.str.str = args[1].data.str.str;
12002 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012003 }
12004
12005 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012006 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012007}
12008
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012009/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012010static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012011{
12012 /* If the constant flag is set or if not size is avalaible at
12013 * the end of the buffer, copy the string in other buffer
12014 * before decoding.
12015 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012016 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012017 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012018 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
12019 smp->data.u.str.str = str->str;
12020 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012021 smp->flags &= ~SMP_F_CONST;
12022 }
12023
12024 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012025 smp->data.u.str.str[smp->data.u.str.len] = '\0';
12026 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012027 return 1;
12028}
12029
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012030static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
12031{
12032 struct proxy *fe = strm_fe(smp->strm);
12033 int idx, i;
12034 struct cap_hdr *hdr;
12035 int len;
12036
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012037 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012038 return 0;
12039
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012040 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012041
12042 /* Check the availibity of the capture id. */
12043 if (idx > fe->nb_req_cap - 1)
12044 return 0;
12045
12046 /* Look for the original configuration. */
12047 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
12048 hdr != NULL && i != idx ;
12049 i--, hdr = hdr->next);
12050 if (!hdr)
12051 return 0;
12052
12053 /* check for the memory allocation */
12054 if (smp->strm->req_cap[hdr->index] == NULL)
12055 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
12056 if (smp->strm->req_cap[hdr->index] == NULL)
12057 return 0;
12058
12059 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012060 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012061 if (len > hdr->len)
12062 len = hdr->len;
12063
12064 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012065 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012066 smp->strm->req_cap[idx][len] = '\0';
12067
12068 return 1;
12069}
12070
12071static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
12072{
12073 struct proxy *fe = strm_fe(smp->strm);
12074 int idx, i;
12075 struct cap_hdr *hdr;
12076 int len;
12077
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012078 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012079 return 0;
12080
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012081 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012082
12083 /* Check the availibity of the capture id. */
12084 if (idx > fe->nb_rsp_cap - 1)
12085 return 0;
12086
12087 /* Look for the original configuration. */
12088 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12089 hdr != NULL && i != idx ;
12090 i--, hdr = hdr->next);
12091 if (!hdr)
12092 return 0;
12093
12094 /* check for the memory allocation */
12095 if (smp->strm->res_cap[hdr->index] == NULL)
12096 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
12097 if (smp->strm->res_cap[hdr->index] == NULL)
12098 return 0;
12099
12100 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012101 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012102 if (len > hdr->len)
12103 len = hdr->len;
12104
12105 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012106 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012107 smp->strm->res_cap[idx][len] = '\0';
12108
12109 return 1;
12110}
12111
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012112/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012113 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012114 * the relevant part of the request line accordingly. Then it updates various
12115 * pointers to the next elements which were moved, and the total buffer length.
12116 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012117 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12118 * error, though this can be revisited when this code is finally exploited.
12119 *
12120 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12121 * query string and 3 to replace uri.
12122 *
12123 * In query string case, the mark question '?' must be set at the start of the
12124 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012125 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012126int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012127 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012128{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012129 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012130 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012131 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012132 int delta;
12133
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012134 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012135 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012136 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012137 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12138
12139 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012140 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012141 txn->req.sl.rq.m_l += delta;
12142 txn->req.sl.rq.u += delta;
12143 txn->req.sl.rq.v += delta;
12144 break;
12145
12146 case 1: // path
12147 cur_ptr = http_get_path(txn);
12148 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012149 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012150
12151 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012152 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 +010012153 cur_end++;
12154
12155 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012156 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012157 txn->req.sl.rq.u_l += delta;
12158 txn->req.sl.rq.v += delta;
12159 break;
12160
12161 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012162 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012163 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012164 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12165 while (cur_ptr < cur_end && *cur_ptr != '?')
12166 cur_ptr++;
12167
12168 /* skip the question mark or indicate that we must insert it
12169 * (but only if the format string is not empty then).
12170 */
12171 if (cur_ptr < cur_end)
12172 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012173 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012174 offset = 0;
12175
12176 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012177 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012178 txn->req.sl.rq.u_l += delta;
12179 txn->req.sl.rq.v += delta;
12180 break;
12181
12182 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012183 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012184 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12185
12186 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012187 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012188 txn->req.sl.rq.u_l += delta;
12189 txn->req.sl.rq.v += delta;
12190 break;
12191
12192 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012193 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012194 }
12195
12196 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012197 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012198 txn->req.sl.rq.l += delta;
12199 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012200 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012201 return 0;
12202}
12203
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012204/* This function replace the HTTP status code and the associated message. The
12205 * variable <status> contains the new status code. This function never fails.
12206 */
12207void http_set_status(unsigned int status, struct stream *s)
12208{
12209 struct http_txn *txn = s->txn;
12210 char *cur_ptr, *cur_end;
12211 int delta;
12212 char *res;
12213 int c_l;
12214 const char *msg;
12215 int msg_len;
12216
12217 chunk_reset(&trash);
12218
12219 res = ultoa_o(status, trash.str, trash.size);
12220 c_l = res - trash.str;
12221
12222 trash.str[c_l] = ' ';
12223 trash.len = c_l + 1;
12224
12225 msg = get_reason(status);
12226 msg_len = strlen(msg);
12227
12228 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
12229 trash.len += msg_len;
12230
12231 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
12232 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
12233
12234 /* commit changes and adjust message */
12235 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
12236
12237 /* adjust res line offsets and lengths */
12238 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
12239 txn->rsp.sl.st.c_l = c_l;
12240 txn->rsp.sl.st.r_l = msg_len;
12241
12242 delta = trash.len - (cur_end - cur_ptr);
12243 txn->rsp.sl.st.l += delta;
12244 txn->hdr_idx.v[0].len += delta;
12245 http_msg_move_end(&txn->rsp, delta);
12246}
12247
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012248/* This function executes one of the set-{method,path,query,uri} actions. It
12249 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012250 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012251 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012252 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12253 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012254 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012255enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012256 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012257{
12258 chunk_reset(&trash);
12259
12260 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012261 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012262 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012263 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012264
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012265 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012266 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012267}
12268
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012269/* This function is just a compliant action wrapper for "set-status". */
12270enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012271 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012272{
12273 http_set_status(rule->arg.status.code, s);
12274 return ACT_RET_CONT;
12275}
12276
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012277/* parse an http-request action among :
12278 * set-method
12279 * set-path
12280 * set-query
12281 * set-uri
12282 *
12283 * All of them accept a single argument of type string representing a log-format.
12284 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12285 * 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 +020012286 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012287 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012288enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12289 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012290{
12291 int cur_arg = *orig_arg;
12292
Thierry FOURNIER42148732015-09-02 17:17:33 +020012293 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012294
12295 switch (args[0][4]) {
12296 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012297 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012298 rule->action_ptr = http_action_set_req_line;
12299 break;
12300 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012301 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012302 rule->action_ptr = http_action_set_req_line;
12303 break;
12304 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012305 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012306 rule->action_ptr = http_action_set_req_line;
12307 break;
12308 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012309 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012310 rule->action_ptr = http_action_set_req_line;
12311 break;
12312 default:
12313 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012314 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012315 }
12316
12317 if (!*args[cur_arg] ||
12318 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12319 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012320 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012321 }
12322
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012323 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012324 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012325 parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012326 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
12327 proxy->conf.args.file, proxy->conf.args.line);
12328
12329 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012330 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012331}
12332
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012333/* parse set-status action:
12334 * This action accepts a single argument of type int representing
12335 * an http status code. It returns ACT_RET_PRS_OK on success,
12336 * ACT_RET_PRS_ERR on error.
12337 */
12338enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12339 struct act_rule *rule, char **err)
12340{
12341 char *error;
12342
Thierry FOURNIER42148732015-09-02 17:17:33 +020012343 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012344 rule->action_ptr = action_http_set_status;
12345
12346 /* Check if an argument is available */
12347 if (!*args[*orig_arg]) {
12348 memprintf(err, "expects exactly 1 argument <status>");
12349 return ACT_RET_PRS_ERR;
12350 }
12351
12352 /* convert status code as integer */
12353 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12354 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12355 memprintf(err, "expects an integer status code between 100 and 999");
12356 return ACT_RET_PRS_ERR;
12357 }
12358
12359 (*orig_arg)++;
12360 return ACT_RET_PRS_OK;
12361}
12362
Willy Tarreaua9083d02015-05-08 15:27:59 +020012363/* This function executes the "capture" action. It executes a fetch expression,
12364 * turns the result into a string and puts it in a capture slot. It always
12365 * returns 1. If an error occurs the action is cancelled, but the rule
12366 * processing continues.
12367 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012368enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012369 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012370{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012371 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012372 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012373 char **cap = s->req_cap;
12374 int len;
12375
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012376 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 +020012377 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012378 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012379
12380 if (cap[h->index] == NULL)
12381 cap[h->index] = pool_alloc2(h->pool);
12382
12383 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012384 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012385
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012386 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012387 if (len > h->len)
12388 len = h->len;
12389
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012390 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012391 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012392 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012393}
12394
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012395/* This function executes the "capture" action and store the result in a
12396 * capture slot if exists. It executes a fetch expression, turns the result
12397 * into a string and puts it in a capture slot. It always returns 1. If an
12398 * error occurs the action is cancelled, but the rule processing continues.
12399 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012400enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012401 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012402{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012403 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012404 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012405 char **cap = s->req_cap;
12406 struct proxy *fe = strm_fe(s);
12407 int len;
12408 int i;
12409
12410 /* Look for the original configuration. */
12411 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012412 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012413 i--, h = h->next);
12414 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012415 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012416
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012417 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 +020012418 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012419 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012420
12421 if (cap[h->index] == NULL)
12422 cap[h->index] = pool_alloc2(h->pool);
12423
12424 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012425 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012426
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012427 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012428 if (len > h->len)
12429 len = h->len;
12430
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012431 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012432 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012433 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012434}
12435
Willy Tarreaua9083d02015-05-08 15:27:59 +020012436/* parse an "http-request capture" action. It takes a single argument which is
12437 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012438 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012439 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012440 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012441enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12442 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012443{
12444 struct sample_expr *expr;
12445 struct cap_hdr *hdr;
12446 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012447 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012448
12449 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12450 if (strcmp(args[cur_arg], "if") == 0 ||
12451 strcmp(args[cur_arg], "unless") == 0)
12452 break;
12453
12454 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012455 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012456 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012457 }
12458
Willy Tarreaua9083d02015-05-08 15:27:59 +020012459 cur_arg = *orig_arg;
12460 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12461 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012462 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012463
12464 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12465 memprintf(err,
12466 "fetch method '%s' extracts information from '%s', none of which is available here",
12467 args[cur_arg-1], sample_src_names(expr->fetch->use));
12468 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012469 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012470 }
12471
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012472 if (!args[cur_arg] || !*args[cur_arg]) {
12473 memprintf(err, "expects 'len or 'id'");
12474 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012475 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012476 }
12477
Willy Tarreaua9083d02015-05-08 15:27:59 +020012478 if (strcmp(args[cur_arg], "len") == 0) {
12479 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012480
12481 if (!(px->cap & PR_CAP_FE)) {
12482 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012483 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012484 }
12485
12486 proxy->conf.args.ctx = ARGC_CAP;
12487
Willy Tarreaua9083d02015-05-08 15:27:59 +020012488 if (!args[cur_arg]) {
12489 memprintf(err, "missing length value");
12490 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012491 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012492 }
12493 /* we copy the table name for now, it will be resolved later */
12494 len = atoi(args[cur_arg]);
12495 if (len <= 0) {
12496 memprintf(err, "length must be > 0");
12497 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012498 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012499 }
12500 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012501
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012502 if (!len) {
12503 memprintf(err, "a positive 'len' argument is mandatory");
12504 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012505 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012506 }
12507
Vincent Bernat02779b62016-04-03 13:48:43 +020012508 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012509 hdr->next = px->req_cap;
12510 hdr->name = NULL; /* not a header capture */
12511 hdr->namelen = 0;
12512 hdr->len = len;
12513 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12514 hdr->index = px->nb_req_cap++;
12515
12516 px->req_cap = hdr;
12517 px->to_log |= LW_REQHDR;
12518
Thierry FOURNIER42148732015-09-02 17:17:33 +020012519 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012520 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012521 rule->arg.cap.expr = expr;
12522 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012523 }
12524
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012525 else if (strcmp(args[cur_arg], "id") == 0) {
12526 int id;
12527 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012528
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012529 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012530
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012531 if (!args[cur_arg]) {
12532 memprintf(err, "missing id value");
12533 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012534 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012535 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012536
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012537 id = strtol(args[cur_arg], &error, 10);
12538 if (*error != '\0') {
12539 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12540 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012541 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012542 }
12543 cur_arg++;
12544
12545 proxy->conf.args.ctx = ARGC_CAP;
12546
Thierry FOURNIER42148732015-09-02 17:17:33 +020012547 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012548 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012549 rule->arg.capid.expr = expr;
12550 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012551 }
12552
12553 else {
12554 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12555 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012556 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012557 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012558
12559 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012560 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012561}
12562
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012563/* This function executes the "capture" action and store the result in a
12564 * capture slot if exists. It executes a fetch expression, turns the result
12565 * into a string and puts it in a capture slot. It always returns 1. If an
12566 * error occurs the action is cancelled, but the rule processing continues.
12567 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012568enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012569 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012570{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012571 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012572 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012573 char **cap = s->res_cap;
12574 struct proxy *fe = strm_fe(s);
12575 int len;
12576 int i;
12577
12578 /* Look for the original configuration. */
12579 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012580 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012581 i--, h = h->next);
12582 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012583 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012584
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012585 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 +020012586 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012587 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012588
12589 if (cap[h->index] == NULL)
12590 cap[h->index] = pool_alloc2(h->pool);
12591
12592 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012593 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012594
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012595 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012596 if (len > h->len)
12597 len = h->len;
12598
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012599 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012600 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012601 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012602}
12603
12604/* parse an "http-response capture" action. It takes a single argument which is
12605 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12606 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012607 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012608 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012609enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12610 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012611{
12612 struct sample_expr *expr;
12613 int cur_arg;
12614 int id;
12615 char *error;
12616
12617 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12618 if (strcmp(args[cur_arg], "if") == 0 ||
12619 strcmp(args[cur_arg], "unless") == 0)
12620 break;
12621
12622 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012623 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012624 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012625 }
12626
12627 cur_arg = *orig_arg;
12628 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12629 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012630 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012631
12632 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12633 memprintf(err,
12634 "fetch method '%s' extracts information from '%s', none of which is available here",
12635 args[cur_arg-1], sample_src_names(expr->fetch->use));
12636 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012637 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012638 }
12639
12640 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012641 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012642 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012643 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012644 }
12645
12646 if (strcmp(args[cur_arg], "id") != 0) {
12647 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12648 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012649 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012650 }
12651
12652 cur_arg++;
12653
12654 if (!args[cur_arg]) {
12655 memprintf(err, "missing id value");
12656 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012657 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012658 }
12659
12660 id = strtol(args[cur_arg], &error, 10);
12661 if (*error != '\0') {
12662 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12663 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012664 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012665 }
12666 cur_arg++;
12667
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012668 proxy->conf.args.ctx = ARGC_CAP;
12669
Thierry FOURNIER42148732015-09-02 17:17:33 +020012670 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012671 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012672 rule->arg.capid.expr = expr;
12673 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012674
12675 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012676 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012677}
12678
William Lallemand73025dd2014-04-24 14:38:37 +020012679/*
12680 * Return the struct http_req_action_kw associated to a keyword.
12681 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012682struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012683{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012684 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012685}
12686
12687/*
12688 * Return the struct http_res_action_kw associated to a keyword.
12689 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012690struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012691{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012692 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012693}
12694
Willy Tarreau4a568972010-05-12 08:08:50 +020012695/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012696/* All supported ACL keywords must be declared here. */
12697/************************************************************************/
12698
12699/* Note: must not be declared <const> as its list will be overwritten.
12700 * Please take care of keeping this list alphabetically sorted.
12701 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012702static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012703 { "base", "base", PAT_MATCH_STR },
12704 { "base_beg", "base", PAT_MATCH_BEG },
12705 { "base_dir", "base", PAT_MATCH_DIR },
12706 { "base_dom", "base", PAT_MATCH_DOM },
12707 { "base_end", "base", PAT_MATCH_END },
12708 { "base_len", "base", PAT_MATCH_LEN },
12709 { "base_reg", "base", PAT_MATCH_REG },
12710 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012711
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012712 { "cook", "req.cook", PAT_MATCH_STR },
12713 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12714 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12715 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12716 { "cook_end", "req.cook", PAT_MATCH_END },
12717 { "cook_len", "req.cook", PAT_MATCH_LEN },
12718 { "cook_reg", "req.cook", PAT_MATCH_REG },
12719 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012720
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012721 { "hdr", "req.hdr", PAT_MATCH_STR },
12722 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12723 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12724 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12725 { "hdr_end", "req.hdr", PAT_MATCH_END },
12726 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12727 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12728 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012729
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012730 /* these two declarations uses strings with list storage (in place
12731 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12732 * and delete functions are relative to the list management. The parse
12733 * and match method are related to the corresponding fetch methods. This
12734 * is very particular ACL declaration mode.
12735 */
12736 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12737 { "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 +020012738
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012739 { "path", "path", PAT_MATCH_STR },
12740 { "path_beg", "path", PAT_MATCH_BEG },
12741 { "path_dir", "path", PAT_MATCH_DIR },
12742 { "path_dom", "path", PAT_MATCH_DOM },
12743 { "path_end", "path", PAT_MATCH_END },
12744 { "path_len", "path", PAT_MATCH_LEN },
12745 { "path_reg", "path", PAT_MATCH_REG },
12746 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012747
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012748 { "req_ver", "req.ver", PAT_MATCH_STR },
12749 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012750
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012751 { "scook", "res.cook", PAT_MATCH_STR },
12752 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12753 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12754 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12755 { "scook_end", "res.cook", PAT_MATCH_END },
12756 { "scook_len", "res.cook", PAT_MATCH_LEN },
12757 { "scook_reg", "res.cook", PAT_MATCH_REG },
12758 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012759
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012760 { "shdr", "res.hdr", PAT_MATCH_STR },
12761 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12762 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12763 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12764 { "shdr_end", "res.hdr", PAT_MATCH_END },
12765 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12766 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12767 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012768
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012769 { "url", "url", PAT_MATCH_STR },
12770 { "url_beg", "url", PAT_MATCH_BEG },
12771 { "url_dir", "url", PAT_MATCH_DIR },
12772 { "url_dom", "url", PAT_MATCH_DOM },
12773 { "url_end", "url", PAT_MATCH_END },
12774 { "url_len", "url", PAT_MATCH_LEN },
12775 { "url_reg", "url", PAT_MATCH_REG },
12776 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012777
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012778 { "urlp", "urlp", PAT_MATCH_STR },
12779 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12780 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12781 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12782 { "urlp_end", "urlp", PAT_MATCH_END },
12783 { "urlp_len", "urlp", PAT_MATCH_LEN },
12784 { "urlp_reg", "urlp", PAT_MATCH_REG },
12785 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012786
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012787 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012788}};
12789
12790/************************************************************************/
12791/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012792/************************************************************************/
12793/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012794static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012795 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012796 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012797 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12798
Willy Tarreau87b09662015-04-03 00:22:06 +020012799 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012800 { "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 +020012801
12802 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012803 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12804 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12805 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012806
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012807 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12808 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012809
Willy Tarreau409bcde2013-01-08 00:31:00 +010012810 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12811 * are only here to match the ACL's name, are request-only and are used
12812 * for ACL compatibility only.
12813 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012814 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12815 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012816 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12817 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012818
12819 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12820 * only here to match the ACL's name, are request-only and are used for
12821 * ACL compatibility only.
12822 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012823 { "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 +020012824 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012825 { "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 +020012826 { "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 +010012827
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012828 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012829 { "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 +010012830 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012831 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012832 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012833 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012834
12835 /* HTTP protocol on the request path */
12836 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012837 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012838
12839 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012840 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12841 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012842
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012843 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012844 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12845 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012846 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012847
Willy Tarreau18ed2562013-01-14 15:56:36 +010012848 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012849 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12850 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012851
Willy Tarreau18ed2562013-01-14 15:56:36 +010012852 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012853 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012854 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12855 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012856
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012857 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012858 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012859 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012860 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012861 { "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 +010012862 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012863 { "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 +010012864
12865 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012866 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012867 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12868 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012869
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012870 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012871 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012872 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012873 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012874 { "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 +010012875 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012876 { "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 +010012877
Willy Tarreau409bcde2013-01-08 00:31:00 +010012878 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012879 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012880 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12881 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012882 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012883
12884 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012885 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012886 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012887 { "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 +020012888 { "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 +010012889
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012890 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020012891 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012892 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012893 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012894 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012895 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012896 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020012897 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12898 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012899 { "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 +010012900 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012901}};
12902
Willy Tarreau8797c062007-05-07 00:55:35 +020012903
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012904/************************************************************************/
12905/* All supported converter keywords must be declared here. */
12906/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012907/* Note: must not be declared <const> as its list will be overwritten */
12908static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012909 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020012910 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012911 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
12912 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012913 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012914 { NULL, NULL, 0, 0, 0 },
12915}};
12916
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012917
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012918/************************************************************************/
12919/* All supported http-request action keywords must be declared here. */
12920/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012921struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012922 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020012923 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012924 { "set-method", parse_set_req_line },
12925 { "set-path", parse_set_req_line },
12926 { "set-query", parse_set_req_line },
12927 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012928 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012929 }
12930};
12931
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012932struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012933 .kw = {
12934 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012935 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012936 { NULL, NULL }
12937 }
12938};
12939
Willy Tarreau8797c062007-05-07 00:55:35 +020012940__attribute__((constructor))
12941static void __http_protocol_init(void)
12942{
12943 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012944 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012945 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012946 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012947 http_res_keywords_register(&http_res_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020012948}
12949
12950
Willy Tarreau58f10d72006-12-04 02:26:12 +010012951/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012952 * Local variables:
12953 * c-indent-level: 8
12954 * c-basic-offset: 8
12955 * End:
12956 */